-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
371 lines (344 loc) · 9.6 KB
/
main.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
let editor = undefined
let activeId = '0'
let activeItems = {}
let dom = {}
const cacheMaxSize = 4 //后悔药
let _inputFocus = false
const nopFunction = () => {}
const initDom = () => {
dom = {
layout: document.getElementById('layout'),
selector: document.getElementById('selector'),
input: document.getElementById('input'),
revert: document.getElementById('revert'),
}
}
const setInputValue = (val) => {
dom.input.value = val
}
const createMarkdown = markdown => {
if (editor) { // only one editormd instance
editor.editor.remove()
}
createEditorDom()
document.getElementById('content').value = markdown
editor = editormd('editor', {
width: "100%",
height: 800,
placeholder : "think before you write...",
taskList : true,
tex : true,
emoji : true,
toolbarIconsClass : {
testIcon : "fa-gears" // 指定一个FontAawsome的图标类
},
toolbarIcons : function() {
let icons = editormd.toolbarModes['full']
if (icons[icons.length - 1] !== 'pdf') {
icons.push('pdf')
}
return icons
},
toolbarCustomIcons : {
pdf: `<image class="tool-icon" src="./images/pdf.svg" id="pdf"/>`
},
onchange : function () {
let content = document.getElementById('content')
if (!content) return
chrome.storage.local.set({[activeId]: content.value}, function() {
chrome.runtime.sendMessage({
type: 'update',
id: activeId
})
})
},
// markdown: markdown, // dynamic set Markdown text
path : "editor.md/lib/" // Autoload modules mode, codemirror, marked... dependents libs path
})
}
const createEditorDom = () => {
let div = document.createElement('div')
let textarea = document.createElement('textarea')
div.id = 'editor'
textarea.style = "display:none;height:100%"
textarea.id = 'content'
div.appendChild(textarea)
dom.layout.appendChild(div)
}
const recreateMarkdown = id => {
chrome.storage.local.get(id, data => {
createMarkdown(data[id])
})
}
function pageLoadHandler() {
initDom()
initListener()
const _init = (data) => {
createList(data.nodeList)
setInputValue(data.nodeList[activeId] && data.nodeList[activeId].name || '')
recreateMarkdown(activeId)
}
chrome.storage.local.get('nodeList', (data) => {
if (!data.nodeList) {
const _initData = {'nodeList': {}}
chrome.storage.local.set(_initData, function() {
_init(_initData)
})
} else {
_init(data)
}
})
}
function selectorListener (e) {
let target = e.target
let targetType = target.nodeName.toLocaleLowerCase()
let id = target.id || target.parentNode.id
if (targetType === 'div') {
if (activeId === id) return
activeItems[activeId].active = false
activeItems[id].active = true
activeId = id
setInputValue(activeItems[id].name)
chrome.storage.local.set({'nodeList': activeItems}, nopFunction)
recreateMarkdown(activeId)
} else if (targetType === 'img') {
deleteListener(id)
}
}
function createListDom (activeItems, isNeedRender) {
if (!isNeedRender) {
dom.selector.style.visibility = 'hidden'
return
}
while (dom.selector.firstChild) {
dom.selector.removeChild(dom.selector.firstChild)
}
dom.selector.style.visibility = 'visible'
const fragment = document.createDocumentFragment()
Object.keys(activeItems).forEach((i, idx) => {
const div = document.createElement('div')
const span = document.createElement('div')
const close = document.createElement('img')
close.src = './images/close.svg'
close.className = 'selector__item-delete'
span.className = 'selector__item-span'
div.appendChild(span)
div.appendChild(close)
span.innerText = activeItems[i].name
div.className = 'selector__item '
if (activeItems[i].active) {
div.className += activeItems[i] ? ' active' : ''
activeId = i
}
div.id = i
fragment.appendChild(div)
})
dom.selector.appendChild(fragment)
}
function createList (arr = {}, isNeedRender = false) {
const len = Object.keys(arr) // {id: isActive | boolean}
activeItems = arr
if (len.length === 0) { // empty
activeItems[0] = {
active: true,
name: 'note'
}
chrome.storage.local.set({'nodeList': activeItems}, nopFunction)
} else {
activeId = Object.keys(activeItems).find(id => {
return activeItems[id].active
}) || Object.keys(activeItems)[0] || '0'
}
createListDom(activeItems ,isNeedRender)
}
function deleteListener (id) {
let keys = Object.keys(activeItems)
if (keys.length === 1) return
chrome.storage.local.get('lastCache', function(data) {
let res = data.lastCache
let needDelete = undefined
if (!res || res.length < 1) {
res = []
}
if (!res.some(item => item.id === id)) {
res.push({
id: id,
name: activeItems[id].name
})
}
if (res.length >= cacheMaxSize) {
needDelete = res.shift()
}
chrome.storage.local.set({'lastCache': res}, nopFunction)
delete activeItems[id]
chrome.storage.local.set({'nodeList': activeItems}, () => {
chrome.runtime.sendMessage({
type: 'delete',
id: id
})
})
needDelete && chrome.storage.local.remove(String(needDelete.id), nopFunction)
if (activeId === id) {
activeId = Object.keys(activeItems)[0]
recreateMarkdown(activeId)
}
})
}
function revertListener () {
chrome.storage.local.get('lastCache', function(data) {
let res = data.lastCache
if (!res || res.length < 1) {
return
}
let _active = res.pop()
chrome.storage.local.set({'lastCache': res}, nopFunction)
activeItems[_active.id] = {
active: true,
name: _active.name
}
activeItems[activeId].active = false
activeId = _active.id
chrome.runtime.sendMessage({
type: 'add',
id: activeId,
name: _active.name
})
chrome.storage.local.set({'nodeList': activeItems}, nopFunction)
chrome.storage.local.get(activeId, (data) => {
setInputValue(_active.name)
createMarkdown(data[activeId])
})
})
}
const _getItems = (activeItems, value) => {
let newList = {}
for (let id in activeItems) {
if (String(activeItems[id].name).indexOf(value) >= 0) {
newList[id] = activeItems[id]
}
}
return newList
}
function inputBlurListener () {
_inputFocus = false
setTimeout(() => {
setInputValue(activeItems[activeId].name)
createListDom({}, false)
}, 200)
}
function inputInputListener (e, val) {
let value = e ? e.target.value.trim() : val
let newList = _getItems(activeItems, value)
let isNeedRender = Object.keys(newList).length > 0
createListDom(newList, isNeedRender)
}
function inputEnterListener (e) {
if (e.key === 'Enter') {
let value = e.target.value.trim()
let newList = _getItems(activeItems, value)
let flag = Object.keys(newList).find(i => {
return value === newList[i].name
})
if (flag === undefined) {
newCache(value)
} else {
let id = flag
if (activeId === id) return
activeItems[activeId].active = false
activeItems[id].active = true
activeId = id
setInputValue(activeItems[id].name)
chrome.storage.local.set({'nodeList': activeItems}, nopFunction)
recreateMarkdown(activeId)
}
}
}
function chromeRuntimeListener (request) {
const _update = (id) => {
if (id === activeId) {
location.reload()
}
}
const _delete = (id) => {
if (id === activeId) {
location.reload()
} else {
// 移除dom即可
delete activeItems[id]
}
}
const _add = (id, name) => {
activeItems[id] = {
active: false,
name: name
}
}
const dealFunctions = {
update: _update,
delete: _delete,
add: _add
}
let type = request.type
dealFunctions[type](String(request.id), request.name)
}
function initPrintListener () {
const reload = () => location.reload()
if (window.matchMedia) {
//返回一个新的 MediaQueryList 对象,表示指定的媒体查询字符串解析后的结果。
const mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (!mql.matches) {
reload();
}
})
}
window.onafterprint = reload;
}
function initListener () {
initPrintListener()
dom.input.addEventListener('blur', inputBlurListener)
dom.input.addEventListener('input', inputInputListener)
dom.input.addEventListener('keypress', inputEnterListener)
dom.input.addEventListener('click', () => {
if (!_inputFocus) {
setInputValue('')
inputInputListener(undefined, '')
}
_inputFocus = true
})
dom.selector.addEventListener('click', selectorListener)
dom.revert.addEventListener('click', revertListener)
chrome.runtime.onMessage.addListener(chromeRuntimeListener)
setTimeout(() => {
const pdf = document.getElementById('pdf')
if (pdf) {
pdf.addEventListener('click', generatePdf)
}
}, 2000)
}
function newCache (name) {
activeItems[activeId].active = false
activeId = Math.max(...Object.keys(activeItems).map(i => Number(i))) + 1
activeItems[activeId] = {
active: true,
name: name
}
chrome.storage.local.set({'nodeList': activeItems}, () => {
chrome.runtime.sendMessage({
type: 'add',
id: activeId,
name: name
})
})
createMarkdown('')
}
function generatePdf () {
const dom = document.getElementsByClassName('editormd-preview')[0]
dom.setAttribute('id', 'print-js')
document.body.innerHTML = ''
document.body.appendChild(dom)
setTimeout(() => {
print()
}, 1000)
}
window.addEventListener('load', pageLoadHandler)