~dricottone/noticable

ref: e5420fd16006ea9714d3a26ad63fcd40f7420704 noticable/preload.js -rw-r--r-- 5.2 KiB
e5420fd1Dominic Ricottone Add new generated package-lock.json for release 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// to disable logging, comment out the console.log line
function debug(message) {
  //console.log("[preload] " + message);
}

// constants
const { ipcRenderer } = require("electron");
const fs = require("fs");
const os = require("os");
const path = require("path");
const md = require("markdown-it")({ html: true });
let currentFilename = "";
let currentFileContent = "";

// configuration
const newFileButton = "+ New Note";
const notesDir = path.join(os.homedir(), "notes");

// messaging
function requestLocalFilename() {
  debug("requesting local filename...");
  ipcRenderer.send("request-local-filename", "");
}
function postFileText(text) {
  debug("posting file text...");
  window.postMessage({ type: "post-file-text", text: text }, "*");
}
function postDisplayFilename(filename) {
  debug("posting display file name '" + filename + "'...");
  window.postMessage({ type: "post-display-filename", text: filename }, "*");
}
function postHTML(html) {
  debug("posting HTML...");
  window.postMessage({ type: "post-html", text: html }, "*");
}
function requestEditorText() {
  debug("requesting editor text...");
  window.postMessage({ type: "request-editor-text" }, "*");
}
function requestFocusEditor() {
  debug("requesting focus editor...");
  window.postMessage({ type: "request-focus-editor" }, "*");
}
function requestUnfocusEditor() {
  debug("requesting unfocus editor...");
  window.postMessage({ type: "request-unfocus-editor" }, "*");
}
function requestToggleEditor() {
  debug("requesting toggle editor focus...");
  window.postMessage({ type: "request-toggle-editor" }, "*");
}
function requestHighlightFilename(filename) {
  debug("requesting highlight filename...");
  window.postMessage({ type: "request-highlight-filename", text: filename }, "*");
}
function requestAlertInvalidFilename() {
  debug("requesting alert about invalid filename...");
  window.postMessage({ type: "request-alert-invalid-filename" }, "*");
}

// utilities
function initNotesList() {
  // initialize the new file button
  postDisplayFilename(newFileButton);
  // initialize the notes files
  fs.readdir(notesDir, (err, files) => {
    if (err) throw err;
    files.forEach(file => {
      postDisplayFilename(getPrettyFilename(file));
    });
  });
}
function getActualFilename(filename) {
  unprettyFilename = path.basename(filename, ".md").split(" ").join("_") + ".md";
  return path.join(notesDir, unprettyFilename);
}
function getPrettyFilename(filename) {
  return path.basename(filename, ".md").split("_").join(" ");
}
function readFile(filename) {
  if (filename == newFileButton) {
    updateState("","");
    requestLocalFilename();
  } else {
    let actualFilename = getActualFilename(filename);
    fs.readFile(actualFilename, "utf8", (err, content) => {
      if (err) requestAlertInvalidFilename();
      updateState(actualFilename,content);
      requestHighlightFilename(filename);
    });
  }
}
function renderMarkdown(content) {
  postHTML(md.render(content));
}
function updateState(filename, content) {
  currentFilename = filename;
  currentFileContent = content;
  postFileText(content);
}
function saveFile(content) {
  if (currentFilename != "") {
    currentFileContent = content;
    debug("writing file '" + currentFilename + "'");
    fs.writeFile(currentFilename, currentFileContent, (err) => {
      if (err) throw err;
    });
  } else {
    debug("no filename; halting write file");
  }
}
function saveFileConditional(content) {
  if (currentFilename != "") {
    if (content != currentFileContent || currentFileContent == "") {
      currentFileContent = content;
      debug("writing updated file '" + currentFilename + "'");
      fs.writeFile(currentFilename, currentFileContent, (err) => {
        if (err) throw err;
      });
      return true;
    } else {
      debug("no updates to file; halting write file");
      return false;
    }
  } else {
    debug("no filename; halting write file");
    return false;
  }
}

// listen to main process
ipcRenderer.on("post-new-filename", (event, filename) => {
  debug("caught file name '" + filename + "'");
  currentFilename = getActualFilename(filename);
  prettyFilename = getPrettyFilename(filename);
  postDisplayFilename(prettyFilename);
  requestHighlightFilename(prettyFilename);
});
ipcRenderer.on("key-save-text", () => {
  debug("caught keybind for save text");
  requestEditorText();
});
ipcRenderer.on("key-render-markdown", () => {
  debug("caught keybind for render markdown");
  requestEditorText();
  requestUnfocusEditor();
});
ipcRenderer.on("key-focus-editor", () => {
  debug("caught keybind for focus editor");
  requestFocusEditor();
});
ipcRenderer.on("key-unfocus-editor", () => {
  debug("caught keybind for unfocus editor");
  requestUnfocusEditor();
});

// listen to renderer
window.addEventListener("message", (event) => {
  if (event.source != window) return;
  if (event.data.type) {
    switch(event.data.type) {
      case "post-editor-text":
        debug("caught editor text");
        saveFileConditional(event.data.text);
        renderMarkdown(event.data.text);
        break;
      case "request-file-text":
        filename = event.data.text
        debug("caught request for text of file '" + filename + "'");
        readFile(filename);
        break;
    }
  }
}, false);

// initialize renderer
window.addEventListener("load", initNotesList);