Skip to content

Commit

Permalink
scrap ini config for json,add ability to enable autosaving and disabl…
Browse files Browse the repository at this point in the history
…e automatic node creation, store/restore GUI state in config file, better fix for #84
  • Loading branch information
blurymind committed Feb 8, 2019
1 parent c089daa commit c54dd56
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 53 deletions.
7 changes: 5 additions & 2 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<!-- import and set global libraries from node_modules -->
<script>
const electron = require("electron");
const ipc = require("electron").ipcRenderer;
const remote = electron.remote;

window.ko = require('knockout'); // keep static file too for now, as this is a bit wobbly on higher zoom levels
window.$ = window.jQuery = require('jquery');
require('jquery-mousewheel')($);
Expand Down Expand Up @@ -172,10 +175,10 @@
</div>
</div> -->

<input type="text" data-bind="value: app.editing().tags;" onfocus="app.togglePreviewMode(true)" onblur="app.togglePreviewMode(false)" >
<input type="text" data-bind="value: app.editing().tags;" onfocus="app.togglePreviewMode(true)">

<div class="bbcode-toolbar">
<button class="bbcode-button" onclick="app.openLastEditedNode()" title="Go Back to last edited">&#8617</button>
<button class="bbcode-button" onclick="app.openLastEditedNode()" title="Go Back to last edited" onfocus="app.togglePreviewMode(false)">&#8617</button>
&nbsp;&nbsp;&nbsp;
<button class="bbcode-button" onclick="app.insertBBcodeTags('b')" title="Bold"><b>B</b></button>
<button class="bbcode-button" onclick="app.insertBBcodeTags('u')" title="Underlined"><u>U</u></button>
Expand Down
88 changes: 45 additions & 43 deletions app/js/classes/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const remote = electron.remote;
// const remote = electron.remote;
const contextMenu = require("jquery-contextmenu")
const { getWordsList } = require('most-common-words-by-language')
const ini = require('multi-ini')

//todo store color palette
var App = function(name, version) {
Expand Down Expand Up @@ -31,20 +30,27 @@ var App = function(name, version) {
this.shifted = false;
this.isElectron = false;
this.editor = null;
this.autocompleteEnabled = true;
this.autocompleteWordsEnabled = true;
this.showCounter = false;
this.spellcheckEnabled = true;
this.nightModeEnabled = false;
this.nodeVisitHistory = [];
this.mouseX = 0;
this.mouseY = 0;

this.UPDATE_ARROWS_THROTTLE_MS = 25;

// this.parser = new ini.Parser();
this.iniFilePath = null
this.config = null
this.configFilePath = null
this.config = {
nightModeEnabled:false,
spellcheckEnabled:true,
showCounter:false,
autocompleteWordsEnabled:true,
autocompleteEnabled:true,
overwrites:{
makeNewNodesFromLinks:true
},
settings:{
autoSave:-1
}
}

//this.editingPath = ko.observable(null);

Expand Down Expand Up @@ -82,15 +88,8 @@ var App = function(name, version) {
if (e.keyCode == 27) self.clearSearch();
});

// Load ini settings
this.iniFilePath = path.join(remote.app.getPath('home'),'.yarn-story-editor.ini')
if (fs.existsSync(this.iniFilePath)){
// const parser = new ini.Parser();
// content = parser.parse(lines);
this.config = ini.read(this.iniFilePath, {line_breaks: 'windows'})
console.log("Settings loaded from:\n" + this.iniFilePath)
// console.log(this.config)
}
// Load json app settings from home folder
data.tryLoadConfigFile()

// prevent click bubbling
ko.bindingHandlers.preventBubble = {
Expand Down Expand Up @@ -830,16 +829,16 @@ var App = function(name, version) {
.transition({ y: "0" }, 250);
self.editor = ace.edit("editor");
var autoCompleteButton = document.getElementById("toglAutocomplete");
autoCompleteButton.checked = self.autocompleteEnabled;
autoCompleteButton.checked = self.config.autocompleteEnabled;
var autoCompleteWordsButton = document.getElementById("toglAutocompleteWords");
autoCompleteWordsButton.checked = self.autocompleteWordsEnabled;
autoCompleteWordsButton.checked = self.config.autocompleteWordsEnabled;
var spellCheckButton = document.getElementById("toglSpellCheck");
spellCheckButton.checked = self.spellcheckEnabled;
spellCheckButton.checked = self.config.spellcheckEnabled;
var nightModeButton = document.getElementById("toglNightMode");
nightModeButton.checked = self.nightModeEnabled;
nightModeButton.checked = self.config.nightModeEnabled;
self.toggleNightMode();
var showCounterButton = document.getElementById("toglShowCounter");
showCounterButton.checked = self.showCounter;
showCounterButton.checked = self.config.showCounter;
self.toggleShowCounter()

/// set color picker
Expand Down Expand Up @@ -885,7 +884,7 @@ var App = function(name, version) {
};

this.openNodeByTitle = function(nodeTitle) {
self.makeNewNodesFromLinks();
self.makeNodeWithName(nodeTitle);
app.nodes().forEach((node) => {
if (node.title() === nodeTitle.trim()){
self.editNode(node)
Expand Down Expand Up @@ -918,7 +917,7 @@ var App = function(name, version) {

this.toggleSpellCheck = function() {
var spellCheckButton = document.getElementById("toglSpellCheck");
self.spellcheckEnabled = spellCheckButton.checked;
self.config.spellcheckEnabled = spellCheckButton.checked;
if (spellCheckButton.checked) {
enable_spellcheck();
} else {
Expand All @@ -928,9 +927,9 @@ var App = function(name, version) {

this.toggleNightMode = function() {
var nightModeButton = document.getElementById("toglNightMode");
self.nightModeEnabled = nightModeButton.checked;
self.config.nightModeEnabled = nightModeButton.checked;
var cssOverwrite = {};
if (self.nightModeEnabled) {
if (self.config.nightModeEnabled) {
cssOverwrite = {filter: 'invert(100%)'}
} else {
cssOverwrite = {filter: 'invert(0%)'}
Expand All @@ -944,8 +943,8 @@ var App = function(name, version) {

this.toggleShowCounter = function() {
var showCounterButton = document.getElementById("toglShowCounter");
self.showCounter = showCounterButton.checked;
if (self.showCounter) {
self.config.showCounter = showCounterButton.checked;
if (self.config.showCounter) {
$(".node-editor .form .bbcode-toolbar .editor-counter").css({display: "initial"});
} else {
$(".node-editor .form .bbcode-toolbar .editor-counter").css({display: "none"});
Expand All @@ -954,10 +953,10 @@ var App = function(name, version) {

this.toggleWordCompletion = function() {
var wordCompletionButton = document.getElementById("toglAutocompleteWords");
self.autocompleteWordsEnabled = wordCompletionButton.checked;
self.config.autocompleteWordsEnabled = wordCompletionButton.checked;
self.editor.setOptions({
enableBasicAutocompletion: self.autocompleteWordsEnabled,
enableLiveAutocompletion: self.autocompleteWordsEnabled
enableBasicAutocompletion: self.config.autocompleteWordsEnabled,
enableLiveAutocompletion: self.config.autocompleteWordsEnabled
});
}

Expand Down Expand Up @@ -1114,10 +1113,10 @@ var App = function(name, version) {
});

var autoCompleteButton = document.getElementById("toglAutocomplete");
self.autocompleteEnabled = autoCompleteButton.checked;
self.config.autocompleteEnabled = autoCompleteButton.checked;

var autoCompleteWordsButton = document.getElementById("toglAutocompleteWords");
self.autocompleteWordsEnabled = autoCompleteWordsButton.checked;
self.config.autocompleteWordsEnabled = autoCompleteWordsButton.checked;

setTimeout(self.updateSearch, 100);
}
Expand Down Expand Up @@ -1178,22 +1177,25 @@ var App = function(name, version) {
};

this.makeNewNodesFromLinks = function(){
if (this.config && this.config.overwrites.makeNewNodesFromLinks === 'false') {
console.info("Autocreation of new nodes from links is disabled in:\n" + this.iniFilePath)
if (this.config && this.config.overwrites && !this.config.overwrites.makeNewNodesFromLinks) {
console.info("Autocreation of new nodes from links is disabled in:\n" + this.configFilePath)
return
}
var otherNodeTitles = self.getOtherNodeTitles()

};
var nodeLinks = self.editing().getLinksInNode();
if (nodeLinks == undefined){return}
for (var i = 0; i < nodeLinks.length; i ++)
{
// Create new Nodes from Node Links
const newNodeName = nodeLinks[i].trim()
if (newNodeName && newNodeName.length > 0 && !otherNodeTitles.includes(newNodeName) && newNodeName != self.editing().title()){
var newNodeOffset = 220 * (i+1);
self.newNodeAt(self.editing().x() + newNodeOffset, self.editing().y() - 120).title(newNodeName);
}
var newNodeOffset = 220 * (i+1);
self.makeNodeWithName(newNodeName,newNodeOffset)
}
};

this.makeNodeWithName = function(newNodeName,newNodeOffset=220){
const otherNodeTitles = self.getOtherNodeTitles();
if (newNodeName && newNodeName.length > 0 && !otherNodeTitles.includes(newNodeName) && newNodeName != self.editing().title()){
self.newNodeAt(self.editing().x() + newNodeOffset, self.editing().y() - 120).title(newNodeName);
}
};

Expand Down
33 changes: 30 additions & 3 deletions app/js/classes/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ var FILETYPE = {
YARNTEXT: "yarn.txt"
};

const ipc = require("electron").ipcRenderer;
var path = require('path');
var fs = require('fs');
// const ipc = require("electron").ipcRenderer;
const path = require('path');
const fs = require('fs');

ipc.on("selected-file", function(event, selectedPath, operation) {
if (operation == "tryOpenFile") {
Expand All @@ -32,6 +32,16 @@ ipc.on("loadYarnDataObject", function(event, yarnData) {
data.loadData(JSON.stringify(yarnData), FILETYPE.JSON, true);
});

ipc.on("appIsClosing", function(event) {
if (app.configFilePath && app.config) {
fs.writeFile(app.configFilePath, JSON.stringify(app.config), function(err) {
if(err) {
return console.log(err);
}
});
}
})

var data = {
editingPath: ko.observable(null),
editingType: ko.observable(""),
Expand All @@ -42,6 +52,23 @@ var data = {
path.join(path.dirname(filePath),addSubPath) :
path.dirname(filePath)
},
tryLoadConfigFile: function(){
app.configFilePath = path.join(remote.app.getPath('home'),'.yarn-story-editor.json')
if (fs.existsSync(app.configFilePath)) {
app.config = JSON.parse(fs.readFileSync(app.configFilePath));
console.log("Settings loaded from:\n" + app.configFilePath)
// if autosaving has a value in the config - value is in minutes
if (app.config && app.config.settings && app.config.settings.autoSave) {
if (app.config.settings.autoSave < 0.1) {return}
setInterval(function () {
if (data.editingPath()) {
data.trySaveCurrent();
console.log("Autosaved:\n" + data.editingPath())
}
}, app.config.settings.autoSave * 60000);
}
}
},
readFile: function(e, filename, clearNodes) {
if (app.fs != undefined) {
if (
Expand Down
7 changes: 3 additions & 4 deletions app/js/libs/mode-yarn.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
var Behaviour = require("./behaviour").Behaviour;

var YarnHighlightRules = function() {

this.$rules = {
start: [
{
Expand Down Expand Up @@ -106,7 +105,7 @@ $.contextMenu({
}
}
// suggest word corrections if the selected word is misspelled
if (app.spellcheckEnabled) {
if (app.config.spellcheckEnabled) {
var suggestedCorrections = app.getSpellCheckSuggestionItems();
if (suggestedCorrections !== false) {
options.items.corrections = {name: "Correct word" ,items: suggestedCorrections}
Expand Down Expand Up @@ -138,8 +137,8 @@ $.contextMenu({
/// Enable autocompletion via word guessing
var langTools = ace.require("ace/ext/language_tools");
app.editor.setOptions({
enableBasicAutocompletion: app.autocompleteWordsEnabled,
enableLiveAutocompletion: app.autocompleteWordsEnabled
enableBasicAutocompletion: app.config.autocompleteWordsEnabled,
enableLiveAutocompletion: app.config.autocompleteWordsEnabled
});

var commonWordList = getWordsList('english');
Expand Down
2 changes: 2 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ function createWindow() {
}

mainWindow.on("close", function(event) {
mainWindow.webContents.send('appIsClosing', event);

event.preventDefault();
if (yarnRunnerWindow) {
yarnRunnerWindow.destroy();
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"jquery.transit": "^0.9.12",
"knockout": "^3.4.2",
"most-common-words-by-language": "^2.17.0",
"multi-ini": "^2.0.0",
"nspell": "^2.1.1",
"spectrum-colorpicker": "^1.8.0"
}
Expand Down

0 comments on commit c54dd56

Please sign in to comment.