@samtv12345/etherpad-webcomponents 0.0.0 → 0.0.14
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/LICENSE +21 -0
- package/README.md +173 -0
- package/dist/EpButton.d.ts +17 -0
- package/dist/EpButton.d.ts.map +1 -0
- package/dist/EpButton.js +168 -0
- package/dist/EpButton.js.map +1 -0
- package/dist/EpCard.d.ts +15 -0
- package/dist/EpCard.d.ts.map +1 -0
- package/dist/EpCard.js +126 -0
- package/dist/EpCard.js.map +1 -0
- package/dist/EpChatMessage.d.ts +15 -0
- package/dist/EpChatMessage.d.ts.map +1 -0
- package/dist/EpChatMessage.js +75 -0
- package/dist/EpChatMessage.js.map +1 -0
- package/dist/EpCheckbox.d.ts +16 -0
- package/dist/EpCheckbox.d.ts.map +1 -0
- package/dist/EpCheckbox.js +154 -0
- package/dist/EpCheckbox.js.map +1 -0
- package/dist/EpColorPicker.d.ts +16 -0
- package/dist/EpColorPicker.d.ts.map +1 -0
- package/dist/EpColorPicker.js +191 -0
- package/dist/EpColorPicker.js.map +1 -0
- package/dist/EpColorWheel.d.ts +61 -0
- package/dist/EpColorWheel.d.ts.map +1 -0
- package/dist/EpColorWheel.js +428 -0
- package/dist/EpColorWheel.js.map +1 -0
- package/dist/EpDropdown.d.ts +44 -0
- package/dist/EpDropdown.d.ts.map +1 -0
- package/dist/EpDropdown.js +298 -0
- package/dist/EpDropdown.js.map +1 -0
- package/dist/EpDropdownItem.d.ts +16 -0
- package/dist/EpDropdownItem.d.ts.map +1 -0
- package/dist/EpDropdownItem.js +88 -0
- package/dist/EpDropdownItem.js.map +1 -0
- package/dist/EpEditor.d.ts +108 -0
- package/dist/EpEditor.d.ts.map +1 -0
- package/dist/EpEditor.js +277 -0
- package/dist/EpEditor.js.map +1 -0
- package/dist/EpInput.d.ts +20 -0
- package/dist/EpInput.d.ts.map +1 -0
- package/dist/EpInput.js +154 -0
- package/dist/EpInput.js.map +1 -0
- package/dist/EpModal.d.ts +45 -0
- package/dist/EpModal.d.ts.map +1 -0
- package/dist/EpModal.js +212 -0
- package/dist/EpModal.js.map +1 -0
- package/dist/EpNotification.d.ts +32 -0
- package/dist/EpNotification.d.ts.map +1 -0
- package/dist/EpNotification.js +215 -0
- package/dist/EpNotification.js.map +1 -0
- package/dist/EpSelect.d.ts +38 -0
- package/dist/EpSelect.d.ts.map +1 -0
- package/dist/EpSelect.js +270 -0
- package/dist/EpSelect.js.map +1 -0
- package/dist/EpTabs.d.ts +64 -0
- package/dist/EpTabs.d.ts.map +1 -0
- package/dist/EpTabs.js +296 -0
- package/dist/EpTabs.js.map +1 -0
- package/dist/EpTheme.d.ts +40 -0
- package/dist/EpTheme.d.ts.map +1 -0
- package/dist/EpTheme.js +136 -0
- package/dist/EpTheme.js.map +1 -0
- package/dist/EpToast.d.ts +37 -0
- package/dist/EpToast.d.ts.map +1 -0
- package/dist/EpToast.js +220 -0
- package/dist/EpToast.js.map +1 -0
- package/dist/EpToolbarSelect.d.ts +24 -0
- package/dist/EpToolbarSelect.d.ts.map +1 -0
- package/dist/EpToolbarSelect.js +138 -0
- package/dist/EpToolbarSelect.js.map +1 -0
- package/dist/EpUserBadge.d.ts +15 -0
- package/dist/EpUserBadge.d.ts.map +1 -0
- package/dist/EpUserBadge.js +90 -0
- package/dist/EpUserBadge.js.map +1 -0
- package/dist/collab/clientMessage.d.ts +102 -0
- package/dist/collab/clientMessage.d.ts.map +1 -0
- package/dist/collab/clientMessage.js +132 -0
- package/dist/collab/clientMessage.js.map +1 -0
- package/dist/editor/AceEditor.d.ts +205 -0
- package/dist/editor/AceEditor.d.ts.map +1 -0
- package/dist/editor/AceEditor.js +2879 -0
- package/dist/editor/AceEditor.js.map +1 -0
- package/dist/editor/AttributeManager.d.ts +25 -0
- package/dist/editor/AttributeManager.d.ts.map +1 -0
- package/dist/editor/AttributeManager.js +236 -0
- package/dist/editor/AttributeManager.js.map +1 -0
- package/dist/editor/AttributeMap.d.ts +13 -0
- package/dist/editor/AttributeMap.d.ts.map +1 -0
- package/dist/editor/AttributeMap.js +37 -0
- package/dist/editor/AttributeMap.js.map +1 -0
- package/dist/editor/AttributePool.d.ts +22 -0
- package/dist/editor/AttributePool.d.ts.map +1 -0
- package/dist/editor/AttributePool.js +101 -0
- package/dist/editor/AttributePool.js.map +1 -0
- package/dist/editor/Builder.d.ts +15 -0
- package/dist/editor/Builder.d.ts.map +1 -0
- package/dist/editor/Builder.js +47 -0
- package/dist/editor/Builder.js.map +1 -0
- package/dist/editor/Changeset.d.ts +61 -0
- package/dist/editor/Changeset.d.ts.map +1 -0
- package/dist/editor/Changeset.js +942 -0
- package/dist/editor/Changeset.js.map +1 -0
- package/dist/editor/ChangesetUtils.d.ts +11 -0
- package/dist/editor/ChangesetUtils.d.ts.map +1 -0
- package/dist/editor/ChangesetUtils.js +30 -0
- package/dist/editor/ChangesetUtils.js.map +1 -0
- package/dist/editor/MergingOpAssembler.d.ts +13 -0
- package/dist/editor/MergingOpAssembler.d.ts.map +1 -0
- package/dist/editor/MergingOpAssembler.js +60 -0
- package/dist/editor/MergingOpAssembler.js.map +1 -0
- package/dist/editor/Op.d.ts +10 -0
- package/dist/editor/Op.d.ts.map +1 -0
- package/dist/editor/Op.js +18 -0
- package/dist/editor/Op.js.map +1 -0
- package/dist/editor/OpAssembler.d.ts +9 -0
- package/dist/editor/OpAssembler.d.ts.map +1 -0
- package/dist/editor/OpAssembler.js +16 -0
- package/dist/editor/OpAssembler.js.map +1 -0
- package/dist/editor/OpIter.d.ts +9 -0
- package/dist/editor/OpIter.d.ts.map +1 -0
- package/dist/editor/OpIter.js +22 -0
- package/dist/editor/OpIter.js.map +1 -0
- package/dist/editor/SmartOpAssembler.d.ts +21 -0
- package/dist/editor/SmartOpAssembler.d.ts.map +1 -0
- package/dist/editor/SmartOpAssembler.js +71 -0
- package/dist/editor/SmartOpAssembler.js.map +1 -0
- package/dist/editor/StringAssembler.d.ts +7 -0
- package/dist/editor/StringAssembler.d.ts.map +1 -0
- package/dist/editor/StringAssembler.js +15 -0
- package/dist/editor/StringAssembler.js.map +1 -0
- package/dist/editor/StringIterator.d.ts +13 -0
- package/dist/editor/StringIterator.d.ts.map +1 -0
- package/dist/editor/StringIterator.js +29 -0
- package/dist/editor/StringIterator.js.map +1 -0
- package/dist/editor/TextLinesMutator.d.ts +31 -0
- package/dist/editor/TextLinesMutator.d.ts.map +1 -0
- package/dist/editor/TextLinesMutator.js +198 -0
- package/dist/editor/TextLinesMutator.js.map +1 -0
- package/dist/editor/ace2_common.d.ts +9 -0
- package/dist/editor/ace2_common.d.ts.map +1 -0
- package/dist/editor/ace2_common.js +31 -0
- package/dist/editor/ace2_common.js.map +1 -0
- package/dist/editor/attributes.d.ts +20 -0
- package/dist/editor/attributes.d.ts.map +1 -0
- package/dist/editor/attributes.js +54 -0
- package/dist/editor/attributes.js.map +1 -0
- package/dist/editor/browser_flags.d.ts +7 -0
- package/dist/editor/browser_flags.d.ts.map +1 -0
- package/dist/editor/browser_flags.js +8 -0
- package/dist/editor/browser_flags.js.map +1 -0
- package/dist/editor/changesettracker.d.ts +54 -0
- package/dist/editor/changesettracker.d.ts.map +1 -0
- package/dist/editor/changesettracker.js +193 -0
- package/dist/editor/changesettracker.js.map +1 -0
- package/dist/editor/colorutils.d.ts +22 -0
- package/dist/editor/colorutils.d.ts.map +1 -0
- package/dist/editor/colorutils.js +81 -0
- package/dist/editor/colorutils.js.map +1 -0
- package/dist/editor/contentcollector.d.ts +52 -0
- package/dist/editor/contentcollector.d.ts.map +1 -0
- package/dist/editor/contentcollector.js +692 -0
- package/dist/editor/contentcollector.js.map +1 -0
- package/dist/editor/core/EventBus.d.ts +187 -0
- package/dist/editor/core/EventBus.d.ts.map +1 -0
- package/dist/editor/core/EventBus.js +169 -0
- package/dist/editor/core/EventBus.js.map +1 -0
- package/dist/editor/cssmanager.d.ts +6 -0
- package/dist/editor/cssmanager.d.ts.map +1 -0
- package/dist/editor/cssmanager.js +35 -0
- package/dist/editor/cssmanager.js.map +1 -0
- package/dist/editor/domline.d.ts +35 -0
- package/dist/editor/domline.d.ts.map +1 -0
- package/dist/editor/domline.js +267 -0
- package/dist/editor/domline.js.map +1 -0
- package/dist/editor/html_escape.d.ts +3 -0
- package/dist/editor/html_escape.d.ts.map +1 -0
- package/dist/editor/html_escape.js +8 -0
- package/dist/editor/html_escape.js.map +1 -0
- package/dist/editor/linestylefilter.d.ts +22 -0
- package/dist/editor/linestylefilter.d.ts.map +1 -0
- package/dist/editor/linestylefilter.js +298 -0
- package/dist/editor/linestylefilter.js.map +1 -0
- package/dist/editor/skiplist.d.ts +51 -0
- package/dist/editor/skiplist.d.ts.map +1 -0
- package/dist/editor/skiplist.js +327 -0
- package/dist/editor/skiplist.js.map +1 -0
- package/dist/editor/types/AText.d.ts +5 -0
- package/dist/editor/types/AText.d.ts.map +1 -0
- package/dist/editor/types/AText.js +2 -0
- package/dist/editor/types/AText.js.map +1 -0
- package/dist/editor/types/Attribute.d.ts +2 -0
- package/dist/editor/types/Attribute.d.ts.map +1 -0
- package/dist/editor/types/Attribute.js +2 -0
- package/dist/editor/types/Attribute.js.map +1 -0
- package/dist/editor/types/ChangeSet.d.ts +7 -0
- package/dist/editor/types/ChangeSet.d.ts.map +1 -0
- package/dist/editor/types/ChangeSet.js +2 -0
- package/dist/editor/types/ChangeSet.js.map +1 -0
- package/dist/editor/types/ChangeSetBuilder.d.ts +7 -0
- package/dist/editor/types/ChangeSetBuilder.d.ts.map +1 -0
- package/dist/editor/types/ChangeSetBuilder.js +2 -0
- package/dist/editor/types/ChangeSetBuilder.js.map +1 -0
- package/dist/editor/types/RepModel.d.ts +25 -0
- package/dist/editor/types/RepModel.d.ts.map +1 -0
- package/dist/editor/types/RepModel.js +2 -0
- package/dist/editor/types/RepModel.js.map +1 -0
- package/dist/editor/undomodule.d.ts +30 -0
- package/dist/editor/undomodule.d.ts.map +1 -0
- package/dist/editor/undomodule.js +276 -0
- package/dist/editor/undomodule.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/themes.d.ts +81 -0
- package/dist/themes.d.ts.map +1 -0
- package/dist/themes.js +227 -0
- package/dist/themes.js.map +1 -0
- package/package.json +73 -7
|
@@ -0,0 +1,2879 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AceEditor - Standalone editor engine adapted from Etherpad's ace2_inner.ts
|
|
3
|
+
*
|
|
4
|
+
* This is the core editing engine that works without iframes or collaboration
|
|
5
|
+
* dependencies. It uses a simple container element (a <div contenteditable="true">)
|
|
6
|
+
* inside Shadow DOM.
|
|
7
|
+
*
|
|
8
|
+
* Copyright 2009 Google Inc.
|
|
9
|
+
* Copyright 2020 John McLear - The Etherpad Foundation.
|
|
10
|
+
* Copyright 2025 - Adapted for standalone use.
|
|
11
|
+
*
|
|
12
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
13
|
+
* you may not use this file except in compliance with the License.
|
|
14
|
+
* You may obtain a copy of the License at
|
|
15
|
+
*
|
|
16
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
17
|
+
*
|
|
18
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
19
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
20
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
+
* See the License for the specific language governing permissions and
|
|
22
|
+
* limitations under the License.
|
|
23
|
+
*/
|
|
24
|
+
import { Builder } from "./Builder.js";
|
|
25
|
+
import AttributeMap from './AttributeMap.js';
|
|
26
|
+
import { browserFlags as browser } from './browser_flags.js';
|
|
27
|
+
import * as Ace2Common from './ace2_common.js';
|
|
28
|
+
import { characterRangeFollow, checkRep, cloneAText, compose, deserializeOps, filterAttribNumbers, inverse, isIdentity, makeAText, makeAttribution, mapAttribNumbers, moveOpsToNewPool, mutateAttributionLines, mutateTextLines, oldLen, opsFromAText, pack, splitAttributionLines, } from './Changeset.js';
|
|
29
|
+
import { colorutils } from './colorutils.js';
|
|
30
|
+
import { makeContentCollector } from './contentcollector.js';
|
|
31
|
+
import { domline } from './domline.js';
|
|
32
|
+
import { linestylefilter } from './linestylefilter.js';
|
|
33
|
+
import { undoModule } from './undomodule.js';
|
|
34
|
+
import AttributeManager from './AttributeManager.js';
|
|
35
|
+
import { editorBus } from './core/EventBus.js';
|
|
36
|
+
import SkipList from "./skiplist.js";
|
|
37
|
+
import AttribPool from './AttributePool.js';
|
|
38
|
+
import { SmartOpAssembler } from "./SmartOpAssembler.js";
|
|
39
|
+
import Op from "./Op.js";
|
|
40
|
+
import { buildKeepRange, buildKeepToStartOfRange, buildRemoveRange } from './ChangesetUtils.js';
|
|
41
|
+
import { makeCSSManager } from './cssmanager.js';
|
|
42
|
+
import { makeChangesetTracker } from './changesettracker.js';
|
|
43
|
+
const isNodeText = Ace2Common.isNodeText;
|
|
44
|
+
const getAssoc = Ace2Common.getAssoc;
|
|
45
|
+
const setAssoc = Ace2Common.setAssoc;
|
|
46
|
+
export class AceEditor {
|
|
47
|
+
// -----------------------------------------------------------------------
|
|
48
|
+
// Constructor
|
|
49
|
+
// -----------------------------------------------------------------------
|
|
50
|
+
constructor(container) {
|
|
51
|
+
/** Callback invoked when the document text changes. */
|
|
52
|
+
this.onContentChanged = null;
|
|
53
|
+
/** Callback invoked when the selection changes. */
|
|
54
|
+
this.onSelectionChanged = null;
|
|
55
|
+
this.changesetTracker = null;
|
|
56
|
+
this.onKeyPressHandler = null;
|
|
57
|
+
this.onKeyDownHandler = null;
|
|
58
|
+
this.notifyDirtyHandler = null;
|
|
59
|
+
// -----------------------------------------------------------------------
|
|
60
|
+
// Private - List Operations
|
|
61
|
+
// -----------------------------------------------------------------------
|
|
62
|
+
this.listAttributeName = 'list';
|
|
63
|
+
this.targetBody = container;
|
|
64
|
+
this.targetDoc = container.ownerDocument;
|
|
65
|
+
const rn = container.getRootNode();
|
|
66
|
+
this.rootNode = (rn instanceof ShadowRoot) ? rn : this.targetDoc;
|
|
67
|
+
this.disposed = false;
|
|
68
|
+
this.isEditable = true;
|
|
69
|
+
this.doesWrap = true;
|
|
70
|
+
this.isStyled = true;
|
|
71
|
+
this.thisAuthor = '';
|
|
72
|
+
this.currentCallStack = null;
|
|
73
|
+
this._nextId = 1;
|
|
74
|
+
this.inInternationalComposition = null;
|
|
75
|
+
this.thisKeyDoesntTriggerNormalize = false;
|
|
76
|
+
this.authorInfos = {};
|
|
77
|
+
this.rep = {
|
|
78
|
+
lines: new SkipList(),
|
|
79
|
+
selStart: null,
|
|
80
|
+
selEnd: null,
|
|
81
|
+
selFocusAtStart: false,
|
|
82
|
+
alltext: '',
|
|
83
|
+
alines: [],
|
|
84
|
+
apool: new AttribPool(),
|
|
85
|
+
};
|
|
86
|
+
if (undoModule.enabled) {
|
|
87
|
+
undoModule.apool = this.rep.apool;
|
|
88
|
+
}
|
|
89
|
+
this.clearObservedChanges();
|
|
90
|
+
// Set up CSS manager
|
|
91
|
+
this.cssManager = this.createCSSManager();
|
|
92
|
+
// Init documentAttributeManager
|
|
93
|
+
this.documentAttributeManager = new AttributeManager(this.rep, (cs) => this.performDocumentApplyChangeset(cs));
|
|
94
|
+
// Create idle work timer
|
|
95
|
+
this.idleWorkTimer = this.makeIdleAction(() => {
|
|
96
|
+
if (this.inInternationalComposition) {
|
|
97
|
+
this.idleWorkTimer.atLeast(500);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
this.inCallStackIfNecessary('idleWorkTimer', () => {
|
|
101
|
+
const isTimeUp = this.newTimeLimit(250);
|
|
102
|
+
let finishedImportantWork = false;
|
|
103
|
+
let finishedWork = false;
|
|
104
|
+
try {
|
|
105
|
+
this.incorporateUserChanges();
|
|
106
|
+
if (isTimeUp())
|
|
107
|
+
return;
|
|
108
|
+
finishedImportantWork = true;
|
|
109
|
+
finishedWork = true;
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
if (finishedWork) {
|
|
113
|
+
this.idleWorkTimer.atMost(1000);
|
|
114
|
+
}
|
|
115
|
+
else if (finishedImportantWork) {
|
|
116
|
+
this.idleWorkTimer.atMost(500);
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
let timeToWait = Math.round(isTimeUp.elapsed() / 2);
|
|
120
|
+
if (timeToWait < 100)
|
|
121
|
+
timeToWait = 100;
|
|
122
|
+
this.idleWorkTimer.atMost(timeToWait);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
// -----------------------------------------------------------------------
|
|
129
|
+
// Lifecycle
|
|
130
|
+
// -----------------------------------------------------------------------
|
|
131
|
+
async init() {
|
|
132
|
+
this.inCallStack('setup', () => {
|
|
133
|
+
if (browser.firefox)
|
|
134
|
+
this.targetBody.classList.add('mozilla');
|
|
135
|
+
if (browser.safari)
|
|
136
|
+
this.targetBody.classList.add('safari');
|
|
137
|
+
this.targetBody.classList.toggle('authorColors', true);
|
|
138
|
+
this.targetBody.classList.toggle('doesWrap', this.doesWrap);
|
|
139
|
+
this.enforceEditability();
|
|
140
|
+
// Set up dom and rep
|
|
141
|
+
while (this.targetBody.firstChild) {
|
|
142
|
+
this.targetBody.removeChild(this.targetBody.firstChild);
|
|
143
|
+
}
|
|
144
|
+
const oneEntry = this.createDomLineEntry('');
|
|
145
|
+
this.doRepLineSplice(0, this.rep.lines.length(), [oneEntry]);
|
|
146
|
+
this.insertDomLines(null, [oneEntry.domInfo]);
|
|
147
|
+
this.rep.alines = splitAttributionLines(makeAttribution('\n'), '\n');
|
|
148
|
+
this.bindTheEventHandlers();
|
|
149
|
+
});
|
|
150
|
+
// Initialize changeset tracker for collaboration support
|
|
151
|
+
this.changesetTracker = makeChangesetTracker(window, this.rep.apool, {
|
|
152
|
+
withCallbacks: (operationName, f) => {
|
|
153
|
+
this.inCallStackIfNecessary(operationName, () => {
|
|
154
|
+
this.fastIncorp(1);
|
|
155
|
+
f({
|
|
156
|
+
setDocumentAttributedText: (atext) => {
|
|
157
|
+
this.setDocAText(atext);
|
|
158
|
+
},
|
|
159
|
+
applyChangesetToDocument: (changeset, preferInsertionAfterCaret) => {
|
|
160
|
+
const oldEventType = this.currentCallStack.editEvent.eventType;
|
|
161
|
+
this.currentCallStack.startNewEvent('nonundoable');
|
|
162
|
+
this.performDocumentApplyChangeset(changeset, preferInsertionAfterCaret);
|
|
163
|
+
this.currentCallStack.startNewEvent(oldEventType);
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
},
|
|
168
|
+
}, () => this.thisAuthor);
|
|
169
|
+
// Note: editor:ace:initialized is NOT emitted here.
|
|
170
|
+
// When used within etherpad, ace.ts emits this event with the shared info object
|
|
171
|
+
// that holds ace_* prefixed methods for plugin compatibility.
|
|
172
|
+
}
|
|
173
|
+
dispose() {
|
|
174
|
+
this.disposed = true;
|
|
175
|
+
if (this.idleWorkTimer)
|
|
176
|
+
this.idleWorkTimer.never();
|
|
177
|
+
}
|
|
178
|
+
// -----------------------------------------------------------------------
|
|
179
|
+
// Public API - Text
|
|
180
|
+
// -----------------------------------------------------------------------
|
|
181
|
+
getText() {
|
|
182
|
+
const alltext = this.rep.alltext;
|
|
183
|
+
let len = alltext.length;
|
|
184
|
+
if (len > 0)
|
|
185
|
+
len--; // final extra newline
|
|
186
|
+
return alltext.substring(0, len);
|
|
187
|
+
}
|
|
188
|
+
setText(text) {
|
|
189
|
+
this.importText(text, false, false);
|
|
190
|
+
}
|
|
191
|
+
getAttributedText() {
|
|
192
|
+
return {
|
|
193
|
+
text: this.rep.alltext,
|
|
194
|
+
attribs: this.rep.alines.join(''),
|
|
195
|
+
pool: this.rep.apool,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
setAttributedText(atext, apoolJsonObj) {
|
|
199
|
+
this.importAText(atext, apoolJsonObj, false);
|
|
200
|
+
}
|
|
201
|
+
// -----------------------------------------------------------------------
|
|
202
|
+
// Public API - Focus / Editable
|
|
203
|
+
// -----------------------------------------------------------------------
|
|
204
|
+
focus() {
|
|
205
|
+
this.targetBody.focus();
|
|
206
|
+
}
|
|
207
|
+
setEditable(val) {
|
|
208
|
+
this.isEditable = val;
|
|
209
|
+
this.targetBody.contentEditable = this.isEditable ? 'true' : 'false';
|
|
210
|
+
this.targetBody.classList.toggle('static', !this.isEditable);
|
|
211
|
+
}
|
|
212
|
+
// -----------------------------------------------------------------------
|
|
213
|
+
// Public API - Formatting
|
|
214
|
+
// -----------------------------------------------------------------------
|
|
215
|
+
toggleAttribute(name) {
|
|
216
|
+
this.inCallStackIfNecessary('toggleAttribute', () => {
|
|
217
|
+
this.fastIncorp(13);
|
|
218
|
+
this.toggleAttributeOnSelection(name);
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
setAttribute(name, value) {
|
|
222
|
+
this.inCallStackIfNecessary('setAttribute', () => {
|
|
223
|
+
this.fastIncorp(13);
|
|
224
|
+
this.setAttributeOnSelection(name, value);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
getAttribute(name) {
|
|
228
|
+
if (!(this.rep.selStart && this.rep.selEnd))
|
|
229
|
+
return false;
|
|
230
|
+
return !!this.getAttributeOnSelection(name);
|
|
231
|
+
}
|
|
232
|
+
// -----------------------------------------------------------------------
|
|
233
|
+
// Public API - Lists
|
|
234
|
+
// -----------------------------------------------------------------------
|
|
235
|
+
setLineListType(lineNum, listType) {
|
|
236
|
+
this.inCallStackIfNecessary('setLineListType', () => {
|
|
237
|
+
this.fastIncorp(9);
|
|
238
|
+
this._setLineListType(lineNum, listType);
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
indentOutdent(isOut) {
|
|
242
|
+
this.inCallStackIfNecessary('indentOutdent', () => {
|
|
243
|
+
this.fastIncorp(9);
|
|
244
|
+
this.doIndentOutdent(isOut);
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
insertUnorderedList() {
|
|
248
|
+
this.inCallStackIfNecessary('insertUnorderedList', () => {
|
|
249
|
+
this.fastIncorp(9);
|
|
250
|
+
this.doInsertUnorderedList();
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
insertOrderedList() {
|
|
254
|
+
this.inCallStackIfNecessary('insertOrderedList', () => {
|
|
255
|
+
this.fastIncorp(9);
|
|
256
|
+
this.doInsertOrderedList();
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
// -----------------------------------------------------------------------
|
|
260
|
+
// Public API - Undo / Redo
|
|
261
|
+
// -----------------------------------------------------------------------
|
|
262
|
+
undo() {
|
|
263
|
+
this.inCallStackIfNecessary('undo', () => {
|
|
264
|
+
this.fastIncorp(6);
|
|
265
|
+
this.doUndoRedo('undo');
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
redo() {
|
|
269
|
+
this.inCallStackIfNecessary('redo', () => {
|
|
270
|
+
this.fastIncorp(10);
|
|
271
|
+
this.doUndoRedo('redo');
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
// -----------------------------------------------------------------------
|
|
275
|
+
// Public API - Changeset (for external collaboration)
|
|
276
|
+
// -----------------------------------------------------------------------
|
|
277
|
+
applyChangeset(cs, _optAuthor, apoolJsonObj) {
|
|
278
|
+
this.inCallStackIfNecessary('applyChangeset', () => {
|
|
279
|
+
this.fastIncorp(1);
|
|
280
|
+
if (apoolJsonObj) {
|
|
281
|
+
const wireApool = (new AttribPool()).fromJsonable(apoolJsonObj);
|
|
282
|
+
cs = moveOpsToNewPool(cs, wireApool, this.rep.apool);
|
|
283
|
+
}
|
|
284
|
+
const oldEventType = this.currentCallStack.editEvent.eventType;
|
|
285
|
+
this.currentCallStack.startNewEvent('nonundoable');
|
|
286
|
+
this.performDocumentApplyChangeset(cs);
|
|
287
|
+
this.currentCallStack.startNewEvent(oldEventType);
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
prepareUserChangeset() {
|
|
291
|
+
if (!this.rep || !this.rep.apool)
|
|
292
|
+
return null;
|
|
293
|
+
// Incorporate any pending user changes first
|
|
294
|
+
if (this.currentCallStack) {
|
|
295
|
+
this.fastIncorp(1);
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
this.inCallStackIfNecessary('prepareUserChangeset', () => {
|
|
299
|
+
this.fastIncorp(1);
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
if (this.changesetTracker) {
|
|
303
|
+
return this.changesetTracker.prepareUserChangeset();
|
|
304
|
+
}
|
|
305
|
+
return {
|
|
306
|
+
changeset: null,
|
|
307
|
+
apool: this.rep.apool.toJsonable(),
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
// -----------------------------------------------------------------------
|
|
311
|
+
// Public API - Author
|
|
312
|
+
// -----------------------------------------------------------------------
|
|
313
|
+
setAuthor(authorId) {
|
|
314
|
+
this.thisAuthor = String(authorId);
|
|
315
|
+
if (this.documentAttributeManager) {
|
|
316
|
+
this.documentAttributeManager.author = this.thisAuthor;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
setAuthorInfo(author, info) {
|
|
320
|
+
if (!author)
|
|
321
|
+
return;
|
|
322
|
+
if (typeof author !== 'string') {
|
|
323
|
+
throw new Error(`setAuthorInfo: author (${author}) is not a string`);
|
|
324
|
+
}
|
|
325
|
+
if (!info) {
|
|
326
|
+
delete this.authorInfos[author];
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
this.authorInfos[author] = info;
|
|
330
|
+
}
|
|
331
|
+
this.setAuthorStyle(author, info);
|
|
332
|
+
}
|
|
333
|
+
// -----------------------------------------------------------------------
|
|
334
|
+
// Public API - Collaboration (changesetTracker delegation)
|
|
335
|
+
// -----------------------------------------------------------------------
|
|
336
|
+
setBaseText(txt) {
|
|
337
|
+
this.changesetTracker?.setBaseText(txt);
|
|
338
|
+
}
|
|
339
|
+
setBaseAttributedText(atxt, apoolJsonObj) {
|
|
340
|
+
this.changesetTracker?.setBaseAttributedText(atxt, apoolJsonObj);
|
|
341
|
+
}
|
|
342
|
+
applyChangesToBase(c, optAuthor, apoolJsonObj) {
|
|
343
|
+
this.changesetTracker?.applyChangesToBase(c, optAuthor, apoolJsonObj);
|
|
344
|
+
}
|
|
345
|
+
applyPreparedChangesetToBase() {
|
|
346
|
+
this.changesetTracker?.applyPreparedChangesetToBase();
|
|
347
|
+
}
|
|
348
|
+
setUserChangeNotificationCallback(f) {
|
|
349
|
+
this.changesetTracker?.setUserChangeNotificationCallback(f);
|
|
350
|
+
}
|
|
351
|
+
// -----------------------------------------------------------------------
|
|
352
|
+
// Public API - Etherpad Compatibility
|
|
353
|
+
// -----------------------------------------------------------------------
|
|
354
|
+
setProperty(key, value) {
|
|
355
|
+
// The original ace2_inner editor lower-cases the key before dispatching,
|
|
356
|
+
// so callers can pass either 'userAuthor' or 'userauthor', 'rtlIsTrue'
|
|
357
|
+
// or 'rtlistrue'. Preserving that behavior here is critical: e.g.
|
|
358
|
+
// collab_client.ts calls setProperty('userAuthor', userId) — if this
|
|
359
|
+
// switch were case-sensitive it would silently ignore the call and
|
|
360
|
+
// every character the user typed would be attributed to the empty
|
|
361
|
+
// author string (no `author-*` class on rendered spans, no authorship
|
|
362
|
+
// coloring, no clearauthorship).
|
|
363
|
+
switch (key.toLowerCase()) {
|
|
364
|
+
case 'wraps':
|
|
365
|
+
this.setWraps(value);
|
|
366
|
+
break;
|
|
367
|
+
case 'showsauthorcolors':
|
|
368
|
+
this.targetBody.classList.toggle('authorColors', !!value);
|
|
369
|
+
break;
|
|
370
|
+
case 'showsuserselections':
|
|
371
|
+
this.targetBody.classList.toggle('userSelections', !!value);
|
|
372
|
+
break;
|
|
373
|
+
case 'userauthor':
|
|
374
|
+
this.setAuthor(value);
|
|
375
|
+
break;
|
|
376
|
+
case 'styled':
|
|
377
|
+
this.setStyled(value);
|
|
378
|
+
break;
|
|
379
|
+
case 'textface':
|
|
380
|
+
this.targetBody.style.fontFamily = value || '';
|
|
381
|
+
break;
|
|
382
|
+
case 'rtlistrue':
|
|
383
|
+
this.targetBody.dir = value ? 'rtl' : 'ltr';
|
|
384
|
+
break;
|
|
385
|
+
case 'showslinenumbers':
|
|
386
|
+
// Line numbers are not managed by the editor itself
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
exportText() {
|
|
391
|
+
return this.getText();
|
|
392
|
+
}
|
|
393
|
+
getInInternationalComposition() {
|
|
394
|
+
return this.inInternationalComposition;
|
|
395
|
+
}
|
|
396
|
+
setOnKeyPress(handler) {
|
|
397
|
+
this.onKeyPressHandler = handler;
|
|
398
|
+
}
|
|
399
|
+
setOnKeyDown(handler) {
|
|
400
|
+
this.onKeyDownHandler = handler;
|
|
401
|
+
}
|
|
402
|
+
setNotifyDirty(handler) {
|
|
403
|
+
this.notifyDirtyHandler = handler;
|
|
404
|
+
}
|
|
405
|
+
callWithAce(fn, callStack, normalize) {
|
|
406
|
+
let wrapper = () => fn(this);
|
|
407
|
+
if (normalize !== undefined) {
|
|
408
|
+
const inner = wrapper;
|
|
409
|
+
wrapper = () => {
|
|
410
|
+
this.fastIncorp(9);
|
|
411
|
+
return inner();
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
if (callStack !== undefined) {
|
|
415
|
+
return this.inCallStackIfNecessary(callStack, wrapper);
|
|
416
|
+
}
|
|
417
|
+
return wrapper();
|
|
418
|
+
}
|
|
419
|
+
// -----------------------------------------------------------------------
|
|
420
|
+
// Public API - Selection info
|
|
421
|
+
// -----------------------------------------------------------------------
|
|
422
|
+
isCaret() {
|
|
423
|
+
return !!(this.rep.selStart && this.rep.selEnd &&
|
|
424
|
+
this.rep.selStart[0] === this.rep.selEnd[0] &&
|
|
425
|
+
this.rep.selStart[1] === this.rep.selEnd[1]);
|
|
426
|
+
}
|
|
427
|
+
getCaretLine() {
|
|
428
|
+
if (!this.rep.selStart)
|
|
429
|
+
return -1;
|
|
430
|
+
return this.rep.selStart[0];
|
|
431
|
+
}
|
|
432
|
+
getCaretColumn() {
|
|
433
|
+
if (!this.rep.selStart)
|
|
434
|
+
return -1;
|
|
435
|
+
return this.rep.selStart[1];
|
|
436
|
+
}
|
|
437
|
+
// -----------------------------------------------------------------------
|
|
438
|
+
// Public API - Misc
|
|
439
|
+
// -----------------------------------------------------------------------
|
|
440
|
+
replaceRange(start, end, text) {
|
|
441
|
+
this.inCallStackIfNecessary('replaceRange', () => {
|
|
442
|
+
this.fastIncorp(9);
|
|
443
|
+
this.performDocumentReplaceRange(start, end, text);
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
execCommand(cmd, ...args) {
|
|
447
|
+
cmd = cmd.toLowerCase();
|
|
448
|
+
const cmds = {
|
|
449
|
+
clearauthorship: (prompt) => {
|
|
450
|
+
if (!(this.rep.selStart && this.rep.selEnd) || this.isCaret()) {
|
|
451
|
+
if (prompt) {
|
|
452
|
+
prompt();
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
this.performDocumentApplyAttributesToCharRange(0, this.rep.alltext.length, [
|
|
456
|
+
['author', ''],
|
|
457
|
+
]);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
this.setAttributeOnSelection('author', '');
|
|
462
|
+
}
|
|
463
|
+
},
|
|
464
|
+
};
|
|
465
|
+
if (cmds[cmd]) {
|
|
466
|
+
this.inCallStackIfNecessary(cmd, () => {
|
|
467
|
+
this.fastIncorp(9);
|
|
468
|
+
cmds[cmd](...args);
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
setWraps(newVal) {
|
|
473
|
+
this.doesWrap = newVal;
|
|
474
|
+
this.targetBody.classList.toggle('doesWrap', this.doesWrap);
|
|
475
|
+
setTimeout(() => {
|
|
476
|
+
this.inCallStackIfNecessary('setWraps', () => {
|
|
477
|
+
this.fastIncorp(7);
|
|
478
|
+
this.recreateDOM();
|
|
479
|
+
});
|
|
480
|
+
}, 0);
|
|
481
|
+
}
|
|
482
|
+
setStyled(newVal) {
|
|
483
|
+
const oldVal = this.isStyled;
|
|
484
|
+
this.isStyled = !!newVal;
|
|
485
|
+
if (newVal !== oldVal) {
|
|
486
|
+
if (!newVal) {
|
|
487
|
+
this.inCallStackIfNecessary('setStyled', () => {
|
|
488
|
+
this.fastIncorp(12);
|
|
489
|
+
const clearStyles = [];
|
|
490
|
+
for (const k of Object.keys(AceEditor.STYLE_ATTRIBS)) {
|
|
491
|
+
clearStyles.push([k, '']);
|
|
492
|
+
}
|
|
493
|
+
this.performDocumentApplyAttributesToCharRange(0, this.rep.alltext.length, clearStyles);
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
// -----------------------------------------------------------------------
|
|
499
|
+
// Private - CSS Manager
|
|
500
|
+
// -----------------------------------------------------------------------
|
|
501
|
+
createCSSManager() {
|
|
502
|
+
const styleElement = this.targetDoc.createElement('style');
|
|
503
|
+
// If the container is inside a shadow root, append there; otherwise append to head
|
|
504
|
+
const root = this.targetBody.getRootNode();
|
|
505
|
+
if (root instanceof ShadowRoot) {
|
|
506
|
+
root.appendChild(styleElement);
|
|
507
|
+
}
|
|
508
|
+
else if (this.targetBody.parentNode) {
|
|
509
|
+
this.targetBody.parentNode.insertBefore(styleElement, this.targetBody);
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
this.targetDoc.head.appendChild(styleElement);
|
|
513
|
+
}
|
|
514
|
+
return makeCSSManager(styleElement.sheet);
|
|
515
|
+
}
|
|
516
|
+
// -----------------------------------------------------------------------
|
|
517
|
+
// Private - Author Styling
|
|
518
|
+
// -----------------------------------------------------------------------
|
|
519
|
+
getAuthorClassName(author) {
|
|
520
|
+
return `author-${author.replace(/[^a-y0-9]/g, (c) => {
|
|
521
|
+
if (c === '.')
|
|
522
|
+
return '-';
|
|
523
|
+
return `z${c.charCodeAt(0)}z`;
|
|
524
|
+
})}`;
|
|
525
|
+
}
|
|
526
|
+
className2Author(className) {
|
|
527
|
+
if (className.substring(0, 7) === 'author-') {
|
|
528
|
+
return className.substring(7).replace(/[a-y0-9]+|-|z.+?z/g, (cc) => {
|
|
529
|
+
if (cc === '-')
|
|
530
|
+
return '.';
|
|
531
|
+
if (cc.charAt(0) === 'z')
|
|
532
|
+
return String.fromCharCode(Number(cc.slice(1, -1)));
|
|
533
|
+
return cc;
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
return null;
|
|
537
|
+
}
|
|
538
|
+
getAuthorColorClassSelector(oneClassName) {
|
|
539
|
+
return `.authorColors .${oneClassName}`;
|
|
540
|
+
}
|
|
541
|
+
fadeColor(colorCSS, fadeFrac) {
|
|
542
|
+
let color = colorutils.css2triple(colorCSS);
|
|
543
|
+
color = colorutils.blend(color, [1, 1, 1], fadeFrac);
|
|
544
|
+
return colorutils.triple2css(color);
|
|
545
|
+
}
|
|
546
|
+
setAuthorStyle(author, info) {
|
|
547
|
+
const authorSelector = this.getAuthorColorClassSelector(this.getAuthorClassName(author));
|
|
548
|
+
if (!info) {
|
|
549
|
+
this.cssManager.removeSelectorStyle(authorSelector);
|
|
550
|
+
}
|
|
551
|
+
else if (info.bgcolor) {
|
|
552
|
+
let bgcolor = info.bgcolor;
|
|
553
|
+
if (typeof info.fade === 'number') {
|
|
554
|
+
bgcolor = this.fadeColor(bgcolor, info.fade);
|
|
555
|
+
}
|
|
556
|
+
const textColor = colorutils.textColorFromBackgroundColor(bgcolor, 'default');
|
|
557
|
+
const style = this.cssManager.selectorStyle(authorSelector);
|
|
558
|
+
style.backgroundColor = bgcolor;
|
|
559
|
+
style.color = textColor;
|
|
560
|
+
style['padding-top'] = '3px';
|
|
561
|
+
style['padding-bottom'] = '4px';
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
// -----------------------------------------------------------------------
|
|
565
|
+
// Private - Call Stack Management
|
|
566
|
+
// -----------------------------------------------------------------------
|
|
567
|
+
inCallStack(type, action) {
|
|
568
|
+
if (this.disposed)
|
|
569
|
+
return;
|
|
570
|
+
const newEditEvent = (eventType) => ({
|
|
571
|
+
eventType,
|
|
572
|
+
backset: null,
|
|
573
|
+
});
|
|
574
|
+
const submitOldEvent = (evt) => {
|
|
575
|
+
if (this.rep.selStart && this.rep.selEnd) {
|
|
576
|
+
const selStartChar = this.rep.lines.offsetOfIndex(this.rep.selStart[0]) +
|
|
577
|
+
this.rep.selStart[1];
|
|
578
|
+
const selEndChar = this.rep.lines.offsetOfIndex(this.rep.selEnd[0]) +
|
|
579
|
+
this.rep.selEnd[1];
|
|
580
|
+
evt.selStart = selStartChar;
|
|
581
|
+
evt.selEnd = selEndChar;
|
|
582
|
+
evt.selFocusAtStart = this.rep.selFocusAtStart;
|
|
583
|
+
}
|
|
584
|
+
if (undoModule.enabled) {
|
|
585
|
+
let undoWorked = false;
|
|
586
|
+
try {
|
|
587
|
+
if (this.isPadLoading(evt.eventType)) {
|
|
588
|
+
undoModule.clearHistory();
|
|
589
|
+
}
|
|
590
|
+
else if (evt.eventType === 'nonundoable') {
|
|
591
|
+
if (evt.changeset) {
|
|
592
|
+
undoModule.reportExternalChange(evt.changeset);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
else {
|
|
596
|
+
undoModule.reportEvent(evt);
|
|
597
|
+
}
|
|
598
|
+
undoWorked = true;
|
|
599
|
+
}
|
|
600
|
+
finally {
|
|
601
|
+
if (!undoWorked) {
|
|
602
|
+
undoModule.enabled = false;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
};
|
|
607
|
+
const startNewEvent = (eventType, dontSubmitOld) => {
|
|
608
|
+
const oldEvent = this.currentCallStack.editEvent;
|
|
609
|
+
if (!dontSubmitOld) {
|
|
610
|
+
submitOldEvent(oldEvent);
|
|
611
|
+
}
|
|
612
|
+
this.currentCallStack.editEvent = newEditEvent(eventType);
|
|
613
|
+
return oldEvent;
|
|
614
|
+
};
|
|
615
|
+
this.currentCallStack = {
|
|
616
|
+
type,
|
|
617
|
+
docTextChanged: false,
|
|
618
|
+
selectionAffected: false,
|
|
619
|
+
userChangedSelection: false,
|
|
620
|
+
domClean: false,
|
|
621
|
+
isUserChange: false,
|
|
622
|
+
repChanged: false,
|
|
623
|
+
editEvent: newEditEvent(type),
|
|
624
|
+
startNewEvent,
|
|
625
|
+
};
|
|
626
|
+
let cleanExit = false;
|
|
627
|
+
let result;
|
|
628
|
+
try {
|
|
629
|
+
result = action();
|
|
630
|
+
editorBus.emit('editor:content:changed', { text: this.rep.alltext });
|
|
631
|
+
if (this.onContentChanged)
|
|
632
|
+
this.onContentChanged(this.rep.alltext);
|
|
633
|
+
if (this.notifyDirtyHandler)
|
|
634
|
+
this.notifyDirtyHandler();
|
|
635
|
+
cleanExit = true;
|
|
636
|
+
}
|
|
637
|
+
finally {
|
|
638
|
+
const cs = this.currentCallStack;
|
|
639
|
+
if (cleanExit) {
|
|
640
|
+
submitOldEvent(cs.editEvent);
|
|
641
|
+
if (cs.domClean && cs.type !== 'setup') {
|
|
642
|
+
if (cs.selectionAffected) {
|
|
643
|
+
this.updateBrowserSelectionFromRep();
|
|
644
|
+
}
|
|
645
|
+
if (cs.docTextChanged && cs.type.indexOf('importText') < 0) {
|
|
646
|
+
// Document changed notification
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
else if (this.currentCallStack.type === 'idleWorkTimer') {
|
|
651
|
+
this.idleWorkTimer.atLeast(1000);
|
|
652
|
+
}
|
|
653
|
+
this.currentCallStack = null;
|
|
654
|
+
}
|
|
655
|
+
return result;
|
|
656
|
+
}
|
|
657
|
+
inCallStackIfNecessary(type, action) {
|
|
658
|
+
if (!this.currentCallStack) {
|
|
659
|
+
return this.inCallStack(type, action);
|
|
660
|
+
}
|
|
661
|
+
else {
|
|
662
|
+
return action();
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
// -----------------------------------------------------------------------
|
|
666
|
+
// Private - Timers
|
|
667
|
+
// -----------------------------------------------------------------------
|
|
668
|
+
now() {
|
|
669
|
+
return Date.now();
|
|
670
|
+
}
|
|
671
|
+
newTimeLimit(ms) {
|
|
672
|
+
const startTime = this.now();
|
|
673
|
+
let exceededAlready = false;
|
|
674
|
+
const isTimeUp = () => {
|
|
675
|
+
if (exceededAlready)
|
|
676
|
+
return true;
|
|
677
|
+
const elapsed = this.now() - startTime;
|
|
678
|
+
if (elapsed > ms) {
|
|
679
|
+
exceededAlready = true;
|
|
680
|
+
return true;
|
|
681
|
+
}
|
|
682
|
+
return false;
|
|
683
|
+
};
|
|
684
|
+
isTimeUp.elapsed = () => this.now() - startTime;
|
|
685
|
+
return isTimeUp;
|
|
686
|
+
}
|
|
687
|
+
makeIdleAction(func) {
|
|
688
|
+
let scheduledTimeout = null;
|
|
689
|
+
let scheduledTime = 0;
|
|
690
|
+
const unschedule = () => {
|
|
691
|
+
if (scheduledTimeout) {
|
|
692
|
+
clearTimeout(scheduledTimeout);
|
|
693
|
+
scheduledTimeout = null;
|
|
694
|
+
}
|
|
695
|
+
};
|
|
696
|
+
const reschedule = (time) => {
|
|
697
|
+
unschedule();
|
|
698
|
+
scheduledTime = time;
|
|
699
|
+
let delay = time - this.now();
|
|
700
|
+
if (delay < 0)
|
|
701
|
+
delay = 0;
|
|
702
|
+
scheduledTimeout = setTimeout(callback, delay);
|
|
703
|
+
};
|
|
704
|
+
const callback = () => {
|
|
705
|
+
scheduledTimeout = null;
|
|
706
|
+
func();
|
|
707
|
+
};
|
|
708
|
+
return {
|
|
709
|
+
atMost: (ms) => {
|
|
710
|
+
const latestTime = this.now() + ms;
|
|
711
|
+
if (!scheduledTimeout || scheduledTime > latestTime) {
|
|
712
|
+
reschedule(latestTime);
|
|
713
|
+
}
|
|
714
|
+
},
|
|
715
|
+
atLeast: (ms) => {
|
|
716
|
+
const earliestTime = this.now() + ms;
|
|
717
|
+
if (!scheduledTimeout || scheduledTime < earliestTime) {
|
|
718
|
+
reschedule(earliestTime);
|
|
719
|
+
}
|
|
720
|
+
},
|
|
721
|
+
never: () => {
|
|
722
|
+
unschedule();
|
|
723
|
+
},
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
// -----------------------------------------------------------------------
|
|
727
|
+
// Private - Document Operations
|
|
728
|
+
// -----------------------------------------------------------------------
|
|
729
|
+
fastIncorp(_n) {
|
|
730
|
+
this.incorporateUserChanges();
|
|
731
|
+
}
|
|
732
|
+
setDocText(text) {
|
|
733
|
+
this.setDocAText(makeAText(text));
|
|
734
|
+
}
|
|
735
|
+
setDocAText(atext) {
|
|
736
|
+
if (atext.text === '') {
|
|
737
|
+
atext.text = '\n';
|
|
738
|
+
}
|
|
739
|
+
this.fastIncorp(8);
|
|
740
|
+
if (this.rep.lines.length() === 0) {
|
|
741
|
+
// No lines yet - bootstrap the initial empty line directly
|
|
742
|
+
const oneEntry = this.createDomLineEntry('');
|
|
743
|
+
oneEntry.width = 1; // newline char
|
|
744
|
+
this.rep.lines.splice(0, 0, [oneEntry]);
|
|
745
|
+
this.rep.alltext = '\n';
|
|
746
|
+
this.rep.alines = splitAttributionLines(makeAttribution('\n'), '\n');
|
|
747
|
+
this.insertDomLines(null, [oneEntry.domInfo]);
|
|
748
|
+
}
|
|
749
|
+
// Reset the edit event so the full doc replacement is captured cleanly for undo.
|
|
750
|
+
// fastIncorp above may have created a partial backset from incorporateUserChanges
|
|
751
|
+
// that has mismatched lengths with the changeset we're about to build.
|
|
752
|
+
if (this.currentCallStack && this.currentCallStack.editEvent) {
|
|
753
|
+
this.currentCallStack.editEvent.backset = null;
|
|
754
|
+
this.currentCallStack.editEvent.changeset = null;
|
|
755
|
+
}
|
|
756
|
+
const currentOldLen = this.rep.lines.totalWidth();
|
|
757
|
+
const numLines = this.rep.lines.length();
|
|
758
|
+
const upToLastLine = this.rep.lines.offsetOfIndex(numLines - 1);
|
|
759
|
+
const lastLineLength = this.rep.lines.atIndex(numLines - 1).text.length;
|
|
760
|
+
const assem = new SmartOpAssembler();
|
|
761
|
+
const o = new Op('-');
|
|
762
|
+
o.chars = upToLastLine;
|
|
763
|
+
o.lines = numLines - 1;
|
|
764
|
+
assem.append(o);
|
|
765
|
+
o.chars = lastLineLength;
|
|
766
|
+
o.lines = 0;
|
|
767
|
+
assem.append(o);
|
|
768
|
+
for (const op of opsFromAText(atext))
|
|
769
|
+
assem.append(op);
|
|
770
|
+
const newLen = currentOldLen + assem.getLengthChange();
|
|
771
|
+
const changeset = checkRep(pack(currentOldLen, newLen, assem.toString(), atext.text.slice(0, -1)));
|
|
772
|
+
this.performDocumentApplyChangeset(changeset);
|
|
773
|
+
this.performSelectionChange([0, this.rep.lines.atIndex(0).lineMarker], [0, this.rep.lines.atIndex(0).lineMarker]);
|
|
774
|
+
this.idleWorkTimer.atMost(100);
|
|
775
|
+
if (this.rep.alltext !== atext.text) {
|
|
776
|
+
throw new Error('mismatch error setting raw text in setDocAText');
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
importText(text, undoable, dontProcess) {
|
|
780
|
+
let lines;
|
|
781
|
+
if (dontProcess) {
|
|
782
|
+
if (text.charAt(text.length - 1) !== '\n') {
|
|
783
|
+
throw new Error('new raw text must end with newline');
|
|
784
|
+
}
|
|
785
|
+
if (/[\r\t\xa0]/.exec(text)) {
|
|
786
|
+
throw new Error('new raw text must not contain CR, tab, or nbsp');
|
|
787
|
+
}
|
|
788
|
+
lines = text.substring(0, text.length - 1).split('\n');
|
|
789
|
+
}
|
|
790
|
+
else {
|
|
791
|
+
lines = text.split('\n').map((s) => this.textify(s));
|
|
792
|
+
}
|
|
793
|
+
let newText = '\n';
|
|
794
|
+
if (lines.length > 0) {
|
|
795
|
+
newText = `${lines.join('\n')}\n`;
|
|
796
|
+
}
|
|
797
|
+
this.inCallStackIfNecessary(`importText${undoable ? 'Undoable' : ''}`, () => {
|
|
798
|
+
this.setDocText(newText);
|
|
799
|
+
});
|
|
800
|
+
if (dontProcess && this.rep.alltext !== text) {
|
|
801
|
+
throw new Error('mismatch error setting raw text in importText');
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
importAText(atext, apoolJsonObj, undoable) {
|
|
805
|
+
atext = cloneAText(atext);
|
|
806
|
+
if (apoolJsonObj) {
|
|
807
|
+
const wireApool = (new AttribPool()).fromJsonable(apoolJsonObj);
|
|
808
|
+
atext.attribs = moveOpsToNewPool(atext.attribs, wireApool, this.rep.apool);
|
|
809
|
+
}
|
|
810
|
+
this.inCallStackIfNecessary(`importText${undoable ? 'Undoable' : ''}`, () => {
|
|
811
|
+
this.setDocAText(atext);
|
|
812
|
+
});
|
|
813
|
+
}
|
|
814
|
+
performDocumentReplaceRange(start, end, newText) {
|
|
815
|
+
if (start === undefined)
|
|
816
|
+
start = this.rep.selStart;
|
|
817
|
+
if (end === undefined)
|
|
818
|
+
end = this.rep.selEnd;
|
|
819
|
+
const builder = new Builder(this.rep.lines.totalWidth());
|
|
820
|
+
buildKeepToStartOfRange(this.rep, builder, start);
|
|
821
|
+
buildRemoveRange(this.rep, builder, start, end);
|
|
822
|
+
builder.insert(newText, [
|
|
823
|
+
['author', this.thisAuthor],
|
|
824
|
+
], this.rep.apool);
|
|
825
|
+
const cs = builder.toString();
|
|
826
|
+
this.performDocumentApplyChangeset(cs);
|
|
827
|
+
}
|
|
828
|
+
performDocumentReplaceSelection(newText) {
|
|
829
|
+
if (!(this.rep.selStart && this.rep.selEnd))
|
|
830
|
+
return;
|
|
831
|
+
this.performDocumentReplaceRange(this.rep.selStart, this.rep.selEnd, newText);
|
|
832
|
+
}
|
|
833
|
+
performDocumentReplaceCharRange(startChar, endChar, newText) {
|
|
834
|
+
if (startChar === endChar && newText.length === 0)
|
|
835
|
+
return;
|
|
836
|
+
if (endChar === this.rep.alltext.length) {
|
|
837
|
+
if (startChar === endChar) {
|
|
838
|
+
startChar--;
|
|
839
|
+
endChar--;
|
|
840
|
+
newText = `\n${newText.substring(0, newText.length - 1)}`;
|
|
841
|
+
}
|
|
842
|
+
else if (newText.length === 0) {
|
|
843
|
+
startChar--;
|
|
844
|
+
endChar--;
|
|
845
|
+
}
|
|
846
|
+
else {
|
|
847
|
+
endChar--;
|
|
848
|
+
newText = newText.substring(0, newText.length - 1);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
this.performDocumentReplaceRange(this.lineAndColumnFromChar(startChar), this.lineAndColumnFromChar(endChar), newText);
|
|
852
|
+
}
|
|
853
|
+
performDocumentApplyAttributesToCharRange(start, end, attribs) {
|
|
854
|
+
end = Math.min(end, this.rep.alltext.length - 1);
|
|
855
|
+
this.documentAttributeManager.setAttributesOnRange(this.lineAndColumnFromChar(start), this.lineAndColumnFromChar(end), attribs);
|
|
856
|
+
}
|
|
857
|
+
performDocumentApplyChangeset(changes, insertsAfterSelection) {
|
|
858
|
+
const domAndRepSplice = (startLine, deleteCount, newLineStrings) => {
|
|
859
|
+
const keysToDelete = [];
|
|
860
|
+
if (deleteCount > 0) {
|
|
861
|
+
let entryToDelete = this.rep.lines.atIndex(startLine);
|
|
862
|
+
for (let i = 0; i < deleteCount; i++) {
|
|
863
|
+
keysToDelete.push(entryToDelete.key);
|
|
864
|
+
entryToDelete = this.rep.lines.next(entryToDelete);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
const lineEntries = newLineStrings.map((s) => this.createDomLineEntry(s));
|
|
868
|
+
this.doRepLineSplice(startLine, deleteCount, lineEntries);
|
|
869
|
+
let nodeToAddAfter;
|
|
870
|
+
if (startLine > 0) {
|
|
871
|
+
nodeToAddAfter = this.getCleanNodeByKey(this.rep.lines.atIndex(startLine - 1).key);
|
|
872
|
+
}
|
|
873
|
+
else {
|
|
874
|
+
nodeToAddAfter = null;
|
|
875
|
+
}
|
|
876
|
+
this.insertDomLines(nodeToAddAfter, lineEntries.map((entry) => entry.domInfo));
|
|
877
|
+
for (const k of keysToDelete) {
|
|
878
|
+
const n = this.rootNode.getElementById(k);
|
|
879
|
+
if (n && n.parentNode)
|
|
880
|
+
n.parentNode.removeChild(n);
|
|
881
|
+
}
|
|
882
|
+
if ((this.rep.selStart &&
|
|
883
|
+
this.rep.selStart[0] >= startLine &&
|
|
884
|
+
this.rep.selStart[0] <= startLine + deleteCount) ||
|
|
885
|
+
(this.rep.selEnd &&
|
|
886
|
+
this.rep.selEnd[0] >= startLine &&
|
|
887
|
+
this.rep.selEnd[0] <= startLine + deleteCount)) {
|
|
888
|
+
this.currentCallStack.selectionAffected = true;
|
|
889
|
+
}
|
|
890
|
+
};
|
|
891
|
+
this.doRepApplyChangeset(changes, insertsAfterSelection);
|
|
892
|
+
let requiredSelectionSetting = null;
|
|
893
|
+
if (this.rep.selStart && this.rep.selEnd) {
|
|
894
|
+
const selStartChar = this.rep.lines.offsetOfIndex(this.rep.selStart[0]) +
|
|
895
|
+
this.rep.selStart[1];
|
|
896
|
+
const selEndChar = this.rep.lines.offsetOfIndex(this.rep.selEnd[0]) +
|
|
897
|
+
this.rep.selEnd[1];
|
|
898
|
+
const result = characterRangeFollow(changes, selStartChar, selEndChar, insertsAfterSelection ? 1 : 0);
|
|
899
|
+
requiredSelectionSetting = [result[0], result[1], this.rep.selFocusAtStart];
|
|
900
|
+
}
|
|
901
|
+
const linesMutatee = {
|
|
902
|
+
splice: (start, numRemoved, ...args) => {
|
|
903
|
+
domAndRepSplice(start, numRemoved, args.map((s) => s.slice(0, -1)));
|
|
904
|
+
},
|
|
905
|
+
get: (i) => `${this.rep.lines.atIndex(i).text}\n`,
|
|
906
|
+
length: () => this.rep.lines.length(),
|
|
907
|
+
};
|
|
908
|
+
mutateTextLines(changes, linesMutatee);
|
|
909
|
+
if (requiredSelectionSetting) {
|
|
910
|
+
this.performSelectionChange(this.lineAndColumnFromChar(requiredSelectionSetting[0]), this.lineAndColumnFromChar(requiredSelectionSetting[1]), requiredSelectionSetting[2]);
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
doRepApplyChangeset(changes, _insertsAfterSelection) {
|
|
914
|
+
checkRep(changes);
|
|
915
|
+
if (oldLen(changes) !== this.rep.alltext.length) {
|
|
916
|
+
const errMsg = `${oldLen(changes)}/${this.rep.alltext.length}`;
|
|
917
|
+
throw new Error(`doRepApplyChangeset length mismatch: ${errMsg}`);
|
|
918
|
+
}
|
|
919
|
+
const editEvent = this.currentCallStack.editEvent;
|
|
920
|
+
if (editEvent.eventType === 'nonundoable') {
|
|
921
|
+
if (!editEvent.changeset) {
|
|
922
|
+
editEvent.changeset = changes;
|
|
923
|
+
}
|
|
924
|
+
else {
|
|
925
|
+
editEvent.changeset = compose(editEvent.changeset, changes, this.rep.apool);
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
else {
|
|
929
|
+
const inverseChangeset = inverse(changes, {
|
|
930
|
+
get: (i) => `${this.rep.lines.atIndex(i).text}\n`,
|
|
931
|
+
length: () => this.rep.lines.length(),
|
|
932
|
+
}, this.rep.alines, this.rep.apool);
|
|
933
|
+
if (!editEvent.backset) {
|
|
934
|
+
editEvent.backset = inverseChangeset;
|
|
935
|
+
}
|
|
936
|
+
else {
|
|
937
|
+
editEvent.backset = compose(inverseChangeset, editEvent.backset, this.rep.apool);
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
mutateAttributionLines(changes, this.rep.alines, this.rep.apool);
|
|
941
|
+
// Track user changes for collaboration
|
|
942
|
+
if (this.changesetTracker?.isTracking()) {
|
|
943
|
+
this.changesetTracker.composeUserChangeset(changes);
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
// -----------------------------------------------------------------------
|
|
947
|
+
// Private - Line / Char Conversion
|
|
948
|
+
// -----------------------------------------------------------------------
|
|
949
|
+
lineAndColumnFromChar(x) {
|
|
950
|
+
const lineEntry = this.rep.lines.atOffset(x);
|
|
951
|
+
const lineStart = this.rep.lines.offsetOfEntry(lineEntry);
|
|
952
|
+
const lineNum = this.rep.lines.indexOfEntry(lineEntry);
|
|
953
|
+
return [lineNum, x - lineStart];
|
|
954
|
+
}
|
|
955
|
+
// -----------------------------------------------------------------------
|
|
956
|
+
// Private - DOM Rendering
|
|
957
|
+
// -----------------------------------------------------------------------
|
|
958
|
+
doCreateDomLine(nonEmpty) {
|
|
959
|
+
return domline.createDomLine(nonEmpty, this.doesWrap, browser, this.targetDoc);
|
|
960
|
+
}
|
|
961
|
+
createDomLineEntry(lineString) {
|
|
962
|
+
const info = this.doCreateDomLine(lineString.length > 0);
|
|
963
|
+
const newNode = info.node;
|
|
964
|
+
return {
|
|
965
|
+
key: this.uniqueId(newNode),
|
|
966
|
+
text: lineString,
|
|
967
|
+
lineNode: newNode,
|
|
968
|
+
domInfo: info,
|
|
969
|
+
lineMarker: 0,
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
insertDomLines(nodeToAddAfter, infoStructs) {
|
|
973
|
+
let lastEntry;
|
|
974
|
+
let lineStartOffset = 0;
|
|
975
|
+
for (const info of infoStructs) {
|
|
976
|
+
const node = info.node;
|
|
977
|
+
const key = this.uniqueId(node);
|
|
978
|
+
let entry;
|
|
979
|
+
if (lastEntry) {
|
|
980
|
+
const next = this.rep.lines.next(lastEntry);
|
|
981
|
+
if (next && next.key === key) {
|
|
982
|
+
entry = next;
|
|
983
|
+
lineStartOffset += lastEntry.width;
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
if (!entry) {
|
|
987
|
+
entry = this.rep.lines.atKey(key);
|
|
988
|
+
lineStartOffset = this.rep.lines.offsetOfKey(key);
|
|
989
|
+
}
|
|
990
|
+
lastEntry = entry;
|
|
991
|
+
this.getSpansForLine(entry, (tokenText, tokenClass) => {
|
|
992
|
+
info.appendSpan(tokenText, tokenClass);
|
|
993
|
+
}, lineStartOffset);
|
|
994
|
+
info.prepareForAdd();
|
|
995
|
+
entry.lineMarker = info.lineMarker;
|
|
996
|
+
if (!nodeToAddAfter) {
|
|
997
|
+
this.targetBody.insertBefore(node, this.targetBody.firstChild);
|
|
998
|
+
}
|
|
999
|
+
else {
|
|
1000
|
+
this.targetBody.insertBefore(node, nodeToAddAfter.nextSibling);
|
|
1001
|
+
}
|
|
1002
|
+
nodeToAddAfter = node;
|
|
1003
|
+
info.notifyAdded();
|
|
1004
|
+
this.markNodeClean(node);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
recolorLinesInRange(startChar, endChar) {
|
|
1008
|
+
if (endChar <= startChar)
|
|
1009
|
+
return;
|
|
1010
|
+
if (startChar < 0 || startChar >= this.rep.lines.totalWidth())
|
|
1011
|
+
return;
|
|
1012
|
+
let lineEntry = this.rep.lines.atOffset(startChar);
|
|
1013
|
+
let lineStart = this.rep.lines.offsetOfEntry(lineEntry);
|
|
1014
|
+
let lineIndex = this.rep.lines.indexOfEntry(lineEntry);
|
|
1015
|
+
let selectionNeedsResetting = false;
|
|
1016
|
+
const tokenFunc = (tokenText, tokenClass) => {
|
|
1017
|
+
lineEntry.domInfo.appendSpan(tokenText, tokenClass);
|
|
1018
|
+
};
|
|
1019
|
+
while (lineEntry && lineStart < endChar) {
|
|
1020
|
+
const lineEnd = lineStart + lineEntry.width;
|
|
1021
|
+
lineEntry.domInfo.clearSpans();
|
|
1022
|
+
this.getSpansForLine(lineEntry, tokenFunc, lineStart);
|
|
1023
|
+
lineEntry.domInfo.finishUpdate();
|
|
1024
|
+
// Sync lineMarker from domInfo (e.g. heading lines have lineMarker=1)
|
|
1025
|
+
lineEntry.lineMarker = lineEntry.domInfo.lineMarker;
|
|
1026
|
+
this.markNodeClean(lineEntry.lineNode);
|
|
1027
|
+
if ((this.rep.selStart && this.rep.selStart[0] === lineIndex) ||
|
|
1028
|
+
(this.rep.selEnd && this.rep.selEnd[0] === lineIndex)) {
|
|
1029
|
+
selectionNeedsResetting = true;
|
|
1030
|
+
}
|
|
1031
|
+
lineStart = lineEnd;
|
|
1032
|
+
lineEntry = this.rep.lines.next(lineEntry);
|
|
1033
|
+
lineIndex++;
|
|
1034
|
+
}
|
|
1035
|
+
if (selectionNeedsResetting) {
|
|
1036
|
+
this.currentCallStack.selectionAffected = true;
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
getSpansForLine(lineEntry, textAndClassFunc, lineEntryOffsetHint) {
|
|
1040
|
+
let lineEntryOffset = lineEntryOffsetHint;
|
|
1041
|
+
if (typeof lineEntryOffset !== 'number') {
|
|
1042
|
+
lineEntryOffset = this.rep.lines.offsetOfEntry(lineEntry);
|
|
1043
|
+
}
|
|
1044
|
+
const text = lineEntry.text;
|
|
1045
|
+
if (text.length === 0) {
|
|
1046
|
+
const func = linestylefilter.getLineStyleFilter(0, '', textAndClassFunc, this.rep.apool);
|
|
1047
|
+
func('', '');
|
|
1048
|
+
}
|
|
1049
|
+
else {
|
|
1050
|
+
let filteredFunc = linestylefilter.getFilterStack(text, textAndClassFunc, browser);
|
|
1051
|
+
const lineNum = this.rep.lines.indexOfEntry(lineEntry);
|
|
1052
|
+
const aline = this.rep.alines[lineNum];
|
|
1053
|
+
filteredFunc = linestylefilter.getLineStyleFilter(text.length, aline, filteredFunc, this.rep.apool);
|
|
1054
|
+
filteredFunc(text, '');
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
recreateDOM() {
|
|
1058
|
+
this.recolorLinesInRange(0, this.rep.alltext.length);
|
|
1059
|
+
}
|
|
1060
|
+
// -----------------------------------------------------------------------
|
|
1061
|
+
// Private - Rep Splice
|
|
1062
|
+
// -----------------------------------------------------------------------
|
|
1063
|
+
doRepLineSplice(startLine, deleteCount, newLineEntries) {
|
|
1064
|
+
for (const entry of newLineEntries)
|
|
1065
|
+
entry.width = entry.text.length + 1;
|
|
1066
|
+
const startOldChar = this.rep.lines.offsetOfIndex(startLine);
|
|
1067
|
+
const endOldChar = this.rep.lines.offsetOfIndex(startLine + deleteCount);
|
|
1068
|
+
this.rep.lines.splice(startLine, deleteCount, newLineEntries);
|
|
1069
|
+
if (this.currentCallStack) {
|
|
1070
|
+
this.currentCallStack.docTextChanged = true;
|
|
1071
|
+
this.currentCallStack.repChanged = true;
|
|
1072
|
+
}
|
|
1073
|
+
const newText = newLineEntries.map((e) => `${e.text}\n`).join('');
|
|
1074
|
+
this.rep.alltext = this.rep.alltext.substring(0, startOldChar) +
|
|
1075
|
+
newText + this.rep.alltext.substring(endOldChar, this.rep.alltext.length);
|
|
1076
|
+
}
|
|
1077
|
+
doIncorpLineSplice(startLine, deleteCount, newLineEntries, lineAttribs, hints) {
|
|
1078
|
+
const startOldChar = this.rep.lines.offsetOfIndex(startLine);
|
|
1079
|
+
const endOldChar = this.rep.lines.offsetOfIndex(startLine + deleteCount);
|
|
1080
|
+
const oldRegionStart = this.rep.lines.offsetOfIndex(startLine);
|
|
1081
|
+
let selStartHintChar;
|
|
1082
|
+
let selEndHintChar;
|
|
1083
|
+
if (hints && hints.selStart) {
|
|
1084
|
+
selStartHintChar =
|
|
1085
|
+
this.rep.lines.offsetOfIndex(hints.selStart[0]) + hints.selStart[1] - oldRegionStart;
|
|
1086
|
+
}
|
|
1087
|
+
if (hints && hints.selEnd) {
|
|
1088
|
+
selEndHintChar =
|
|
1089
|
+
this.rep.lines.offsetOfIndex(hints.selEnd[0]) + hints.selEnd[1] - oldRegionStart;
|
|
1090
|
+
}
|
|
1091
|
+
const newText = newLineEntries.map((e) => `${e.text}\n`).join('');
|
|
1092
|
+
const oldText = this.rep.alltext.substring(startOldChar, endOldChar);
|
|
1093
|
+
const oldAttribs = this.rep.alines.slice(startLine, startLine + deleteCount).join('');
|
|
1094
|
+
const newAttribs = `${lineAttribs.join('|1+1')}|1+1`;
|
|
1095
|
+
const analysis = this.analyzeChange(oldText, newText, oldAttribs, newAttribs, selStartHintChar, selEndHintChar);
|
|
1096
|
+
const commonStart = analysis[0];
|
|
1097
|
+
let commonEnd = analysis[1];
|
|
1098
|
+
let shortOldText = oldText.substring(commonStart, oldText.length - commonEnd);
|
|
1099
|
+
let shortNewText = newText.substring(commonStart, newText.length - commonEnd);
|
|
1100
|
+
let spliceStart = startOldChar + commonStart;
|
|
1101
|
+
let spliceEnd = endOldChar - commonEnd;
|
|
1102
|
+
let shiftFinalNewlineToBeforeNewText = false;
|
|
1103
|
+
if (shortOldText.charAt(shortOldText.length - 1) === '\n' &&
|
|
1104
|
+
shortNewText.charAt(shortNewText.length - 1) === '\n') {
|
|
1105
|
+
shortOldText = shortOldText.slice(0, -1);
|
|
1106
|
+
shortNewText = shortNewText.slice(0, -1);
|
|
1107
|
+
spliceEnd--;
|
|
1108
|
+
commonEnd++;
|
|
1109
|
+
}
|
|
1110
|
+
if (shortOldText.length === 0 &&
|
|
1111
|
+
spliceStart === this.rep.alltext.length &&
|
|
1112
|
+
shortNewText.length > 0) {
|
|
1113
|
+
spliceStart--;
|
|
1114
|
+
spliceEnd--;
|
|
1115
|
+
shortNewText = `\n${shortNewText.slice(0, -1)}`;
|
|
1116
|
+
shiftFinalNewlineToBeforeNewText = true;
|
|
1117
|
+
}
|
|
1118
|
+
if (spliceEnd === this.rep.alltext.length &&
|
|
1119
|
+
shortOldText.length > 0 &&
|
|
1120
|
+
shortNewText.length === 0) {
|
|
1121
|
+
if (this.rep.alltext.charAt(spliceStart - 1) === '\n') {
|
|
1122
|
+
spliceStart--;
|
|
1123
|
+
spliceEnd--;
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
if (!(shortOldText.length === 0 && shortNewText.length === 0)) {
|
|
1127
|
+
const oldDocText = this.rep.alltext;
|
|
1128
|
+
const docOldLen = oldDocText.length;
|
|
1129
|
+
const spliceStartLine = this.rep.lines.indexOfOffset(spliceStart);
|
|
1130
|
+
const spliceStartLineStart = this.rep.lines.offsetOfIndex(spliceStartLine);
|
|
1131
|
+
const startBuilder = () => {
|
|
1132
|
+
const builder = new Builder(docOldLen);
|
|
1133
|
+
builder.keep(spliceStartLineStart, spliceStartLine);
|
|
1134
|
+
builder.keep(spliceStart - spliceStartLineStart);
|
|
1135
|
+
return builder;
|
|
1136
|
+
};
|
|
1137
|
+
const eachAttribRun = (attribs, func) => {
|
|
1138
|
+
let textIndex = 0;
|
|
1139
|
+
const newTextStart = commonStart;
|
|
1140
|
+
const newTextEnd = newText.length - commonEnd -
|
|
1141
|
+
(shiftFinalNewlineToBeforeNewText ? 1 : 0);
|
|
1142
|
+
for (const op of deserializeOps(attribs)) {
|
|
1143
|
+
const nextIndex = textIndex + op.chars;
|
|
1144
|
+
if (!(nextIndex <= newTextStart || textIndex >= newTextEnd)) {
|
|
1145
|
+
func(Math.max(newTextStart, textIndex), Math.min(newTextEnd, nextIndex), op.attribs);
|
|
1146
|
+
}
|
|
1147
|
+
textIndex = nextIndex;
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
const justApplyStyles = (shortNewText === shortOldText);
|
|
1151
|
+
let theChangeset;
|
|
1152
|
+
if (justApplyStyles) {
|
|
1153
|
+
const incorpedAttribClearer = this.cachedStrFunc((oldAtts) => mapAttribNumbers(oldAtts, (n) => {
|
|
1154
|
+
const k = this.rep.apool.getAttribKey(n);
|
|
1155
|
+
if (this.isStyleAttribute(k)) {
|
|
1156
|
+
return this.rep.apool.putAttrib([k, '']);
|
|
1157
|
+
}
|
|
1158
|
+
return false;
|
|
1159
|
+
}));
|
|
1160
|
+
const builder1 = startBuilder();
|
|
1161
|
+
if (shiftFinalNewlineToBeforeNewText) {
|
|
1162
|
+
builder1.keep(1, 1);
|
|
1163
|
+
}
|
|
1164
|
+
eachAttribRun(oldAttribs, (start, end, attribs) => {
|
|
1165
|
+
builder1.keepText(newText.substring(start, end), incorpedAttribClearer(attribs));
|
|
1166
|
+
});
|
|
1167
|
+
const clearer = builder1.toString();
|
|
1168
|
+
const builder2 = startBuilder();
|
|
1169
|
+
if (shiftFinalNewlineToBeforeNewText) {
|
|
1170
|
+
builder2.keep(1, 1);
|
|
1171
|
+
}
|
|
1172
|
+
eachAttribRun(newAttribs, (start, end, attribs) => {
|
|
1173
|
+
builder2.keepText(newText.substring(start, end), attribs);
|
|
1174
|
+
});
|
|
1175
|
+
const styler = builder2.toString();
|
|
1176
|
+
theChangeset = compose(clearer, styler, this.rep.apool);
|
|
1177
|
+
}
|
|
1178
|
+
else {
|
|
1179
|
+
const builder = startBuilder();
|
|
1180
|
+
const spliceEndLine = this.rep.lines.indexOfOffset(spliceEnd);
|
|
1181
|
+
const spliceEndLineStart = this.rep.lines.offsetOfIndex(spliceEndLine);
|
|
1182
|
+
if (spliceEndLineStart > spliceStart) {
|
|
1183
|
+
builder.remove(spliceEndLineStart - spliceStart, spliceEndLine - spliceStartLine);
|
|
1184
|
+
builder.remove(spliceEnd - spliceEndLineStart);
|
|
1185
|
+
}
|
|
1186
|
+
else {
|
|
1187
|
+
builder.remove(spliceEnd - spliceStart);
|
|
1188
|
+
}
|
|
1189
|
+
let isNewTextMultiauthor = false;
|
|
1190
|
+
const authorizer = this.cachedStrFunc((oldAtts) => {
|
|
1191
|
+
const attribs = AttributeMap.fromString(oldAtts, this.rep.apool);
|
|
1192
|
+
if (!isNewTextMultiauthor || !attribs.has('author')) {
|
|
1193
|
+
attribs.set('author', this.thisAuthor);
|
|
1194
|
+
}
|
|
1195
|
+
return attribs.toString();
|
|
1196
|
+
});
|
|
1197
|
+
let foundDomAuthor = '';
|
|
1198
|
+
eachAttribRun(newAttribs, (_start, _end, attribs) => {
|
|
1199
|
+
const a = AttributeMap.fromString(attribs, this.rep.apool).get('author');
|
|
1200
|
+
if (a && a !== foundDomAuthor) {
|
|
1201
|
+
if (!foundDomAuthor) {
|
|
1202
|
+
foundDomAuthor = a;
|
|
1203
|
+
}
|
|
1204
|
+
else {
|
|
1205
|
+
isNewTextMultiauthor = true;
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
});
|
|
1209
|
+
if (shiftFinalNewlineToBeforeNewText) {
|
|
1210
|
+
builder.insert('\n', authorizer(''));
|
|
1211
|
+
}
|
|
1212
|
+
eachAttribRun(newAttribs, (start, end, attribs) => {
|
|
1213
|
+
builder.insert(newText.substring(start, end), authorizer(attribs));
|
|
1214
|
+
});
|
|
1215
|
+
theChangeset = builder.toString();
|
|
1216
|
+
}
|
|
1217
|
+
this.doRepApplyChangeset(theChangeset);
|
|
1218
|
+
}
|
|
1219
|
+
this.doRepLineSplice(startLine, deleteCount, newLineEntries);
|
|
1220
|
+
}
|
|
1221
|
+
// -----------------------------------------------------------------------
|
|
1222
|
+
// Private - Change Analysis
|
|
1223
|
+
// -----------------------------------------------------------------------
|
|
1224
|
+
analyzeChange(oldText, newText, oldAttribs, newAttribs, _optSelStartHint, optSelEndHint) {
|
|
1225
|
+
const incorpedAttribFilter = (anum) => !this.isDefaultLineAttribute(this.rep.apool.getAttribKey(anum));
|
|
1226
|
+
const attribRuns = (attribs) => {
|
|
1227
|
+
const lengs = [];
|
|
1228
|
+
const atts = [];
|
|
1229
|
+
for (const op of deserializeOps(attribs)) {
|
|
1230
|
+
lengs.push(op.chars);
|
|
1231
|
+
atts.push(op.attribs);
|
|
1232
|
+
}
|
|
1233
|
+
return [lengs, atts];
|
|
1234
|
+
};
|
|
1235
|
+
const attribIterator = (runs, backward) => {
|
|
1236
|
+
const lengs = runs[0];
|
|
1237
|
+
const atts = runs[1];
|
|
1238
|
+
let i = backward ? lengs.length - 1 : 0;
|
|
1239
|
+
let j = 0;
|
|
1240
|
+
const next = () => {
|
|
1241
|
+
while (j >= lengs[i]) {
|
|
1242
|
+
if (backward)
|
|
1243
|
+
i--;
|
|
1244
|
+
else
|
|
1245
|
+
i++;
|
|
1246
|
+
j = 0;
|
|
1247
|
+
}
|
|
1248
|
+
const a = atts[i];
|
|
1249
|
+
j++;
|
|
1250
|
+
return a;
|
|
1251
|
+
};
|
|
1252
|
+
return next;
|
|
1253
|
+
};
|
|
1254
|
+
const oldTextLen = oldText.length;
|
|
1255
|
+
const newTextLen = newText.length;
|
|
1256
|
+
const minLen = Math.min(oldTextLen, newTextLen);
|
|
1257
|
+
const oldARuns = attribRuns(filterAttribNumbers(oldAttribs, incorpedAttribFilter));
|
|
1258
|
+
const newARuns = attribRuns(filterAttribNumbers(newAttribs, incorpedAttribFilter));
|
|
1259
|
+
let commonStart = 0;
|
|
1260
|
+
const oldStartIter = attribIterator(oldARuns, false);
|
|
1261
|
+
const newStartIter = attribIterator(newARuns, false);
|
|
1262
|
+
while (commonStart < minLen) {
|
|
1263
|
+
if (oldText.charAt(commonStart) === newText.charAt(commonStart) &&
|
|
1264
|
+
oldStartIter() === newStartIter()) {
|
|
1265
|
+
commonStart++;
|
|
1266
|
+
}
|
|
1267
|
+
else {
|
|
1268
|
+
break;
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
let commonEnd = 0;
|
|
1272
|
+
const oldEndIter = attribIterator(oldARuns, true);
|
|
1273
|
+
const newEndIter = attribIterator(newARuns, true);
|
|
1274
|
+
while (commonEnd < minLen) {
|
|
1275
|
+
if (commonEnd === 0) {
|
|
1276
|
+
oldEndIter();
|
|
1277
|
+
newEndIter();
|
|
1278
|
+
commonEnd++;
|
|
1279
|
+
}
|
|
1280
|
+
else if (oldText.charAt(oldTextLen - 1 - commonEnd) ===
|
|
1281
|
+
newText.charAt(newTextLen - 1 - commonEnd) &&
|
|
1282
|
+
oldEndIter() === newEndIter()) {
|
|
1283
|
+
commonEnd++;
|
|
1284
|
+
}
|
|
1285
|
+
else {
|
|
1286
|
+
break;
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
let hintedCommonEnd = -1;
|
|
1290
|
+
if (typeof optSelEndHint === 'number') {
|
|
1291
|
+
hintedCommonEnd = newTextLen - optSelEndHint;
|
|
1292
|
+
}
|
|
1293
|
+
if (commonStart + commonEnd > oldTextLen) {
|
|
1294
|
+
const minCommonEnd = oldTextLen - commonStart;
|
|
1295
|
+
const maxCommonEnd = commonEnd;
|
|
1296
|
+
if (hintedCommonEnd >= minCommonEnd && hintedCommonEnd <= maxCommonEnd) {
|
|
1297
|
+
commonEnd = hintedCommonEnd;
|
|
1298
|
+
}
|
|
1299
|
+
else {
|
|
1300
|
+
commonEnd = minCommonEnd;
|
|
1301
|
+
}
|
|
1302
|
+
commonStart = oldTextLen - commonEnd;
|
|
1303
|
+
}
|
|
1304
|
+
if (commonStart + commonEnd > newTextLen) {
|
|
1305
|
+
const minCommonEnd = newTextLen - commonStart;
|
|
1306
|
+
const maxCommonEnd = commonEnd;
|
|
1307
|
+
if (hintedCommonEnd >= minCommonEnd && hintedCommonEnd <= maxCommonEnd) {
|
|
1308
|
+
commonEnd = hintedCommonEnd;
|
|
1309
|
+
}
|
|
1310
|
+
else {
|
|
1311
|
+
commonEnd = minCommonEnd;
|
|
1312
|
+
}
|
|
1313
|
+
commonStart = newTextLen - commonEnd;
|
|
1314
|
+
}
|
|
1315
|
+
return [commonStart, commonEnd];
|
|
1316
|
+
}
|
|
1317
|
+
cachedStrFunc(func) {
|
|
1318
|
+
const cache = {};
|
|
1319
|
+
return (s) => {
|
|
1320
|
+
if (!cache[s]) {
|
|
1321
|
+
cache[s] = func(s);
|
|
1322
|
+
}
|
|
1323
|
+
return cache[s];
|
|
1324
|
+
};
|
|
1325
|
+
}
|
|
1326
|
+
// -----------------------------------------------------------------------
|
|
1327
|
+
// Private - Content Collection / Change Detection
|
|
1328
|
+
// -----------------------------------------------------------------------
|
|
1329
|
+
incorporateUserChanges() {
|
|
1330
|
+
if (this.currentCallStack && this.currentCallStack.domClean)
|
|
1331
|
+
return false;
|
|
1332
|
+
if (this.currentCallStack) {
|
|
1333
|
+
this.currentCallStack.isUserChange = true;
|
|
1334
|
+
}
|
|
1335
|
+
if (!this.targetBody.firstChild) {
|
|
1336
|
+
this.targetBody.innerHTML = '<div><!-- --></div>';
|
|
1337
|
+
}
|
|
1338
|
+
try {
|
|
1339
|
+
this.observeChangesAroundSelection();
|
|
1340
|
+
this.observeSuspiciousNodes();
|
|
1341
|
+
let dirtyRanges = this.getDirtyRanges();
|
|
1342
|
+
let dirtyRangesCheckOut = true;
|
|
1343
|
+
let j = 0;
|
|
1344
|
+
let a, b;
|
|
1345
|
+
while (j < dirtyRanges.length) {
|
|
1346
|
+
a = dirtyRanges[j][0];
|
|
1347
|
+
b = dirtyRanges[j][1];
|
|
1348
|
+
if (!((a === 0 || this.getCleanNodeByKey(this.rep.lines.atIndex(a - 1).key)) &&
|
|
1349
|
+
(b === this.rep.lines.length() ||
|
|
1350
|
+
this.getCleanNodeByKey(this.rep.lines.atIndex(b).key)))) {
|
|
1351
|
+
dirtyRangesCheckOut = false;
|
|
1352
|
+
break;
|
|
1353
|
+
}
|
|
1354
|
+
j++;
|
|
1355
|
+
}
|
|
1356
|
+
if (!dirtyRangesCheckOut) {
|
|
1357
|
+
for (const bodyNode of this.targetBody.childNodes) {
|
|
1358
|
+
if (bodyNode.tagName &&
|
|
1359
|
+
(!bodyNode.id ||
|
|
1360
|
+
!this.rep.lines.containsKey(bodyNode.id))) {
|
|
1361
|
+
this.observeChangesAroundNode(bodyNode);
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
dirtyRanges = this.getDirtyRanges();
|
|
1365
|
+
}
|
|
1366
|
+
this.clearObservedChanges();
|
|
1367
|
+
const selection = this.getSelection();
|
|
1368
|
+
let selStart;
|
|
1369
|
+
let selEnd;
|
|
1370
|
+
let i = 0;
|
|
1371
|
+
const splicesToDo = [];
|
|
1372
|
+
let netNumLinesChangeSoFar = 0;
|
|
1373
|
+
const toDeleteAtEnd = [];
|
|
1374
|
+
const domInsertsNeeded = [];
|
|
1375
|
+
while (i < dirtyRanges.length) {
|
|
1376
|
+
const range = dirtyRanges[i];
|
|
1377
|
+
a = range[0];
|
|
1378
|
+
b = range[1];
|
|
1379
|
+
let firstDirtyNode = ((a === 0 && this.targetBody.firstChild) ||
|
|
1380
|
+
this.getCleanNodeByKey(this.rep.lines.atIndex(a - 1).key)?.nextSibling);
|
|
1381
|
+
firstDirtyNode = (firstDirtyNode && this.isNodeDirty(firstDirtyNode) && firstDirtyNode);
|
|
1382
|
+
let lastDirtyNode = ((b === this.rep.lines.length() && this.targetBody.lastChild) ||
|
|
1383
|
+
this.getCleanNodeByKey(this.rep.lines.atIndex(b).key)?.previousSibling);
|
|
1384
|
+
lastDirtyNode = (lastDirtyNode && this.isNodeDirty(lastDirtyNode) && lastDirtyNode);
|
|
1385
|
+
if (firstDirtyNode && lastDirtyNode) {
|
|
1386
|
+
const cc = makeContentCollector(this.isStyled, browser, this.rep.apool, this.className2Author.bind(this));
|
|
1387
|
+
cc.notifySelection(selection);
|
|
1388
|
+
const dirtyNodes = [];
|
|
1389
|
+
for (let n = firstDirtyNode; n && !(n.previousSibling && n.previousSibling === lastDirtyNode); n = n.nextSibling) {
|
|
1390
|
+
cc.collectContent(n);
|
|
1391
|
+
dirtyNodes.push(n);
|
|
1392
|
+
}
|
|
1393
|
+
cc.notifyNextNode(lastDirtyNode.nextSibling);
|
|
1394
|
+
let lines = cc.getLines();
|
|
1395
|
+
if ((lines.length <= 1 || lines[lines.length - 1] !== '') &&
|
|
1396
|
+
lastDirtyNode.nextSibling) {
|
|
1397
|
+
b++;
|
|
1398
|
+
const cleanLine = lastDirtyNode.nextSibling;
|
|
1399
|
+
cc.collectContent(cleanLine);
|
|
1400
|
+
toDeleteAtEnd.push(cleanLine);
|
|
1401
|
+
cc.notifyNextNode(cleanLine.nextSibling);
|
|
1402
|
+
}
|
|
1403
|
+
const ccData = cc.finish();
|
|
1404
|
+
const ss = ccData.selStart;
|
|
1405
|
+
const se = ccData.selEnd;
|
|
1406
|
+
lines = ccData.lines;
|
|
1407
|
+
const lineAttribs = ccData.lineAttribs;
|
|
1408
|
+
const linesWrapped = ccData.linesWrapped;
|
|
1409
|
+
if (linesWrapped > 0) {
|
|
1410
|
+
// Scroll to left needed to fix Chrome wrapping issue
|
|
1411
|
+
this.targetBody.scrollLeft = 0;
|
|
1412
|
+
}
|
|
1413
|
+
if (ss[0] >= 0)
|
|
1414
|
+
selStart = [ss[0] + a + netNumLinesChangeSoFar, ss[1]];
|
|
1415
|
+
if (se[0] >= 0)
|
|
1416
|
+
selEnd = [se[0] + a + netNumLinesChangeSoFar, se[1]];
|
|
1417
|
+
const entries = [];
|
|
1418
|
+
const nodeToAddAfter = lastDirtyNode;
|
|
1419
|
+
const lineNodeInfos = [];
|
|
1420
|
+
for (const lineString of lines) {
|
|
1421
|
+
const newEntry = this.createDomLineEntry(lineString);
|
|
1422
|
+
entries.push(newEntry);
|
|
1423
|
+
lineNodeInfos.push(newEntry.domInfo);
|
|
1424
|
+
}
|
|
1425
|
+
domInsertsNeeded.push([nodeToAddAfter, lineNodeInfos]);
|
|
1426
|
+
for (const n of dirtyNodes)
|
|
1427
|
+
toDeleteAtEnd.push(n);
|
|
1428
|
+
const spliceHints = {};
|
|
1429
|
+
if (selStart)
|
|
1430
|
+
spliceHints.selStart = selStart;
|
|
1431
|
+
if (selEnd)
|
|
1432
|
+
spliceHints.selEnd = selEnd;
|
|
1433
|
+
splicesToDo.push([
|
|
1434
|
+
a + netNumLinesChangeSoFar, b - a, entries, lineAttribs, spliceHints
|
|
1435
|
+
]);
|
|
1436
|
+
netNumLinesChangeSoFar += (lines.length - (b - a));
|
|
1437
|
+
}
|
|
1438
|
+
else if (b > a) {
|
|
1439
|
+
splicesToDo.push([a + netNumLinesChangeSoFar, b - a, [], []]);
|
|
1440
|
+
}
|
|
1441
|
+
i++;
|
|
1442
|
+
}
|
|
1443
|
+
const domChanges = splicesToDo.length > 0;
|
|
1444
|
+
for (const splice of splicesToDo)
|
|
1445
|
+
this.doIncorpLineSplice(splice[0], splice[1], splice[2], splice[3], splice[4]);
|
|
1446
|
+
for (const ins of domInsertsNeeded)
|
|
1447
|
+
this.insertDomLines(ins[0], ins[1]);
|
|
1448
|
+
for (const n of toDeleteAtEnd)
|
|
1449
|
+
n.remove();
|
|
1450
|
+
// If selection nodes weren't encountered during content collection,
|
|
1451
|
+
// figure out where those nodes are now.
|
|
1452
|
+
if (selection && !selStart) {
|
|
1453
|
+
selStart = this.getLineAndCharForPoint(selection.startPoint);
|
|
1454
|
+
}
|
|
1455
|
+
if (selection && !selEnd) {
|
|
1456
|
+
selEnd = this.getLineAndCharForPoint(selection.endPoint);
|
|
1457
|
+
}
|
|
1458
|
+
// Cap selection to valid line range
|
|
1459
|
+
const numLines = this.rep.lines.length();
|
|
1460
|
+
if (numLines > 0) {
|
|
1461
|
+
if (selStart && selStart[0] >= numLines) {
|
|
1462
|
+
selStart[0] = numLines - 1;
|
|
1463
|
+
selStart[1] = this.rep.lines.atIndex(selStart[0]).text.length;
|
|
1464
|
+
}
|
|
1465
|
+
if (selEnd && selEnd[0] >= numLines) {
|
|
1466
|
+
selEnd[0] = numLines - 1;
|
|
1467
|
+
selEnd[1] = this.rep.lines.atIndex(selEnd[0]).text.length;
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
else {
|
|
1471
|
+
selStart = null;
|
|
1472
|
+
selEnd = null;
|
|
1473
|
+
}
|
|
1474
|
+
if (selection) {
|
|
1475
|
+
this.repSelectionChange(selStart, selEnd, selection && selection.focusAtStart);
|
|
1476
|
+
}
|
|
1477
|
+
if (selection && (domChanges || this.isCaret())) {
|
|
1478
|
+
if (this.currentCallStack) {
|
|
1479
|
+
this.currentCallStack.selectionAffected = true;
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
if (this.currentCallStack) {
|
|
1483
|
+
this.currentCallStack.domClean = true;
|
|
1484
|
+
}
|
|
1485
|
+
return domChanges;
|
|
1486
|
+
}
|
|
1487
|
+
catch (e) {
|
|
1488
|
+
// Guard against DOM/rep desync crashes (e.g. node IDs not in SkipList).
|
|
1489
|
+
// Mark clean so the idle timer doesn't spin indefinitely.
|
|
1490
|
+
if (this.currentCallStack) {
|
|
1491
|
+
this.currentCallStack.domClean = true;
|
|
1492
|
+
}
|
|
1493
|
+
return false;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
getDirtyRanges() {
|
|
1497
|
+
const cleanNodeForIndexCache = {};
|
|
1498
|
+
const N = this.rep.lines.length();
|
|
1499
|
+
const cleanNodeForIndex = (i) => {
|
|
1500
|
+
if (cleanNodeForIndexCache[i] === undefined) {
|
|
1501
|
+
let result;
|
|
1502
|
+
if (i < 0 || i >= N) {
|
|
1503
|
+
result = true;
|
|
1504
|
+
}
|
|
1505
|
+
else {
|
|
1506
|
+
const key = this.rep.lines.atIndex(i).key;
|
|
1507
|
+
result = this.getCleanNodeByKey(key) || false;
|
|
1508
|
+
}
|
|
1509
|
+
cleanNodeForIndexCache[i] = result;
|
|
1510
|
+
}
|
|
1511
|
+
return cleanNodeForIndexCache[i];
|
|
1512
|
+
};
|
|
1513
|
+
const isConsecutiveCache = {};
|
|
1514
|
+
const isConsecutive = (i) => {
|
|
1515
|
+
if (isConsecutiveCache[i] === undefined) {
|
|
1516
|
+
isConsecutiveCache[i] = (() => {
|
|
1517
|
+
const a = cleanNodeForIndex(i - 1);
|
|
1518
|
+
const b = cleanNodeForIndex(i);
|
|
1519
|
+
if (!a || !b)
|
|
1520
|
+
return false;
|
|
1521
|
+
if (a === true && b === true)
|
|
1522
|
+
return !this.targetBody.firstChild;
|
|
1523
|
+
if (a === true && b.previousSibling)
|
|
1524
|
+
return false;
|
|
1525
|
+
if (b === true && a.nextSibling)
|
|
1526
|
+
return false;
|
|
1527
|
+
if (a === true || b === true)
|
|
1528
|
+
return true;
|
|
1529
|
+
return a.nextSibling === b;
|
|
1530
|
+
})();
|
|
1531
|
+
}
|
|
1532
|
+
return isConsecutiveCache[i];
|
|
1533
|
+
};
|
|
1534
|
+
const isClean = (i) => !!cleanNodeForIndex(i);
|
|
1535
|
+
const cleanRanges = [[-1, N + 1]];
|
|
1536
|
+
const rangeForLine = (i) => {
|
|
1537
|
+
for (const [idx, r] of cleanRanges.entries()) {
|
|
1538
|
+
if (i < r[0])
|
|
1539
|
+
return -1;
|
|
1540
|
+
if (i < r[1])
|
|
1541
|
+
return idx;
|
|
1542
|
+
}
|
|
1543
|
+
return -1;
|
|
1544
|
+
};
|
|
1545
|
+
const removeLineFromRange = (rng, line) => {
|
|
1546
|
+
const a = cleanRanges[rng][0];
|
|
1547
|
+
const b = cleanRanges[rng][1];
|
|
1548
|
+
if (a + 1 === b)
|
|
1549
|
+
cleanRanges.splice(rng, 1);
|
|
1550
|
+
else if (line === a)
|
|
1551
|
+
cleanRanges[rng][0]++;
|
|
1552
|
+
else if (line === b - 1)
|
|
1553
|
+
cleanRanges[rng][1]--;
|
|
1554
|
+
else
|
|
1555
|
+
cleanRanges.splice(rng, 1, [a, line], [line + 1, b]);
|
|
1556
|
+
};
|
|
1557
|
+
const splitRange = (rng, pt) => {
|
|
1558
|
+
const a = cleanRanges[rng][0];
|
|
1559
|
+
const b = cleanRanges[rng][1];
|
|
1560
|
+
cleanRanges.splice(rng, 1, [a, pt], [pt, b]);
|
|
1561
|
+
};
|
|
1562
|
+
const correctedLines = {};
|
|
1563
|
+
const correctlyAssignLine = (line) => {
|
|
1564
|
+
if (correctedLines[line])
|
|
1565
|
+
return true;
|
|
1566
|
+
correctedLines[line] = true;
|
|
1567
|
+
const rng = rangeForLine(line);
|
|
1568
|
+
const lineClean = isClean(line);
|
|
1569
|
+
if (rng < 0) {
|
|
1570
|
+
return true;
|
|
1571
|
+
}
|
|
1572
|
+
if (!lineClean) {
|
|
1573
|
+
removeLineFromRange(rng, line);
|
|
1574
|
+
return false;
|
|
1575
|
+
}
|
|
1576
|
+
else {
|
|
1577
|
+
const a = cleanRanges[rng][0];
|
|
1578
|
+
const b = cleanRanges[rng][1];
|
|
1579
|
+
let didSomething = false;
|
|
1580
|
+
if (a < line && isClean(line - 1) && !isConsecutive(line)) {
|
|
1581
|
+
splitRange(rng, line);
|
|
1582
|
+
didSomething = true;
|
|
1583
|
+
}
|
|
1584
|
+
if (b > line + 1 && isClean(line + 1) && !isConsecutive(line + 1)) {
|
|
1585
|
+
splitRange(rng, line + 1);
|
|
1586
|
+
didSomething = true;
|
|
1587
|
+
}
|
|
1588
|
+
return !didSomething;
|
|
1589
|
+
}
|
|
1590
|
+
};
|
|
1591
|
+
const detectChangesAroundLine = (line, reqInARow) => {
|
|
1592
|
+
let correctInARow = 0;
|
|
1593
|
+
let currentIndex = line;
|
|
1594
|
+
while (correctInARow < reqInARow && currentIndex >= 0) {
|
|
1595
|
+
if (correctlyAssignLine(currentIndex)) {
|
|
1596
|
+
correctInARow++;
|
|
1597
|
+
}
|
|
1598
|
+
else {
|
|
1599
|
+
correctInARow = 0;
|
|
1600
|
+
}
|
|
1601
|
+
currentIndex--;
|
|
1602
|
+
}
|
|
1603
|
+
correctInARow = 0;
|
|
1604
|
+
currentIndex = line;
|
|
1605
|
+
while (correctInARow < reqInARow && currentIndex < N) {
|
|
1606
|
+
if (correctlyAssignLine(currentIndex)) {
|
|
1607
|
+
correctInARow++;
|
|
1608
|
+
}
|
|
1609
|
+
else {
|
|
1610
|
+
correctInARow = 0;
|
|
1611
|
+
}
|
|
1612
|
+
currentIndex++;
|
|
1613
|
+
}
|
|
1614
|
+
};
|
|
1615
|
+
if (N === 0) {
|
|
1616
|
+
if (!isConsecutive(0)) {
|
|
1617
|
+
splitRange(0, 0);
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
else {
|
|
1621
|
+
detectChangesAroundLine(0, 1);
|
|
1622
|
+
detectChangesAroundLine(N - 1, 1);
|
|
1623
|
+
for (const k of Object.keys(this.observedChanges.cleanNodesNearChanges)) {
|
|
1624
|
+
const key = k.substring(1);
|
|
1625
|
+
if (this.rep.lines.containsKey(key)) {
|
|
1626
|
+
const line = this.rep.lines.indexOfKey(key);
|
|
1627
|
+
detectChangesAroundLine(line, 2);
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
const dirtyRanges = [];
|
|
1632
|
+
for (let r = 0; r < cleanRanges.length - 1; r++) {
|
|
1633
|
+
dirtyRanges.push([cleanRanges[r][1], cleanRanges[r + 1][0]]);
|
|
1634
|
+
}
|
|
1635
|
+
return dirtyRanges;
|
|
1636
|
+
}
|
|
1637
|
+
// -----------------------------------------------------------------------
|
|
1638
|
+
// Private - Node Tracking
|
|
1639
|
+
// -----------------------------------------------------------------------
|
|
1640
|
+
uniqueId(n) {
|
|
1641
|
+
const nid = n.id;
|
|
1642
|
+
if (nid)
|
|
1643
|
+
return nid;
|
|
1644
|
+
return (n.id = `magicdomid${this._nextId++}`);
|
|
1645
|
+
}
|
|
1646
|
+
topLevel(n) {
|
|
1647
|
+
if (!n || n === this.targetBody)
|
|
1648
|
+
return null;
|
|
1649
|
+
while (n.parentNode !== this.targetBody) {
|
|
1650
|
+
n = n.parentNode;
|
|
1651
|
+
if (!n)
|
|
1652
|
+
return null;
|
|
1653
|
+
}
|
|
1654
|
+
return n;
|
|
1655
|
+
}
|
|
1656
|
+
markNodeClean(n) {
|
|
1657
|
+
setAssoc(n, 'dirtiness', { nodeId: this.uniqueId(n), knownHTML: n.innerHTML });
|
|
1658
|
+
}
|
|
1659
|
+
isNodeDirty(n) {
|
|
1660
|
+
if (n.parentNode !== this.targetBody)
|
|
1661
|
+
return true;
|
|
1662
|
+
const data = getAssoc(n, 'dirtiness');
|
|
1663
|
+
if (!data)
|
|
1664
|
+
return true;
|
|
1665
|
+
if (n.id !== data.nodeId)
|
|
1666
|
+
return true;
|
|
1667
|
+
if (n.innerHTML !== data.knownHTML)
|
|
1668
|
+
return true;
|
|
1669
|
+
return false;
|
|
1670
|
+
}
|
|
1671
|
+
getCleanNodeByKey(key) {
|
|
1672
|
+
let n = this.rootNode.getElementById(key);
|
|
1673
|
+
while (n && this.isNodeDirty(n)) {
|
|
1674
|
+
n.id = '';
|
|
1675
|
+
n = this.rootNode.getElementById(key);
|
|
1676
|
+
}
|
|
1677
|
+
return n;
|
|
1678
|
+
}
|
|
1679
|
+
clearObservedChanges() {
|
|
1680
|
+
this.observedChanges = {
|
|
1681
|
+
cleanNodesNearChanges: {},
|
|
1682
|
+
};
|
|
1683
|
+
}
|
|
1684
|
+
observeChangesAroundNode(node) {
|
|
1685
|
+
let cleanNode = null;
|
|
1686
|
+
let hasAdjacentDirtyness = false;
|
|
1687
|
+
if (!this.isNodeDirty(node)) {
|
|
1688
|
+
cleanNode = node;
|
|
1689
|
+
const prevSib = cleanNode.previousSibling;
|
|
1690
|
+
const nextSib = cleanNode.nextSibling;
|
|
1691
|
+
hasAdjacentDirtyness = !!((prevSib && this.isNodeDirty(prevSib)) ||
|
|
1692
|
+
(nextSib && this.isNodeDirty(nextSib)));
|
|
1693
|
+
}
|
|
1694
|
+
else {
|
|
1695
|
+
let upNode = node.previousSibling;
|
|
1696
|
+
while (upNode && this.isNodeDirty(upNode)) {
|
|
1697
|
+
upNode = upNode.previousSibling;
|
|
1698
|
+
}
|
|
1699
|
+
if (upNode) {
|
|
1700
|
+
cleanNode = upNode;
|
|
1701
|
+
}
|
|
1702
|
+
else {
|
|
1703
|
+
let downNode = node.nextSibling;
|
|
1704
|
+
while (downNode && this.isNodeDirty(downNode)) {
|
|
1705
|
+
downNode = downNode.nextSibling;
|
|
1706
|
+
}
|
|
1707
|
+
if (downNode) {
|
|
1708
|
+
cleanNode = downNode;
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
if (!cleanNode)
|
|
1712
|
+
return;
|
|
1713
|
+
hasAdjacentDirtyness = true;
|
|
1714
|
+
}
|
|
1715
|
+
if (hasAdjacentDirtyness) {
|
|
1716
|
+
this.observedChanges.cleanNodesNearChanges[`$${this.uniqueId(cleanNode)}`] = true;
|
|
1717
|
+
}
|
|
1718
|
+
else {
|
|
1719
|
+
const lineKey = this.uniqueId(cleanNode);
|
|
1720
|
+
if (!this.rep.lines.containsKey(lineKey))
|
|
1721
|
+
return;
|
|
1722
|
+
const prevSib = cleanNode.previousSibling;
|
|
1723
|
+
const nextSib = cleanNode.nextSibling;
|
|
1724
|
+
const actualPrevKey = (prevSib && this.uniqueId(prevSib)) || null;
|
|
1725
|
+
const actualNextKey = (nextSib && this.uniqueId(nextSib)) || null;
|
|
1726
|
+
const repPrevEntry = this.rep.lines.prev(this.rep.lines.atKey(lineKey));
|
|
1727
|
+
const repNextEntry = this.rep.lines.next(this.rep.lines.atKey(lineKey));
|
|
1728
|
+
const repPrevKey = (repPrevEntry && repPrevEntry.key) || null;
|
|
1729
|
+
const repNextKey = (repNextEntry && repNextEntry.key) || null;
|
|
1730
|
+
if (actualPrevKey !== repPrevKey || actualNextKey !== repNextKey) {
|
|
1731
|
+
this.observedChanges.cleanNodesNearChanges[`$${this.uniqueId(cleanNode)}`] = true;
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
observeChangesAroundSelection() {
|
|
1736
|
+
if (this.currentCallStack && this.currentCallStack.observedSelection)
|
|
1737
|
+
return;
|
|
1738
|
+
if (this.currentCallStack)
|
|
1739
|
+
this.currentCallStack.observedSelection = true;
|
|
1740
|
+
const selection = this.getSelection();
|
|
1741
|
+
if (selection) {
|
|
1742
|
+
const node1 = this.topLevel(selection.startPoint.node);
|
|
1743
|
+
const node2 = this.topLevel(selection.endPoint.node);
|
|
1744
|
+
if (node1)
|
|
1745
|
+
this.observeChangesAroundNode(node1);
|
|
1746
|
+
if (node2 && node1 !== node2) {
|
|
1747
|
+
this.observeChangesAroundNode(node2);
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
observeSuspiciousNodes() {
|
|
1752
|
+
if (this.targetBody.getElementsByTagName) {
|
|
1753
|
+
const elts = this.targetBody.getElementsByTagName('style');
|
|
1754
|
+
for (const elt of elts) {
|
|
1755
|
+
const n = this.topLevel(elt);
|
|
1756
|
+
if (n && n.parentNode === this.targetBody) {
|
|
1757
|
+
this.observeChangesAroundNode(n);
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
// -----------------------------------------------------------------------
|
|
1763
|
+
// Private - Selection
|
|
1764
|
+
// -----------------------------------------------------------------------
|
|
1765
|
+
nodeMaxIndex(nd) {
|
|
1766
|
+
if (isNodeText(nd))
|
|
1767
|
+
return nd.nodeValue.length;
|
|
1768
|
+
return 1;
|
|
1769
|
+
}
|
|
1770
|
+
nodeText(n) {
|
|
1771
|
+
return n.textContent || n.nodeValue || '';
|
|
1772
|
+
}
|
|
1773
|
+
childIndex(n) {
|
|
1774
|
+
let idx = 0;
|
|
1775
|
+
while (n.previousSibling) {
|
|
1776
|
+
idx++;
|
|
1777
|
+
n = n.previousSibling;
|
|
1778
|
+
}
|
|
1779
|
+
return idx;
|
|
1780
|
+
}
|
|
1781
|
+
getDocSelection() {
|
|
1782
|
+
// In Shadow DOM, try shadowRoot.getSelection (Chrome-specific) first,
|
|
1783
|
+
// then fall back to document.getSelection which works for shadow DOM in Chromium.
|
|
1784
|
+
if (this.rootNode instanceof ShadowRoot) {
|
|
1785
|
+
const sr = this.rootNode;
|
|
1786
|
+
if (typeof sr.getSelection === 'function') {
|
|
1787
|
+
return sr.getSelection();
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
return this.targetDoc.getSelection();
|
|
1791
|
+
}
|
|
1792
|
+
// Obtain the live selection range. Inside a shadow root, getRangeAt(0)
|
|
1793
|
+
// retargets the range to the shadow host (losing the real caret, which then
|
|
1794
|
+
// reads as offset 0 — every keystroke would insert at the document start).
|
|
1795
|
+
// getComposedRanges({shadowRoots}) returns a range that descends into the
|
|
1796
|
+
// shadow tree, so we use it whenever the editor lives in a shadow root.
|
|
1797
|
+
getSelectionRange(browserSelection) {
|
|
1798
|
+
if (this.rootNode instanceof ShadowRoot &&
|
|
1799
|
+
typeof browserSelection.getComposedRanges === 'function') {
|
|
1800
|
+
const ranges = browserSelection.getComposedRanges({ shadowRoots: [this.rootNode] });
|
|
1801
|
+
if (ranges && ranges.length > 0) {
|
|
1802
|
+
const r = ranges[0];
|
|
1803
|
+
if (r.startContainer)
|
|
1804
|
+
return r;
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
if (browserSelection.rangeCount === 0)
|
|
1808
|
+
return null;
|
|
1809
|
+
return browserSelection.getRangeAt(0);
|
|
1810
|
+
}
|
|
1811
|
+
getSelection() {
|
|
1812
|
+
const browserSelection = this.getDocSelection();
|
|
1813
|
+
if (!browserSelection)
|
|
1814
|
+
return null;
|
|
1815
|
+
// In shadow DOM the legacy Selection reports type 'None' / rangeCount 0
|
|
1816
|
+
// even when the caret is in the shadow tree, but getComposedRanges still
|
|
1817
|
+
// resolves it — so only apply these guards outside a shadow root.
|
|
1818
|
+
if (!(this.rootNode instanceof ShadowRoot) &&
|
|
1819
|
+
(browserSelection.type === 'None' || browserSelection.rangeCount === 0)) {
|
|
1820
|
+
return null;
|
|
1821
|
+
}
|
|
1822
|
+
const range = this.getSelectionRange(browserSelection);
|
|
1823
|
+
if (!range)
|
|
1824
|
+
return null;
|
|
1825
|
+
const isInBody = (n) => {
|
|
1826
|
+
while (n) {
|
|
1827
|
+
if (n === this.targetBody)
|
|
1828
|
+
return true;
|
|
1829
|
+
n = n.parentNode;
|
|
1830
|
+
}
|
|
1831
|
+
return false;
|
|
1832
|
+
};
|
|
1833
|
+
const pointFromRangeBound = (container, offset) => {
|
|
1834
|
+
if (!isInBody(container)) {
|
|
1835
|
+
return {
|
|
1836
|
+
node: this.targetBody,
|
|
1837
|
+
index: 0,
|
|
1838
|
+
maxIndex: 1,
|
|
1839
|
+
};
|
|
1840
|
+
}
|
|
1841
|
+
const n = container;
|
|
1842
|
+
const childCount = n.childNodes.length;
|
|
1843
|
+
if (isNodeText(n)) {
|
|
1844
|
+
return {
|
|
1845
|
+
node: n,
|
|
1846
|
+
index: offset,
|
|
1847
|
+
maxIndex: n.nodeValue.length,
|
|
1848
|
+
};
|
|
1849
|
+
}
|
|
1850
|
+
else if (childCount === 0) {
|
|
1851
|
+
return {
|
|
1852
|
+
node: n,
|
|
1853
|
+
index: 0,
|
|
1854
|
+
maxIndex: 1,
|
|
1855
|
+
};
|
|
1856
|
+
}
|
|
1857
|
+
else if (offset === childCount) {
|
|
1858
|
+
const nd = n.childNodes.item(childCount - 1);
|
|
1859
|
+
const max = this.nodeMaxIndex(nd);
|
|
1860
|
+
return {
|
|
1861
|
+
node: nd,
|
|
1862
|
+
index: max,
|
|
1863
|
+
maxIndex: max,
|
|
1864
|
+
};
|
|
1865
|
+
}
|
|
1866
|
+
else {
|
|
1867
|
+
const nd = n.childNodes.item(offset);
|
|
1868
|
+
const max = this.nodeMaxIndex(nd);
|
|
1869
|
+
return {
|
|
1870
|
+
node: nd,
|
|
1871
|
+
index: 0,
|
|
1872
|
+
maxIndex: max,
|
|
1873
|
+
};
|
|
1874
|
+
}
|
|
1875
|
+
};
|
|
1876
|
+
const selection = {
|
|
1877
|
+
startPoint: pointFromRangeBound(range.startContainer, range.startOffset),
|
|
1878
|
+
endPoint: pointFromRangeBound(range.endContainer, range.endOffset),
|
|
1879
|
+
focusAtStart: (range.startContainer !== range.endContainer ||
|
|
1880
|
+
range.startOffset !== range.endOffset) &&
|
|
1881
|
+
browserSelection.anchorNode &&
|
|
1882
|
+
browserSelection.anchorNode === range.endContainer &&
|
|
1883
|
+
browserSelection.anchorOffset === range.endOffset,
|
|
1884
|
+
};
|
|
1885
|
+
if (selection.startPoint.node.ownerDocument !== this.targetDoc) {
|
|
1886
|
+
return null;
|
|
1887
|
+
}
|
|
1888
|
+
return selection;
|
|
1889
|
+
}
|
|
1890
|
+
setSelection(selection) {
|
|
1891
|
+
const copyPoint = (pt) => ({
|
|
1892
|
+
node: pt.node,
|
|
1893
|
+
index: pt.index,
|
|
1894
|
+
maxIndex: pt.maxIndex,
|
|
1895
|
+
});
|
|
1896
|
+
let isCollapsed;
|
|
1897
|
+
const pointToRangeBound = (pt) => {
|
|
1898
|
+
const p = copyPoint(pt);
|
|
1899
|
+
if (isCollapsed) {
|
|
1900
|
+
const diveDeep = () => {
|
|
1901
|
+
while (p.node.childNodes.length > 0) {
|
|
1902
|
+
if (p.index === 0) {
|
|
1903
|
+
p.node = p.node.firstChild;
|
|
1904
|
+
p.maxIndex = this.nodeMaxIndex(p.node);
|
|
1905
|
+
}
|
|
1906
|
+
else if (p.index === p.maxIndex) {
|
|
1907
|
+
p.node = p.node.lastChild;
|
|
1908
|
+
p.maxIndex = this.nodeMaxIndex(p.node);
|
|
1909
|
+
p.index = p.maxIndex;
|
|
1910
|
+
}
|
|
1911
|
+
else {
|
|
1912
|
+
break;
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1915
|
+
};
|
|
1916
|
+
if (isNodeText(p.node) && p.index === p.maxIndex) {
|
|
1917
|
+
let n = p.node;
|
|
1918
|
+
while (!n.nextSibling && n !== this.targetBody && n.parentNode !== this.targetBody) {
|
|
1919
|
+
n = n.parentNode;
|
|
1920
|
+
}
|
|
1921
|
+
if (n.nextSibling &&
|
|
1922
|
+
!(typeof n.nextSibling.tagName === 'string' &&
|
|
1923
|
+
n.nextSibling.tagName.toLowerCase() === 'br') &&
|
|
1924
|
+
n !== p.node && n !== this.targetBody && n.parentNode !== this.targetBody) {
|
|
1925
|
+
p.node = n.nextSibling;
|
|
1926
|
+
p.maxIndex = this.nodeMaxIndex(p.node);
|
|
1927
|
+
p.index = 0;
|
|
1928
|
+
diveDeep();
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
if (!isNodeText(p.node)) {
|
|
1932
|
+
diveDeep();
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
if (isNodeText(p.node)) {
|
|
1936
|
+
return {
|
|
1937
|
+
container: p.node,
|
|
1938
|
+
offset: p.index,
|
|
1939
|
+
};
|
|
1940
|
+
}
|
|
1941
|
+
else {
|
|
1942
|
+
return {
|
|
1943
|
+
container: p.node.parentNode,
|
|
1944
|
+
offset: this.childIndex(p.node) + p.index,
|
|
1945
|
+
};
|
|
1946
|
+
}
|
|
1947
|
+
};
|
|
1948
|
+
const browserSelection = this.getDocSelection();
|
|
1949
|
+
if (browserSelection) {
|
|
1950
|
+
browserSelection.removeAllRanges();
|
|
1951
|
+
if (selection) {
|
|
1952
|
+
isCollapsed = (selection.startPoint.node === selection.endPoint.node &&
|
|
1953
|
+
selection.startPoint.index === selection.endPoint.index);
|
|
1954
|
+
const start = pointToRangeBound(selection.startPoint);
|
|
1955
|
+
const end = pointToRangeBound(selection.endPoint);
|
|
1956
|
+
if (!isCollapsed && selection.focusAtStart &&
|
|
1957
|
+
browserSelection.collapse && browserSelection.extend) {
|
|
1958
|
+
browserSelection.collapse(end.container, end.offset);
|
|
1959
|
+
browserSelection.extend(start.container, start.offset);
|
|
1960
|
+
}
|
|
1961
|
+
else {
|
|
1962
|
+
const range = this.targetDoc.createRange();
|
|
1963
|
+
range.setStart(start.container, start.offset);
|
|
1964
|
+
range.setEnd(end.container, end.offset);
|
|
1965
|
+
browserSelection.removeAllRanges();
|
|
1966
|
+
browserSelection.addRange(range);
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
}
|
|
1971
|
+
getPointForLineAndChar(lineAndChar) {
|
|
1972
|
+
const line = lineAndChar[0];
|
|
1973
|
+
let charsLeft = lineAndChar[1];
|
|
1974
|
+
const lineEntry = this.rep.lines.atIndex(line);
|
|
1975
|
+
charsLeft -= lineEntry.lineMarker;
|
|
1976
|
+
if (charsLeft < 0) {
|
|
1977
|
+
charsLeft = 0;
|
|
1978
|
+
}
|
|
1979
|
+
const lineNode = lineEntry.lineNode;
|
|
1980
|
+
let n = lineNode;
|
|
1981
|
+
let after = false;
|
|
1982
|
+
if (charsLeft === 0) {
|
|
1983
|
+
return {
|
|
1984
|
+
node: lineNode,
|
|
1985
|
+
index: 0,
|
|
1986
|
+
maxIndex: 1,
|
|
1987
|
+
};
|
|
1988
|
+
}
|
|
1989
|
+
while (!(n === lineNode && after)) {
|
|
1990
|
+
if (after) {
|
|
1991
|
+
if (n.nextSibling) {
|
|
1992
|
+
n = n.nextSibling;
|
|
1993
|
+
after = false;
|
|
1994
|
+
}
|
|
1995
|
+
else {
|
|
1996
|
+
n = n.parentNode;
|
|
1997
|
+
}
|
|
1998
|
+
}
|
|
1999
|
+
else if (isNodeText(n)) {
|
|
2000
|
+
const len = n.nodeValue.length;
|
|
2001
|
+
if (charsLeft <= len) {
|
|
2002
|
+
return {
|
|
2003
|
+
node: n,
|
|
2004
|
+
index: charsLeft,
|
|
2005
|
+
maxIndex: len,
|
|
2006
|
+
};
|
|
2007
|
+
}
|
|
2008
|
+
charsLeft -= len;
|
|
2009
|
+
after = true;
|
|
2010
|
+
}
|
|
2011
|
+
else if (n.firstChild) {
|
|
2012
|
+
n = n.firstChild;
|
|
2013
|
+
}
|
|
2014
|
+
else {
|
|
2015
|
+
after = true;
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
return {
|
|
2019
|
+
node: lineNode,
|
|
2020
|
+
index: 1,
|
|
2021
|
+
maxIndex: 1,
|
|
2022
|
+
};
|
|
2023
|
+
}
|
|
2024
|
+
getLineAndCharForPoint(point) {
|
|
2025
|
+
const N = this.rep.lines.length();
|
|
2026
|
+
if (N === 0)
|
|
2027
|
+
return [0, 0];
|
|
2028
|
+
if (point.node === this.targetBody) {
|
|
2029
|
+
if (point.index === 0) {
|
|
2030
|
+
return [0, 0];
|
|
2031
|
+
}
|
|
2032
|
+
else {
|
|
2033
|
+
const ln = this.rep.lines.atIndex(N - 1);
|
|
2034
|
+
if (!ln)
|
|
2035
|
+
return [0, 0];
|
|
2036
|
+
return [N - 1, ln.text.length];
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
else {
|
|
2040
|
+
let n = point.node;
|
|
2041
|
+
let col = 0;
|
|
2042
|
+
if (isNodeText(n)) {
|
|
2043
|
+
col = point.index;
|
|
2044
|
+
}
|
|
2045
|
+
else if (point.index > 0) {
|
|
2046
|
+
col = this.nodeText(n).length;
|
|
2047
|
+
}
|
|
2048
|
+
let parNode;
|
|
2049
|
+
let prevSib;
|
|
2050
|
+
while (n && (parNode = n.parentNode) && parNode !== this.targetBody) {
|
|
2051
|
+
if ((prevSib = n.previousSibling)) {
|
|
2052
|
+
n = prevSib;
|
|
2053
|
+
col += this.nodeText(n).length;
|
|
2054
|
+
}
|
|
2055
|
+
else {
|
|
2056
|
+
n = parNode;
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
if (!n || !n.parentNode) {
|
|
2060
|
+
// Node not found in targetBody, return safe default
|
|
2061
|
+
return [0, 0];
|
|
2062
|
+
}
|
|
2063
|
+
const lineEntry = n.id ? this.rep.lines.atKey(n.id) : null;
|
|
2064
|
+
if (!lineEntry)
|
|
2065
|
+
return [0, 0];
|
|
2066
|
+
// Use the actual lineMarker from rep (set by domline during rendering).
|
|
2067
|
+
// This correctly accounts for plugin block elements (h1-h6, etc.)
|
|
2068
|
+
// without needing a hardcoded list of block element tags.
|
|
2069
|
+
col += lineEntry.lineMarker;
|
|
2070
|
+
const lineNum = this.rep.lines.indexOfEntry(lineEntry);
|
|
2071
|
+
return [lineNum, col];
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
performSelectionChange(selectStart, selectEnd, focusAtStart) {
|
|
2075
|
+
if (this.repSelectionChange(selectStart, selectEnd, focusAtStart)) {
|
|
2076
|
+
if (this.currentCallStack) {
|
|
2077
|
+
this.currentCallStack.selectionAffected = true;
|
|
2078
|
+
}
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
repSelectionChange(selectStart, selectEnd, focusAtStart) {
|
|
2082
|
+
focusAtStart = !!focusAtStart;
|
|
2083
|
+
const newSelFocusAtStart = focusAtStart && (!selectStart ||
|
|
2084
|
+
!selectEnd ||
|
|
2085
|
+
selectStart[0] !== selectEnd[0] ||
|
|
2086
|
+
selectStart[1] !== selectEnd[1]);
|
|
2087
|
+
if (!this.equalLineAndChars(this.rep.selStart, selectStart) ||
|
|
2088
|
+
!this.equalLineAndChars(this.rep.selEnd, selectEnd) ||
|
|
2089
|
+
this.rep.selFocusAtStart !== newSelFocusAtStart) {
|
|
2090
|
+
this.rep.selStart = selectStart;
|
|
2091
|
+
this.rep.selEnd = selectEnd;
|
|
2092
|
+
this.rep.selFocusAtStart = newSelFocusAtStart;
|
|
2093
|
+
if (this.currentCallStack) {
|
|
2094
|
+
this.currentCallStack.repChanged = true;
|
|
2095
|
+
}
|
|
2096
|
+
// Emit selection changed event
|
|
2097
|
+
if (this.rep.selStart && this.rep.selEnd) {
|
|
2098
|
+
editorBus.emit('editor:selection:changed', {
|
|
2099
|
+
start: [this.rep.selStart[0], this.rep.selStart[1]],
|
|
2100
|
+
end: [this.rep.selEnd[0], this.rep.selEnd[1]],
|
|
2101
|
+
});
|
|
2102
|
+
if (this.onSelectionChanged)
|
|
2103
|
+
this.onSelectionChanged(this.rep.selStart, this.rep.selEnd);
|
|
2104
|
+
}
|
|
2105
|
+
return true;
|
|
2106
|
+
}
|
|
2107
|
+
return false;
|
|
2108
|
+
}
|
|
2109
|
+
updateBrowserSelectionFromRep() {
|
|
2110
|
+
// Don't steal focus from other UI elements (dropdowns, selects, inputs, etc.).
|
|
2111
|
+
// In the old iframe approach this wasn't needed because the editor had its own
|
|
2112
|
+
// document. Now that the editor is in the main document, setting the selection
|
|
2113
|
+
// here would close native <select> dropdowns and blur focused inputs.
|
|
2114
|
+
const active = document.activeElement;
|
|
2115
|
+
if (active && active !== this.targetBody && !this.targetBody.contains(active)) {
|
|
2116
|
+
return;
|
|
2117
|
+
}
|
|
2118
|
+
const selStart = this.rep.selStart;
|
|
2119
|
+
const selEnd = this.rep.selEnd;
|
|
2120
|
+
if (!(selStart && selEnd)) {
|
|
2121
|
+
this.setSelection(null);
|
|
2122
|
+
return;
|
|
2123
|
+
}
|
|
2124
|
+
const selection = {};
|
|
2125
|
+
const ss = [selStart[0], selStart[1]];
|
|
2126
|
+
selection.startPoint = this.getPointForLineAndChar(ss);
|
|
2127
|
+
const se = [selEnd[0], selEnd[1]];
|
|
2128
|
+
selection.endPoint = this.getPointForLineAndChar(se);
|
|
2129
|
+
selection.focusAtStart = !!this.rep.selFocusAtStart;
|
|
2130
|
+
this.setSelection(selection);
|
|
2131
|
+
}
|
|
2132
|
+
equalLineAndChars(a, b) {
|
|
2133
|
+
if (!a)
|
|
2134
|
+
return !b;
|
|
2135
|
+
if (!b)
|
|
2136
|
+
return !a;
|
|
2137
|
+
return a[0] === b[0] && a[1] === b[1];
|
|
2138
|
+
}
|
|
2139
|
+
// -----------------------------------------------------------------------
|
|
2140
|
+
// Private - Input Handling
|
|
2141
|
+
// -----------------------------------------------------------------------
|
|
2142
|
+
handleKeyEvent(evt) {
|
|
2143
|
+
if (!this.isEditable)
|
|
2144
|
+
return;
|
|
2145
|
+
// Invoke external key handlers
|
|
2146
|
+
if (evt.type === 'keypress' && this.onKeyPressHandler) {
|
|
2147
|
+
this.onKeyPressHandler(evt);
|
|
2148
|
+
}
|
|
2149
|
+
if (evt.type === 'keydown' && this.onKeyDownHandler) {
|
|
2150
|
+
this.onKeyDownHandler(evt);
|
|
2151
|
+
}
|
|
2152
|
+
const { type, charCode, keyCode, which } = evt;
|
|
2153
|
+
let altKey = evt.altKey;
|
|
2154
|
+
if (typeof evt.location === 'number') {
|
|
2155
|
+
altKey = altKey && evt.location === KeyboardEvent.DOM_KEY_LOCATION_LEFT;
|
|
2156
|
+
}
|
|
2157
|
+
const isModKey = !charCode && (type === 'keyup' || type === 'keydown') &&
|
|
2158
|
+
(keyCode === 16 || keyCode === 17 || keyCode === 18 ||
|
|
2159
|
+
keyCode === 20 || keyCode === 224 || keyCode === 91);
|
|
2160
|
+
if (isModKey)
|
|
2161
|
+
return;
|
|
2162
|
+
if (keyCode === 13 && browser.opera && type === 'keypress')
|
|
2163
|
+
return;
|
|
2164
|
+
const isTypeForSpecialKey = browser.safari || browser.chrome || browser.firefox
|
|
2165
|
+
? type === 'keydown' : type === 'keypress';
|
|
2166
|
+
const isTypeForCmdKey = browser.safari || browser.chrome || browser.firefox
|
|
2167
|
+
? type === 'keydown' : type === 'keypress';
|
|
2168
|
+
let stopped = false;
|
|
2169
|
+
this.inCallStackIfNecessary('handleKeyEvent', () => {
|
|
2170
|
+
if (type === 'keypress' || (isTypeForSpecialKey && keyCode === 13)) {
|
|
2171
|
+
// Default: allow keypress
|
|
2172
|
+
}
|
|
2173
|
+
else if (evt.key === 'Dead') {
|
|
2174
|
+
stopped = true;
|
|
2175
|
+
return true;
|
|
2176
|
+
}
|
|
2177
|
+
let specialHandled = false;
|
|
2178
|
+
if (!stopped) {
|
|
2179
|
+
// Delete/Backspace
|
|
2180
|
+
if (!specialHandled && isTypeForSpecialKey && keyCode === 8) {
|
|
2181
|
+
this.fastIncorp(3);
|
|
2182
|
+
evt.preventDefault();
|
|
2183
|
+
this.doDeleteKey(evt);
|
|
2184
|
+
specialHandled = true;
|
|
2185
|
+
}
|
|
2186
|
+
// Return
|
|
2187
|
+
if (!specialHandled && isTypeForSpecialKey && keyCode === 13) {
|
|
2188
|
+
this.fastIncorp(4);
|
|
2189
|
+
evt.preventDefault();
|
|
2190
|
+
this.doReturnKey();
|
|
2191
|
+
specialHandled = true;
|
|
2192
|
+
}
|
|
2193
|
+
// Escape
|
|
2194
|
+
if (!specialHandled && isTypeForSpecialKey && keyCode === 27) {
|
|
2195
|
+
this.fastIncorp(4);
|
|
2196
|
+
evt.preventDefault();
|
|
2197
|
+
specialHandled = true;
|
|
2198
|
+
}
|
|
2199
|
+
// Tab
|
|
2200
|
+
if (!specialHandled && isTypeForSpecialKey && keyCode === 9 &&
|
|
2201
|
+
!(evt.metaKey || evt.ctrlKey)) {
|
|
2202
|
+
this.fastIncorp(5);
|
|
2203
|
+
evt.preventDefault();
|
|
2204
|
+
this.doTabKey(evt.shiftKey);
|
|
2205
|
+
specialHandled = true;
|
|
2206
|
+
}
|
|
2207
|
+
// Ctrl+Z (undo)
|
|
2208
|
+
if (!specialHandled && isTypeForCmdKey &&
|
|
2209
|
+
(evt.metaKey || evt.ctrlKey) &&
|
|
2210
|
+
String.fromCharCode(which).toLowerCase() === 'z' && !evt.altKey) {
|
|
2211
|
+
this.fastIncorp(6);
|
|
2212
|
+
evt.preventDefault();
|
|
2213
|
+
if (evt.shiftKey) {
|
|
2214
|
+
this.doUndoRedo('redo');
|
|
2215
|
+
}
|
|
2216
|
+
else {
|
|
2217
|
+
this.doUndoRedo('undo');
|
|
2218
|
+
}
|
|
2219
|
+
specialHandled = true;
|
|
2220
|
+
}
|
|
2221
|
+
// Ctrl+Y (redo)
|
|
2222
|
+
if (!specialHandled && isTypeForCmdKey &&
|
|
2223
|
+
(evt.metaKey || evt.ctrlKey) &&
|
|
2224
|
+
String.fromCharCode(which).toLowerCase() === 'y') {
|
|
2225
|
+
this.fastIncorp(10);
|
|
2226
|
+
evt.preventDefault();
|
|
2227
|
+
this.doUndoRedo('redo');
|
|
2228
|
+
specialHandled = true;
|
|
2229
|
+
}
|
|
2230
|
+
// Ctrl+B (bold)
|
|
2231
|
+
if (!specialHandled && isTypeForCmdKey &&
|
|
2232
|
+
(evt.metaKey || evt.ctrlKey) &&
|
|
2233
|
+
String.fromCharCode(which).toLowerCase() === 'b') {
|
|
2234
|
+
this.fastIncorp(13);
|
|
2235
|
+
evt.preventDefault();
|
|
2236
|
+
this.toggleAttributeOnSelection('bold');
|
|
2237
|
+
specialHandled = true;
|
|
2238
|
+
}
|
|
2239
|
+
// Ctrl+I (italic)
|
|
2240
|
+
if (!specialHandled && isTypeForCmdKey &&
|
|
2241
|
+
(evt.metaKey || evt.ctrlKey) &&
|
|
2242
|
+
String.fromCharCode(which).toLowerCase() === 'i') {
|
|
2243
|
+
this.fastIncorp(14);
|
|
2244
|
+
evt.preventDefault();
|
|
2245
|
+
this.toggleAttributeOnSelection('italic');
|
|
2246
|
+
specialHandled = true;
|
|
2247
|
+
}
|
|
2248
|
+
// Ctrl+U (underline)
|
|
2249
|
+
if (!specialHandled && isTypeForCmdKey &&
|
|
2250
|
+
(evt.metaKey || evt.ctrlKey) &&
|
|
2251
|
+
String.fromCharCode(which).toLowerCase() === 'u') {
|
|
2252
|
+
this.fastIncorp(15);
|
|
2253
|
+
evt.preventDefault();
|
|
2254
|
+
this.toggleAttributeOnSelection('underline');
|
|
2255
|
+
specialHandled = true;
|
|
2256
|
+
}
|
|
2257
|
+
// Ctrl+5 (strikethrough)
|
|
2258
|
+
if (!specialHandled && isTypeForCmdKey &&
|
|
2259
|
+
(evt.metaKey || evt.ctrlKey) &&
|
|
2260
|
+
String.fromCharCode(which).toLowerCase() === '5' &&
|
|
2261
|
+
evt.altKey !== true) {
|
|
2262
|
+
this.fastIncorp(13);
|
|
2263
|
+
evt.preventDefault();
|
|
2264
|
+
this.toggleAttributeOnSelection('strikethrough');
|
|
2265
|
+
specialHandled = true;
|
|
2266
|
+
}
|
|
2267
|
+
// Ctrl+Shift+L (unordered list)
|
|
2268
|
+
if (!specialHandled && isTypeForCmdKey &&
|
|
2269
|
+
(evt.metaKey || evt.ctrlKey) &&
|
|
2270
|
+
String.fromCharCode(which).toLowerCase() === 'l' &&
|
|
2271
|
+
evt.shiftKey) {
|
|
2272
|
+
this.fastIncorp(9);
|
|
2273
|
+
evt.preventDefault();
|
|
2274
|
+
this.doInsertUnorderedList();
|
|
2275
|
+
specialHandled = true;
|
|
2276
|
+
}
|
|
2277
|
+
// Ctrl+Shift+N or Ctrl+Shift+1 (ordered list)
|
|
2278
|
+
if (!specialHandled && isTypeForCmdKey &&
|
|
2279
|
+
(evt.metaKey || evt.ctrlKey) && evt.shiftKey &&
|
|
2280
|
+
(String.fromCharCode(which).toLowerCase() === 'n' ||
|
|
2281
|
+
String.fromCharCode(which) === '1')) {
|
|
2282
|
+
this.fastIncorp(9);
|
|
2283
|
+
evt.preventDefault();
|
|
2284
|
+
this.doInsertOrderedList();
|
|
2285
|
+
specialHandled = true;
|
|
2286
|
+
}
|
|
2287
|
+
// Ctrl+Shift+C (clear authorship)
|
|
2288
|
+
if (!specialHandled && isTypeForCmdKey &&
|
|
2289
|
+
(evt.metaKey || evt.ctrlKey) && evt.shiftKey &&
|
|
2290
|
+
String.fromCharCode(which).toLowerCase() === 'c') {
|
|
2291
|
+
this.fastIncorp(9);
|
|
2292
|
+
evt.preventDefault();
|
|
2293
|
+
this.execCommand('clearauthorship');
|
|
2294
|
+
specialHandled = true;
|
|
2295
|
+
}
|
|
2296
|
+
// Ctrl+H (backspace)
|
|
2297
|
+
if (!specialHandled && isTypeForCmdKey &&
|
|
2298
|
+
evt.ctrlKey &&
|
|
2299
|
+
String.fromCharCode(which).toLowerCase() === 'h') {
|
|
2300
|
+
this.fastIncorp(20);
|
|
2301
|
+
evt.preventDefault();
|
|
2302
|
+
this.doDeleteKey();
|
|
2303
|
+
specialHandled = true;
|
|
2304
|
+
}
|
|
2305
|
+
}
|
|
2306
|
+
if (type === 'keydown') {
|
|
2307
|
+
this.idleWorkTimer.atLeast(500);
|
|
2308
|
+
}
|
|
2309
|
+
else if (type === 'keypress') {
|
|
2310
|
+
if (!specialHandled) {
|
|
2311
|
+
this.idleWorkTimer.atMost(0);
|
|
2312
|
+
}
|
|
2313
|
+
else {
|
|
2314
|
+
this.idleWorkTimer.atLeast(500);
|
|
2315
|
+
}
|
|
2316
|
+
}
|
|
2317
|
+
else if (type === 'keyup') {
|
|
2318
|
+
this.idleWorkTimer.atLeast(0);
|
|
2319
|
+
this.idleWorkTimer.atMost(0);
|
|
2320
|
+
}
|
|
2321
|
+
const isFirefoxHalfCharacter = browser.firefox && evt.altKey && charCode === 0 && keyCode === 0;
|
|
2322
|
+
const isSafariHalfCharacter = browser.safari && evt.altKey && keyCode === 229;
|
|
2323
|
+
if (this.thisKeyDoesntTriggerNormalize || isFirefoxHalfCharacter ||
|
|
2324
|
+
isSafariHalfCharacter) {
|
|
2325
|
+
this.idleWorkTimer.atLeast(3000);
|
|
2326
|
+
this.thisKeyDoesntTriggerNormalize = true;
|
|
2327
|
+
}
|
|
2328
|
+
if (!specialHandled && !this.thisKeyDoesntTriggerNormalize &&
|
|
2329
|
+
!this.inInternationalComposition && type !== 'keyup') {
|
|
2330
|
+
this.observeChangesAroundSelection();
|
|
2331
|
+
}
|
|
2332
|
+
if (type === 'keyup') {
|
|
2333
|
+
this.thisKeyDoesntTriggerNormalize = false;
|
|
2334
|
+
}
|
|
2335
|
+
});
|
|
2336
|
+
}
|
|
2337
|
+
doReturnKey() {
|
|
2338
|
+
if (!(this.rep.selStart && this.rep.selEnd))
|
|
2339
|
+
return;
|
|
2340
|
+
const lineNum = this.rep.selStart[0];
|
|
2341
|
+
let listType = this.getLineListType(lineNum);
|
|
2342
|
+
if (listType) {
|
|
2343
|
+
const text = this.rep.lines.atIndex(lineNum).text;
|
|
2344
|
+
const listTypeParsed = /([a-z]+)([0-9]+)/.exec(listType);
|
|
2345
|
+
if (!listTypeParsed)
|
|
2346
|
+
return;
|
|
2347
|
+
const type = listTypeParsed[1];
|
|
2348
|
+
const level = Number(listTypeParsed[2]);
|
|
2349
|
+
if (text === '*' && type !== 'indent') {
|
|
2350
|
+
if (level > 1) {
|
|
2351
|
+
this._setLineListType(lineNum, type + (level - 1));
|
|
2352
|
+
}
|
|
2353
|
+
else {
|
|
2354
|
+
this._setLineListType(lineNum, '');
|
|
2355
|
+
this.renumberList(lineNum + 1);
|
|
2356
|
+
}
|
|
2357
|
+
}
|
|
2358
|
+
else if (lineNum + 1 <= this.rep.lines.length()) {
|
|
2359
|
+
this.performDocumentReplaceSelection('\n');
|
|
2360
|
+
this._setLineListType(lineNum + 1, type + level);
|
|
2361
|
+
}
|
|
2362
|
+
}
|
|
2363
|
+
else {
|
|
2364
|
+
this.performDocumentReplaceSelection('\n');
|
|
2365
|
+
this.handleReturnIndentation();
|
|
2366
|
+
}
|
|
2367
|
+
}
|
|
2368
|
+
handleReturnIndentation() {
|
|
2369
|
+
if (this.isCaret() && this.getCaretColumn() === 0 && this.getCaretLine() > 0) {
|
|
2370
|
+
const lineNum = this.getCaretLine();
|
|
2371
|
+
const thisLine = this.rep.lines.atIndex(lineNum);
|
|
2372
|
+
const prevLine = this.rep.lines.prev(thisLine);
|
|
2373
|
+
if (!prevLine)
|
|
2374
|
+
return;
|
|
2375
|
+
const prevLineText = prevLine.text;
|
|
2376
|
+
let theIndent = /^ *(?:)/.exec(prevLineText)[0];
|
|
2377
|
+
if (/[[(:{]\s*$/.exec(prevLineText)) {
|
|
2378
|
+
theIndent += AceEditor.THE_TAB;
|
|
2379
|
+
}
|
|
2380
|
+
const cs = new Builder(this.rep.lines.totalWidth()).keep(this.rep.lines.offsetOfIndex(lineNum), lineNum).insert(theIndent, [
|
|
2381
|
+
['author', this.thisAuthor],
|
|
2382
|
+
], this.rep.apool).toString();
|
|
2383
|
+
this.performDocumentApplyChangeset(cs);
|
|
2384
|
+
this.performSelectionChange([lineNum, theIndent.length], [lineNum, theIndent.length]);
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
doDeleteKey(optEvt) {
|
|
2388
|
+
const evt = optEvt || {};
|
|
2389
|
+
let handled = false;
|
|
2390
|
+
if (this.rep.selStart) {
|
|
2391
|
+
if (this.isCaret()) {
|
|
2392
|
+
const lineNum = this.getCaretLine();
|
|
2393
|
+
const col = this.getCaretColumn();
|
|
2394
|
+
const lineEntry = this.rep.lines.atIndex(lineNum);
|
|
2395
|
+
const lineText = lineEntry.text;
|
|
2396
|
+
const lineMarker = lineEntry.lineMarker;
|
|
2397
|
+
if (evt.metaKey && col > lineMarker) {
|
|
2398
|
+
this.performDocumentReplaceRange([lineNum, lineMarker], [lineNum, col], '');
|
|
2399
|
+
handled = true;
|
|
2400
|
+
}
|
|
2401
|
+
else if (/^ +$/.exec(lineText.substring(lineMarker, col))) {
|
|
2402
|
+
const col2 = col - lineMarker;
|
|
2403
|
+
const tabSize = AceEditor.THE_TAB.length;
|
|
2404
|
+
const toDelete = ((col2 - 1) % tabSize) + 1;
|
|
2405
|
+
this.performDocumentReplaceRange([lineNum, col - toDelete], [lineNum, col], '');
|
|
2406
|
+
handled = true;
|
|
2407
|
+
}
|
|
2408
|
+
}
|
|
2409
|
+
if (!handled) {
|
|
2410
|
+
if (this.isCaret()) {
|
|
2411
|
+
const theLine = this.getCaretLine();
|
|
2412
|
+
const lineEntry = this.rep.lines.atIndex(theLine);
|
|
2413
|
+
if (this.getCaretColumn() <= lineEntry.lineMarker) {
|
|
2414
|
+
const prevLineListType = theLine > 0 ? this.getLineListType(theLine - 1) : '';
|
|
2415
|
+
const thisLineListType = this.getLineListType(theLine);
|
|
2416
|
+
const prevLineEntry = theLine > 0 && this.rep.lines.atIndex(theLine - 1);
|
|
2417
|
+
const prevLineBlank = prevLineEntry &&
|
|
2418
|
+
prevLineEntry.text.length === prevLineEntry.lineMarker;
|
|
2419
|
+
const thisLineHasMarker = this.documentAttributeManager.lineHasMarker(theLine);
|
|
2420
|
+
if (thisLineListType) {
|
|
2421
|
+
if (prevLineBlank && !prevLineListType) {
|
|
2422
|
+
this.performDocumentReplaceRange([theLine - 1, prevLineEntry.text.length], [theLine, 0], '');
|
|
2423
|
+
}
|
|
2424
|
+
else {
|
|
2425
|
+
this.performDocumentReplaceRange([theLine, 0], [theLine, lineEntry.lineMarker], '');
|
|
2426
|
+
}
|
|
2427
|
+
}
|
|
2428
|
+
else if (thisLineHasMarker && prevLineEntry) {
|
|
2429
|
+
this.performDocumentReplaceRange([theLine - 1, prevLineEntry.text.length], [theLine, lineEntry.lineMarker], '');
|
|
2430
|
+
}
|
|
2431
|
+
else if (theLine > 0) {
|
|
2432
|
+
this.performDocumentReplaceRange([theLine - 1, prevLineEntry.text.length], [theLine, 0], '');
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
else {
|
|
2436
|
+
const docChar = this.caretDocChar();
|
|
2437
|
+
if (docChar > 0) {
|
|
2438
|
+
if (evt.metaKey || evt.ctrlKey || evt.altKey) {
|
|
2439
|
+
let deleteBackTo = docChar - 1;
|
|
2440
|
+
while (deleteBackTo > lineEntry.lineMarker &&
|
|
2441
|
+
this.isWordChar(this.rep.alltext.charAt(deleteBackTo - 1))) {
|
|
2442
|
+
deleteBackTo--;
|
|
2443
|
+
}
|
|
2444
|
+
this.performDocumentReplaceCharRange(deleteBackTo, docChar, '');
|
|
2445
|
+
}
|
|
2446
|
+
else {
|
|
2447
|
+
this.performDocumentReplaceCharRange(docChar - 1, docChar, '');
|
|
2448
|
+
}
|
|
2449
|
+
}
|
|
2450
|
+
}
|
|
2451
|
+
}
|
|
2452
|
+
else {
|
|
2453
|
+
this.performDocumentReplaceSelection('');
|
|
2454
|
+
}
|
|
2455
|
+
}
|
|
2456
|
+
}
|
|
2457
|
+
const line = this.getCaretLine();
|
|
2458
|
+
if (line !== -1 && this.renumberList(line + 1) == null) {
|
|
2459
|
+
this.renumberList(line);
|
|
2460
|
+
}
|
|
2461
|
+
}
|
|
2462
|
+
doTabKey(shiftDown) {
|
|
2463
|
+
if (!this.doIndentOutdent(shiftDown)) {
|
|
2464
|
+
this.performDocumentReplaceSelection(AceEditor.THE_TAB);
|
|
2465
|
+
}
|
|
2466
|
+
}
|
|
2467
|
+
doIndentOutdent(isOut) {
|
|
2468
|
+
if (!((this.rep.selStart && this.rep.selEnd) ||
|
|
2469
|
+
(this.rep.selStart[0] === this.rep.selEnd[0] &&
|
|
2470
|
+
this.rep.selStart[1] === this.rep.selEnd[1] &&
|
|
2471
|
+
this.rep.selEnd[1] > 1)) &&
|
|
2472
|
+
isOut !== true) {
|
|
2473
|
+
return false;
|
|
2474
|
+
}
|
|
2475
|
+
const firstLine = this.rep.selStart[0];
|
|
2476
|
+
const lastLine = Math.max(firstLine, this.rep.selEnd[0] - (this.rep.selEnd[1] === 0 ? 1 : 0));
|
|
2477
|
+
const mods = [];
|
|
2478
|
+
for (let n = firstLine; n <= lastLine; n++) {
|
|
2479
|
+
let listType = this.getLineListType(n);
|
|
2480
|
+
let t = 'indent';
|
|
2481
|
+
let level = 0;
|
|
2482
|
+
if (listType) {
|
|
2483
|
+
const parsed = /([a-z]+)([0-9]+)/.exec(listType);
|
|
2484
|
+
if (parsed) {
|
|
2485
|
+
t = parsed[1];
|
|
2486
|
+
level = Number(parsed[2]);
|
|
2487
|
+
}
|
|
2488
|
+
}
|
|
2489
|
+
const newLevel = Math.max(0, Math.min(AceEditor.MAX_LIST_LEVEL, level + (isOut ? -1 : 1)));
|
|
2490
|
+
if (level !== newLevel) {
|
|
2491
|
+
mods.push([n, newLevel > 0 ? t + newLevel : '']);
|
|
2492
|
+
}
|
|
2493
|
+
}
|
|
2494
|
+
for (const mod of mods)
|
|
2495
|
+
this._setLineListType(mod[0], mod[1]);
|
|
2496
|
+
return true;
|
|
2497
|
+
}
|
|
2498
|
+
handleClick(evt) {
|
|
2499
|
+
this.inCallStackIfNecessary('handleClick', () => {
|
|
2500
|
+
this.idleWorkTimer.atMost(200);
|
|
2501
|
+
});
|
|
2502
|
+
const isLink = (n) => (n.tagName || '').toLowerCase() === 'a' && n.href;
|
|
2503
|
+
if (evt.button !== 2 && evt.button !== 3) {
|
|
2504
|
+
let n = evt.target;
|
|
2505
|
+
while (n && n.parentNode && !isLink(n)) {
|
|
2506
|
+
n = n.parentNode;
|
|
2507
|
+
}
|
|
2508
|
+
if (n && isLink(n)) {
|
|
2509
|
+
try {
|
|
2510
|
+
window.open(n.href, '_blank', 'noopener,noreferrer');
|
|
2511
|
+
if (evt.ctrlKey)
|
|
2512
|
+
window.focus();
|
|
2513
|
+
}
|
|
2514
|
+
catch (e) {
|
|
2515
|
+
// absorb error
|
|
2516
|
+
}
|
|
2517
|
+
evt.preventDefault();
|
|
2518
|
+
}
|
|
2519
|
+
}
|
|
2520
|
+
}
|
|
2521
|
+
// -----------------------------------------------------------------------
|
|
2522
|
+
// Private - Undo / Redo
|
|
2523
|
+
// -----------------------------------------------------------------------
|
|
2524
|
+
doUndoRedo(which) {
|
|
2525
|
+
if (undoModule.enabled) {
|
|
2526
|
+
let whichMethod;
|
|
2527
|
+
if (which === 'undo')
|
|
2528
|
+
whichMethod = 'performUndo';
|
|
2529
|
+
if (which === 'redo')
|
|
2530
|
+
whichMethod = 'performRedo';
|
|
2531
|
+
if (whichMethod) {
|
|
2532
|
+
const oldEventType = this.currentCallStack.editEvent.eventType;
|
|
2533
|
+
this.currentCallStack.startNewEvent(which);
|
|
2534
|
+
undoModule[whichMethod]((backset, selectionInfo) => {
|
|
2535
|
+
if (backset) {
|
|
2536
|
+
this.performDocumentApplyChangeset(backset);
|
|
2537
|
+
}
|
|
2538
|
+
if (selectionInfo) {
|
|
2539
|
+
this.performSelectionChange(this.lineAndColumnFromChar(selectionInfo.selStart), this.lineAndColumnFromChar(selectionInfo.selEnd), selectionInfo.selFocusAtStart);
|
|
2540
|
+
}
|
|
2541
|
+
const oldEvent = this.currentCallStack.startNewEvent(oldEventType, true);
|
|
2542
|
+
return oldEvent;
|
|
2543
|
+
});
|
|
2544
|
+
}
|
|
2545
|
+
}
|
|
2546
|
+
}
|
|
2547
|
+
// -----------------------------------------------------------------------
|
|
2548
|
+
// Private - Formatting
|
|
2549
|
+
// -----------------------------------------------------------------------
|
|
2550
|
+
setAttributeOnSelection(attributeName, attributeValue) {
|
|
2551
|
+
if (!(this.rep.selStart && this.rep.selEnd))
|
|
2552
|
+
return;
|
|
2553
|
+
this.documentAttributeManager.setAttributesOnRange(this.rep.selStart, this.rep.selEnd, [
|
|
2554
|
+
[attributeName, attributeValue],
|
|
2555
|
+
]);
|
|
2556
|
+
}
|
|
2557
|
+
getAttributeOnSelection(attributeName, prevChar) {
|
|
2558
|
+
if (!(this.rep.selStart && this.rep.selEnd))
|
|
2559
|
+
return false;
|
|
2560
|
+
const isNotSelection = (this.rep.selStart[0] === this.rep.selEnd[0] &&
|
|
2561
|
+
this.rep.selEnd[1] === this.rep.selStart[1]);
|
|
2562
|
+
if (isNotSelection) {
|
|
2563
|
+
if (prevChar) {
|
|
2564
|
+
if (this.rep.selStart[1] !== 0) {
|
|
2565
|
+
this.rep.selStart[1]--;
|
|
2566
|
+
}
|
|
2567
|
+
}
|
|
2568
|
+
}
|
|
2569
|
+
const withIt = new AttributeMap(this.rep.apool).set(attributeName, 'true').toString();
|
|
2570
|
+
const withItRegex = new RegExp(`${withIt.replace(/\*/g, '\\*')}(\\*|$)`);
|
|
2571
|
+
const hasIt = (attribs) => withItRegex.test(attribs);
|
|
2572
|
+
const rangeHasAttrib = (selStart, selEnd) => {
|
|
2573
|
+
if (selStart[1] === selEnd[1] && selStart[0] === selEnd[0])
|
|
2574
|
+
return false;
|
|
2575
|
+
if (selStart[0] !== selEnd[0]) {
|
|
2576
|
+
let hasAttrib = true;
|
|
2577
|
+
hasAttrib = hasAttrib && rangeHasAttrib(selStart, [selStart[0], this.rep.lines.atIndex(selStart[0]).text.length]);
|
|
2578
|
+
for (let n = selStart[0] + 1; n < selEnd[0]; n++) {
|
|
2579
|
+
hasAttrib = hasAttrib &&
|
|
2580
|
+
rangeHasAttrib([n, 0], [n, this.rep.lines.atIndex(n).text.length]);
|
|
2581
|
+
}
|
|
2582
|
+
hasAttrib = hasAttrib && rangeHasAttrib([selEnd[0], 0], [selEnd[0], selEnd[1]]);
|
|
2583
|
+
return hasAttrib;
|
|
2584
|
+
}
|
|
2585
|
+
const lineNum = selStart[0];
|
|
2586
|
+
const start = selStart[1];
|
|
2587
|
+
const end = selEnd[1];
|
|
2588
|
+
let hasAttrib = true;
|
|
2589
|
+
let indexIntoLine = 0;
|
|
2590
|
+
for (const op of deserializeOps(this.rep.alines[lineNum])) {
|
|
2591
|
+
const opStartInLine = indexIntoLine;
|
|
2592
|
+
const opEndInLine = opStartInLine + op.chars;
|
|
2593
|
+
if (!hasIt(op.attribs)) {
|
|
2594
|
+
if (!(opEndInLine <= start || opStartInLine >= end)) {
|
|
2595
|
+
hasAttrib = false;
|
|
2596
|
+
break;
|
|
2597
|
+
}
|
|
2598
|
+
}
|
|
2599
|
+
indexIntoLine = opEndInLine;
|
|
2600
|
+
}
|
|
2601
|
+
return hasAttrib;
|
|
2602
|
+
};
|
|
2603
|
+
return rangeHasAttrib(this.rep.selStart, this.rep.selEnd);
|
|
2604
|
+
}
|
|
2605
|
+
toggleAttributeOnSelection(attributeName) {
|
|
2606
|
+
if (!(this.rep.selStart && this.rep.selEnd))
|
|
2607
|
+
return;
|
|
2608
|
+
let selectionAllHasIt = true;
|
|
2609
|
+
const withIt = new AttributeMap(this.rep.apool).set(attributeName, 'true').toString();
|
|
2610
|
+
const withItRegex = new RegExp(`${withIt.replace(/\*/g, '\\*')}(\\*|$)`);
|
|
2611
|
+
const hasIt = (attribs) => withItRegex.test(attribs);
|
|
2612
|
+
const selStartLine = this.rep.selStart[0];
|
|
2613
|
+
const selEndLine = this.rep.selEnd[0];
|
|
2614
|
+
for (let n = selStartLine; n <= selEndLine; n++) {
|
|
2615
|
+
let indexIntoLine = 0;
|
|
2616
|
+
let selectionStartInLine = 0;
|
|
2617
|
+
if (this.documentAttributeManager.lineHasMarker(n)) {
|
|
2618
|
+
selectionStartInLine = 1;
|
|
2619
|
+
}
|
|
2620
|
+
let selectionEndInLine = this.rep.lines.atIndex(n).text.length;
|
|
2621
|
+
if (n === selStartLine) {
|
|
2622
|
+
selectionStartInLine = this.rep.selStart[1];
|
|
2623
|
+
}
|
|
2624
|
+
if (n === selEndLine) {
|
|
2625
|
+
selectionEndInLine = this.rep.selEnd[1];
|
|
2626
|
+
}
|
|
2627
|
+
for (const op of deserializeOps(this.rep.alines[n])) {
|
|
2628
|
+
const opStartInLine = indexIntoLine;
|
|
2629
|
+
const opEndInLine = opStartInLine + op.chars;
|
|
2630
|
+
if (!hasIt(op.attribs)) {
|
|
2631
|
+
if (!(opEndInLine <= selectionStartInLine || opStartInLine >= selectionEndInLine)) {
|
|
2632
|
+
selectionAllHasIt = false;
|
|
2633
|
+
break;
|
|
2634
|
+
}
|
|
2635
|
+
}
|
|
2636
|
+
indexIntoLine = opEndInLine;
|
|
2637
|
+
}
|
|
2638
|
+
if (!selectionAllHasIt)
|
|
2639
|
+
break;
|
|
2640
|
+
}
|
|
2641
|
+
const attributeValue = selectionAllHasIt ? '' : 'true';
|
|
2642
|
+
this.documentAttributeManager.setAttributesOnRange(this.rep.selStart, this.rep.selEnd, [[attributeName, attributeValue]]);
|
|
2643
|
+
}
|
|
2644
|
+
getLineListType(lineNum) {
|
|
2645
|
+
return this.documentAttributeManager.getAttributeOnLine(lineNum, this.listAttributeName);
|
|
2646
|
+
}
|
|
2647
|
+
_setLineListType(lineNum, listType) {
|
|
2648
|
+
if (listType === '') {
|
|
2649
|
+
this.documentAttributeManager.removeAttributeOnLine(lineNum, this.listAttributeName);
|
|
2650
|
+
this.documentAttributeManager.removeAttributeOnLine(lineNum, 'start');
|
|
2651
|
+
}
|
|
2652
|
+
else {
|
|
2653
|
+
this.documentAttributeManager.setAttributeOnLine(lineNum, this.listAttributeName, listType);
|
|
2654
|
+
}
|
|
2655
|
+
if (this.renumberList(lineNum + 1) == null) {
|
|
2656
|
+
this.renumberList(lineNum);
|
|
2657
|
+
}
|
|
2658
|
+
}
|
|
2659
|
+
renumberList(lineNum) {
|
|
2660
|
+
let type = this.getLineListType(lineNum);
|
|
2661
|
+
if (!type)
|
|
2662
|
+
return null;
|
|
2663
|
+
const parsed = /([a-z]+)[0-9]+/.exec(type);
|
|
2664
|
+
if (!parsed)
|
|
2665
|
+
return null;
|
|
2666
|
+
if (parsed[1] === 'indent')
|
|
2667
|
+
return null;
|
|
2668
|
+
while (lineNum - 1 >= 0 && (type = this.getLineListType(lineNum - 1))) {
|
|
2669
|
+
const p = /([a-z]+)[0-9]+/.exec(type);
|
|
2670
|
+
if (!p || p[1] === 'indent')
|
|
2671
|
+
break;
|
|
2672
|
+
lineNum--;
|
|
2673
|
+
}
|
|
2674
|
+
const builder = new Builder(this.rep.lines.totalWidth());
|
|
2675
|
+
let loc = [0, 0];
|
|
2676
|
+
const applyNumberList = (line, level) => {
|
|
2677
|
+
let position = 1;
|
|
2678
|
+
let curLevel = level;
|
|
2679
|
+
let lt;
|
|
2680
|
+
while ((lt = this.getLineListType(line))) {
|
|
2681
|
+
const ltParsed = /([a-z]+)([0-9]+)/.exec(lt);
|
|
2682
|
+
if (!ltParsed)
|
|
2683
|
+
return line;
|
|
2684
|
+
curLevel = Number(ltParsed[2]);
|
|
2685
|
+
if (isNaN(curLevel) || ltParsed[0] === 'indent') {
|
|
2686
|
+
return line;
|
|
2687
|
+
}
|
|
2688
|
+
else if (curLevel === level) {
|
|
2689
|
+
buildKeepRange(this.rep, builder, loc, (loc = [line, 0]));
|
|
2690
|
+
buildKeepRange(this.rep, builder, loc, (loc = [line, 1]), [
|
|
2691
|
+
['start', position],
|
|
2692
|
+
], this.rep.apool);
|
|
2693
|
+
position++;
|
|
2694
|
+
line++;
|
|
2695
|
+
}
|
|
2696
|
+
else if (curLevel < level) {
|
|
2697
|
+
return line;
|
|
2698
|
+
}
|
|
2699
|
+
else {
|
|
2700
|
+
line = applyNumberList(line, level + 1);
|
|
2701
|
+
}
|
|
2702
|
+
}
|
|
2703
|
+
return line;
|
|
2704
|
+
};
|
|
2705
|
+
applyNumberList(lineNum, 1);
|
|
2706
|
+
const cs = builder.toString();
|
|
2707
|
+
if (!isIdentity(cs)) {
|
|
2708
|
+
this.performDocumentApplyChangeset(cs);
|
|
2709
|
+
}
|
|
2710
|
+
return lineNum;
|
|
2711
|
+
}
|
|
2712
|
+
doInsertList(type) {
|
|
2713
|
+
if (!(this.rep.selStart && this.rep.selEnd))
|
|
2714
|
+
return;
|
|
2715
|
+
const firstLine = this.rep.selStart[0];
|
|
2716
|
+
const lastLine = Math.max(firstLine, this.rep.selEnd[0] - (this.rep.selEnd[1] === 0 ? 1 : 0));
|
|
2717
|
+
let allLinesAreList = true;
|
|
2718
|
+
for (let n = firstLine; n <= lastLine; n++) {
|
|
2719
|
+
const lt = this.getLineListType(n);
|
|
2720
|
+
if (!lt || lt.slice(0, type.length) !== type) {
|
|
2721
|
+
allLinesAreList = false;
|
|
2722
|
+
break;
|
|
2723
|
+
}
|
|
2724
|
+
}
|
|
2725
|
+
const mods = [];
|
|
2726
|
+
for (let n = firstLine; n <= lastLine; n++) {
|
|
2727
|
+
let level = 0;
|
|
2728
|
+
let togglingOn = true;
|
|
2729
|
+
const t = this.getLineListType(n);
|
|
2730
|
+
const listTypeParsed = /([a-z]+)([0-9]+)/.exec(t);
|
|
2731
|
+
if (allLinesAreList) {
|
|
2732
|
+
togglingOn = false;
|
|
2733
|
+
}
|
|
2734
|
+
if (listTypeParsed) {
|
|
2735
|
+
level = Number(listTypeParsed[2]);
|
|
2736
|
+
}
|
|
2737
|
+
if (togglingOn) {
|
|
2738
|
+
mods.push([n, allLinesAreList
|
|
2739
|
+
? `indent${level}`
|
|
2740
|
+
: (t ? type + level : `${type}1`)]);
|
|
2741
|
+
}
|
|
2742
|
+
else {
|
|
2743
|
+
// Scrap the entire indentation and list type.
|
|
2744
|
+
if (level === 1) {
|
|
2745
|
+
mods.push([n, '']);
|
|
2746
|
+
}
|
|
2747
|
+
else if (level > 1) {
|
|
2748
|
+
mods.push([n, '']);
|
|
2749
|
+
mods.push([n, `indent${level}`]);
|
|
2750
|
+
}
|
|
2751
|
+
}
|
|
2752
|
+
}
|
|
2753
|
+
for (const mod of mods)
|
|
2754
|
+
this._setLineListType(mod[0], mod[1]);
|
|
2755
|
+
}
|
|
2756
|
+
doInsertUnorderedList() {
|
|
2757
|
+
this.doInsertList('bullet');
|
|
2758
|
+
}
|
|
2759
|
+
doInsertOrderedList() {
|
|
2760
|
+
this.doInsertList('number');
|
|
2761
|
+
}
|
|
2762
|
+
// -----------------------------------------------------------------------
|
|
2763
|
+
// Private - Helper Functions
|
|
2764
|
+
// -----------------------------------------------------------------------
|
|
2765
|
+
caretDocChar() {
|
|
2766
|
+
return this.rep.lines.offsetOfIndex(this.getCaretLine()) + this.getCaretColumn();
|
|
2767
|
+
}
|
|
2768
|
+
isWordChar(c) {
|
|
2769
|
+
return /[\w]/.test(c);
|
|
2770
|
+
}
|
|
2771
|
+
textify(str) {
|
|
2772
|
+
return str.replace(/[\n\r ]/g, ' ').replace(/\xa0/g, ' ').replace(/\t/g, ' ');
|
|
2773
|
+
}
|
|
2774
|
+
/* @internal */ isBlockElement(n) {
|
|
2775
|
+
return !!AceEditor._blockElems[(n.tagName || '').toLowerCase()];
|
|
2776
|
+
}
|
|
2777
|
+
isStyleAttribute(aname) {
|
|
2778
|
+
return !!AceEditor.STYLE_ATTRIBS[aname];
|
|
2779
|
+
}
|
|
2780
|
+
isDefaultLineAttribute(aname) {
|
|
2781
|
+
return AttributeManager.DEFAULT_LINE_ATTRIBUTES.indexOf(aname) !== -1;
|
|
2782
|
+
}
|
|
2783
|
+
isPadLoading(t) {
|
|
2784
|
+
return t === 'setup' || t === 'setBaseText' || t === 'importText';
|
|
2785
|
+
}
|
|
2786
|
+
enforceEditability() {
|
|
2787
|
+
this.setEditable(this.isEditable);
|
|
2788
|
+
}
|
|
2789
|
+
// -----------------------------------------------------------------------
|
|
2790
|
+
// Private - Event Binding
|
|
2791
|
+
// -----------------------------------------------------------------------
|
|
2792
|
+
bindTheEventHandlers() {
|
|
2793
|
+
// Key events
|
|
2794
|
+
this.targetBody.addEventListener('keydown', (e) => this.handleKeyEvent(e));
|
|
2795
|
+
this.targetBody.addEventListener('keypress', (e) => this.handleKeyEvent(e));
|
|
2796
|
+
this.targetBody.addEventListener('keyup', (e) => this.handleKeyEvent(e));
|
|
2797
|
+
// Click events
|
|
2798
|
+
this.targetBody.addEventListener('click', (e) => this.handleClick(e));
|
|
2799
|
+
this.targetBody.addEventListener('mouseup', () => {
|
|
2800
|
+
this.inCallStackIfNecessary('handleClick', () => {
|
|
2801
|
+
this.idleWorkTimer.atMost(200);
|
|
2802
|
+
});
|
|
2803
|
+
});
|
|
2804
|
+
// Input event - schedule incorporateUserChanges
|
|
2805
|
+
this.targetBody.addEventListener('input', () => {
|
|
2806
|
+
this.idleWorkTimer.atMost(0);
|
|
2807
|
+
});
|
|
2808
|
+
// Paste event
|
|
2809
|
+
this.targetBody.addEventListener('paste', (e) => {
|
|
2810
|
+
editorBus.emit('custom:ace:paste', {
|
|
2811
|
+
rep: this.rep,
|
|
2812
|
+
documentAttributeManager: this.documentAttributeManager,
|
|
2813
|
+
e,
|
|
2814
|
+
});
|
|
2815
|
+
this.idleWorkTimer.atMost(0);
|
|
2816
|
+
});
|
|
2817
|
+
// Cut event
|
|
2818
|
+
this.targetBody.addEventListener('cut', () => {
|
|
2819
|
+
this.idleWorkTimer.atMost(0);
|
|
2820
|
+
});
|
|
2821
|
+
// Drop event
|
|
2822
|
+
this.targetBody.addEventListener('drop', (e) => {
|
|
2823
|
+
const isLinkTarget = (target) => target instanceof Element &&
|
|
2824
|
+
(target.localName === 'a' || target.closest('a') != null);
|
|
2825
|
+
if (isLinkTarget(e.target)) {
|
|
2826
|
+
e.preventDefault();
|
|
2827
|
+
}
|
|
2828
|
+
// Mark origin with <style> to make content be observed by incorporateUserChanges
|
|
2829
|
+
const selection = this.getSelection();
|
|
2830
|
+
if (selection) {
|
|
2831
|
+
const firstLineSelected = this.topLevel(selection.startPoint.node);
|
|
2832
|
+
const lastLineSelected = this.topLevel(selection.endPoint.node);
|
|
2833
|
+
if (firstLineSelected && lastLineSelected) {
|
|
2834
|
+
const lineBeforeSelection = firstLineSelected.previousSibling;
|
|
2835
|
+
const lineAfterSelection = lastLineSelected.nextSibling;
|
|
2836
|
+
const neighbor = lineBeforeSelection || lineAfterSelection;
|
|
2837
|
+
if (neighbor) {
|
|
2838
|
+
neighbor.appendChild(this.targetDoc.createElement('style'));
|
|
2839
|
+
}
|
|
2840
|
+
}
|
|
2841
|
+
}
|
|
2842
|
+
this.idleWorkTimer.atMost(0);
|
|
2843
|
+
});
|
|
2844
|
+
// Composition events
|
|
2845
|
+
this.targetBody.addEventListener('compositionstart', () => {
|
|
2846
|
+
if (this.inInternationalComposition)
|
|
2847
|
+
return;
|
|
2848
|
+
this.inInternationalComposition = new Promise((resolve) => {
|
|
2849
|
+
this.targetBody.addEventListener('compositionend', () => {
|
|
2850
|
+
this.inInternationalComposition = null;
|
|
2851
|
+
resolve();
|
|
2852
|
+
// After composition ends, incorporate changes
|
|
2853
|
+
this.idleWorkTimer.atMost(0);
|
|
2854
|
+
}, { once: true });
|
|
2855
|
+
});
|
|
2856
|
+
});
|
|
2857
|
+
}
|
|
2858
|
+
}
|
|
2859
|
+
// -----------------------------------------------------------------------
|
|
2860
|
+
// Constants
|
|
2861
|
+
// -----------------------------------------------------------------------
|
|
2862
|
+
AceEditor.THE_TAB = ' '; // 4 spaces
|
|
2863
|
+
AceEditor.MAX_LIST_LEVEL = 16;
|
|
2864
|
+
AceEditor.STYLE_ATTRIBS = {
|
|
2865
|
+
bold: true,
|
|
2866
|
+
italic: true,
|
|
2867
|
+
underline: true,
|
|
2868
|
+
strikethrough: true,
|
|
2869
|
+
list: true,
|
|
2870
|
+
};
|
|
2871
|
+
AceEditor._blockElems = {
|
|
2872
|
+
div: 1,
|
|
2873
|
+
p: 1,
|
|
2874
|
+
pre: 1,
|
|
2875
|
+
li: 1,
|
|
2876
|
+
ol: 1,
|
|
2877
|
+
ul: 1,
|
|
2878
|
+
};
|
|
2879
|
+
//# sourceMappingURL=AceEditor.js.map
|