~dricottone/noticable

ref: 0a6377c125879214ce5190763e811a8d6f6f75f4 noticable/renderer.js -rw-r--r-- 4.2 KiB
0a6377c1Dominic Ricottone 0.2 rewrite 2 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
////////////////////////////
// Monaco magic goes here //
////////////////////////////
require.config({ paths: { vs: 'node_modules/monaco-editor/min/vs' } });
require(['vs/editor/editor.main'], function () {
  window.editor = monaco.editor.create(document.getElementById('container-editor'), {
    automaticLayout: true,
    language: 'markdown',
    minimap: {
      enabled: false
    },
    value: '',
    wordWrap: 'on',
  });
});


///////////////////////
// Functions go here //
///////////////////////

// TODO: Add documentation for all functions.

// Parse a file name and return a pretty note title.
function cleanTitle(filename) {
  return filename.split(".")[0].split("_").join(" ");
};

function showEditor() {
  $("#container-editor").addClass("focused");
  $("#container-viewer").removeClass("focused");
};

function showViewer() {
  $("#container-editor").removeClass("focused");
  $("#container-viewer").addClass("focused");
};

function addTitle(filename) {
  let element = document.createElement("li");
  element.innerHTML = cleanTitle(filename);
  element.addEventListener("click", () => { preloadContentForCheckThenRead(filename); });
  $("#list-filenames").append(element)
};

function newTitle(filename) {
  $("#list-filenames li").removeClass("focused")
  let selector = "#list-filenames li:contains(" + cleanTitle(filename) + ")"
  $(selector).addClass("focused")
};

function sortTitles() {
  let sidebar = $("#list-filenames")
  let titles = sidebar.children("li")
  titles.detach().sort((a,b) => a.innerText.localeCompare(b.innerText))
  sidebar.append(titles)
};

function newContent(content) {
  window.editor.setValue(content);
};

function newHTML(html) {
  $("#container-viewer").html(html);
};

function preloadContentForSave() {
  window.postMessage({ type: "contentForSave", text: window.editor.getValue() }, "*");
};

function preloadContentForSaveThenNew() {
  window.postMessage({ type: "contentForSaveThenNew", text: window.editor.getValue() }, "*");
};

function preloadContentForSaveThenRead(filename) {
  window.postMessage({ type: "contentForSaveThenRead", text: { note: window.editor.getValue(), title: filename } }, "*");
};

function preloadContentForCheckThenNew() {
  window.postMessage({ type: "contentForCheckThenNew", text: window.editor.getValue() }, "*");
};

function preloadContentForCheckThenRead(filename) {
  window.postMessage({ type: "contentForCheckThenRead", text: { note: window.editor.getValue(), title: filename } }, "*");
};

function preloadContentForRender() {
  window.postMessage({ type: "contentForRender", text: window.editor.getValue() }, "*");
};


////////////////////////////
// Listen for events here //
////////////////////////////
window.addEventListener("message", (event) => {
  if (event.source != window) return;
  if (event.data.type) {
    let parameter = event.data.text;
    switch(event.data.type) {
      case "addTitle":
        addTitle(parameter);
        sortTitles();
        break;
      case "addTitleOrdered":
        addTitle(parameter);
        break;
      case "newTitle":
        newTitle(parameter);
        break;
      case "newContent":
        newContent(parameter);
        break;
      case "newHTML":
        newHTML(parameter);
        break;
      case "sendContentForSave":
        preloadContentForSave();
        break;
      case "sendContentForSaveThenNew":
        preloadContentForSaveThenNew();
        break;
      case "sendContentForSaveThenRead":
        preloadContentForSaveThenRead(parameter);
        break;
      case "sendContentForCheckThenNew":
        preloadContentForCheckThenNew();
        break;
      case "sendContentForRender":
        preloadContentForRender();
        break;
      case "showEditor":
        showEditor();
        break;
      case "showViewer":
        showViewer();
        break;
      case "fileNotSaved":
        // TODO: add some UI component to indicate an error occured
        break;
      case "fileDiscarded":
        // TODO: add some UI component to indicate an error occured
        break;
      case "fileUnreadable":
        // TODO: add some UI component to indicate an error occured
        break;
      case "fileUnwritable":
        // TODO: add some UI component to indicate an error occured
        break;
    }
  }
}, false);