tiny-markdown-editor 0.2.6 → 0.2.8
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/README.md +3 -1
- package/dist/tiny-mde.js +8 -1
- package/dist/tiny-mde.min.js +1 -1
- package/dist/tiny-mde.tiny.js +1 -1
- package/lib/TinyMDE.d.ts +108 -0
- package/lib/TinyMDE.js +1558 -0
- package/lib/TinyMDECommandBar.d.ts +35 -0
- package/lib/TinyMDECommandBar.js +329 -0
- package/lib/grammar.d.ts +77 -0
- package/lib/grammar.js +311 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +10 -0
- package/lib/svg/svg.d.ts +2 -0
- package/lib/svg/svg.js +21 -0
- package/lib/tiny.d.ts +2 -0
- package/lib/tiny.js +8 -0
- package/package.json +3 -2
package/lib/TinyMDE.js
ADDED
|
@@ -0,0 +1,1558 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Editor = void 0;
|
|
4
|
+
const grammar_1 = require("./grammar");
|
|
5
|
+
class Editor {
|
|
6
|
+
constructor(props = {}) {
|
|
7
|
+
this.e = null;
|
|
8
|
+
this.textarea = null;
|
|
9
|
+
this.lines = [];
|
|
10
|
+
this.lineElements = [];
|
|
11
|
+
this.lineTypes = [];
|
|
12
|
+
this.lineCaptures = [];
|
|
13
|
+
this.lineReplacements = [];
|
|
14
|
+
this.linkLabels = [];
|
|
15
|
+
this.lineDirty = [];
|
|
16
|
+
this.lastCommandState = null;
|
|
17
|
+
this.customInlineGrammar = {};
|
|
18
|
+
this.mergedInlineGrammar = grammar_1.inlineGrammar;
|
|
19
|
+
this.hasFocus = true;
|
|
20
|
+
this.listeners = {
|
|
21
|
+
change: [],
|
|
22
|
+
selection: [],
|
|
23
|
+
drop: [],
|
|
24
|
+
};
|
|
25
|
+
this.undoStack = [];
|
|
26
|
+
this.redoStack = [];
|
|
27
|
+
this.isRestoringHistory = false;
|
|
28
|
+
this.maxHistory = 100;
|
|
29
|
+
this.e = null;
|
|
30
|
+
this.textarea = null;
|
|
31
|
+
this.lines = [];
|
|
32
|
+
this.lineElements = [];
|
|
33
|
+
this.lineTypes = [];
|
|
34
|
+
this.lineCaptures = [];
|
|
35
|
+
this.lineReplacements = [];
|
|
36
|
+
this.linkLabels = [];
|
|
37
|
+
this.lineDirty = [];
|
|
38
|
+
this.lastCommandState = null;
|
|
39
|
+
this.hasFocus = true;
|
|
40
|
+
this.customInlineGrammar = props.customInlineGrammar || {};
|
|
41
|
+
this.mergedInlineGrammar = (0, grammar_1.createMergedInlineGrammar)(this.customInlineGrammar);
|
|
42
|
+
this.listeners = {
|
|
43
|
+
change: [],
|
|
44
|
+
selection: [],
|
|
45
|
+
drop: [],
|
|
46
|
+
};
|
|
47
|
+
let element = null;
|
|
48
|
+
if (typeof props.element === 'string') {
|
|
49
|
+
element = document.getElementById(props.element);
|
|
50
|
+
}
|
|
51
|
+
else if (props.element) {
|
|
52
|
+
element = props.element;
|
|
53
|
+
}
|
|
54
|
+
if (typeof props.textarea === 'string') {
|
|
55
|
+
this.textarea = document.getElementById(props.textarea);
|
|
56
|
+
}
|
|
57
|
+
else if (props.textarea) {
|
|
58
|
+
this.textarea = props.textarea;
|
|
59
|
+
}
|
|
60
|
+
if (this.textarea) {
|
|
61
|
+
if (!element)
|
|
62
|
+
element = this.textarea;
|
|
63
|
+
}
|
|
64
|
+
if (!element) {
|
|
65
|
+
element = document.getElementsByTagName("body")[0];
|
|
66
|
+
}
|
|
67
|
+
if (element && element.tagName === "TEXTAREA") {
|
|
68
|
+
this.textarea = element;
|
|
69
|
+
element = this.textarea.parentNode;
|
|
70
|
+
}
|
|
71
|
+
if (this.textarea) {
|
|
72
|
+
this.textarea.style.display = "none";
|
|
73
|
+
}
|
|
74
|
+
this.undoStack = [];
|
|
75
|
+
this.redoStack = [];
|
|
76
|
+
this.isRestoringHistory = false;
|
|
77
|
+
this.maxHistory = 100;
|
|
78
|
+
this.createEditorElement(element, props);
|
|
79
|
+
this.setContent(typeof props.content === "string"
|
|
80
|
+
? props.content
|
|
81
|
+
: this.textarea
|
|
82
|
+
? this.textarea.value
|
|
83
|
+
: "# Hello TinyMDE!\nEdit **here**");
|
|
84
|
+
this.e.addEventListener("keydown", (e) => this.handleUndoRedoKey(e));
|
|
85
|
+
}
|
|
86
|
+
get canUndo() {
|
|
87
|
+
return this.undoStack.length >= 2;
|
|
88
|
+
}
|
|
89
|
+
get canRedo() {
|
|
90
|
+
return this.redoStack.length > 0;
|
|
91
|
+
}
|
|
92
|
+
pushHistory() {
|
|
93
|
+
if (this.isRestoringHistory)
|
|
94
|
+
return;
|
|
95
|
+
this.pushCurrentState();
|
|
96
|
+
this.redoStack = [];
|
|
97
|
+
}
|
|
98
|
+
pushCurrentState() {
|
|
99
|
+
this.undoStack.push({
|
|
100
|
+
content: this.getContent(),
|
|
101
|
+
selection: this.getSelection(),
|
|
102
|
+
anchor: this.getSelection(true),
|
|
103
|
+
});
|
|
104
|
+
if (this.undoStack.length > this.maxHistory)
|
|
105
|
+
this.undoStack.shift();
|
|
106
|
+
}
|
|
107
|
+
undo() {
|
|
108
|
+
if (this.undoStack.length < 2)
|
|
109
|
+
return;
|
|
110
|
+
this.isRestoringHistory = true;
|
|
111
|
+
this.pushCurrentState();
|
|
112
|
+
const current = this.undoStack.pop();
|
|
113
|
+
this.redoStack.push(current);
|
|
114
|
+
const prev = this.undoStack[this.undoStack.length - 1];
|
|
115
|
+
this.setContent(prev.content);
|
|
116
|
+
if (prev.selection)
|
|
117
|
+
this.setSelection(prev.selection, prev.anchor);
|
|
118
|
+
this.undoStack.pop();
|
|
119
|
+
this.isRestoringHistory = false;
|
|
120
|
+
}
|
|
121
|
+
redo() {
|
|
122
|
+
if (!this.redoStack.length)
|
|
123
|
+
return;
|
|
124
|
+
this.isRestoringHistory = true;
|
|
125
|
+
this.pushCurrentState();
|
|
126
|
+
const next = this.redoStack.pop();
|
|
127
|
+
this.setContent(next.content);
|
|
128
|
+
if (next.selection)
|
|
129
|
+
this.setSelection(next.selection, next.anchor);
|
|
130
|
+
this.isRestoringHistory = false;
|
|
131
|
+
}
|
|
132
|
+
handleUndoRedoKey(e) {
|
|
133
|
+
const isMac = /Mac|iPod|iPhone|iPad/.test(navigator.platform);
|
|
134
|
+
const ctrl = isMac ? e.metaKey : e.ctrlKey;
|
|
135
|
+
if (ctrl && !e.altKey) {
|
|
136
|
+
if (e.key === "z" || e.key === "Z") {
|
|
137
|
+
if (e.shiftKey) {
|
|
138
|
+
this.redo();
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
this.undo();
|
|
142
|
+
}
|
|
143
|
+
e.preventDefault();
|
|
144
|
+
}
|
|
145
|
+
else if (e.key === "y" || e.key === "Y") {
|
|
146
|
+
this.redo();
|
|
147
|
+
e.preventDefault();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
createEditorElement(element, props) {
|
|
152
|
+
if (props && props.editor !== undefined) {
|
|
153
|
+
if (typeof props.editor === 'string') {
|
|
154
|
+
this.e = document.getElementById(props.editor);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
this.e = props.editor;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
this.e = document.createElement("div");
|
|
162
|
+
}
|
|
163
|
+
this.e.classList.add("TinyMDE");
|
|
164
|
+
this.e.contentEditable = "true";
|
|
165
|
+
this.e.style.whiteSpace = "pre-wrap";
|
|
166
|
+
this.e.style.webkitUserModify = "read-write-plaintext-only";
|
|
167
|
+
if (props.editor === undefined) {
|
|
168
|
+
if (this.textarea &&
|
|
169
|
+
this.textarea.parentNode === element &&
|
|
170
|
+
this.textarea.nextSibling) {
|
|
171
|
+
element.insertBefore(this.e, this.textarea.nextSibling);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
element.appendChild(this.e);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
this.e.addEventListener("input", (e) => this.handleInputEvent(e));
|
|
178
|
+
this.e.addEventListener("beforeinput", (e) => this.handleBeforeInputEvent(e));
|
|
179
|
+
this.e.addEventListener("compositionend", (e) => this.handleInputEvent(e));
|
|
180
|
+
document.addEventListener("selectionchange", (e) => {
|
|
181
|
+
if (this.hasFocus) {
|
|
182
|
+
this.handleSelectionChangeEvent(e);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
this.e.addEventListener("blur", () => this.hasFocus = false);
|
|
186
|
+
this.e.addEventListener("focus", () => this.hasFocus = true);
|
|
187
|
+
this.e.addEventListener("paste", (e) => this.handlePaste(e));
|
|
188
|
+
this.e.addEventListener("drop", (e) => this.handleDrop(e));
|
|
189
|
+
this.lineElements = this.e.childNodes;
|
|
190
|
+
}
|
|
191
|
+
setContent(content) {
|
|
192
|
+
while (this.e.firstChild) {
|
|
193
|
+
this.e.removeChild(this.e.firstChild);
|
|
194
|
+
}
|
|
195
|
+
this.lines = content.split(/(?:\r\n|\r|\n)/);
|
|
196
|
+
this.lineDirty = [];
|
|
197
|
+
for (let lineNum = 0; lineNum < this.lines.length; lineNum++) {
|
|
198
|
+
let le = document.createElement("div");
|
|
199
|
+
this.e.appendChild(le);
|
|
200
|
+
this.lineDirty.push(true);
|
|
201
|
+
}
|
|
202
|
+
this.lineTypes = new Array(this.lines.length);
|
|
203
|
+
this.updateFormatting();
|
|
204
|
+
this.fireChange();
|
|
205
|
+
if (!this.isRestoringHistory)
|
|
206
|
+
this.pushHistory();
|
|
207
|
+
}
|
|
208
|
+
getContent() {
|
|
209
|
+
return this.lines.join("\n");
|
|
210
|
+
}
|
|
211
|
+
updateFormatting() {
|
|
212
|
+
this.updateLineTypes();
|
|
213
|
+
this.updateLinkLabels();
|
|
214
|
+
this.applyLineTypes();
|
|
215
|
+
}
|
|
216
|
+
updateLinkLabels() {
|
|
217
|
+
this.linkLabels = [];
|
|
218
|
+
for (let l = 0; l < this.lines.length; l++) {
|
|
219
|
+
if (this.lineTypes[l] === "TMLinkReferenceDefinition") {
|
|
220
|
+
this.linkLabels.push(this.lineCaptures[l][grammar_1.lineGrammar.TMLinkReferenceDefinition.labelPlaceholder]);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
replace(replacement, capture) {
|
|
225
|
+
return replacement.replace(/(\${1,2})([0-9])/g, (str, p1, p2) => {
|
|
226
|
+
if (p1 === "$")
|
|
227
|
+
return (0, grammar_1.htmlescape)(capture[parseInt(p2)]);
|
|
228
|
+
else
|
|
229
|
+
return `<span class="TMInlineFormatted">${this.processInlineStyles(capture[parseInt(p2)])}</span>`;
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
applyLineTypes() {
|
|
233
|
+
for (let lineNum = 0; lineNum < this.lines.length; lineNum++) {
|
|
234
|
+
if (this.lineDirty[lineNum]) {
|
|
235
|
+
let contentHTML = this.replace(this.lineReplacements[lineNum], this.lineCaptures[lineNum]);
|
|
236
|
+
this.lineElements[lineNum].className = this.lineTypes[lineNum];
|
|
237
|
+
this.lineElements[lineNum].removeAttribute("style");
|
|
238
|
+
this.lineElements[lineNum].innerHTML =
|
|
239
|
+
contentHTML === "" ? "<br />" : contentHTML;
|
|
240
|
+
}
|
|
241
|
+
this.lineElements[lineNum].dataset.lineNum = lineNum.toString();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
updateLineTypes() {
|
|
245
|
+
let codeBlockType = false;
|
|
246
|
+
let codeBlockSeqLength = 0;
|
|
247
|
+
let htmlBlock = false;
|
|
248
|
+
for (let lineNum = 0; lineNum < this.lines.length; lineNum++) {
|
|
249
|
+
let lineType = "TMPara";
|
|
250
|
+
let lineCapture = [this.lines[lineNum]];
|
|
251
|
+
let lineReplacement = "$$0";
|
|
252
|
+
// Check ongoing code blocks
|
|
253
|
+
if (codeBlockType === "TMCodeFenceBacktickOpen") {
|
|
254
|
+
let capture = grammar_1.lineGrammar.TMCodeFenceBacktickClose.regexp.exec(this.lines[lineNum]);
|
|
255
|
+
if (capture && capture.groups["seq"].length >= codeBlockSeqLength) {
|
|
256
|
+
lineType = "TMCodeFenceBacktickClose";
|
|
257
|
+
lineReplacement = grammar_1.lineGrammar.TMCodeFenceBacktickClose.replacement;
|
|
258
|
+
lineCapture = capture;
|
|
259
|
+
codeBlockType = false;
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
lineType = "TMFencedCodeBacktick";
|
|
263
|
+
lineReplacement = '<span class="TMFencedCode">$0<br /></span>';
|
|
264
|
+
lineCapture = [this.lines[lineNum]];
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
else if (codeBlockType === "TMCodeFenceTildeOpen") {
|
|
268
|
+
let capture = grammar_1.lineGrammar.TMCodeFenceTildeClose.regexp.exec(this.lines[lineNum]);
|
|
269
|
+
if (capture && capture.groups["seq"].length >= codeBlockSeqLength) {
|
|
270
|
+
lineType = "TMCodeFenceTildeClose";
|
|
271
|
+
lineReplacement = grammar_1.lineGrammar.TMCodeFenceTildeClose.replacement;
|
|
272
|
+
lineCapture = capture;
|
|
273
|
+
codeBlockType = false;
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
lineType = "TMFencedCodeTilde";
|
|
277
|
+
lineReplacement = '<span class="TMFencedCode">$0<br /></span>';
|
|
278
|
+
lineCapture = [this.lines[lineNum]];
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
// Check HTML block types
|
|
282
|
+
if (lineType === "TMPara" && htmlBlock === false) {
|
|
283
|
+
for (let htmlBlockType of grammar_1.htmlBlockGrammar) {
|
|
284
|
+
if (this.lines[lineNum].match(htmlBlockType.start)) {
|
|
285
|
+
if (htmlBlockType.paraInterrupt ||
|
|
286
|
+
lineNum === 0 ||
|
|
287
|
+
!(this.lineTypes[lineNum - 1] === "TMPara" ||
|
|
288
|
+
this.lineTypes[lineNum - 1] === "TMUL" ||
|
|
289
|
+
this.lineTypes[lineNum - 1] === "TMOL" ||
|
|
290
|
+
this.lineTypes[lineNum - 1] === "TMBlockquote")) {
|
|
291
|
+
htmlBlock = htmlBlockType;
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (htmlBlock !== false) {
|
|
298
|
+
lineType = "TMHTMLBlock";
|
|
299
|
+
lineReplacement = '<span class="TMHTMLContent">$0<br /></span>';
|
|
300
|
+
lineCapture = [this.lines[lineNum]];
|
|
301
|
+
if (htmlBlock.end) {
|
|
302
|
+
if (this.lines[lineNum].match(htmlBlock.end)) {
|
|
303
|
+
htmlBlock = false;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
if (lineNum === this.lines.length - 1 ||
|
|
308
|
+
this.lines[lineNum + 1].match(grammar_1.lineGrammar.TMBlankLine.regexp)) {
|
|
309
|
+
htmlBlock = false;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
// Check all regexps if we haven't applied one of the code block types
|
|
314
|
+
if (lineType === "TMPara") {
|
|
315
|
+
for (let type in grammar_1.lineGrammar) {
|
|
316
|
+
if (grammar_1.lineGrammar[type].regexp) {
|
|
317
|
+
let capture = grammar_1.lineGrammar[type].regexp.exec(this.lines[lineNum]);
|
|
318
|
+
if (capture) {
|
|
319
|
+
lineType = type;
|
|
320
|
+
lineReplacement = grammar_1.lineGrammar[type].replacement;
|
|
321
|
+
lineCapture = capture;
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
// If we've opened a code block, remember that
|
|
328
|
+
if (lineType === "TMCodeFenceBacktickOpen" || lineType === "TMCodeFenceTildeOpen") {
|
|
329
|
+
codeBlockType = lineType;
|
|
330
|
+
codeBlockSeqLength = lineCapture.groups["seq"].length;
|
|
331
|
+
}
|
|
332
|
+
// Link reference definition and indented code can't interrupt a paragraph
|
|
333
|
+
if ((lineType === "TMIndentedCode" || lineType === "TMLinkReferenceDefinition") &&
|
|
334
|
+
lineNum > 0 &&
|
|
335
|
+
(this.lineTypes[lineNum - 1] === "TMPara" ||
|
|
336
|
+
this.lineTypes[lineNum - 1] === "TMUL" ||
|
|
337
|
+
this.lineTypes[lineNum - 1] === "TMOL" ||
|
|
338
|
+
this.lineTypes[lineNum - 1] === "TMBlockquote")) {
|
|
339
|
+
lineType = "TMPara";
|
|
340
|
+
lineCapture = [this.lines[lineNum]];
|
|
341
|
+
lineReplacement = "$$0";
|
|
342
|
+
}
|
|
343
|
+
// Setext H2 markers that can also be interpreted as an empty list item should be regarded as such
|
|
344
|
+
if (lineType === "TMSetextH2Marker") {
|
|
345
|
+
let capture = grammar_1.lineGrammar.TMUL.regexp.exec(this.lines[lineNum]);
|
|
346
|
+
if (capture) {
|
|
347
|
+
lineType = "TMUL";
|
|
348
|
+
lineReplacement = grammar_1.lineGrammar.TMUL.replacement;
|
|
349
|
+
lineCapture = capture;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
// Setext headings are only valid if preceded by a paragraph
|
|
353
|
+
if (lineType === "TMSetextH1Marker" || lineType === "TMSetextH2Marker") {
|
|
354
|
+
if (lineNum === 0 || this.lineTypes[lineNum - 1] !== "TMPara") {
|
|
355
|
+
let capture = grammar_1.lineGrammar.TMHR.regexp.exec(this.lines[lineNum]);
|
|
356
|
+
if (capture) {
|
|
357
|
+
lineType = "TMHR";
|
|
358
|
+
lineCapture = capture;
|
|
359
|
+
lineReplacement = grammar_1.lineGrammar.TMHR.replacement;
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
lineType = "TMPara";
|
|
363
|
+
lineCapture = [this.lines[lineNum]];
|
|
364
|
+
lineReplacement = "$$0";
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
let headingLine = lineNum - 1;
|
|
369
|
+
const headingLineType = lineType === "TMSetextH1Marker" ? "TMSetextH1" : "TMSetextH2";
|
|
370
|
+
do {
|
|
371
|
+
if (this.lineTypes[headingLine] !== headingLineType) {
|
|
372
|
+
this.lineTypes[headingLine] = headingLineType;
|
|
373
|
+
this.lineDirty[headingLine] = true;
|
|
374
|
+
}
|
|
375
|
+
this.lineReplacements[headingLine] = "$$0";
|
|
376
|
+
this.lineCaptures[headingLine] = [this.lines[headingLine]];
|
|
377
|
+
headingLine--;
|
|
378
|
+
} while (headingLine >= 0 && this.lineTypes[headingLine] === "TMPara");
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (this.lineTypes[lineNum] !== lineType) {
|
|
382
|
+
this.lineTypes[lineNum] = lineType;
|
|
383
|
+
this.lineDirty[lineNum] = true;
|
|
384
|
+
}
|
|
385
|
+
this.lineReplacements[lineNum] = lineReplacement;
|
|
386
|
+
this.lineCaptures[lineNum] = lineCapture;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
getSelection(getAnchor = false) {
|
|
390
|
+
var _a, _b;
|
|
391
|
+
const selection = window.getSelection();
|
|
392
|
+
let startNode = getAnchor ? selection.anchorNode : selection.focusNode;
|
|
393
|
+
if (!startNode)
|
|
394
|
+
return null;
|
|
395
|
+
let offset = getAnchor ? selection.anchorOffset : selection.focusOffset;
|
|
396
|
+
if (startNode === this.e) {
|
|
397
|
+
if (offset < this.lines.length)
|
|
398
|
+
return {
|
|
399
|
+
row: offset,
|
|
400
|
+
col: 0,
|
|
401
|
+
};
|
|
402
|
+
return {
|
|
403
|
+
row: offset - 1,
|
|
404
|
+
col: this.lines[offset - 1].length,
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
let col = this.computeColumn(startNode, offset);
|
|
408
|
+
if (col === null)
|
|
409
|
+
return null;
|
|
410
|
+
let node = startNode;
|
|
411
|
+
while (node.parentElement !== this.e) {
|
|
412
|
+
node = node.parentElement;
|
|
413
|
+
}
|
|
414
|
+
let row = 0;
|
|
415
|
+
// If the node doesn't have a previous sibling, it must be the first line
|
|
416
|
+
if (node.previousSibling) {
|
|
417
|
+
const currentLineNumData = (_a = node.dataset) === null || _a === void 0 ? void 0 : _a.lineNum;
|
|
418
|
+
const previousLineNumData = (_b = node.previousSibling.dataset) === null || _b === void 0 ? void 0 : _b.lineNum;
|
|
419
|
+
if (currentLineNumData && previousLineNumData) {
|
|
420
|
+
const currentLineNum = parseInt(currentLineNumData);
|
|
421
|
+
const previousLineNum = parseInt(previousLineNumData);
|
|
422
|
+
if (currentLineNum === previousLineNum + 1) {
|
|
423
|
+
row = currentLineNum;
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
// If the current line is NOT the previous line + 1, then either
|
|
427
|
+
// the current line got split in two or merged with the previous line
|
|
428
|
+
// Either way, we need to recalculate the row number
|
|
429
|
+
while (node.previousSibling) {
|
|
430
|
+
row++;
|
|
431
|
+
node = node.previousSibling;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return { row: row, col: col };
|
|
437
|
+
}
|
|
438
|
+
setSelection(focus, anchor = null) {
|
|
439
|
+
if (!focus)
|
|
440
|
+
return;
|
|
441
|
+
let { node: focusNode, offset: focusOffset } = this.computeNodeAndOffset(focus.row, focus.col, anchor ? anchor.row === focus.row && anchor.col > focus.col : false);
|
|
442
|
+
let anchorNode = null, anchorOffset = null;
|
|
443
|
+
if (anchor && (anchor.row !== focus.row || anchor.col !== focus.col)) {
|
|
444
|
+
let { node, offset } = this.computeNodeAndOffset(anchor.row, anchor.col, focus.row === anchor.row && focus.col > anchor.col);
|
|
445
|
+
anchorNode = node;
|
|
446
|
+
anchorOffset = offset;
|
|
447
|
+
}
|
|
448
|
+
let windowSelection = window.getSelection();
|
|
449
|
+
windowSelection.setBaseAndExtent(focusNode, focusOffset, anchorNode || focusNode, anchorNode ? anchorOffset : focusOffset);
|
|
450
|
+
}
|
|
451
|
+
paste(text, anchor = null, focus = null) {
|
|
452
|
+
if (!anchor)
|
|
453
|
+
anchor = this.getSelection(true);
|
|
454
|
+
if (!focus)
|
|
455
|
+
focus = this.getSelection(false);
|
|
456
|
+
let beginning, end;
|
|
457
|
+
if (!focus) {
|
|
458
|
+
focus = {
|
|
459
|
+
row: this.lines.length - 1,
|
|
460
|
+
col: this.lines[this.lines.length - 1].length,
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
if (!anchor) {
|
|
464
|
+
anchor = focus;
|
|
465
|
+
}
|
|
466
|
+
if (anchor.row < focus.row ||
|
|
467
|
+
(anchor.row === focus.row && anchor.col <= focus.col)) {
|
|
468
|
+
beginning = anchor;
|
|
469
|
+
end = focus;
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
beginning = focus;
|
|
473
|
+
end = anchor;
|
|
474
|
+
}
|
|
475
|
+
let insertedLines = text.split(/(?:\r\n|\r|\n)/);
|
|
476
|
+
let lineBefore = this.lines[beginning.row].substr(0, beginning.col);
|
|
477
|
+
let lineEnd = this.lines[end.row].substr(end.col);
|
|
478
|
+
insertedLines[0] = lineBefore.concat(insertedLines[0]);
|
|
479
|
+
let endColPos = insertedLines[insertedLines.length - 1].length;
|
|
480
|
+
insertedLines[insertedLines.length - 1] = insertedLines[insertedLines.length - 1].concat(lineEnd);
|
|
481
|
+
this.spliceLines(beginning.row, 1 + end.row - beginning.row, insertedLines);
|
|
482
|
+
focus.row = beginning.row + insertedLines.length - 1;
|
|
483
|
+
focus.col = endColPos;
|
|
484
|
+
this.updateFormatting();
|
|
485
|
+
this.setSelection(focus);
|
|
486
|
+
this.fireChange();
|
|
487
|
+
}
|
|
488
|
+
wrapSelection(pre, post, focus = null, anchor = null) {
|
|
489
|
+
if (!this.isRestoringHistory)
|
|
490
|
+
this.pushHistory();
|
|
491
|
+
if (!focus)
|
|
492
|
+
focus = this.getSelection(false);
|
|
493
|
+
if (!anchor)
|
|
494
|
+
anchor = this.getSelection(true);
|
|
495
|
+
if (!focus || !anchor || focus.row !== anchor.row)
|
|
496
|
+
return;
|
|
497
|
+
this.lineDirty[focus.row] = true;
|
|
498
|
+
const startCol = focus.col < anchor.col ? focus.col : anchor.col;
|
|
499
|
+
const endCol = focus.col < anchor.col ? anchor.col : focus.col;
|
|
500
|
+
const left = this.lines[focus.row].substr(0, startCol).concat(pre);
|
|
501
|
+
const mid = endCol === startCol ? "" : this.lines[focus.row].substr(startCol, endCol - startCol);
|
|
502
|
+
const right = post.concat(this.lines[focus.row].substr(endCol));
|
|
503
|
+
this.lines[focus.row] = left.concat(mid, right);
|
|
504
|
+
anchor.col = left.length;
|
|
505
|
+
focus.col = anchor.col + mid.length;
|
|
506
|
+
this.updateFormatting();
|
|
507
|
+
this.setSelection(focus, anchor);
|
|
508
|
+
}
|
|
509
|
+
addEventListener(type, listener) {
|
|
510
|
+
if (type.match(/^(?:change|input)$/i)) {
|
|
511
|
+
this.listeners.change.push(listener);
|
|
512
|
+
}
|
|
513
|
+
if (type.match(/^(?:selection|selectionchange)$/i)) {
|
|
514
|
+
this.listeners.selection.push(listener);
|
|
515
|
+
}
|
|
516
|
+
if (type.match(/^(?:drop)$/i)) {
|
|
517
|
+
this.listeners.drop.push(listener);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
fireChange() {
|
|
521
|
+
if (!this.textarea && !this.listeners.change.length)
|
|
522
|
+
return;
|
|
523
|
+
const content = this.getContent();
|
|
524
|
+
if (this.textarea)
|
|
525
|
+
this.textarea.value = content;
|
|
526
|
+
for (let listener of this.listeners.change) {
|
|
527
|
+
listener({
|
|
528
|
+
content: content,
|
|
529
|
+
linesDirty: [...this.lineDirty],
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* beforeinput handler, exclusively to handle insertParagraph and
|
|
535
|
+
* insertLineBreak events. These used to be handled in the input event,
|
|
536
|
+
* but that caused issues with Firefox where the input event would be
|
|
537
|
+
* sometimes not be fired for these input types.
|
|
538
|
+
* @param event The input event
|
|
539
|
+
* @returns nothing
|
|
540
|
+
*/
|
|
541
|
+
handleBeforeInputEvent(event) {
|
|
542
|
+
const beforeInputEvent = event;
|
|
543
|
+
if (beforeInputEvent.inputType !== "insertParagraph" &&
|
|
544
|
+
beforeInputEvent.inputType !== "insertLineBreak")
|
|
545
|
+
return;
|
|
546
|
+
if (!this.isRestoringHistory)
|
|
547
|
+
this.pushHistory();
|
|
548
|
+
event.preventDefault();
|
|
549
|
+
this.clearDirtyFlag();
|
|
550
|
+
const focus = this.getSelection();
|
|
551
|
+
const anchor = this.getSelection(true);
|
|
552
|
+
if (!focus || !anchor)
|
|
553
|
+
return;
|
|
554
|
+
// If focus and anchor are in different lines, simply remove everything
|
|
555
|
+
// after the beginning of the selection and before the end of the selection
|
|
556
|
+
// and remove any lines in between
|
|
557
|
+
if (focus.row !== anchor.row) {
|
|
558
|
+
const beginning = focus.row < anchor.row ? focus : anchor;
|
|
559
|
+
const end = focus.row < anchor.row ? anchor : focus;
|
|
560
|
+
this.lines[beginning.row] = this.lines[beginning.row].substring(0, beginning.col);
|
|
561
|
+
this.lines[end.row] = this.lines[end.row].substring(end.col);
|
|
562
|
+
this.spliceLines(beginning.row + 1, end.row - beginning.row - 1);
|
|
563
|
+
focus.row = beginning.row + 1;
|
|
564
|
+
focus.col = 0;
|
|
565
|
+
}
|
|
566
|
+
else {
|
|
567
|
+
let continuableType = false;
|
|
568
|
+
switch (this.lineTypes[focus.row]) {
|
|
569
|
+
case "TMUL":
|
|
570
|
+
continuableType = "TMUL";
|
|
571
|
+
break;
|
|
572
|
+
case "TMOL":
|
|
573
|
+
continuableType = "TMOL";
|
|
574
|
+
break;
|
|
575
|
+
case "TMIndentedCode":
|
|
576
|
+
continuableType = "TMIndentedCode";
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
const lineBeforeBreak = this.lines[focus.row].substring(0, focus.col <= anchor.col ? focus.col : anchor.col);
|
|
580
|
+
const lineAfterBreak = this.lines[focus.row].substring(focus.col <= anchor.col ? anchor.col : focus.col);
|
|
581
|
+
this.spliceLines(focus.row, 1, [lineBeforeBreak, lineAfterBreak]);
|
|
582
|
+
focus.row += 1;
|
|
583
|
+
focus.col = 0;
|
|
584
|
+
if (continuableType) {
|
|
585
|
+
let capture = grammar_1.lineGrammar[continuableType].regexp.exec(this.lines[focus.row - 1]);
|
|
586
|
+
if (capture) {
|
|
587
|
+
if (capture[2]) {
|
|
588
|
+
if (continuableType === "TMOL") {
|
|
589
|
+
capture[1] = capture[1].replace(/\d{1,9}/, (result) => {
|
|
590
|
+
return (parseInt(result) + 1).toString();
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
this.lines[focus.row] = `${capture[1]}${this.lines[focus.row]}`;
|
|
594
|
+
this.lineDirty[focus.row] = true;
|
|
595
|
+
focus.col = capture[1].length;
|
|
596
|
+
}
|
|
597
|
+
else {
|
|
598
|
+
this.lines[focus.row - 1] = "";
|
|
599
|
+
this.lineDirty[focus.row - 1] = true;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
this.updateFormatting();
|
|
605
|
+
this.setSelection(focus);
|
|
606
|
+
// Scroll the element containing the selection into view
|
|
607
|
+
if (focus && focus.row < this.lineElements.length) {
|
|
608
|
+
this.lineElements[focus.row].scrollIntoView({
|
|
609
|
+
block: 'nearest',
|
|
610
|
+
inline: 'nearest'
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
this.fireChange();
|
|
614
|
+
}
|
|
615
|
+
handleInputEvent(event) {
|
|
616
|
+
const inputEvent = event;
|
|
617
|
+
if (inputEvent.inputType === "insertCompositionText")
|
|
618
|
+
return;
|
|
619
|
+
if (!this.isRestoringHistory)
|
|
620
|
+
this.pushHistory();
|
|
621
|
+
let focus = this.getSelection();
|
|
622
|
+
if ((inputEvent.inputType === "insertParagraph" || inputEvent.inputType === "insertLineBreak") && focus) {
|
|
623
|
+
// This should never happen since these are handled by the beforeinput handler
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
else {
|
|
627
|
+
if (!this.e.firstChild) {
|
|
628
|
+
this.e.innerHTML = '<div class="TMBlankLine"><br></div>';
|
|
629
|
+
}
|
|
630
|
+
else {
|
|
631
|
+
this.fixNodeHierarchy();
|
|
632
|
+
}
|
|
633
|
+
this.updateLineContentsAndFormatting();
|
|
634
|
+
}
|
|
635
|
+
if (focus) {
|
|
636
|
+
this.setSelection(focus);
|
|
637
|
+
}
|
|
638
|
+
this.fireChange();
|
|
639
|
+
}
|
|
640
|
+
handleSelectionChangeEvent(_e) {
|
|
641
|
+
this.fireSelection();
|
|
642
|
+
}
|
|
643
|
+
handlePaste(event) {
|
|
644
|
+
if (!this.isRestoringHistory)
|
|
645
|
+
this.pushHistory();
|
|
646
|
+
event.preventDefault();
|
|
647
|
+
let text = event.clipboardData.getData("text/plain");
|
|
648
|
+
this.paste(text);
|
|
649
|
+
}
|
|
650
|
+
handleDrop(event) {
|
|
651
|
+
event.preventDefault();
|
|
652
|
+
this.fireDrop(event.dataTransfer);
|
|
653
|
+
}
|
|
654
|
+
processInlineStyles(originalString) {
|
|
655
|
+
let processed = "";
|
|
656
|
+
let stack = [];
|
|
657
|
+
let offset = 0;
|
|
658
|
+
let string = originalString;
|
|
659
|
+
outer: while (string) {
|
|
660
|
+
// Process simple rules (non-delimiter)
|
|
661
|
+
for (let rule of ["escape", "code", "autolink", "html"]) {
|
|
662
|
+
if (this.mergedInlineGrammar[rule]) {
|
|
663
|
+
let cap = this.mergedInlineGrammar[rule].regexp.exec(string);
|
|
664
|
+
if (cap) {
|
|
665
|
+
string = string.substr(cap[0].length);
|
|
666
|
+
offset += cap[0].length;
|
|
667
|
+
processed += this.mergedInlineGrammar[rule].replacement.replace(/\$([1-9])/g, (str, p1) => (0, grammar_1.htmlescape)(cap[p1]));
|
|
668
|
+
continue outer;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
// Process custom inline grammar rules
|
|
673
|
+
for (let rule in this.customInlineGrammar) {
|
|
674
|
+
if (rule !== "escape" && rule !== "code" && rule !== "autolink" && rule !== "html" && rule !== "linkOpen" && rule !== "imageOpen" && rule !== "linkLabel" && rule !== "default") {
|
|
675
|
+
let cap = this.mergedInlineGrammar[rule].regexp.exec(string);
|
|
676
|
+
if (cap) {
|
|
677
|
+
string = string.substr(cap[0].length);
|
|
678
|
+
offset += cap[0].length;
|
|
679
|
+
processed += this.mergedInlineGrammar[rule].replacement.replace(/\$([1-9])/g, (str, p1) => (0, grammar_1.htmlescape)(cap[p1]));
|
|
680
|
+
continue outer;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
// Check for links / images
|
|
685
|
+
let potentialLink = string.match(this.mergedInlineGrammar.linkOpen.regexp);
|
|
686
|
+
let potentialImage = string.match(this.mergedInlineGrammar.imageOpen.regexp);
|
|
687
|
+
if (potentialImage || potentialLink) {
|
|
688
|
+
let result = this.parseLinkOrImage(string, !!potentialImage);
|
|
689
|
+
if (result) {
|
|
690
|
+
processed = `${processed}${result.output}`;
|
|
691
|
+
string = string.substr(result.charCount);
|
|
692
|
+
offset += result.charCount;
|
|
693
|
+
continue outer;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
// Check for em / strong delimiters
|
|
697
|
+
let cap = /(^\*+)|(^_+)/.exec(string);
|
|
698
|
+
if (cap) {
|
|
699
|
+
let delimCount = cap[0].length;
|
|
700
|
+
const delimString = cap[0];
|
|
701
|
+
const currentDelimiter = cap[0][0];
|
|
702
|
+
string = string.substr(cap[0].length);
|
|
703
|
+
const preceding = offset > 0 ? originalString.substr(0, offset) : " ";
|
|
704
|
+
const following = offset + cap[0].length < originalString.length ? string : " ";
|
|
705
|
+
const punctuationFollows = following.match(grammar_1.punctuationLeading);
|
|
706
|
+
const punctuationPrecedes = preceding.match(grammar_1.punctuationTrailing);
|
|
707
|
+
const whitespaceFollows = following.match(/^\s/);
|
|
708
|
+
const whitespacePrecedes = preceding.match(/\s$/);
|
|
709
|
+
let canOpen = !whitespaceFollows && (!punctuationFollows || !!whitespacePrecedes || !!punctuationPrecedes);
|
|
710
|
+
let canClose = !whitespacePrecedes && (!punctuationPrecedes || !!whitespaceFollows || !!punctuationFollows);
|
|
711
|
+
if (currentDelimiter === "_" && canOpen && canClose) {
|
|
712
|
+
canOpen = !!punctuationPrecedes;
|
|
713
|
+
canClose = !!punctuationFollows;
|
|
714
|
+
}
|
|
715
|
+
if (canClose) {
|
|
716
|
+
let stackPointer = stack.length - 1;
|
|
717
|
+
while (delimCount && stackPointer >= 0) {
|
|
718
|
+
if (stack[stackPointer].delimiter === currentDelimiter) {
|
|
719
|
+
while (stackPointer < stack.length - 1) {
|
|
720
|
+
const entry = stack.pop();
|
|
721
|
+
processed = `${entry.output}${entry.delimString.substr(0, entry.count)}${processed}`;
|
|
722
|
+
}
|
|
723
|
+
if (delimCount >= 2 && stack[stackPointer].count >= 2) {
|
|
724
|
+
processed = `<span class="TMMark">${currentDelimiter}${currentDelimiter}</span><strong class="TMStrong">${processed}</strong><span class="TMMark">${currentDelimiter}${currentDelimiter}</span>`;
|
|
725
|
+
delimCount -= 2;
|
|
726
|
+
stack[stackPointer].count -= 2;
|
|
727
|
+
}
|
|
728
|
+
else {
|
|
729
|
+
processed = `<span class="TMMark">${currentDelimiter}</span><em class="TMEm">${processed}</em><span class="TMMark">${currentDelimiter}</span>`;
|
|
730
|
+
delimCount -= 1;
|
|
731
|
+
stack[stackPointer].count -= 1;
|
|
732
|
+
}
|
|
733
|
+
if (stack[stackPointer].count === 0) {
|
|
734
|
+
let entry = stack.pop();
|
|
735
|
+
processed = `${entry.output}${processed}`;
|
|
736
|
+
stackPointer--;
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
else {
|
|
740
|
+
stackPointer--;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
if (delimCount && canOpen) {
|
|
745
|
+
stack.push({
|
|
746
|
+
delimiter: currentDelimiter,
|
|
747
|
+
delimString: delimString,
|
|
748
|
+
count: delimCount,
|
|
749
|
+
output: processed,
|
|
750
|
+
});
|
|
751
|
+
processed = "";
|
|
752
|
+
delimCount = 0;
|
|
753
|
+
}
|
|
754
|
+
if (delimCount) {
|
|
755
|
+
processed = `${processed}${delimString.substr(0, delimCount)}`;
|
|
756
|
+
}
|
|
757
|
+
offset += cap[0].length;
|
|
758
|
+
continue outer;
|
|
759
|
+
}
|
|
760
|
+
// Check for strikethrough delimiter
|
|
761
|
+
cap = /^~~/.exec(string);
|
|
762
|
+
if (cap) {
|
|
763
|
+
let consumed = false;
|
|
764
|
+
let stackPointer = stack.length - 1;
|
|
765
|
+
while (!consumed && stackPointer >= 0) {
|
|
766
|
+
if (stack[stackPointer].delimiter === "~") {
|
|
767
|
+
while (stackPointer < stack.length - 1) {
|
|
768
|
+
const entry = stack.pop();
|
|
769
|
+
processed = `${entry.output}${entry.delimString.substr(0, entry.count)}${processed}`;
|
|
770
|
+
}
|
|
771
|
+
processed = `<span class="TMMark">~~</span><del class="TMStrikethrough">${processed}</del><span class="TMMark">~~</span>`;
|
|
772
|
+
let entry = stack.pop();
|
|
773
|
+
processed = `${entry.output}${processed}`;
|
|
774
|
+
consumed = true;
|
|
775
|
+
}
|
|
776
|
+
else {
|
|
777
|
+
stackPointer--;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
if (!consumed) {
|
|
781
|
+
stack.push({
|
|
782
|
+
delimiter: "~",
|
|
783
|
+
delimString: "~~",
|
|
784
|
+
count: 2,
|
|
785
|
+
output: processed,
|
|
786
|
+
});
|
|
787
|
+
processed = "";
|
|
788
|
+
}
|
|
789
|
+
offset += cap[0].length;
|
|
790
|
+
string = string.substr(cap[0].length);
|
|
791
|
+
continue outer;
|
|
792
|
+
}
|
|
793
|
+
// Process 'default' rule
|
|
794
|
+
cap = this.mergedInlineGrammar.default.regexp.exec(string);
|
|
795
|
+
if (cap) {
|
|
796
|
+
string = string.substr(cap[0].length);
|
|
797
|
+
offset += cap[0].length;
|
|
798
|
+
processed += this.mergedInlineGrammar.default.replacement.replace(/\$([1-9])/g, (str, p1) => (0, grammar_1.htmlescape)(cap[p1]));
|
|
799
|
+
continue outer;
|
|
800
|
+
}
|
|
801
|
+
throw "Infinite loop!";
|
|
802
|
+
}
|
|
803
|
+
while (stack.length) {
|
|
804
|
+
const entry = stack.pop();
|
|
805
|
+
processed = `${entry.output}${entry.delimString.substr(0, entry.count)}${processed}`;
|
|
806
|
+
}
|
|
807
|
+
return processed;
|
|
808
|
+
}
|
|
809
|
+
computeColumn(startNode, offset) {
|
|
810
|
+
let node = startNode;
|
|
811
|
+
let col;
|
|
812
|
+
while (node && node.parentNode !== this.e) {
|
|
813
|
+
node = node.parentNode;
|
|
814
|
+
}
|
|
815
|
+
if (node === null)
|
|
816
|
+
return null;
|
|
817
|
+
if (startNode.nodeType === Node.TEXT_NODE || offset === 0) {
|
|
818
|
+
col = offset;
|
|
819
|
+
node = startNode;
|
|
820
|
+
}
|
|
821
|
+
else if (offset > 0) {
|
|
822
|
+
node = startNode.childNodes[offset - 1];
|
|
823
|
+
col = node.textContent.length;
|
|
824
|
+
}
|
|
825
|
+
else {
|
|
826
|
+
col = 0;
|
|
827
|
+
node = startNode;
|
|
828
|
+
}
|
|
829
|
+
while (node.parentNode !== this.e) {
|
|
830
|
+
if (node.previousSibling) {
|
|
831
|
+
node = node.previousSibling;
|
|
832
|
+
col += node.textContent.length;
|
|
833
|
+
}
|
|
834
|
+
else {
|
|
835
|
+
node = node.parentNode;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
return col;
|
|
839
|
+
}
|
|
840
|
+
computeNodeAndOffset(row, col, bindRight = false) {
|
|
841
|
+
if (row >= this.lineElements.length) {
|
|
842
|
+
row = this.lineElements.length - 1;
|
|
843
|
+
col = this.lines[row].length;
|
|
844
|
+
}
|
|
845
|
+
if (col > this.lines[row].length) {
|
|
846
|
+
col = this.lines[row].length;
|
|
847
|
+
}
|
|
848
|
+
const parentNode = this.lineElements[row];
|
|
849
|
+
let node = parentNode.firstChild;
|
|
850
|
+
let childrenComplete = false;
|
|
851
|
+
let rv = {
|
|
852
|
+
node: parentNode.firstChild ? parentNode.firstChild : parentNode,
|
|
853
|
+
offset: 0,
|
|
854
|
+
};
|
|
855
|
+
while (node !== parentNode) {
|
|
856
|
+
if (!childrenComplete && node.nodeType === Node.TEXT_NODE) {
|
|
857
|
+
if (node.nodeValue.length >= col) {
|
|
858
|
+
if (bindRight && node.nodeValue.length === col) {
|
|
859
|
+
rv = { node: node, offset: col };
|
|
860
|
+
col = 0;
|
|
861
|
+
}
|
|
862
|
+
else {
|
|
863
|
+
return { node: node, offset: col };
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
else {
|
|
867
|
+
col -= node.nodeValue.length;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
if (!childrenComplete && node.firstChild) {
|
|
871
|
+
node = node.firstChild;
|
|
872
|
+
}
|
|
873
|
+
else if (node.nextSibling) {
|
|
874
|
+
childrenComplete = false;
|
|
875
|
+
node = node.nextSibling;
|
|
876
|
+
}
|
|
877
|
+
else {
|
|
878
|
+
childrenComplete = true;
|
|
879
|
+
node = node.parentNode;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
return rv;
|
|
883
|
+
}
|
|
884
|
+
updateLineContentsAndFormatting() {
|
|
885
|
+
this.clearDirtyFlag();
|
|
886
|
+
this.updateLineContents();
|
|
887
|
+
this.updateFormatting();
|
|
888
|
+
}
|
|
889
|
+
clearDirtyFlag() {
|
|
890
|
+
this.lineDirty = new Array(this.lines.length);
|
|
891
|
+
for (let i = 0; i < this.lineDirty.length; i++) {
|
|
892
|
+
this.lineDirty[i] = false;
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
updateLineContents() {
|
|
896
|
+
let lineDelta = this.e.childElementCount - this.lines.length;
|
|
897
|
+
if (lineDelta) {
|
|
898
|
+
let firstChangedLine = 0;
|
|
899
|
+
while (firstChangedLine <= this.lines.length &&
|
|
900
|
+
firstChangedLine <= this.lineElements.length &&
|
|
901
|
+
this.lineElements[firstChangedLine] &&
|
|
902
|
+
this.lines[firstChangedLine] === this.lineElements[firstChangedLine].textContent &&
|
|
903
|
+
this.lineTypes[firstChangedLine] === this.lineElements[firstChangedLine].className) {
|
|
904
|
+
firstChangedLine++;
|
|
905
|
+
}
|
|
906
|
+
let lastChangedLine = -1;
|
|
907
|
+
while (-lastChangedLine < this.lines.length &&
|
|
908
|
+
-lastChangedLine < this.lineElements.length &&
|
|
909
|
+
this.lines[this.lines.length + lastChangedLine] ===
|
|
910
|
+
this.lineElements[this.lineElements.length + lastChangedLine].textContent &&
|
|
911
|
+
this.lineTypes[this.lines.length + lastChangedLine] ===
|
|
912
|
+
this.lineElements[this.lineElements.length + lastChangedLine].className) {
|
|
913
|
+
lastChangedLine--;
|
|
914
|
+
}
|
|
915
|
+
let linesToDelete = this.lines.length + lastChangedLine + 1 - firstChangedLine;
|
|
916
|
+
if (linesToDelete < -lineDelta)
|
|
917
|
+
linesToDelete = -lineDelta;
|
|
918
|
+
if (linesToDelete < 0)
|
|
919
|
+
linesToDelete = 0;
|
|
920
|
+
let linesToAdd = [];
|
|
921
|
+
for (let l = 0; l < linesToDelete + lineDelta; l++) {
|
|
922
|
+
linesToAdd.push(this.lineElements[firstChangedLine + l].textContent || "");
|
|
923
|
+
}
|
|
924
|
+
this.spliceLines(firstChangedLine, linesToDelete, linesToAdd, false);
|
|
925
|
+
}
|
|
926
|
+
else {
|
|
927
|
+
for (let line = 0; line < this.lineElements.length; line++) {
|
|
928
|
+
let e = this.lineElements[line];
|
|
929
|
+
let ct = e.textContent || "";
|
|
930
|
+
if (this.lines[line] !== ct || this.lineTypes[line] !== e.className) {
|
|
931
|
+
this.lines[line] = ct;
|
|
932
|
+
this.lineTypes[line] = e.className;
|
|
933
|
+
this.lineDirty[line] = true;
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
spliceLines(startLine, linesToDelete = 0, linesToInsert = [], adjustLineElements = true) {
|
|
939
|
+
if (adjustLineElements) {
|
|
940
|
+
for (let i = 0; i < linesToDelete; i++) {
|
|
941
|
+
this.e.removeChild(this.e.childNodes[startLine]);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
let insertedBlank = [];
|
|
945
|
+
let insertedDirty = [];
|
|
946
|
+
for (let i = 0; i < linesToInsert.length; i++) {
|
|
947
|
+
insertedBlank.push("");
|
|
948
|
+
insertedDirty.push(true);
|
|
949
|
+
if (adjustLineElements) {
|
|
950
|
+
if (this.e.childNodes[startLine])
|
|
951
|
+
this.e.insertBefore(document.createElement("div"), this.e.childNodes[startLine]);
|
|
952
|
+
else
|
|
953
|
+
this.e.appendChild(document.createElement("div"));
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
this.lines.splice(startLine, linesToDelete, ...linesToInsert);
|
|
957
|
+
this.lineTypes.splice(startLine, linesToDelete, ...insertedBlank);
|
|
958
|
+
this.lineDirty.splice(startLine, linesToDelete, ...insertedDirty);
|
|
959
|
+
}
|
|
960
|
+
fixNodeHierarchy() {
|
|
961
|
+
const originalChildren = Array.from(this.e.childNodes);
|
|
962
|
+
const replaceChild = (child, ...newChildren) => {
|
|
963
|
+
const parent = child.parentElement;
|
|
964
|
+
const nextSibling = child.nextSibling;
|
|
965
|
+
parent.removeChild(child);
|
|
966
|
+
newChildren.forEach((newChild) => nextSibling ? parent.insertBefore(newChild, nextSibling) : parent.appendChild(newChild));
|
|
967
|
+
};
|
|
968
|
+
originalChildren.forEach((child) => {
|
|
969
|
+
if (child.nodeType !== Node.ELEMENT_NODE || child.tagName !== "DIV") {
|
|
970
|
+
const divWrapper = document.createElement("div");
|
|
971
|
+
replaceChild(child, divWrapper);
|
|
972
|
+
divWrapper.appendChild(child);
|
|
973
|
+
}
|
|
974
|
+
else if (child.childNodes.length === 0) {
|
|
975
|
+
child.appendChild(document.createElement("br"));
|
|
976
|
+
}
|
|
977
|
+
else {
|
|
978
|
+
const grandChildren = Array.from(child.childNodes);
|
|
979
|
+
if (grandChildren.some((grandChild) => grandChild.nodeType === Node.ELEMENT_NODE && grandChild.tagName === "DIV")) {
|
|
980
|
+
return replaceChild(child, ...grandChildren);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
});
|
|
984
|
+
}
|
|
985
|
+
parseLinkOrImage(originalString, isImage) {
|
|
986
|
+
// Skip the opening bracket
|
|
987
|
+
let textOffset = isImage ? 2 : 1;
|
|
988
|
+
let opener = originalString.substr(0, textOffset);
|
|
989
|
+
let type = isImage ? "TMImage" : "TMLink";
|
|
990
|
+
let currentOffset = textOffset;
|
|
991
|
+
let bracketLevel = 1;
|
|
992
|
+
let linkText = false;
|
|
993
|
+
let linkRef = false;
|
|
994
|
+
let linkLabel = [];
|
|
995
|
+
let linkDetails = [];
|
|
996
|
+
textOuter: while (currentOffset < originalString.length &&
|
|
997
|
+
linkText === false) {
|
|
998
|
+
let string = originalString.substr(currentOffset);
|
|
999
|
+
// Capture any escapes and code blocks at current position
|
|
1000
|
+
for (let rule of ["escape", "code", "autolink", "html"]) {
|
|
1001
|
+
let cap = this.mergedInlineGrammar[rule].regexp.exec(string);
|
|
1002
|
+
if (cap) {
|
|
1003
|
+
currentOffset += cap[0].length;
|
|
1004
|
+
continue textOuter;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
// Check for image
|
|
1008
|
+
if (string.match(this.mergedInlineGrammar.imageOpen.regexp)) {
|
|
1009
|
+
bracketLevel++;
|
|
1010
|
+
currentOffset += 2;
|
|
1011
|
+
continue textOuter;
|
|
1012
|
+
}
|
|
1013
|
+
// Check for link
|
|
1014
|
+
if (string.match(this.mergedInlineGrammar.linkOpen.regexp)) {
|
|
1015
|
+
bracketLevel++;
|
|
1016
|
+
if (!isImage) {
|
|
1017
|
+
if (this.parseLinkOrImage(string, false)) {
|
|
1018
|
+
return false;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
currentOffset += 1;
|
|
1022
|
+
continue textOuter;
|
|
1023
|
+
}
|
|
1024
|
+
// Check for closing bracket
|
|
1025
|
+
if (string.match(/^\]/)) {
|
|
1026
|
+
bracketLevel--;
|
|
1027
|
+
if (bracketLevel === 0) {
|
|
1028
|
+
linkText = originalString.substr(textOffset, currentOffset - textOffset);
|
|
1029
|
+
currentOffset++;
|
|
1030
|
+
continue textOuter;
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
// Nothing matches, proceed to next char
|
|
1034
|
+
currentOffset++;
|
|
1035
|
+
}
|
|
1036
|
+
// Did we find a link text?
|
|
1037
|
+
if (linkText === false)
|
|
1038
|
+
return false;
|
|
1039
|
+
// Check what type of link this is
|
|
1040
|
+
let nextChar = currentOffset < originalString.length ? originalString.substr(currentOffset, 1) : "";
|
|
1041
|
+
// REFERENCE LINKS
|
|
1042
|
+
if (nextChar === "[") {
|
|
1043
|
+
let string = originalString.substr(currentOffset);
|
|
1044
|
+
let cap = this.mergedInlineGrammar.linkLabel.regexp.exec(string);
|
|
1045
|
+
if (cap) {
|
|
1046
|
+
currentOffset += cap[0].length;
|
|
1047
|
+
linkLabel.push(cap[1], cap[2], cap[3]);
|
|
1048
|
+
if (cap[this.mergedInlineGrammar.linkLabel.labelPlaceholder]) {
|
|
1049
|
+
linkRef = cap[this.mergedInlineGrammar.linkLabel.labelPlaceholder];
|
|
1050
|
+
}
|
|
1051
|
+
else {
|
|
1052
|
+
linkRef = linkText.trim();
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
else {
|
|
1056
|
+
return false;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
else if (nextChar !== "(") {
|
|
1060
|
+
// Shortcut ref link
|
|
1061
|
+
linkRef = linkText.trim();
|
|
1062
|
+
}
|
|
1063
|
+
else {
|
|
1064
|
+
// INLINE LINKS
|
|
1065
|
+
currentOffset++;
|
|
1066
|
+
let parenthesisLevel = 1;
|
|
1067
|
+
inlineOuter: while (currentOffset < originalString.length && parenthesisLevel > 0) {
|
|
1068
|
+
let string = originalString.substr(currentOffset);
|
|
1069
|
+
// Process whitespace
|
|
1070
|
+
let cap = /^\s+/.exec(string);
|
|
1071
|
+
if (cap) {
|
|
1072
|
+
switch (linkDetails.length) {
|
|
1073
|
+
case 0:
|
|
1074
|
+
linkDetails.push(cap[0]);
|
|
1075
|
+
break;
|
|
1076
|
+
case 1:
|
|
1077
|
+
linkDetails.push(cap[0]);
|
|
1078
|
+
break;
|
|
1079
|
+
case 2:
|
|
1080
|
+
if (linkDetails[0].match(/</)) {
|
|
1081
|
+
linkDetails[1] = linkDetails[1].concat(cap[0]);
|
|
1082
|
+
}
|
|
1083
|
+
else {
|
|
1084
|
+
if (parenthesisLevel !== 1)
|
|
1085
|
+
return false;
|
|
1086
|
+
linkDetails.push("");
|
|
1087
|
+
linkDetails.push(cap[0]);
|
|
1088
|
+
}
|
|
1089
|
+
break;
|
|
1090
|
+
case 3:
|
|
1091
|
+
linkDetails.push(cap[0]);
|
|
1092
|
+
break;
|
|
1093
|
+
case 4:
|
|
1094
|
+
return false;
|
|
1095
|
+
case 5:
|
|
1096
|
+
linkDetails.push("");
|
|
1097
|
+
case 6:
|
|
1098
|
+
linkDetails[5] = linkDetails[5].concat(cap[0]);
|
|
1099
|
+
break;
|
|
1100
|
+
case 7:
|
|
1101
|
+
linkDetails[6] = linkDetails[6].concat(cap[0]);
|
|
1102
|
+
break;
|
|
1103
|
+
default:
|
|
1104
|
+
return false;
|
|
1105
|
+
}
|
|
1106
|
+
currentOffset += cap[0].length;
|
|
1107
|
+
continue inlineOuter;
|
|
1108
|
+
}
|
|
1109
|
+
// Process backslash escapes
|
|
1110
|
+
cap = this.mergedInlineGrammar.escape.regexp.exec(string);
|
|
1111
|
+
if (cap) {
|
|
1112
|
+
switch (linkDetails.length) {
|
|
1113
|
+
case 0:
|
|
1114
|
+
linkDetails.push("");
|
|
1115
|
+
case 1:
|
|
1116
|
+
linkDetails.push(cap[0]);
|
|
1117
|
+
break;
|
|
1118
|
+
case 2:
|
|
1119
|
+
linkDetails[1] = linkDetails[1].concat(cap[0]);
|
|
1120
|
+
break;
|
|
1121
|
+
case 3:
|
|
1122
|
+
return false;
|
|
1123
|
+
case 4:
|
|
1124
|
+
return false;
|
|
1125
|
+
case 5:
|
|
1126
|
+
linkDetails.push("");
|
|
1127
|
+
case 6:
|
|
1128
|
+
linkDetails[5] = linkDetails[5].concat(cap[0]);
|
|
1129
|
+
break;
|
|
1130
|
+
default:
|
|
1131
|
+
return false;
|
|
1132
|
+
}
|
|
1133
|
+
currentOffset += cap[0].length;
|
|
1134
|
+
continue inlineOuter;
|
|
1135
|
+
}
|
|
1136
|
+
// Process opening angle bracket
|
|
1137
|
+
if (linkDetails.length < 2 && string.match(/^</)) {
|
|
1138
|
+
if (linkDetails.length === 0)
|
|
1139
|
+
linkDetails.push("");
|
|
1140
|
+
linkDetails[0] = linkDetails[0].concat("<");
|
|
1141
|
+
currentOffset++;
|
|
1142
|
+
continue inlineOuter;
|
|
1143
|
+
}
|
|
1144
|
+
// Process closing angle bracket
|
|
1145
|
+
if ((linkDetails.length === 1 || linkDetails.length === 2) && string.match(/^>/)) {
|
|
1146
|
+
if (linkDetails.length === 1)
|
|
1147
|
+
linkDetails.push("");
|
|
1148
|
+
linkDetails.push(">");
|
|
1149
|
+
currentOffset++;
|
|
1150
|
+
continue inlineOuter;
|
|
1151
|
+
}
|
|
1152
|
+
// Process non-parenthesis delimiter for title
|
|
1153
|
+
cap = /^["']/.exec(string);
|
|
1154
|
+
if (cap && (linkDetails.length === 0 || linkDetails.length === 1 || linkDetails.length === 4)) {
|
|
1155
|
+
while (linkDetails.length < 4)
|
|
1156
|
+
linkDetails.push("");
|
|
1157
|
+
linkDetails.push(cap[0]);
|
|
1158
|
+
currentOffset++;
|
|
1159
|
+
continue inlineOuter;
|
|
1160
|
+
}
|
|
1161
|
+
if (cap && (linkDetails.length === 5 || linkDetails.length === 6) && linkDetails[4] === cap[0]) {
|
|
1162
|
+
if (linkDetails.length === 5)
|
|
1163
|
+
linkDetails.push("");
|
|
1164
|
+
linkDetails.push(cap[0]);
|
|
1165
|
+
currentOffset++;
|
|
1166
|
+
continue inlineOuter;
|
|
1167
|
+
}
|
|
1168
|
+
// Process opening parenthesis
|
|
1169
|
+
if (string.match(/^\(/)) {
|
|
1170
|
+
switch (linkDetails.length) {
|
|
1171
|
+
case 0:
|
|
1172
|
+
linkDetails.push("");
|
|
1173
|
+
case 1:
|
|
1174
|
+
linkDetails.push("");
|
|
1175
|
+
case 2:
|
|
1176
|
+
linkDetails[1] = linkDetails[1].concat("(");
|
|
1177
|
+
if (!linkDetails[0].match(/<$/))
|
|
1178
|
+
parenthesisLevel++;
|
|
1179
|
+
break;
|
|
1180
|
+
case 3:
|
|
1181
|
+
linkDetails.push("");
|
|
1182
|
+
case 4:
|
|
1183
|
+
linkDetails.push("(");
|
|
1184
|
+
break;
|
|
1185
|
+
case 5:
|
|
1186
|
+
linkDetails.push("");
|
|
1187
|
+
case 6:
|
|
1188
|
+
if (linkDetails[4] === "(")
|
|
1189
|
+
return false;
|
|
1190
|
+
linkDetails[5] = linkDetails[5].concat("(");
|
|
1191
|
+
break;
|
|
1192
|
+
default:
|
|
1193
|
+
return false;
|
|
1194
|
+
}
|
|
1195
|
+
currentOffset++;
|
|
1196
|
+
continue inlineOuter;
|
|
1197
|
+
}
|
|
1198
|
+
// Process closing parenthesis
|
|
1199
|
+
if (string.match(/^\)/)) {
|
|
1200
|
+
if (linkDetails.length <= 2) {
|
|
1201
|
+
while (linkDetails.length < 2)
|
|
1202
|
+
linkDetails.push("");
|
|
1203
|
+
if (!linkDetails[0].match(/<$/))
|
|
1204
|
+
parenthesisLevel--;
|
|
1205
|
+
if (parenthesisLevel > 0) {
|
|
1206
|
+
linkDetails[1] = linkDetails[1].concat(")");
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
else if (linkDetails.length === 5 || linkDetails.length === 6) {
|
|
1210
|
+
if (linkDetails[4] === "(") {
|
|
1211
|
+
if (linkDetails.length === 5)
|
|
1212
|
+
linkDetails.push("");
|
|
1213
|
+
linkDetails.push(")");
|
|
1214
|
+
}
|
|
1215
|
+
else {
|
|
1216
|
+
if (linkDetails.length === 5)
|
|
1217
|
+
linkDetails.push(")");
|
|
1218
|
+
else
|
|
1219
|
+
linkDetails[5] = linkDetails[5].concat(")");
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
else {
|
|
1223
|
+
parenthesisLevel--;
|
|
1224
|
+
}
|
|
1225
|
+
if (parenthesisLevel === 0) {
|
|
1226
|
+
while (linkDetails.length < 7)
|
|
1227
|
+
linkDetails.push("");
|
|
1228
|
+
}
|
|
1229
|
+
currentOffset++;
|
|
1230
|
+
continue inlineOuter;
|
|
1231
|
+
}
|
|
1232
|
+
// Any old character
|
|
1233
|
+
cap = /^./.exec(string);
|
|
1234
|
+
if (cap) {
|
|
1235
|
+
switch (linkDetails.length) {
|
|
1236
|
+
case 0:
|
|
1237
|
+
linkDetails.push("");
|
|
1238
|
+
case 1:
|
|
1239
|
+
linkDetails.push(cap[0]);
|
|
1240
|
+
break;
|
|
1241
|
+
case 2:
|
|
1242
|
+
linkDetails[1] = linkDetails[1].concat(cap[0]);
|
|
1243
|
+
break;
|
|
1244
|
+
case 3:
|
|
1245
|
+
return false;
|
|
1246
|
+
case 4:
|
|
1247
|
+
return false;
|
|
1248
|
+
case 5:
|
|
1249
|
+
linkDetails.push("");
|
|
1250
|
+
case 6:
|
|
1251
|
+
linkDetails[5] = linkDetails[5].concat(cap[0]);
|
|
1252
|
+
break;
|
|
1253
|
+
default:
|
|
1254
|
+
return false;
|
|
1255
|
+
}
|
|
1256
|
+
currentOffset += cap[0].length;
|
|
1257
|
+
continue inlineOuter;
|
|
1258
|
+
}
|
|
1259
|
+
throw "Infinite loop";
|
|
1260
|
+
}
|
|
1261
|
+
if (parenthesisLevel > 0)
|
|
1262
|
+
return false;
|
|
1263
|
+
}
|
|
1264
|
+
if (linkRef !== false) {
|
|
1265
|
+
// Reference link; check that linkRef is valid
|
|
1266
|
+
let valid = false;
|
|
1267
|
+
for (let label of this.linkLabels) {
|
|
1268
|
+
if (label === linkRef) {
|
|
1269
|
+
valid = true;
|
|
1270
|
+
break;
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
let labelClass = valid ? "TMLinkLabel TMLinkLabel_Valid" : "TMLinkLabel TMLinkLabel_Invalid";
|
|
1274
|
+
let output = `<span class="TMMark TMMark_${type}">${opener}</span><span class="${type} ${linkLabel.length < 3 || !linkLabel[1] ? labelClass : ""}">${this.processInlineStyles(linkText)}</span><span class="TMMark TMMark_${type}">]</span>`;
|
|
1275
|
+
if (linkLabel.length >= 3) {
|
|
1276
|
+
output = output.concat(`<span class="TMMark TMMark_${type}">${linkLabel[0]}</span>`, `<span class="${labelClass}">${linkLabel[1]}</span>`, `<span class="TMMark TMMark_${type}">${linkLabel[2]}</span>`);
|
|
1277
|
+
}
|
|
1278
|
+
return {
|
|
1279
|
+
output: output,
|
|
1280
|
+
charCount: currentOffset,
|
|
1281
|
+
};
|
|
1282
|
+
}
|
|
1283
|
+
else if (linkDetails.length > 0) {
|
|
1284
|
+
// Inline link
|
|
1285
|
+
while (linkDetails.length < 7) {
|
|
1286
|
+
linkDetails.push("");
|
|
1287
|
+
}
|
|
1288
|
+
return {
|
|
1289
|
+
output: `<span class="TMMark TMMark_${type}">${opener}</span><span class="${type}">${this.processInlineStyles(linkText)}</span><span class="TMMark TMMark_${type}">](${linkDetails[0]}</span><span class="${type}Destination">${linkDetails[1]}</span><span class="TMMark TMMark_${type}">${linkDetails[2]}${linkDetails[3]}${linkDetails[4]}</span><span class="${type}Title">${linkDetails[5]}</span><span class="TMMark TMMark_${type}">${linkDetails[6]})</span>`,
|
|
1290
|
+
charCount: currentOffset,
|
|
1291
|
+
};
|
|
1292
|
+
}
|
|
1293
|
+
return false;
|
|
1294
|
+
}
|
|
1295
|
+
computeCommonAncestor(node1, node2) {
|
|
1296
|
+
if (!node1 || !node2)
|
|
1297
|
+
return null;
|
|
1298
|
+
if (node1 === node2)
|
|
1299
|
+
return node1;
|
|
1300
|
+
const ancestry = (node) => {
|
|
1301
|
+
let ancestry = [];
|
|
1302
|
+
while (node) {
|
|
1303
|
+
ancestry.unshift(node);
|
|
1304
|
+
node = node.parentNode;
|
|
1305
|
+
}
|
|
1306
|
+
return ancestry;
|
|
1307
|
+
};
|
|
1308
|
+
const ancestry1 = ancestry(node1);
|
|
1309
|
+
const ancestry2 = ancestry(node2);
|
|
1310
|
+
if (ancestry1[0] !== ancestry2[0])
|
|
1311
|
+
return null;
|
|
1312
|
+
let i;
|
|
1313
|
+
for (i = 0; ancestry1[i] === ancestry2[i]; i++)
|
|
1314
|
+
;
|
|
1315
|
+
return ancestry1[i - 1];
|
|
1316
|
+
}
|
|
1317
|
+
computeEnclosingMarkupNode(focus, anchor, className) {
|
|
1318
|
+
let node = null;
|
|
1319
|
+
if (!focus)
|
|
1320
|
+
return null;
|
|
1321
|
+
if (!anchor) {
|
|
1322
|
+
const sel = window.getSelection();
|
|
1323
|
+
if (!sel || !sel.focusNode)
|
|
1324
|
+
return null;
|
|
1325
|
+
node = sel.focusNode;
|
|
1326
|
+
}
|
|
1327
|
+
else {
|
|
1328
|
+
if (focus.row !== anchor.row)
|
|
1329
|
+
return null;
|
|
1330
|
+
const sel = window.getSelection();
|
|
1331
|
+
if (!sel)
|
|
1332
|
+
return null;
|
|
1333
|
+
node = this.computeCommonAncestor(sel.focusNode, sel.anchorNode);
|
|
1334
|
+
}
|
|
1335
|
+
if (!node)
|
|
1336
|
+
return null;
|
|
1337
|
+
while (node !== this.e) {
|
|
1338
|
+
if (node.className && node.className.includes(className))
|
|
1339
|
+
return node;
|
|
1340
|
+
node = node.parentNode;
|
|
1341
|
+
}
|
|
1342
|
+
return null;
|
|
1343
|
+
}
|
|
1344
|
+
getCommandState(focus = null, anchor = null) {
|
|
1345
|
+
let commandState = {};
|
|
1346
|
+
if (!focus)
|
|
1347
|
+
focus = this.getSelection(false);
|
|
1348
|
+
if (!anchor)
|
|
1349
|
+
anchor = this.getSelection(true);
|
|
1350
|
+
if (!focus) {
|
|
1351
|
+
for (let cmd in grammar_1.commands) {
|
|
1352
|
+
commandState[cmd] = null;
|
|
1353
|
+
}
|
|
1354
|
+
return commandState;
|
|
1355
|
+
}
|
|
1356
|
+
if (!anchor)
|
|
1357
|
+
anchor = focus;
|
|
1358
|
+
let start, end;
|
|
1359
|
+
if (anchor.row < focus.row || (anchor.row === focus.row && anchor.col < focus.col)) {
|
|
1360
|
+
start = anchor;
|
|
1361
|
+
end = focus;
|
|
1362
|
+
}
|
|
1363
|
+
else {
|
|
1364
|
+
start = focus;
|
|
1365
|
+
end = anchor;
|
|
1366
|
+
}
|
|
1367
|
+
if (end.row > start.row && end.col === 0) {
|
|
1368
|
+
end.row--;
|
|
1369
|
+
end.col = this.lines[end.row].length;
|
|
1370
|
+
}
|
|
1371
|
+
for (let cmd in grammar_1.commands) {
|
|
1372
|
+
if (grammar_1.commands[cmd].type === "inline") {
|
|
1373
|
+
if (!focus || focus.row !== anchor.row || !this.isInlineFormattingAllowed()) {
|
|
1374
|
+
commandState[cmd] = null;
|
|
1375
|
+
}
|
|
1376
|
+
else {
|
|
1377
|
+
commandState[cmd] =
|
|
1378
|
+
!!this.computeEnclosingMarkupNode(focus, anchor, grammar_1.commands[cmd].className) ||
|
|
1379
|
+
(focus.col === anchor.col &&
|
|
1380
|
+
!!this.lines[focus.row].substr(0, focus.col).match(grammar_1.commands[cmd].unset.prePattern) &&
|
|
1381
|
+
!!this.lines[focus.row].substr(focus.col).match(grammar_1.commands[cmd].unset.postPattern));
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
if (grammar_1.commands[cmd].type === "line") {
|
|
1385
|
+
if (!focus) {
|
|
1386
|
+
commandState[cmd] = null;
|
|
1387
|
+
}
|
|
1388
|
+
else {
|
|
1389
|
+
let state = this.lineTypes[start.row] === grammar_1.commands[cmd].className;
|
|
1390
|
+
for (let line = start.row; line <= end.row; line++) {
|
|
1391
|
+
if ((this.lineTypes[line] === grammar_1.commands[cmd].className) !== state) {
|
|
1392
|
+
state = null;
|
|
1393
|
+
break;
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
commandState[cmd] = state;
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
return commandState;
|
|
1401
|
+
}
|
|
1402
|
+
setCommandState(command, state) {
|
|
1403
|
+
if (!this.isRestoringHistory)
|
|
1404
|
+
this.pushHistory();
|
|
1405
|
+
if (grammar_1.commands[command].type === "inline") {
|
|
1406
|
+
let anchor = this.getSelection(true);
|
|
1407
|
+
let focus = this.getSelection(false);
|
|
1408
|
+
if (!anchor)
|
|
1409
|
+
anchor = focus;
|
|
1410
|
+
if (!anchor)
|
|
1411
|
+
return;
|
|
1412
|
+
if (anchor.row !== focus.row)
|
|
1413
|
+
return;
|
|
1414
|
+
if (!this.isInlineFormattingAllowed())
|
|
1415
|
+
return;
|
|
1416
|
+
let markupNode = this.computeEnclosingMarkupNode(focus, anchor, grammar_1.commands[command].className);
|
|
1417
|
+
this.clearDirtyFlag();
|
|
1418
|
+
if (markupNode) {
|
|
1419
|
+
this.lineDirty[focus.row] = true;
|
|
1420
|
+
const startCol = this.computeColumn(markupNode, 0);
|
|
1421
|
+
const len = markupNode.textContent.length;
|
|
1422
|
+
const left = this.lines[focus.row]
|
|
1423
|
+
.substr(0, startCol)
|
|
1424
|
+
.replace(grammar_1.commands[command].unset.prePattern, "");
|
|
1425
|
+
const mid = this.lines[focus.row].substr(startCol, len);
|
|
1426
|
+
const right = this.lines[focus.row]
|
|
1427
|
+
.substr(startCol + len)
|
|
1428
|
+
.replace(grammar_1.commands[command].unset.postPattern, "");
|
|
1429
|
+
this.lines[focus.row] = left.concat(mid, right);
|
|
1430
|
+
anchor.col = left.length;
|
|
1431
|
+
focus.col = anchor.col + len;
|
|
1432
|
+
this.updateFormatting();
|
|
1433
|
+
this.setSelection(focus, anchor);
|
|
1434
|
+
this.fireChange();
|
|
1435
|
+
}
|
|
1436
|
+
else if (focus.col === anchor.col &&
|
|
1437
|
+
!!this.lines[focus.row].substr(0, focus.col).match(grammar_1.commands[command].unset.prePattern) &&
|
|
1438
|
+
!!this.lines[focus.row].substr(focus.col).match(grammar_1.commands[command].unset.postPattern)) {
|
|
1439
|
+
this.lineDirty[focus.row] = true;
|
|
1440
|
+
const left = this.lines[focus.row]
|
|
1441
|
+
.substr(0, focus.col)
|
|
1442
|
+
.replace(grammar_1.commands[command].unset.prePattern, "");
|
|
1443
|
+
const right = this.lines[focus.row]
|
|
1444
|
+
.substr(focus.col)
|
|
1445
|
+
.replace(grammar_1.commands[command].unset.postPattern, "");
|
|
1446
|
+
this.lines[focus.row] = left.concat(right);
|
|
1447
|
+
focus.col = anchor.col = left.length;
|
|
1448
|
+
this.updateFormatting();
|
|
1449
|
+
this.setSelection(focus, anchor);
|
|
1450
|
+
this.fireChange();
|
|
1451
|
+
}
|
|
1452
|
+
else {
|
|
1453
|
+
let { startCol, endCol } = focus.col < anchor.col
|
|
1454
|
+
? { startCol: focus.col, endCol: anchor.col }
|
|
1455
|
+
: { startCol: anchor.col, endCol: focus.col };
|
|
1456
|
+
let match = this.lines[focus.row]
|
|
1457
|
+
.substr(startCol, endCol - startCol)
|
|
1458
|
+
.match(/^(?<leading>\s*).*\S(?<trailing>\s*)$/);
|
|
1459
|
+
if (match) {
|
|
1460
|
+
startCol += match.groups.leading.length;
|
|
1461
|
+
endCol -= match.groups.trailing.length;
|
|
1462
|
+
}
|
|
1463
|
+
focus.col = startCol;
|
|
1464
|
+
anchor.col = endCol;
|
|
1465
|
+
this.wrapSelection(grammar_1.commands[command].set.pre, grammar_1.commands[command].set.post, focus, anchor);
|
|
1466
|
+
this.fireChange();
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
else if (grammar_1.commands[command].type === "line") {
|
|
1470
|
+
let anchor = this.getSelection(true);
|
|
1471
|
+
let focus = this.getSelection(false);
|
|
1472
|
+
if (!anchor)
|
|
1473
|
+
anchor = focus;
|
|
1474
|
+
if (!focus)
|
|
1475
|
+
return;
|
|
1476
|
+
this.clearDirtyFlag();
|
|
1477
|
+
let start = anchor.row > focus.row ? focus : anchor;
|
|
1478
|
+
let end = anchor.row > focus.row ? anchor : focus;
|
|
1479
|
+
if (end.row > start.row && end.col === 0) {
|
|
1480
|
+
end.row--;
|
|
1481
|
+
}
|
|
1482
|
+
for (let line = start.row; line <= end.row; line++) {
|
|
1483
|
+
if (state && this.lineTypes[line] !== grammar_1.commands[command].className) {
|
|
1484
|
+
this.lines[line] = this.lines[line].replace(grammar_1.commands[command].set.pattern, grammar_1.commands[command].set.replacement.replace("$#", (line - start.row + 1).toString()));
|
|
1485
|
+
this.lineDirty[line] = true;
|
|
1486
|
+
}
|
|
1487
|
+
if (!state && this.lineTypes[line] === grammar_1.commands[command].className) {
|
|
1488
|
+
this.lines[line] = this.lines[line].replace(grammar_1.commands[command].unset.pattern, grammar_1.commands[command].unset.replacement);
|
|
1489
|
+
this.lineDirty[line] = true;
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
this.updateFormatting();
|
|
1493
|
+
this.setSelection({ row: end.row, col: this.lines[end.row].length }, { row: start.row, col: 0 });
|
|
1494
|
+
this.fireChange();
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
isInlineFormattingAllowed() {
|
|
1498
|
+
const sel = window.getSelection();
|
|
1499
|
+
if (!sel || !sel.focusNode || !sel.anchorNode)
|
|
1500
|
+
return false;
|
|
1501
|
+
if (sel.isCollapsed &&
|
|
1502
|
+
sel.focusNode.nodeType === 3 &&
|
|
1503
|
+
sel.focusOffset === sel.focusNode.nodeValue.length) {
|
|
1504
|
+
let node;
|
|
1505
|
+
for (node = sel.focusNode; node && node.nextSibling === null; node = node.parentNode)
|
|
1506
|
+
;
|
|
1507
|
+
if (node &&
|
|
1508
|
+
node.nextSibling &&
|
|
1509
|
+
node.nextSibling.className &&
|
|
1510
|
+
node.nextSibling.className.includes("TMInlineFormatted"))
|
|
1511
|
+
return true;
|
|
1512
|
+
}
|
|
1513
|
+
let ancestor = this.computeCommonAncestor(sel.focusNode, sel.anchorNode);
|
|
1514
|
+
if (!ancestor)
|
|
1515
|
+
return false;
|
|
1516
|
+
while (ancestor && ancestor !== this.e) {
|
|
1517
|
+
if (ancestor.className &&
|
|
1518
|
+
typeof ancestor.className.includes === "function" &&
|
|
1519
|
+
(ancestor.className.includes("TMInlineFormatted") ||
|
|
1520
|
+
ancestor.className.includes("TMBlankLine")))
|
|
1521
|
+
return true;
|
|
1522
|
+
ancestor = ancestor.parentNode;
|
|
1523
|
+
}
|
|
1524
|
+
return false;
|
|
1525
|
+
}
|
|
1526
|
+
toggleCommandState(command) {
|
|
1527
|
+
if (!this.lastCommandState)
|
|
1528
|
+
this.lastCommandState = this.getCommandState();
|
|
1529
|
+
this.setCommandState(command, !this.lastCommandState[command]);
|
|
1530
|
+
}
|
|
1531
|
+
fireDrop(dataTransfer) {
|
|
1532
|
+
for (let listener of this.listeners.drop) {
|
|
1533
|
+
listener({ dataTransfer });
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
fireSelection() {
|
|
1537
|
+
if (this.listeners.selection && this.listeners.selection.length) {
|
|
1538
|
+
let focus = this.getSelection(false);
|
|
1539
|
+
let anchor = this.getSelection(true);
|
|
1540
|
+
let commandState = this.getCommandState(focus, anchor);
|
|
1541
|
+
if (this.lastCommandState) {
|
|
1542
|
+
Object.assign(this.lastCommandState, commandState);
|
|
1543
|
+
}
|
|
1544
|
+
else {
|
|
1545
|
+
this.lastCommandState = Object.assign({}, commandState);
|
|
1546
|
+
}
|
|
1547
|
+
for (let listener of this.listeners.selection) {
|
|
1548
|
+
listener({
|
|
1549
|
+
focus: focus,
|
|
1550
|
+
anchor: anchor,
|
|
1551
|
+
commandState: this.lastCommandState,
|
|
1552
|
+
});
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
exports.Editor = Editor;
|
|
1558
|
+
exports.default = Editor;
|