Repository navigation
Expand file tree
/
Copy patheditor.html
More file actions
147 lines (138 loc) · 6.63 KB
/
Copy patheditor.html
File metadata and controls
147 lines (138 loc) · 6.63 KB
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
<!doctype html>
<html lang="en" data-unitas-app="editor">
<head>
<meta charset="utf-8">
<title>Editor</title>
<style>
html, body { height:100%; margin:0; background:#1e1e1e; color:#d4d4d4; overflow:hidden;
font: 14px -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif; }
#root { display:flex; flex-direction:column; height:100%; }
#panes { flex:1; display:flex; min-height:0; }
#editor { flex:1; min-width:0; }
#preview { flex:1; min-width:0; overflow:auto; padding:20px 32px; border-left:1px solid #333; background:#1e1e1e; display:none; }
#preview.on { display:block; }
#status { height:24px; display:flex; align-items:center; gap:16px; padding:0 12px; background:#007acc; color:#fff; font-size:12px; }
#status .sp { flex:1; }
#status .link { cursor:pointer; }
#msg { padding:24px; color:#f48771; }
/* markdown preview */
#preview h1, #preview h2 { border-bottom:1px solid #333; padding-bottom:.3em; }
#preview h1, #preview h2, #preview h3 { margin:1.4em 0 .6em; line-height:1.3; }
#preview a { color:#4aa3ff; }
#preview code { background:#2d2d2d; padding:.15em .4em; border-radius:4px; font-family:Consolas, "SF Mono", Menlo, monospace; font-size:.9em; }
#preview pre { background:#252526; padding:12px 14px; border-radius:6px; overflow:auto; }
#preview pre code { background:none; padding:0; }
#preview blockquote { margin:1em 0; padding:0 1em; color:#9a9a9a; border-left:4px solid #444; }
#preview table { border-collapse:collapse; }
#preview th, #preview td { border:1px solid #444; padding:6px 12px; }
#preview img { max-width:100%; }
#preview hr { border:0; border-top:1px solid #333; }
</style>
</head>
<body>
<div id="root">
<div id="panes">
<div id="editor"></div>
<div id="preview"></div>
</div>
<div id="status">
<span id="fname"></span>
<span id="lang"></span>
<span class="sp"></span>
<span id="pvbtn" class="link" style="display:none">Preview (:p)</span>
<span id="savebtn" class="link">Save (:w)</span>
</div>
</div>
<!-- UMD libs must load before Monaco's AMD loader, or they register as AMD modules instead of globals -->
<script src="./node_modules/marked/lib/marked.umd.js"></script>
<script src="./node_modules/dompurify/dist/purify.min.js"></script>
<script src="./node_modules/monaco-editor/min/vs/loader.js"></script>
<script>
(async function () {
const params = new URLSearchParams(location.search);
const filePath = params.get('file');
const fileUrl = params.get('url');
const base = location.href.split('?')[0].replace(/\/[^/]*$/, '') + '/node_modules/monaco-editor/min';
const name = filePath.split(/[\\/]/).pop();
const ext = (name.split('.').pop() || '').toLowerCase();
let text;
try { text = await api.read(); }
catch (e) { document.getElementById('root').innerHTML = '<div id="msg">Could not load file: ' + String(e.message || e) + '</div>'; return; }
self.MonacoEnvironment = {
getWorkerUrl: () => 'data:text/javascript;charset=utf-8,' + encodeURIComponent(
"self.MonacoEnvironment={baseUrl:'" + base + "/'};importScripts('" + base + "/vs/base/worker/workerMain.js');")
};
require.config({ paths: { vs: base + '/vs' } });
require(['vs/editor/editor.main'], function () {
const lang = (monaco.languages.getLanguages().find(l => (l.extensions || []).includes('.' + ext)) || { id: 'plaintext' }).id;
const model = monaco.editor.createModel(text, lang);
const editor = monaco.editor.create(document.getElementById('editor'), {
model, theme: 'vs-dark', automaticLayout: true, wordWrap: lang === 'markdown' ? 'on' : 'off',
minimap: { enabled: false }, fontSize: 14, scrollBeyondLastLine: false
});
const isMd = lang === 'markdown';
const preview = document.getElementById('preview');
const pvbtn = document.getElementById('pvbtn');
document.getElementById('fname').textContent = filePath;
document.getElementById('lang').textContent = lang;
// --- dirty / save ---
let saved = text;
const setTitle = () => { document.title = (model.getValue() !== saved ? '● ' : '') + name; };
setTitle();
async function save() {
const v = model.getValue();
try { await api.write(v); saved = v; setTitle(); }
catch (e) { alert('Could not save file: ' + (e.message || e)); }
}
document.getElementById('savebtn').onclick = save;
let togglePreview = () => {};
// Save / preview come from the app's :w / :p commands (there are no Ctrl shortcuts)
api.onCommand(name => { if (name === 'save') save(); else if (name === 'preview') togglePreview(); });
// --- markdown preview ---
if (isMd) {
pvbtn.style.display = '';
let timer;
const render = () => {
const html = DOMPurify.sanitize(marked.parse(model.getValue(), { gfm: true, breaks: false }));
preview.innerHTML = html;
preview.querySelectorAll('img').forEach(img => {
const s = img.getAttribute('src');
if (s && !/^[a-z][a-z0-9+.-]*:/i.test(s)) { try { img.src = new URL(s, fileUrl).href; } catch (e) {} }
});
preview.querySelectorAll('a[href]').forEach(a => { a.target = '_blank'; });
};
const toggle = () => { preview.classList.toggle('on'); if (preview.classList.contains('on')) render(); };
pvbtn.onclick = toggle;
togglePreview = toggle;
model.onDidChangeContent(() => {
setTitle();
if (!preview.classList.contains('on')) return;
clearTimeout(timer); timer = setTimeout(render, 150);
});
// proportional scroll sync editor -> preview
editor.onDidScrollChange(() => {
if (!preview.classList.contains('on')) return;
const max = editor.getScrollHeight() - editor.getLayoutInfo().height;
if (max > 0) preview.scrollTop = (editor.getScrollTop() / max) * (preview.scrollHeight - preview.clientHeight);
});
preview.classList.add('on'); render(); // split view by default for markdown
} else {
model.onDidChangeContent(setTitle);
}
// Page commands from the content script (content/scroller.js, content/insert.js).
// The editor opens in NORMAL mode: "i" (or a click) puts the cursor in it.
addEventListener('unitas:app-scroll', e => {
const d = JSON.parse(e.detail || '{}');
const y = d.axis !== 'x';
if (d.to) {
if (y) editor.setScrollTop(d.to === 'start' ? 0 : editor.getScrollHeight());
else editor.setScrollLeft(d.to === 'start' ? 0 : editor.getScrollWidth());
} else if (y) editor.setScrollTop(editor.getScrollTop() + (d.by || 0));
else editor.setScrollLeft(editor.getScrollLeft() + (d.by || 0));
});
addEventListener('unitas:app-focus', () => editor.focus());
});
})();
</script>
</body>
</html>