wikiparser-node 1.24.0 → 1.24.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundle/bundle-lsp.min.js +36 -0
- package/bundle/bundle.min.js +29 -0
- package/dist/bin/config.js +1 -1
- package/dist/lib/lsp.d.ts +2 -2
- package/dist/lib/lsp.js +2 -2
- package/extensions/dist/base.js +375 -0
- package/extensions/dist/codejar.js +57 -0
- package/extensions/dist/editor.js +159 -0
- package/extensions/dist/highlight.js +30 -0
- package/extensions/dist/lint.js +80 -0
- package/extensions/dist/lsp.js +89 -0
- package/extensions/editor.css +59 -0
- package/extensions/ui.css +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
var _a;
|
|
3
|
+
const version = '1.24.1', src = (_a = document.currentScript) === null || _a === void 0 ? void 0 : _a.src, file = /\/extensions\/dist\/base\.(?:min\.)?js$/u, CDN = src && file.test(src)
|
|
4
|
+
? src.replace(file, '')
|
|
5
|
+
: `https://testingcf.jsdelivr.net/npm/wikiparser-node@${version}`;
|
|
6
|
+
const workerJS = () => {
|
|
7
|
+
importScripts('$CDN/bundle/bundle-lsp.min.js');
|
|
8
|
+
const entities = { '&': 'amp', '<': 'lt', '>': 'gt' }, lsps = new Map(), last = { include: true };
|
|
9
|
+
const parse = (wikitext, include = false, stage) => {
|
|
10
|
+
if (stage === undefined && last.wikitext === wikitext && last.include === include) {
|
|
11
|
+
return last.root;
|
|
12
|
+
}
|
|
13
|
+
const root = Parser.parse(wikitext, include, stage);
|
|
14
|
+
if (stage === undefined) {
|
|
15
|
+
last.wikitext = wikitext;
|
|
16
|
+
last.include = include;
|
|
17
|
+
last.root = root;
|
|
18
|
+
}
|
|
19
|
+
return root;
|
|
20
|
+
};
|
|
21
|
+
const getLSP = (id, include = true) => {
|
|
22
|
+
if (lsps.has(id)) {
|
|
23
|
+
return lsps.get(id);
|
|
24
|
+
}
|
|
25
|
+
const lsp = Parser.createLanguageService();
|
|
26
|
+
lsp.include = include;
|
|
27
|
+
lsps.set(id, lsp);
|
|
28
|
+
return lsp;
|
|
29
|
+
};
|
|
30
|
+
const parseColor = (s) => {
|
|
31
|
+
var _a;
|
|
32
|
+
if (s.startsWith('#')) {
|
|
33
|
+
const short = s.length < 7;
|
|
34
|
+
return [
|
|
35
|
+
parseInt(short ? s.charAt(1).repeat(2) : s.slice(1, 3), 16),
|
|
36
|
+
parseInt(short ? s.charAt(2).repeat(2) : s.slice(3, 5), 16),
|
|
37
|
+
parseInt(short ? s.charAt(3).repeat(2) : s.slice(5, 7), 16),
|
|
38
|
+
parseInt((short ? s.charAt(4).repeat(2) : s.slice(7, 9)) || 'ff', 16)
|
|
39
|
+
/ 255,
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
const values = s.slice(s.indexOf('(') + 1, -1).trim()
|
|
43
|
+
.split(/\s+(?:[,/]\s*)?|[,/]\s*/u)
|
|
44
|
+
.map(v => parseFloat(v) / (v.endsWith('%') ? 100 : 1));
|
|
45
|
+
return [
|
|
46
|
+
values[0],
|
|
47
|
+
values[1],
|
|
48
|
+
values[2],
|
|
49
|
+
(_a = values[3]) !== null && _a !== void 0 ? _a : 1,
|
|
50
|
+
];
|
|
51
|
+
};
|
|
52
|
+
self.onmessage = ({ data }) => {
|
|
53
|
+
const [command, qid, wikitext, include, stage, newName] = data;
|
|
54
|
+
switch (command) {
|
|
55
|
+
case 'setI18N':
|
|
56
|
+
Parser.i18n = qid;
|
|
57
|
+
break;
|
|
58
|
+
case 'setLintConfig':
|
|
59
|
+
Parser.lintConfig = qid;
|
|
60
|
+
break;
|
|
61
|
+
case 'setConfig':
|
|
62
|
+
Parser.config = qid;
|
|
63
|
+
delete last.wikitext;
|
|
64
|
+
break;
|
|
65
|
+
case 'getConfig':
|
|
66
|
+
postMessage([command, qid, Parser.getConfig()]);
|
|
67
|
+
break;
|
|
68
|
+
case 'json':
|
|
69
|
+
postMessage([command, qid, parse(wikitext, include, stage).json()]);
|
|
70
|
+
break;
|
|
71
|
+
case 'lint':
|
|
72
|
+
postMessage([command, qid, parse(wikitext, include).lint(), wikitext]);
|
|
73
|
+
break;
|
|
74
|
+
case 'print':
|
|
75
|
+
postMessage([
|
|
76
|
+
command,
|
|
77
|
+
qid,
|
|
78
|
+
parse(wikitext, include, stage).childNodes.map(child => [
|
|
79
|
+
stage !== null && stage !== void 0 ? stage : Infinity,
|
|
80
|
+
String(child),
|
|
81
|
+
child.type === 'text'
|
|
82
|
+
? String(child).replace(/[&<>]/gu, p => `&${entities[p]};`)
|
|
83
|
+
: child.print(),
|
|
84
|
+
]),
|
|
85
|
+
]);
|
|
86
|
+
break;
|
|
87
|
+
case 'destroy':
|
|
88
|
+
getLSP(qid).destroy();
|
|
89
|
+
lsps.delete(qid);
|
|
90
|
+
break;
|
|
91
|
+
case 'data':
|
|
92
|
+
getLSP(qid, include).data = wikitext;
|
|
93
|
+
break;
|
|
94
|
+
case 'colorPresentations':
|
|
95
|
+
postMessage([command, qid, getLSP(qid, include).provideColorPresentations(wikitext)]);
|
|
96
|
+
break;
|
|
97
|
+
case 'documentColors':
|
|
98
|
+
(async () => {
|
|
99
|
+
postMessage([
|
|
100
|
+
command,
|
|
101
|
+
qid,
|
|
102
|
+
await getLSP(qid, include).provideDocumentColors(parseColor, wikitext, false),
|
|
103
|
+
wikitext,
|
|
104
|
+
]);
|
|
105
|
+
})();
|
|
106
|
+
break;
|
|
107
|
+
case 'foldingRanges':
|
|
108
|
+
(async () => {
|
|
109
|
+
postMessage([command, qid, await getLSP(qid, include).provideFoldingRanges(wikitext), wikitext]);
|
|
110
|
+
})();
|
|
111
|
+
break;
|
|
112
|
+
case 'links':
|
|
113
|
+
(async () => {
|
|
114
|
+
postMessage([command, qid, await getLSP(qid, include).provideLinks(wikitext), wikitext]);
|
|
115
|
+
})();
|
|
116
|
+
break;
|
|
117
|
+
case 'diagnostics':
|
|
118
|
+
(async () => {
|
|
119
|
+
postMessage([
|
|
120
|
+
command,
|
|
121
|
+
qid,
|
|
122
|
+
await getLSP(qid, include).provideDiagnostics(wikitext, stage),
|
|
123
|
+
wikitext,
|
|
124
|
+
]);
|
|
125
|
+
})();
|
|
126
|
+
break;
|
|
127
|
+
case 'completionItems':
|
|
128
|
+
(async () => {
|
|
129
|
+
postMessage([
|
|
130
|
+
command,
|
|
131
|
+
qid,
|
|
132
|
+
await getLSP(qid, include).provideCompletionItems(wikitext, stage),
|
|
133
|
+
wikitext,
|
|
134
|
+
]);
|
|
135
|
+
})();
|
|
136
|
+
break;
|
|
137
|
+
case 'references':
|
|
138
|
+
(async () => {
|
|
139
|
+
postMessage([
|
|
140
|
+
command,
|
|
141
|
+
qid,
|
|
142
|
+
await getLSP(qid, include).provideReferences(wikitext, stage),
|
|
143
|
+
wikitext,
|
|
144
|
+
]);
|
|
145
|
+
})();
|
|
146
|
+
break;
|
|
147
|
+
case 'definition':
|
|
148
|
+
(async () => {
|
|
149
|
+
postMessage([
|
|
150
|
+
command,
|
|
151
|
+
qid,
|
|
152
|
+
await getLSP(qid, include).provideDefinition(wikitext, stage),
|
|
153
|
+
wikitext,
|
|
154
|
+
]);
|
|
155
|
+
})();
|
|
156
|
+
break;
|
|
157
|
+
case 'renameLocation':
|
|
158
|
+
(async () => {
|
|
159
|
+
postMessage([
|
|
160
|
+
command,
|
|
161
|
+
qid,
|
|
162
|
+
await getLSP(qid, include).resolveRenameLocation(wikitext, stage),
|
|
163
|
+
wikitext,
|
|
164
|
+
]);
|
|
165
|
+
})();
|
|
166
|
+
break;
|
|
167
|
+
case 'renameEdits':
|
|
168
|
+
(async () => {
|
|
169
|
+
postMessage([
|
|
170
|
+
command,
|
|
171
|
+
qid,
|
|
172
|
+
await getLSP(qid, include).provideRenameEdits(wikitext, stage, newName),
|
|
173
|
+
wikitext,
|
|
174
|
+
]);
|
|
175
|
+
})();
|
|
176
|
+
break;
|
|
177
|
+
case 'hover':
|
|
178
|
+
(async () => {
|
|
179
|
+
postMessage([command, qid, await getLSP(qid, include).provideHover(wikitext, stage), wikitext]);
|
|
180
|
+
})();
|
|
181
|
+
break;
|
|
182
|
+
case 'signatureHelp':
|
|
183
|
+
(async () => {
|
|
184
|
+
postMessage([
|
|
185
|
+
command,
|
|
186
|
+
qid,
|
|
187
|
+
await getLSP(qid, include).provideSignatureHelp(wikitext, stage),
|
|
188
|
+
wikitext,
|
|
189
|
+
]);
|
|
190
|
+
})();
|
|
191
|
+
break;
|
|
192
|
+
case 'inlayHints':
|
|
193
|
+
(async () => {
|
|
194
|
+
postMessage([command, qid, await getLSP(qid, include).provideInlayHints(wikitext), wikitext]);
|
|
195
|
+
})();
|
|
196
|
+
break;
|
|
197
|
+
case 'codeAction':
|
|
198
|
+
(async () => {
|
|
199
|
+
postMessage([
|
|
200
|
+
command,
|
|
201
|
+
qid,
|
|
202
|
+
await getLSP(qid, include).provideRefactoringAction(wikitext, stage),
|
|
203
|
+
wikitext,
|
|
204
|
+
]);
|
|
205
|
+
})();
|
|
206
|
+
break;
|
|
207
|
+
case 'resolveCodeAction':
|
|
208
|
+
postMessage([
|
|
209
|
+
command,
|
|
210
|
+
qid,
|
|
211
|
+
getLSP(qid, include).resolveCodeAction({
|
|
212
|
+
title: `Fix all: ${wikitext || 'WikiLint'}`,
|
|
213
|
+
kind: 'source.fixAll',
|
|
214
|
+
data: { rule: wikitext },
|
|
215
|
+
}),
|
|
216
|
+
wikitext,
|
|
217
|
+
]);
|
|
218
|
+
break;
|
|
219
|
+
case 'findStyleTokens':
|
|
220
|
+
postMessage([command, qid, getLSP(qid).findStyleTokens().map(token => token.json())]);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
const blob = new Blob([`(${String(workerJS).replace('$CDN', CDN)})()`], { type: 'text/javascript' }), url = URL.createObjectURL(blob), worker = new Worker(url);
|
|
225
|
+
URL.revokeObjectURL(url);
|
|
226
|
+
const getListener = (command, qid, resolve, raw) => {
|
|
227
|
+
const listener = ({ data: [cmd, rid, res, resRaw] }) => {
|
|
228
|
+
if (cmd === command && rid === qid && (raw === undefined || raw === resRaw)) {
|
|
229
|
+
worker.removeEventListener('message', listener);
|
|
230
|
+
resolve(res);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
return listener;
|
|
234
|
+
};
|
|
235
|
+
const setI18N = (i18n) => {
|
|
236
|
+
worker.postMessage(['setI18N', i18n]);
|
|
237
|
+
};
|
|
238
|
+
const setLintConfig = (config) => {
|
|
239
|
+
worker.postMessage(['setLintConfig', config]);
|
|
240
|
+
};
|
|
241
|
+
const setConfig = (config) => {
|
|
242
|
+
worker.postMessage(['setConfig', config]);
|
|
243
|
+
wikiparse.config = config;
|
|
244
|
+
};
|
|
245
|
+
const getFeedback = (command, qid, strict, raw, ...args) => new Promise(resolve => {
|
|
246
|
+
worker.addEventListener('message', getListener(command, qid, resolve, strict ? raw : undefined));
|
|
247
|
+
worker.postMessage([command, qid, raw, ...args]);
|
|
248
|
+
});
|
|
249
|
+
const getConfig = () => getFeedback('getConfig', -3);
|
|
250
|
+
const json = (wikitext, include, qid = -4, stage) => getFeedback('json', qid, false, wikitext, include, stage);
|
|
251
|
+
const print = (wikitext, include, stage, qid = -1) => getFeedback('print', qid, false, wikitext, include, stage);
|
|
252
|
+
const lint = (wikitext, include, qid = -2) => getFeedback('lint', qid, true, wikitext, include);
|
|
253
|
+
const provide = (command, qid, wikitext, ...args) => getFeedback(command, qid, typeof wikitext === 'string', wikitext, ...args);
|
|
254
|
+
const append = (parent, text) => {
|
|
255
|
+
if (text) {
|
|
256
|
+
parent.append(text);
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
const splitNewLine = (html) => {
|
|
260
|
+
let cur = html.cloneNode();
|
|
261
|
+
cur.style.padding = '';
|
|
262
|
+
const result = [cur];
|
|
263
|
+
for (const child of html.childNodes) {
|
|
264
|
+
const { textContent } = child;
|
|
265
|
+
if (!(textContent === null || textContent === void 0 ? void 0 : textContent.includes('\n'))) {
|
|
266
|
+
cur.append(child.cloneNode(true));
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
const lines = child.nodeType === Node.TEXT_NODE
|
|
270
|
+
? textContent.split('\n')
|
|
271
|
+
: splitNewLine(child);
|
|
272
|
+
append(cur, lines[0]);
|
|
273
|
+
for (const text of lines.slice(1)) {
|
|
274
|
+
cur = html.cloneNode();
|
|
275
|
+
cur.style.padding = '';
|
|
276
|
+
result.push(cur);
|
|
277
|
+
append(cur, text);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return result;
|
|
281
|
+
};
|
|
282
|
+
const getGutter = (container) => container.querySelector('.wikiparser-line-numbers');
|
|
283
|
+
const size = (html) => {
|
|
284
|
+
const container = html.parentElement, { isContentEditable } = html, { clientHeight } = container, gutter = getGutter(container);
|
|
285
|
+
if (!gutter) {
|
|
286
|
+
intersectionObserver.unobserve(html);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
html.style.marginLeft = '';
|
|
290
|
+
const start = Number(html.dataset['start'] || 1), lines = splitNewLine(html), width = `${String(lines.length + start - 1).length + 1.5}ch`;
|
|
291
|
+
html.style.marginLeft = width;
|
|
292
|
+
gutter.style.width = width;
|
|
293
|
+
if (isContentEditable) {
|
|
294
|
+
gutter.style.minHeight = `${clientHeight + 1}px`;
|
|
295
|
+
}
|
|
296
|
+
const sizer = document.createElement('span'), { style: { paddingLeft, paddingRight } } = html;
|
|
297
|
+
sizer.className = 'wikiparser-sizer';
|
|
298
|
+
sizer.style.paddingLeft = paddingLeft;
|
|
299
|
+
sizer.style.paddingRight = paddingRight;
|
|
300
|
+
for (const child of lines) {
|
|
301
|
+
sizer.append(child, '\n');
|
|
302
|
+
}
|
|
303
|
+
html.append(sizer);
|
|
304
|
+
let line, lastTop;
|
|
305
|
+
for (let i = 0; i < lines.length; i++) {
|
|
306
|
+
const child = lines[i], { top } = child.getBoundingClientRect();
|
|
307
|
+
if (line) {
|
|
308
|
+
line.style.height = `${top - lastTop}px`;
|
|
309
|
+
}
|
|
310
|
+
line = document.createElement('span');
|
|
311
|
+
line.textContent = String(i + start);
|
|
312
|
+
gutter.append(line);
|
|
313
|
+
lastTop = top;
|
|
314
|
+
}
|
|
315
|
+
if (line) {
|
|
316
|
+
if (!isContentEditable && html.offsetHeight <= clientHeight) {
|
|
317
|
+
line.style.height = `${container.getBoundingClientRect().top + container.scrollHeight - lastTop}px`;
|
|
318
|
+
container.style.overflowY = 'hidden';
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
line.style.height = `${html.getBoundingClientRect().bottom - lastTop}px`;
|
|
322
|
+
if (!isContentEditable) {
|
|
323
|
+
container.style.overflowY = '';
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
sizer.remove();
|
|
328
|
+
intersectionObserver.unobserve(html);
|
|
329
|
+
};
|
|
330
|
+
const intersectionObserver = new IntersectionObserver(entries => {
|
|
331
|
+
for (const entry of entries) {
|
|
332
|
+
if (!entry.isIntersecting) {
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
size(entry.target);
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
const lineNumbers = (html, start = 1, paddingTop = '', paddingBottom = '') => {
|
|
339
|
+
const container = html.parentElement, gutter = document.createElement('span');
|
|
340
|
+
html.dataset['start'] = String(start);
|
|
341
|
+
gutter.className = 'wikiparser-line-numbers';
|
|
342
|
+
gutter.style.paddingTop = paddingTop;
|
|
343
|
+
gutter.style.paddingBottom = paddingBottom;
|
|
344
|
+
container.classList.add('wikiparse-container');
|
|
345
|
+
container.append(gutter);
|
|
346
|
+
if (html.isContentEditable) {
|
|
347
|
+
html.style.paddingBottom = `${container.clientHeight}px`;
|
|
348
|
+
}
|
|
349
|
+
if (getComputedStyle(html).whiteSpace !== 'pre') {
|
|
350
|
+
html.style.whiteSpace = 'pre-wrap';
|
|
351
|
+
}
|
|
352
|
+
if (html.offsetParent) {
|
|
353
|
+
size(html);
|
|
354
|
+
}
|
|
355
|
+
else {
|
|
356
|
+
intersectionObserver.observe(html);
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
const wikiparse = {
|
|
360
|
+
version,
|
|
361
|
+
CDN,
|
|
362
|
+
id: 0,
|
|
363
|
+
config: {},
|
|
364
|
+
setI18N,
|
|
365
|
+
setLintConfig,
|
|
366
|
+
setConfig,
|
|
367
|
+
getConfig,
|
|
368
|
+
print,
|
|
369
|
+
lint,
|
|
370
|
+
json,
|
|
371
|
+
lineNumbers,
|
|
372
|
+
provide,
|
|
373
|
+
};
|
|
374
|
+
Object.assign(window, { wikiparse });
|
|
375
|
+
})();
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
const codejar = (async () => {
|
|
3
|
+
const { CodeJar } = 'CodeJar' in globalThis
|
|
4
|
+
? globalThis
|
|
5
|
+
: await import('https://testingcf.jsdelivr.net/npm/codejar-async');
|
|
6
|
+
return (textbox, include, linenums) => {
|
|
7
|
+
var _a;
|
|
8
|
+
if (!(textbox instanceof HTMLTextAreaElement)) {
|
|
9
|
+
throw new TypeError('wikiparse.codejar方法仅可用于textarea元素!');
|
|
10
|
+
}
|
|
11
|
+
const preview = document.createElement('div'), root = document.createElement('span'), { offsetHeight, selectionStart: start, selectionEnd: end, style: { height, paddingTop, paddingBottom, paddingLeft, paddingRight }, } = textbox;
|
|
12
|
+
preview.className = 'wikiparser wikiparse-container';
|
|
13
|
+
preview.tabIndex = 0;
|
|
14
|
+
preview.style.height = offsetHeight ? `${offsetHeight}px` : height;
|
|
15
|
+
preview.style.paddingTop = paddingTop;
|
|
16
|
+
preview.style.paddingBottom = paddingBottom;
|
|
17
|
+
root.className = 'wpb-root';
|
|
18
|
+
root.style.paddingLeft = paddingLeft;
|
|
19
|
+
root.style.paddingRight = paddingRight;
|
|
20
|
+
preview.append(root);
|
|
21
|
+
textbox.after(preview);
|
|
22
|
+
textbox.style.display = 'none';
|
|
23
|
+
preview.addEventListener('focus', () => {
|
|
24
|
+
root.focus();
|
|
25
|
+
});
|
|
26
|
+
const id = wikiparse.id++;
|
|
27
|
+
const highlight = async (e) => (await wikiparse.print(e.textContent, jar.include, undefined, id)).map(([, , printed]) => printed)
|
|
28
|
+
.join('');
|
|
29
|
+
const jar = {
|
|
30
|
+
...CodeJar(root, highlight, {
|
|
31
|
+
spellcheck: true,
|
|
32
|
+
autoclose: { open: '', close: '' },
|
|
33
|
+
}),
|
|
34
|
+
include: Boolean(include),
|
|
35
|
+
editor: root,
|
|
36
|
+
};
|
|
37
|
+
if (linenums) {
|
|
38
|
+
jar.onHighlight(e => {
|
|
39
|
+
var _a;
|
|
40
|
+
(_a = e.parentNode.querySelector('.wikiparser-line-numbers')) === null || _a === void 0 ? void 0 : _a.remove();
|
|
41
|
+
wikiparse.lineNumbers(e, 1, paddingTop, paddingBottom);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
jar.restore({ start: 0, end: 0 });
|
|
45
|
+
jar.updateCode(textbox.value);
|
|
46
|
+
jar.restore({ start, end });
|
|
47
|
+
(_a = textbox.form) === null || _a === void 0 ? void 0 : _a.addEventListener('submit', () => {
|
|
48
|
+
textbox.value = jar.toString();
|
|
49
|
+
});
|
|
50
|
+
return jar;
|
|
51
|
+
};
|
|
52
|
+
})();
|
|
53
|
+
wikiparse.codejar = codejar;
|
|
54
|
+
(async () => {
|
|
55
|
+
wikiparse.codejar = await codejar;
|
|
56
|
+
})();
|
|
57
|
+
})();
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _Printer_instances, _Printer_id, _Printer_preview, _Printer_textbox, _Printer_root, _Printer_viewportChanged, _Printer_running, _Printer_ticks, _Printer_tick, _Printer_exec, _Printer_paint, _Printer_coarsePrint, _Printer_finePrint;
|
|
14
|
+
class Printer {
|
|
15
|
+
constructor(preview, textbox, include) {
|
|
16
|
+
_Printer_instances.add(this);
|
|
17
|
+
_Printer_id.set(this, void 0);
|
|
18
|
+
_Printer_preview.set(this, void 0);
|
|
19
|
+
_Printer_textbox.set(this, void 0);
|
|
20
|
+
_Printer_root.set(this, void 0);
|
|
21
|
+
_Printer_viewportChanged.set(this, void 0);
|
|
22
|
+
_Printer_running.set(this, void 0);
|
|
23
|
+
_Printer_ticks.set(this, void 0);
|
|
24
|
+
__classPrivateFieldSet(this, _Printer_id, wikiparse.id++, "f");
|
|
25
|
+
__classPrivateFieldSet(this, _Printer_preview, preview, "f");
|
|
26
|
+
__classPrivateFieldSet(this, _Printer_textbox, textbox, "f");
|
|
27
|
+
__classPrivateFieldSet(this, _Printer_root, [], "f");
|
|
28
|
+
__classPrivateFieldSet(this, _Printer_viewportChanged, false, "f");
|
|
29
|
+
this.include = Boolean(include);
|
|
30
|
+
__classPrivateFieldSet(this, _Printer_ticks, [0, undefined], "f");
|
|
31
|
+
}
|
|
32
|
+
queue(delay, method) {
|
|
33
|
+
const [state] = __classPrivateFieldGet(this, _Printer_ticks, "f");
|
|
34
|
+
if (delay === 0 || state <= 0 || method === 0 || __classPrivateFieldGet(this, _Printer_ticks, "f")[1] !== 0) {
|
|
35
|
+
__classPrivateFieldSet(this, _Printer_ticks, [delay, method], "f");
|
|
36
|
+
if (delay === 0) {
|
|
37
|
+
__classPrivateFieldGet(this, _Printer_instances, "m", _Printer_exec).call(this, method);
|
|
38
|
+
}
|
|
39
|
+
else if (state <= 0) {
|
|
40
|
+
__classPrivateFieldGet(this, _Printer_instances, "m", _Printer_tick).call(this);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
_Printer_id = new WeakMap(), _Printer_preview = new WeakMap(), _Printer_textbox = new WeakMap(), _Printer_root = new WeakMap(), _Printer_viewportChanged = new WeakMap(), _Printer_running = new WeakMap(), _Printer_ticks = new WeakMap(), _Printer_instances = new WeakSet(), _Printer_tick = function _Printer_tick() {
|
|
46
|
+
setTimeout(() => {
|
|
47
|
+
const [t, method] = __classPrivateFieldGet(this, _Printer_ticks, "f");
|
|
48
|
+
if (t > 0) {
|
|
49
|
+
__classPrivateFieldGet(this, _Printer_ticks, "f")[0] -= 500;
|
|
50
|
+
if (t <= 500) {
|
|
51
|
+
__classPrivateFieldGet(this, _Printer_instances, "m", _Printer_exec).call(this, method);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
__classPrivateFieldGet(this, _Printer_instances, "m", _Printer_tick).call(this);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}, 500);
|
|
58
|
+
}, _Printer_exec = function _Printer_exec(method) {
|
|
59
|
+
if (method === 0) {
|
|
60
|
+
__classPrivateFieldGet(this, _Printer_instances, "m", _Printer_coarsePrint).call(this);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
__classPrivateFieldGet(this, _Printer_instances, "m", _Printer_finePrint).call(this);
|
|
64
|
+
}
|
|
65
|
+
}, _Printer_paint = function _Printer_paint() {
|
|
66
|
+
__classPrivateFieldGet(this, _Printer_preview, "f").innerHTML = `<span class="wpb-root">${__classPrivateFieldGet(this, _Printer_root, "f").map(([, , printed]) => printed).join('')}</span> `;
|
|
67
|
+
__classPrivateFieldGet(this, _Printer_preview, "f").scrollTop = __classPrivateFieldGet(this, _Printer_textbox, "f").scrollTop;
|
|
68
|
+
__classPrivateFieldGet(this, _Printer_preview, "f").classList.remove('active');
|
|
69
|
+
__classPrivateFieldGet(this, _Printer_textbox, "f").style.color = 'transparent';
|
|
70
|
+
}, _Printer_coarsePrint = async function _Printer_coarsePrint() {
|
|
71
|
+
if (__classPrivateFieldGet(this, _Printer_running, "f")) {
|
|
72
|
+
return __classPrivateFieldGet(this, _Printer_running, "f");
|
|
73
|
+
}
|
|
74
|
+
const { include } = this, { value } = __classPrivateFieldGet(this, _Printer_textbox, "f"), parsed = await wikiparse.print(value, include, 2, __classPrivateFieldGet(this, _Printer_id, "f"));
|
|
75
|
+
if (this.include !== include || __classPrivateFieldGet(this, _Printer_textbox, "f").value !== value) {
|
|
76
|
+
__classPrivateFieldSet(this, _Printer_running, undefined, "f");
|
|
77
|
+
__classPrivateFieldSet(this, _Printer_running, __classPrivateFieldGet(this, _Printer_instances, "m", _Printer_coarsePrint).call(this), "f");
|
|
78
|
+
return __classPrivateFieldGet(this, _Printer_running, "f");
|
|
79
|
+
}
|
|
80
|
+
__classPrivateFieldSet(this, _Printer_root, parsed, "f");
|
|
81
|
+
__classPrivateFieldGet(this, _Printer_instances, "m", _Printer_paint).call(this);
|
|
82
|
+
__classPrivateFieldSet(this, _Printer_running, undefined, "f");
|
|
83
|
+
__classPrivateFieldSet(this, _Printer_running, __classPrivateFieldGet(this, _Printer_instances, "m", _Printer_finePrint).call(this), "f");
|
|
84
|
+
return __classPrivateFieldGet(this, _Printer_running, "f");
|
|
85
|
+
}, _Printer_finePrint = async function _Printer_finePrint() {
|
|
86
|
+
if (__classPrivateFieldGet(this, _Printer_running, "f")) {
|
|
87
|
+
__classPrivateFieldSet(this, _Printer_viewportChanged, true, "f");
|
|
88
|
+
return __classPrivateFieldGet(this, _Printer_running, "f");
|
|
89
|
+
}
|
|
90
|
+
__classPrivateFieldSet(this, _Printer_viewportChanged, false, "f");
|
|
91
|
+
const { include } = this, { value } = __classPrivateFieldGet(this, _Printer_textbox, "f"), { scrollHeight, offsetHeight: parentHeight, scrollTop, children } = __classPrivateFieldGet(this, _Printer_preview, "f");
|
|
92
|
+
let text = value, start = 0, end = __classPrivateFieldGet(this, _Printer_root, "f").length;
|
|
93
|
+
if (scrollHeight > parentHeight) {
|
|
94
|
+
const childNodes = [...children[0].childNodes], headings = childNodes.filter(({ className }) => className === 'wpb-heading'), { length } = headings;
|
|
95
|
+
if (length > 0) {
|
|
96
|
+
let i = headings.findIndex(({ offsetTop, offsetHeight }) => offsetTop + offsetHeight > scrollTop);
|
|
97
|
+
i = i === -1 ? length : i;
|
|
98
|
+
let j = headings.slice(i).findIndex(({ offsetTop }) => offsetTop >= scrollTop + parentHeight);
|
|
99
|
+
j = j === -1 ? length : i + j;
|
|
100
|
+
start = i ? childNodes.indexOf(headings[i - 1]) : 0;
|
|
101
|
+
while (i <= j && __classPrivateFieldGet(this, _Printer_root, "f")[start][0] === Infinity) {
|
|
102
|
+
start = childNodes.indexOf(headings[i++]);
|
|
103
|
+
}
|
|
104
|
+
end = j === length ? end : childNodes.indexOf(headings[j]);
|
|
105
|
+
while (i <= j && __classPrivateFieldGet(this, _Printer_root, "f")[end - 1][0] === Infinity) {
|
|
106
|
+
end = childNodes.indexOf(headings[--j]);
|
|
107
|
+
}
|
|
108
|
+
text = __classPrivateFieldGet(this, _Printer_root, "f").slice(start, end).map(([, str]) => str).join('');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (start === end) {
|
|
112
|
+
__classPrivateFieldSet(this, _Printer_running, undefined, "f");
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
const parsed = await wikiparse.print(text, include, undefined, __classPrivateFieldGet(this, _Printer_id, "f"));
|
|
116
|
+
if (this.include === include && __classPrivateFieldGet(this, _Printer_textbox, "f").value === value) {
|
|
117
|
+
__classPrivateFieldGet(this, _Printer_root, "f").splice(start, end - start, ...parsed);
|
|
118
|
+
__classPrivateFieldGet(this, _Printer_instances, "m", _Printer_paint).call(this);
|
|
119
|
+
__classPrivateFieldSet(this, _Printer_running, undefined, "f");
|
|
120
|
+
if (__classPrivateFieldGet(this, _Printer_viewportChanged, "f")) {
|
|
121
|
+
__classPrivateFieldSet(this, _Printer_running, __classPrivateFieldGet(this, _Printer_instances, "m", _Printer_finePrint).call(this), "f");
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
__classPrivateFieldSet(this, _Printer_running, undefined, "f");
|
|
126
|
+
__classPrivateFieldSet(this, _Printer_running, __classPrivateFieldGet(this, _Printer_instances, "m", _Printer_coarsePrint).call(this), "f");
|
|
127
|
+
}
|
|
128
|
+
return __classPrivateFieldGet(this, _Printer_running, "f");
|
|
129
|
+
};
|
|
130
|
+
const edit = (textbox, include) => {
|
|
131
|
+
if (!(textbox instanceof HTMLTextAreaElement)) {
|
|
132
|
+
throw new TypeError('wikiparse.edit方法仅可用于textarea元素!');
|
|
133
|
+
}
|
|
134
|
+
const preview = document.createElement('div'), container = document.createElement('div'), printer = new Printer(preview, textbox, include);
|
|
135
|
+
preview.id = 'wikiPretty';
|
|
136
|
+
preview.classList.add('wikiparser', 'active');
|
|
137
|
+
container.className = 'wikiparse-container';
|
|
138
|
+
textbox.replaceWith(container);
|
|
139
|
+
textbox.classList.add('wikiparsed');
|
|
140
|
+
container.append(preview, textbox);
|
|
141
|
+
textbox.addEventListener('input', e => {
|
|
142
|
+
if (!e.isComposing) {
|
|
143
|
+
printer.queue(2000, 0);
|
|
144
|
+
}
|
|
145
|
+
textbox.style.color = '';
|
|
146
|
+
preview.classList.add('active');
|
|
147
|
+
});
|
|
148
|
+
textbox.addEventListener('scroll', () => {
|
|
149
|
+
if (preview.scrollHeight > preview.offsetHeight && !preview.classList.contains('active')) {
|
|
150
|
+
preview.scrollTop = textbox.scrollTop;
|
|
151
|
+
printer.queue(500, 1);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
printer.queue(0, 0);
|
|
155
|
+
return printer;
|
|
156
|
+
};
|
|
157
|
+
wikiparse.Printer = Printer;
|
|
158
|
+
wikiparse.edit = edit;
|
|
159
|
+
})();
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
"use strict";
|
|
3
|
+
const highlight = async (ele, include, linenums, start) => {
|
|
4
|
+
if (ele.classList.contains('wikiparser')) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
let { innerText } = ele;
|
|
8
|
+
if (innerText.endsWith('\n')) {
|
|
9
|
+
innerText = innerText.slice(0, -1);
|
|
10
|
+
}
|
|
11
|
+
innerText || (innerText = ' ');
|
|
12
|
+
const html = `<span class="wpb-root">${(await wikiparse.print(innerText, include)).map(([, , printed]) => printed).join('')}</span>`;
|
|
13
|
+
ele.classList.add('wikiparser');
|
|
14
|
+
ele.tabIndex = 0;
|
|
15
|
+
ele.innerHTML = html;
|
|
16
|
+
if (linenums) {
|
|
17
|
+
wikiparse.lineNumbers(ele.firstChild, start);
|
|
18
|
+
}
|
|
19
|
+
ele.addEventListener('keydown', e => {
|
|
20
|
+
if ((e.metaKey || e.ctrlKey) && e.key === 'a') {
|
|
21
|
+
e.preventDefault();
|
|
22
|
+
const range = document.createRange(), selection = getSelection();
|
|
23
|
+
range.selectNodeContents(ele.firstChild);
|
|
24
|
+
selection.removeAllRanges();
|
|
25
|
+
selection.addRange(range);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
wikiparse.highlight = highlight;
|
|
30
|
+
})();
|