Skip to content

Commit

Permalink
add new, save as support
Browse files Browse the repository at this point in the history
  • Loading branch information
zag committed Jan 8, 2022
1 parent 7d8328c commit 32a43db
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
7 changes: 7 additions & 0 deletions app/main/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ const openAbout = () => {
const fileMenu:MenuItemConstructorOptions = {
label: menuLabel('&File'),
submenu: [
{
label: menuLabel('&New'),
accelerator: 'CmdOrCtrl+N',
click(model, item, win) {
mainApp.createWindow({})
},
},
{
label: menuLabel('&Open'),
accelerator: 'CmdOrCtrl+O',
Expand Down
3 changes: 3 additions & 0 deletions app/renderer/client-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ vmd = Object.assign({
saveFile(content) {
electron.ipc.send('save-file', content);
},
saveFileAs(content) {
electron.ipc.send('save-file-as', content);
},

openFile(filePath) {
electron.ipc.send('open-file', filePath);
Expand Down
12 changes: 7 additions & 5 deletions app/renderer/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const App = ()=>{

useEffect(()=>{
const fileName = filePath ? vmd.path.parse(filePath)['name'] : filePath
vmd.setWindowTitle(`${fileName}${isTextChanged ? ' *' : '' }`)
vmd.setWindowTitle(`${fileName||'[new]'}${isTextChanged ? ' *' : '' }`)
},[isTextChanged, filePath])

useEffect(() => {
Expand All @@ -176,23 +176,25 @@ const App = ()=>{
useEffect( () => {
const saveFileAction = () => {
if (isTextChanged) {
console.warn("Save File")
vmd.saveFile({content:text, filePath})
}
}

const saveFileAsAction = () => {
vmd.saveFileAs({content:text})
}
const togglePreviewMode = (e) => {
console.log("tooflge")
Object.hasOwnProperty.call(e, 'preventDefault') && e.preventDefault();
setPreviewMode(!isPreviewMode)
}

// make menu command listener
// make menu command listeners
vmd.on('menu-file-save', saveFileAction)
vmd.on('menu-file-save-as', saveFileAsAction)
vmd.on('view-preview-toggle', togglePreviewMode)

return () => {
vmd.off('menu-file-save', saveFileAction)
vmd.off('menu-file-save-as', saveFileAsAction)
vmd.off('view-preview-toggle', togglePreviewMode)
}

Expand Down

0 comments on commit 32a43db

Please sign in to comment.