~dricottone/noticable

ref: 2b896b8a90b76ebf5befb8d4f9dbff6b8e1f68f9 noticable/preload.js -rw-r--r-- 5.1 KiB
2b896b8aDominic Ricottone Minimal working program 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
// constants
const { ipcRenderer } = require("electron");
const fs = require("fs")
const path = require("path");
const md = require("markdown-it")();
let currentFilename = "";
let currentFileContent = "";

// configuration
const newFileButton = "+ New Note";
const notesDir = "notes";

// messaging
function requestLocalFilename() {
  console.log("[preload -> main] requesting local filename...");
  ipcRenderer.send("request-local-filename", "");
}
function postFileText(text) {
  console.log("[preload -> renderer] posting file text...");
  window.postMessage({ type: "post-file-text", text: text }, "*");
}
function postDisplayFilename(filename) {
  console.log("[preload -> renderer] posting display file name...");
  window.postMessage({ type: "post-display-filename", text: filename }, "*");
}
function postHTML(html) {
  console.log("[preload -> renderer] posting HTML...");
  window.postMessage({ type: "post-html", text: html }, "*");
}
function requestEditorText() {
  console.log("[preload -> renderer] requesting editor text...");
  window.postMessage({ type: "request-editor-text" }, "*");
}
function requestFocusEditor() {
  console.log("[preload -> renderer] requesting focus editor...");
  window.postMessage({ type: "request-focus-editor" }, "*");
}
function requestUnfocusEditor() {
  console.log("[preload -> renderer] requesting unfocus editor...");
  window.postMessage({ type: "request-unfocus-editor" }, "*");
}
function requestToggleEditor() {
  console.log("[preload -> renderer] requesting toggle editor focus...");
  window.postMessage({ type: "request-toggle-editor" }, "*");
}
function requestAlertInvalidFilename() {
  console.log("[preload -> renderer] 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);
    });
  }
}
function renderMarkdown(content) {
  postHTML(md.render(content));
}
function updateState(filename, content) {
  currentFilename = filename;
  currentFileContent = content;
  postFileText(content);
}
function saveFile(content) {
  if (currentFilename != "") {
    currentFileContent = content;
    console.log("[preload] writing file '" + currentFilename + "'");
    fs.writeFile(currentFilename, currentFileContent, (err) => {
      if (err) throw err;
    });
  } else {
    console.log("[preload] no filename; cannot write file");
  }
}
function saveFileConditional(content) {
  if (currentFilename != "") {
    if (content != currentFileContent) {
      currentFileContent = content;
      console.log("[preload] writing updated file '" + currentFilename + "'");
      fs.writeFile(currentFilename, currentFileContent, (err) => {
        if (err) throw err;
      });
      return true;
    } else {
      console.log("[preload] no updates; not writing file");
      return false;
    }
  } else {
    console.log("[preload] no filename; cannot write file");
    return false;
  }
}

// listen to main process
ipcRenderer.on("post-new-filename", (event, filename) => {
  console.log("[preload] caught file name");
  currentFilename = getActualFilename(filename);
  postDisplayFilename(getPrettyFilename(filename));
});
ipcRenderer.on("key-save-text", () => {
  console.log("[preload] caught keybind for save text");
  requestEditorText();
});
ipcRenderer.on("key-render-markdown", () => {
  console.log("[preload] caught keybind for render markdown");
  requestEditorText();
  requestUnfocusEditor();
});
ipcRenderer.on("key-focus-editor", () => {
  console.log("[preload] caught keybind for focus editor");
  requestFocusEditor();
});
ipcRenderer.on("key-unfocus-editor", () => {
  console.log("[preload] 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":
        console.log("[preload] caught editor text");
        saveFileConditional(event.data.text);
        renderMarkdown(event.data.text);
        break;
      case "request-file-text":
        console.log("[preload] caught request for file text");
        readFile(event.data.text);
        break;
    }
  }
}, false);

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