Skip to content

Commit

Permalink
#10 fix save file and read code from file issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankll committed Jan 6, 2024
1 parent cf963f2 commit 4a1ceb0
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 124 deletions.
2 changes: 1 addition & 1 deletion src/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ const loadDevTools = async () => {
};

const createWindow = async () => {
createMenu();
const mainWindow = new BrowserWindow(BrowserWindowOptions);

createMenu(mainWindow);
bypassCors(mainWindow);

// and load the index.html of the app.
Expand Down
236 changes: 120 additions & 116 deletions src/electron/menu.ts
Original file line number Diff line number Diff line change
@@ -1,120 +1,124 @@
import { app, Menu, MenuItemConstructorOptions } from 'electron';
import { app, Menu, MenuItemConstructorOptions, BrowserWindow } from 'electron';

const menuTemplate = [
{
label: 'dockit',
submenu: [
{
label: 'About dockit',
selector: 'dockit:',
},
{
type: 'separator',
},
{
label: 'Services',
submenu: [],
},
{
type: 'separator',
},
{
label: 'Hide App',
accelerator: 'CmdOrCtrl+H',
selector: 'hide:',
},
{
label: 'Hide Others',
accelerator: 'CmdOrCtrl+Shift+H',
selector: 'hideOtherApplications:',
},
{
label: 'Show All',
selector: 'unhideAllApplications:',
},
{
type: 'separator',
},
{
label: 'Quit',
accelerator: 'Command+Q',
click: () => app.quit(),
},
],
},
{
label: 'File',
submenu: [
{
label: 'Save',
accelerator: 'CmdOrCtrl+S',
selector: 'save:',
},
],
},
{
label: 'Edit',
submenu: [
{
label: 'Undo',
accelerator: 'CmdOrCtrl+Z',
selector: 'undo:',
},
{
label: 'Redo',
accelerator: 'Shift+CmdOrCtrl+Z',
selector: 'redo:',
},
{
type: 'separator',
},
{
label: 'Cut',
accelerator: 'CmdOrCtrl+X',
selector: 'cut:',
},
{
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
selector: 'copy:',
},
{
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
selector: 'paste:',
},
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
selector: 'selectAll:',
},
],
},
{
label: 'Window',
submenu: [
{
label: 'Minimize',
accelerator: 'Command+M',
selector: 'performMiniaturize:',
},
{
label: 'Close',
accelerator: 'Command+W',
selector: 'performClose:',
},
{
type: 'separator',
},
{
label: 'Bring All to Front',
selector: 'arrangeInFront:',
},
],
},
] as Array<MenuItemConstructorOptions>;
const buildMenuTemplate = (mainWindow: BrowserWindow) =>
[
{
label: 'dockit',
submenu: [
{
label: 'About dockit',
selector: 'dockit:',
},
{
type: 'separator',
},
{
label: 'Services',
submenu: [],
},
{
type: 'separator',
},
{
label: 'Hide App',
accelerator: 'CmdOrCtrl+H',
selector: 'hide:',
},
{
label: 'Hide Others',
accelerator: 'CmdOrCtrl+Shift+H',
selector: 'hideOtherApplications:',
},
{
label: 'Show All',
selector: 'unhideAllApplications:',
},
{
type: 'separator',
},
{
label: 'Quit',
accelerator: 'Command+Q',
click: () => app.quit(),
},
],
},
{
label: 'File',
submenu: [
{
label: 'Save',
accelerator: 'CmdOrCtrl+S',
selector: 'save:',
click: () => {
mainWindow.webContents.send('save-shortcout', {});
},
},
],
},
{
label: 'Edit',
submenu: [
{
label: 'Undo',
accelerator: 'CmdOrCtrl+Z',
selector: 'undo:',
},
{
label: 'Redo',
accelerator: 'Shift+CmdOrCtrl+Z',
selector: 'redo:',
},
{
type: 'separator',
},
{
label: 'Cut',
accelerator: 'CmdOrCtrl+X',
selector: 'cut:',
},
{
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
selector: 'copy:',
},
{
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
selector: 'paste:',
},
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
selector: 'selectAll:',
},
],
},
{
label: 'Window',
submenu: [
{
label: 'Minimize',
accelerator: 'Command+M',
selector: 'performMiniaturize:',
},
{
label: 'Close',
accelerator: 'Command+W',
selector: 'performClose:',
},
{
type: 'separator',
},
{
label: 'Bring All to Front',
selector: 'arrangeInFront:',
},
],
},
] as Array<MenuItemConstructorOptions>;

export const createMenu = () => {
const menu = Menu.buildFromTemplate(menuTemplate);
export const createMenu = (mainWindow: BrowserWindow) => {
const menu = Menu.buildFromTemplate(buildMenuTemplate(mainWindow));
Menu.setApplicationMenu(menu);
};
2 changes: 2 additions & 0 deletions src/electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ contextBridge.exposeInMainWorld('sourceFileAPI', {
saveFile: async (content: string) =>
ipcRenderer.invoke('sourceFileAPI', { method: 'SAVE_FILE', content }),
readFile: async () => ipcRenderer.invoke('sourceFileAPI', { method: 'READ_FILE' }),
onSaveChortcut: (callback: (value: unknown) => void) =>
ipcRenderer.on('save-shortcout', (_event, value) => callback(value)),
});
21 changes: 14 additions & 7 deletions src/views/editor/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ const message = useMessage();
const lang = useLang();
const sourceFileStore = useSourceFileStore();
const { readSourceFromFile } = sourceFileStore;
const { readSourceFromFile, saveSourceToFile } = sourceFileStore;
const { defaultFile } = storeToRefs(sourceFileStore);
readSourceFromFile();
const connectionStore = useConnectionStore();
const { searchQDSL } = connectionStore;
Expand Down Expand Up @@ -90,8 +89,6 @@ watch(
},
);
const code = defaultFile.value;
const executionGutterClass = 'execute-button-decoration';
const executeDecorationType = 'action-execute-decoration.search';
type Decoration = {
Expand Down Expand Up @@ -174,7 +171,7 @@ const executeQueryAction = async (
});
}
};
const setupQueryEditor = () => {
const setupQueryEditor = (code: string) => {
queryEditor = monaco.editor.create(queryEditorRef.value, {
automaticLayout: true,
theme: editorTheme.value,
Expand All @@ -201,10 +198,18 @@ const setupJsonEditor = () => {
language: 'json',
});
};
onMounted(() => {
setupQueryEditor();
onMounted(async () => {
await readSourceFromFile();
const code = defaultFile.value;
setupQueryEditor(code);
setupJsonEditor();
});
const { sourceFileAPI } = window;
sourceFileAPI.onSaveChortcut(async (_: unknown) => {
const content = queryEditor.getModel()!.getValue() || '';
await saveSourceToFile(content);
});
</script>

<style lang="scss">
Expand All @@ -213,10 +218,12 @@ onMounted(() => {
height: 100%;
display: flex;
flex-flow: row nowrap;
#query-editor {
width: 50%;
height: 100%;
}
#display-editor {
width: 50%;
height: 100%;
Expand Down
1 change: 1 addition & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface IStoreAPI {
export interface ISourceFileAPI {
saveFile: (content: string) => Promise<void>;
readFile: () => Promise<string>;
onSaveChortcut: () => string;
}

declare global {
Expand Down

0 comments on commit 4a1ceb0

Please sign in to comment.