-
Notifications
You must be signed in to change notification settings - Fork 1
/
org_mode.js
172 lines (153 loc) · 5.35 KB
/
org_mode.js
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
document.addEventListener("DOMContentLoaded", (event) => {
"use strict";
let componentManagerInstance;
let workingNote;
let clientData;
let lastValue;
let lastUUID;
let editor;
let ignoreTextChange = false;
let initialLoad = true;
const onReceivedNote = (note) => {
if (note.uuid !== lastUUID) {
// Note has changed, reset last values:
lastValue = null;
initialLoad = true;
lastUUID = note.uuid;
}
workingNote = note;
// Only update UI on non-metadata updates.
if (note.isMetadataUpdate) {
return;
}
clientData = note.clientData;
if (editor) {
if (note.content.text !== lastValue) {
ignoreTextChange = true;
editor.getDoc().setValue(workingNote.content.text);
ignoreTextChange = false;
}
if (initialLoad) {
initialLoad = false;
editor.getDoc().clearHistory();
// Initialize with everything folded, like how Emacs does it:
editor.execCommand('unfoldAll');
editor.execCommand('foldAll');
}
}
};
const loadComponentManager = () => {
let permissions = [{ name: "stream-context-item" }];
componentManagerInstance = new ComponentManager(permissions, () => {
// Ready, go!
const platform = componentManagerInstance.platform;
if (platform) {
document.body.classList.add(platform);
}
});
componentManagerInstance.streamContextItem((note) => {
onReceivedNote(note);
});
};
const save = () => {
if (workingNote) {
// Be sure to capture this object as a variable, as this.note may be reassigned in `streamContextItem`, so by the time
// you modify it in the presave block, it may not be the same object anymore, so the presave values will not be
// applied to the right object, and it will save incorrectly.
let note = workingNote;
componentManagerInstance.saveItemWithPresave(note, () => {
lastValue = editor.getValue();
note.content.text = lastValue;
note.clientData = clientData;
note.content.preview_plain = null;
note.content.preview_html = null;
});
}
};
const loadEditor = () => {
editor = CodeMirror.fromTextArea(document.querySelector(".orgmode"), {
autofocus: true,
foldGutter: {
minFoldSize: 1,
},
foldOptions: {
widget: " ...",
},
gutters: ["CodeMirror-foldgutter"],
indentUnit: 4,
// keyMap: "emacs",
lineNumbers: false,
lineWrapping: true,
mode: "orgmode",
});
editor.setSize("100%", "100%");
let wait;
let changing = false;
editor.on("change", (cm, change) => {
if (ignoreTextChange) {
return;
}
clearTimeout(wait);
wait = setTimeout(() => {
changing = true;
cm.wrapParagraphsInRange(
change.from,
CodeMirror.changeEnd(change)
);
changing = false;
}, 200);
save();
});
};
loadEditor();
loadComponentManager();
// Crazy dark mode detector.
// try {
// const dataFromRoot = document.querySelector('.__data_from_root__');
// if (dataFromRoot) {
// let isDarkMode =
// JSON.parse(
// getComputedStyle(dataFromRoot)
// .color.replace(/^rgb\(/, '[')
// .replace(/\)$/, ']')
// ).filter((color) => {
// return (color < 150);
// }).length > 1;
// if (isDarkMode) {
// editor.getWrapperElement().style.filter =
// 'invert(1) hue-rotate(180deg)';
// } else {
// editor.getWrapperElement().style.filter = '';
// }
// }
// } catch (err) {
// console.warn('Dark mode detection failed: ', err);
// }
// Change themes:
const themeFilters = {
light: "",
dark: "invert(1) hue-rotate(180deg)",
};
const themeChooser = document.querySelector(".theme-chooser");
themeChooser.addEventListener("click", (event) => {
if (/INPUT/.test(event.target.tagName)) {
editor.getWrapperElement().style.filter =
themeFilters[event.target.value];
window.localStorage.setItem(
"orgModePreferences",
JSON.stringify({
themeFilter: event.target.value,
})
);
}
});
// Load saved theme filters from local storage:
const result = window.localStorage.getItem("orgModePreferences");
if (result) {
const orgModePreferences = JSON.parse(result);
const defaultTheme = orgModePreferences
? orgModePreferences.themeFilter
: "light";
document.querySelector(`[value=${defaultTheme}]`).click();
}
});