save last build dir
This commit is contained in:
1 parent
9af6aef614
commit
3a2f09541c
4 files changed
+29
-22
No files matched your search
@@ -58,9 +58,7 @@ export const Menu: FC<LayoutProps> = () => {
|
||||
);
|
||||
const enabled = !!workspace.workdir;
|
||||
const homedir = app.getPath("home");
|
||||
const settings = {
|
||||
recent: useSetting((state) => state.recent),
|
||||
};
|
||||
const { settings } = useSetting(useShallow((state) => ({ settings: state.data })));
|
||||
|
||||
const menuTemplate: MenuItemConstructorOptions[] = useMemo(() => {
|
||||
const recentWorkspaces: MenuItemConstructorOptions[] = settings.recent.map((path, i) => ({
|
||||
|
||||
@@ -39,7 +39,7 @@ export const Workspace: FC = () => {
|
||||
find: state.find,
|
||||
}))
|
||||
);
|
||||
const settings = useSetting(useShallow((state) => ({ recent: state.recent })));
|
||||
const { settings } = useSetting(useShallow((state) => ({ settings: state.data })));
|
||||
const [isShowingAlert, setShowingAlert] = useState(false);
|
||||
const { t } = useTranslation();
|
||||
const forceUpdate = useForceUpdate();
|
||||
|
||||
@@ -7,47 +7,53 @@ const settingPath = app.getPath("userData") + "/settings.json";
|
||||
|
||||
export type SettingModel = {
|
||||
recent: string[];
|
||||
buildDir: string;
|
||||
};
|
||||
|
||||
export type SettingStore = {
|
||||
recent: string[];
|
||||
data: SettingModel;
|
||||
|
||||
load: () => void;
|
||||
save: () => void;
|
||||
appendRecent: (path: string) => void;
|
||||
removeRecent: (path: string) => void;
|
||||
setBuildDir: (dir: string) => void;
|
||||
};
|
||||
|
||||
export const useSetting = create<SettingStore>((set, get) => ({
|
||||
recent: [],
|
||||
data: {
|
||||
recent: [],
|
||||
buildDir: "",
|
||||
},
|
||||
load: () => {
|
||||
try {
|
||||
if (fs.existsSync(settingPath)) {
|
||||
const settings = readJson(settingPath) as SettingModel;
|
||||
if (!(settings.recent instanceof Array)) {
|
||||
settings.recent = [];
|
||||
}
|
||||
|
||||
set({ recent: settings.recent });
|
||||
set({ data: settings });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
},
|
||||
save: () => {
|
||||
const settings = get();
|
||||
writeJson(settingPath, {
|
||||
recent: settings.recent,
|
||||
} as SettingModel);
|
||||
writeJson(settingPath, get().data);
|
||||
},
|
||||
appendRecent: (path: string) => {
|
||||
const recent = get().recent.filter((v) => v !== path);
|
||||
const { data, save } = get();
|
||||
const recent = data.recent.filter((v) => v !== path);
|
||||
recent.unshift(path);
|
||||
set({ recent });
|
||||
get().save();
|
||||
set({ data: { ...data, recent } });
|
||||
save();
|
||||
},
|
||||
removeRecent: (path: string) => {
|
||||
const recent = get().recent.filter((v) => v !== path);
|
||||
set({ recent });
|
||||
get().save();
|
||||
const { data, save } = get();
|
||||
const recent = data.recent.filter((v) => v !== path);
|
||||
set({ data: { ...data, recent } });
|
||||
save();
|
||||
},
|
||||
setBuildDir: (dir: string) => {
|
||||
const { data, save } = get();
|
||||
set({ data: { ...data, buildDir: dir } });
|
||||
save();
|
||||
},
|
||||
}));
|
||||
@@ -342,12 +342,14 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
|
||||
|
||||
buildProject: () => {
|
||||
const workspace = get();
|
||||
if (workspace.path)
|
||||
if (workspace.path) {
|
||||
if (!buildDir) {
|
||||
buildDir = dialog.showOpenDialogSync({
|
||||
properties: ["openDirectory", "createDirectory"],
|
||||
defaultPath: useSetting.getState().data.buildDir,
|
||||
})?.[0];
|
||||
}
|
||||
}
|
||||
if (buildDir) {
|
||||
for (const editor of workspace.editors) {
|
||||
editor.dispatch("save");
|
||||
@@ -382,6 +384,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
|
||||
} else {
|
||||
message.success(i18n.t("buildCompleted"));
|
||||
}
|
||||
useSetting.getState().setBuildDir(buildDir);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
message.error(i18n.t("buildFailed"));
|
||||
|
||||
Reference in new issue
Block a user