Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 51 additions & 46 deletions main.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
65 changes: 34 additions & 31 deletions src/DataStructures/History.js
Original file line number Diff line number Diff line change
@@ -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;
module.exports = History;
Loading