-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
46 lines (36 loc) · 1016 Bytes
/
editor.js
File metadata and controls
46 lines (36 loc) · 1016 Bytes
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
// @ts-check
import { renderGraphdown } from './src';
/** @type {HTMLTextAreaElement} */
// @ts-ignore
const editor = document.getElementById('editor');
const output = document.getElementById('output');
/** Load editor data */
function loadData() {
return localStorage.getItem('editor-data');
}
/**
* Save editor data
* @param {string} data
*/
function saveData(data) {
localStorage.setItem('editor-data', data);
}
// Render data into target
let scheduledRefresh = null;
function scheduleRefresh(data, target) {
cancelAnimationFrame(scheduledRefresh);
scheduledRefresh = requestAnimationFrame(() => {
target.innerHTML = renderGraphdown(data, { minWidth: 80 });
});
}
// Refresh output from editor value
function refresh() {
scheduleRefresh(editor.value, output);
}
// Load & render data on init
editor.value = loadData();
// Save data on edit
editor.addEventListener('input', () => saveData(editor.value));
// Render on edit
editor.addEventListener('input', () => refresh());
refresh();