diff --git a/main.js b/main.js index 16915bd..75aae2d 100644 --- a/main.js +++ b/main.js @@ -1,64 +1,69 @@ -const {app, BrowserWindow, Menu} = require('electron'); -const Trie = require('./src/DataStructures/Trie.js'); -const fs = require('fs'); -const History = require('./src/DataStructures/History.js'); -const menuTemplate= require('./src/js/menuBar.js'); - +const { app, BrowserWindow, Menu } = require("electron"); +const Trie = require("./src/DataStructures/Trie.js"); +const fs = require("fs"); +const History = require("./src/DataStructures/History.js"); +const menuTemplate = require("./src/js/menuBar.js"); let dictionary = new Trie(); let dictionaryDB; let history = new History(); -let mainWindow; try { - dictionaryDB = require(`${__dirname}/dotDictionaryData.json`); + dictionaryDB = require(`${__dirname}/dotDictionaryData.json`); } catch (err) { - dictionaryDB = { - data: [], - }; - fs.writeFile(`${__dirname}/dotDictionaryData.json`, JSON.stringify(dictionaryDB), 'utf8', function (err) { - if (err) { - console.log(err); - } else { - console.log("The Dictionary Data File was created successfully."); - } - }); + dictionaryDB = { + data: [], + }; + fs.writeFile( + `${__dirname}/dotDictionaryData.json`, + JSON.stringify(dictionaryDB), + "utf8", + function (err) { + if (err) { + console.log(err); + } else { + console.log("The Dictionary Data File was created successfully."); + } + } + ); } loadTrieData(dictionaryDB.data); - -app.on('ready', () => { - const mainWindow = new BrowserWindow({ - minWidth: 800, - minHeight: 600, - show: false - }); - mainWindow.loadURL(`file://${__dirname}/src/index.html`); - mainWindow.on('ready-to-show', () => { - mainWindow.show(); - }); - const mainMenu = Menu.buildFromTemplate(menuTemplate); - Menu.setApplicationMenu(mainMenu); - +app.on("ready", () => { + const mainWindow = new BrowserWindow({ + minWidth: 800, + minHeight: 600, + show: false, + }); + mainWindow.loadURL(`file://${__dirname}/src/index.html`); + mainWindow.on("ready-to-show", () => { + mainWindow.show(); + }); + const mainMenu = Menu.buildFromTemplate(menuTemplate); + Menu.setApplicationMenu(mainMenu); }); -app.on('before-quit', () => { - dictionaryDB.data = dictionary.getAll(); - fs.writeFile(`${__dirname}/dotDictionaryData.json`, JSON.stringify(dictionaryDB), 'utf8', function (err) { - if (err) { - console.log(err); - } else { - console.log("The Dictionary Data File was saved successfully."); - } - }); -}) +app.on("before-quit", () => { + dictionaryDB.data = dictionary.getAll(); + fs.writeFile( + `${__dirname}/dotDictionaryData.json`, + JSON.stringify(dictionaryDB), + "utf8", + function (err) { + if (err) { + console.log(err); + } else { + console.log("The Dictionary Data File was saved successfully."); + } + } + ); +}); async function loadTrieData(dictionaryDB) { - for (let word of dictionaryDB) { - dictionary.insert(word.word, word.type, word.definition); - - } + for (let word of dictionaryDB) { + dictionary.insert(word.word, word.type, word.definition); + } } exports.dictionary = dictionary; diff --git a/src/DataStructures/History.js b/src/DataStructures/History.js index b485476..0ed34ef 100644 --- a/src/DataStructures/History.js +++ b/src/DataStructures/History.js @@ -1,38 +1,41 @@ class HistoryNode { - constructor(actionCallback, undoCallback, actionParams, undoParams) { - this.actionCallback = actionCallback; - this.undoCallback = undoCallback; - this.actionParams = actionParams; - this.undoParams = undoParams; - } -}; + constructor(actionCallback, undoCallback, actionParams, undoParams) { + this.actionCallback = actionCallback; + this.undoCallback = undoCallback; + this.actionParams = actionParams; + this.undoParams = undoParams; + } +} class History { - constructor() { - this.head = -1; - this.historyArr = []; - } + constructor() { + this.head = -1; + this.historyArr = []; + } - addAction(actionCallback, undoCallback, actionParams, undoParams) { - this.historyArr - .splice(this.head + 1, 0, new HistoryNode(actionCallback, undoCallback, actionParams, undoParams)); - this.historyArr = this.historyArr.slice(0, this.head + 2); - this.head++; - } + addAction(actionCallback, undoCallback, actionParams, undoParams) { + this.historyArr.splice( + this.head + 1, + 0, + new HistoryNode(actionCallback, undoCallback, actionParams, undoParams) + ); + this.historyArr = this.historyArr.slice(0, this.head + 2); + this.head++; + } - undo() { - if (this.head == -1) return; - let params = this.historyArr[this.head].undoParams; - this.historyArr[this.head].undoCallback(...params); - this.head--; - } + undo() { + if (this.head == -1) return; + let params = this.historyArr[this.head].undoParams; + this.historyArr[this.head].undoCallback(...params); + this.head--; + } - redo() { - if(this.head == this.historyArr.length - 1) return; - let params = this.historyArr[this.head + 1].actionParams; - this.historyArr[this.head + 1].actionCallback(...params); - this.head++; - } -}; + redo() { + if (this.head == this.historyArr.length - 1) return; + let params = this.historyArr[this.head + 1].actionParams; + this.historyArr[this.head + 1].actionCallback(...params); + this.head++; + } +} -module.exports = History; \ No newline at end of file +module.exports = History; diff --git a/src/DataStructures/Trie.js b/src/DataStructures/Trie.js index 2cc7044..bc8960d 100644 --- a/src/DataStructures/Trie.js +++ b/src/DataStructures/Trie.js @@ -1,156 +1,155 @@ class TrieNode { - constructor() { - this.children = []; - this.definition = null; - this.type = null; - this.end = false; - } + constructor() { + this.children = []; + this.definition = null; + this.type = null; + this.end = false; + } } class Trie { - constructor() { - this.root = new TrieNode(); - this.size = 0; - } + constructor() { + this.root = new TrieNode(); + this.size = 0; + } + + insert(word, type, definition) { + word = word.toLowerCase(); + + let node = this.root; + for (let i = 0; i < word.length; ++i) { + if (!node.children[word[i]]) { + node.children[word[i]] = new TrieNode(); + } - insert(word, type, definition) { - word = word.toLowerCase(); - - let node = this.root; - for (let i = 0; i < word.length; ++i) { - if (!node.children[word[i]]) { - node.children[word[i]] = new TrieNode(); - } - - node = node.children[word[i]]; - } - node.type = type; - node.definition = definition; - node.end = true; - this.size++; + node = node.children[word[i]]; + } + node.type = type; + node.definition = definition; + node.end = true; + this.size++; + } + + edit(word, newType, newDefinition) { + word = word.toLowerCase(); + let node = this.root; + for (let i = 0; i < word.length; ++i) { + if (!node.children[word[i]]) { + return false; + } + + node = node.children[word[i]]; } - edit(word, newType, newDefinition) { - word = word.toLowerCase(); - let node = this.root; - for (let i = 0; i < word.length; ++i) { - if (!node.children[word[i]]) { - return false; - } - - node = node.children[word[i]]; - } - - if (newType) { - node.type = newType; - } - if (newDefinition) { - node.definition = newDefinition; - } - - return true; + if (newType) { + node.type = newType; + } + if (newDefinition) { + node.definition = newDefinition; } - delete(word) { - word = word.toLowerCase(); - if (!this.contains(word)) { - return; - } - let node = this.root; - let i; - for (i = 0; i < word.length; ++i) { - if (this.search(word.substring(0, i)).length == 1) { - node.children = []; - this.size--; - return; - } else { - node = node.children[word[i]]; - } - } - - if (this.search(word.substring(0, i)).length == 1) { - node = undefined; - } else { - node.type = undefined; - node.definition = undefined; - node.end = undefined; - } + return true; + } + + delete(word) { + word = word.toLowerCase(); + if (!this.contains(word)) { + return; + } + let node = this.root; + let i; + for (i = 0; i < word.length; ++i) { + if (this.search(word.substring(0, i)).length == 1) { + node.children = []; this.size--; + return; + } else { + node = node.children[word[i]]; + } } - getAll(head = this.root, path = "", tree = []) { - if (!head) return; - - if (head.end) { - tree.push({ - word: path, - type: head.type, - definition: head.definition - }); - } - - for(let i = 0; i < 26; ++i) { - const child = String.fromCharCode(97 + i); - if (head.children[child]) { - this.getAll(head.children[child], path + child, tree); - } - } - - return tree; + if (this.search(word.substring(0, i)).length == 1) { + node = undefined; + } else { + node.type = undefined; + node.definition = undefined; + node.end = undefined; + } + this.size--; + } + + getAll(head = this.root, path = "", tree = []) { + if (!head) return; + + if (head.end) { + tree.push({ + word: path, + type: head.type, + definition: head.definition, + }); + } + + for (let i = 0; i < 26; ++i) { + const child = String.fromCharCode(97 + i); + if (head.children[child]) { + this.getAll(head.children[child], path + child, tree); + } } - search(prefix) { - prefix = prefix.toLowerCase(); - let node = this.root; + return tree; + } - for(let i = 0; i < prefix.length; ++i) { - if (!node.children[prefix[i]]) { - return []; - } + search(prefix) { + prefix = prefix.toLowerCase(); + let node = this.root; - node = node.children[prefix[i]]; - } + for (let i = 0; i < prefix.length; ++i) { + if (!node.children[prefix[i]]) { + return []; + } - return this.getAll(node, prefix); + node = node.children[prefix[i]]; } - getInfo(word) { - word = word.toLowerCase(); - let node = this.root; - for (let i = 0; i < word.length; ++i) { - if (!node.children[word[i]]) { - return null; - } - - node = node.children[word[i]]; - } - if (!node.end) { - return null; - } - - return ({ - type: node.type, - definition: node.definition - }); - } + return this.getAll(node, prefix); + } - contains(word) { - word = word.toLowerCase(); - let node = this.root; - for (let i = 0; i < word.length; ++i) { - if (node.children[word[i]]) { - node = node.children[word[i]]; - } else { - return false; - } - } - return node.end; + getInfo(word) { + word = word.toLowerCase(); + let node = this.root; + for (let i = 0; i < word.length; ++i) { + if (!node.children[word[i]]) { + return null; + } + + node = node.children[word[i]]; + } + if (!node.end) { + return null; } - length() { - return this.size; + return { + type: node.type, + definition: node.definition, + }; + } + + contains(word) { + word = word.toLowerCase(); + let node = this.root; + for (let i = 0; i < word.length; ++i) { + if (node.children[word[i]]) { + node = node.children[word[i]]; + } else { + return false; + } } - -}; + return node.end; + } + + length() { + return this.size; + } +} module.exports = Trie; diff --git a/src/js/menuBar.js b/src/js/menuBar.js index 1de0c39..343b237 100644 --- a/src/js/menuBar.js +++ b/src/js/menuBar.js @@ -1,96 +1,87 @@ -let main = require('../../main.js') -const { remote, BrowserWindow } = require('electron'); +let main = require("../../main.js"); +const { BrowserWindow } = require("electron"); const menuTemplate = [ - - process.platform == 'darwin' ? { - label: 'dotDictionary', + process.platform == "darwin" + ? { + label: "dotDictionary", submenu: [ - { - label:'Quit dotDictionary', - role: 'quit' - } - ] - } : - {}, - { - label:'File', - submenu: [ - { - label: 'Add new word', - click(){ - BrowserWindow.getFocusedWindow().webContents.send('addWord'); - - }, - accelerator: 'CmdOrCtrl+N' - }, - process.platform=='win32'? - { - label: 'Quit dotDictionary', - role: 'quit' - - }: - {} - ] - }, - { - - label: 'Edit', - submenu: [ - { - label: 'Undo', - accelerator: 'CmdOrCtrl+Z', - click() { - main.history.undo(); - } - }, - { - label: 'Redo', - accelerator: process.platform === 'darwin' ? 'Command+Shift+Z' : 'Ctrl+Y', - click() { - main.history.redo(); - } - }, - { - label: 'Reload', - role: 'reload', - - }, - { - label: 'Toggle Developer Tools', - accelerator: 'CmdOrCtrl+Shift+I', - click(item, focusedWindow) { - if (focusedWindow) focusedWindow.webContents.toggleDevTools() - } - }, - { - label: 'Copy', - role: 'copy', - accelerator: 'CmdOrCtrl+C', - }, - { - label: 'Paste', - role: 'paste', - accelerator: 'CmdOrCtrl+V', - }, - - ] - }, - { - label: 'Window', -submenu:[ - { - role: 'minimize' + { + label: "Quit dotDictionary", + role: "quit", + }, + ], + } + : {}, + { + label: "File", + submenu: [ + { + label: "Add new word", + click() { + BrowserWindow.getFocusedWindow().webContents.send("addWord"); + }, + accelerator: "CmdOrCtrl+N", }, + process.platform == "win32" + ? { + label: "Quit dotDictionary", + role: "quit", + } + : {}, + ], + }, + { + label: "Edit", + submenu: [ { - role: 'togglefullscreen' - } -] - } - - - - - -] -module.exports = menuTemplate; \ No newline at end of file + label: "Undo", + accelerator: "CmdOrCtrl+Z", + click() { + main.history.undo(); + }, + }, + { + label: "Redo", + accelerator: + process.platform === "darwin" ? "Command+Shift+Z" : "Ctrl+Y", + click() { + main.history.redo(); + }, + }, + { + label: "Reload", + role: "reload", + }, + { + label: "Toggle Developer Tools", + accelerator: "CmdOrCtrl+Shift+I", + click(item, focusedWindow) { + if (focusedWindow) focusedWindow.webContents.toggleDevTools(); + }, + }, + { + label: "Copy", + role: "copy", + accelerator: "CmdOrCtrl+C", + }, + { + label: "Paste", + role: "paste", + accelerator: "CmdOrCtrl+V", + }, + ], + }, + { + label: "Window", + submenu: [ + { + role: "minimize", + }, + { + role: "togglefullscreen", + }, + ], + }, +]; +module.exports = menuTemplate;