| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 | 1×
1×
1×
1×
4170×
1×
539×
539×
1×
538×
538×
538×
538×
2×
536×
1×
644×
2×
642×
642×
642×
642×
642×
642×
1×
1072×
1072×
1072×
1072×
1072×
1072×
1×
1284×
1284×
1284×
1284×
1284×
1638×
1638×
1284×
354×
354×
1×
809×
1×
103×
103×
103×
1×
34×
1×
206×
1×
155×
155×
1×
1983×
1983×
1×
2×
2×
1×
1×
1×
1×
59×
59×
1×
58×
58×
1×
664×
664×
664×
664×
664×
664×
664×
1×
5×
5×
1×
4×
4×
4×
4×
3×
1×
1×
65×
65×
1×
64×
64×
64×
1×
63×
14×
49×
7×
42×
42×
42×
42×
42×
42×
1×
458×
458×
1×
457×
457×
457×
457×
457×
457×
457×
457×
457×
457×
457×
457×
457×
20×
437×
437×
437×
1×
1×
| define(["require", "exports", "../common/utils/index", "../common/constant", "../common/utils/dom"], function (require, exports, index_1, constants, dom_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var NodeSelection = (function () {
function NodeSelection(wrapper) {
this.editorWrapper = wrapper;
}
NodeSelection.prototype.saveSelection = function () {
var range = this.getRange();
if (!range) {
return;
}
this.currentRange = range.cloneRange();
var startBlockElement = dom_1.findClosestParent(range.startContainer, ('.' + constants.BLOCK_CLS));
var endBlockElement = dom_1.findClosestParent(range.endContainer, ('.' + constants.BLOCK_CLS));
if (!startBlockElement || !endBlockElement) {
return;
}
this.savedSelectionState = {
startBlockId: startBlockElement.id,
endBlockId: endBlockElement.id,
startOffset: this.calculateOffset(range, index_1.getBlockContentElement(startBlockElement), true),
endOffset: this.calculateOffset(range, index_1.getBlockContentElement(endBlockElement), false)
};
};
NodeSelection.prototype.restoreSelection = function () {
if (!this.savedSelectionState) {
return;
}
var startBlockElement = this.editorWrapper.querySelector('#' + this.savedSelectionState.startBlockId);
var endBlockElement = this.editorWrapper.querySelector('#' + this.savedSelectionState.endBlockId);
var startInfo = this.findNodeAndOffsetFromTextPosition(index_1.getBlockContentElement(startBlockElement), this.savedSelectionState.startOffset);
var endInfo = this.findNodeAndOffsetFromTextPosition(index_1.getBlockContentElement(endBlockElement), this.savedSelectionState.endOffset);
Eif (startInfo && endInfo) {
this.createRangeWithOffsets(startInfo.node, endInfo.node, startInfo.offset, endInfo.offset);
}
};
NodeSelection.prototype.calculateOffset = function (globalRange, contentElement, isStart) {
var node = isStart ? globalRange.startContainer : globalRange.endContainer;
var offset = isStart ? globalRange.startOffset : globalRange.endOffset;
var range = document.createRange();
range.selectNodeContents(contentElement);
range.setEnd(node, offset);
return range.toString().length;
};
NodeSelection.prototype.findNodeAndOffsetFromTextPosition = function (container, targetOffset) {
Iif (targetOffset > (container.textContent.length)) {
targetOffset = container.textContent.length;
}
var treeWalker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT, null);
var currentOffset = 0;
var currentNode = treeWalker.nextNode();
while (currentNode) {
var nodeLength = currentNode.textContent.length;
if (currentOffset + nodeLength >= targetOffset) {
return {
node: currentNode,
offset: targetOffset - currentOffset
};
}
currentOffset += nodeLength;
currentNode = treeWalker.nextNode();
}
var lastNode = container.lastChild;
if (lastNode && lastNode.nodeType === Node.TEXT_NODE) {
return {
node: lastNode,
offset: lastNode.textContent.length || 0
};
}
return { node: container, offset: 0 };
};
NodeSelection.prototype.getSelection = function () {
return window.getSelection();
};
NodeSelection.prototype.clearSelection = function () {
var sel = this.getSelection();
Eif (sel) {
sel.removeAllRanges();
}
};
NodeSelection.prototype.getStoredRange = function () {
return this.currentRange;
};
NodeSelection.prototype.getStoredBackupRange = function () {
return this.rangeBackup;
};
NodeSelection.prototype.storeCurrentRange = function () {
this.currentRange = this.getRange();
this.rangeBackup = {
startContainer: this.currentRange.startContainer,
startOffset: this.currentRange.startOffset,
endContainer: this.currentRange.endContainer,
endOffset: this.currentRange.endOffset,
parentElement: index_1.getClosestContentElementInDocument(this.currentRange.startContainer)
};
};
NodeSelection.prototype.getRange = function () {
var selection = window.getSelection();
return selection && selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
};
NodeSelection.prototype.getSelectionPosition = function () {
var range = this.getRange();
if (!range) {
return { x: 0, y: 0 };
}
var rect = range.getBoundingClientRect();
return {
x: rect.left,
y: rect.bottom + window.scrollY + 10
};
};
NodeSelection.prototype.isCollapsed = function () {
var selection = this.getSelection();
return !selection || selection.isCollapsed;
};
NodeSelection.prototype.getSelectedText = function () {
var selection = this.getSelection();
return selection ? selection.toString() : '';
};
NodeSelection.prototype.createRangeWithOffsets = function (startNode, endNode, startOffset, endOffset) {
var selection = window.getSelection();
var range = document.createRange();
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
selection.removeAllRanges();
selection.addRange(range);
return range;
};
NodeSelection.prototype.selectionContainsNodeType = function (tagName, container) {
var selection = this.getSelection();
if (!selection) {
return false;
}
var range = selection.getRangeAt(0);
var nodes = container.querySelectorAll(tagName);
for (var i = 0; i < nodes.length; i++) {
if (range.intersectsNode(nodes[parseInt(i.toString(), 10)])) {
return true;
}
}
return false;
};
NodeSelection.prototype.getNodeFromSelection = function (tagName) {
var selection = this.getSelection();
if (!selection || selection.rangeCount === 0) {
return null;
}
var range = selection.getRangeAt(0);
var commonAncestor = range.commonAncestorContainer;
if (commonAncestor.nodeType === Node.ELEMENT_NODE &&
commonAncestor.tagName.toLowerCase() === tagName.toLowerCase()) {
return commonAncestor;
}
if (commonAncestor.nodeType === Node.TEXT_NODE &&
commonAncestor.parentElement &&
commonAncestor.parentElement.tagName.toLowerCase() === tagName.toLowerCase()) {
return commonAncestor.parentElement;
}
if (commonAncestor.nodeType === Node.ELEMENT_NODE) {
return commonAncestor.querySelector(tagName);
}
var startContainer = range.startContainer;
var endContainer = range.endContainer;
Eif (startContainer && endContainer) {
var startElement = startContainer.nodeType === Node.ELEMENT_NODE
? startContainer
: startContainer.parentElement;
var endElement = endContainer.nodeType === Node.ELEMENT_NODE
? endContainer
: endContainer.parentElement;
return (startElement.closest(tagName) || endElement.closest(tagName));
}
return null;
};
NodeSelection.prototype.checkIsEntireEditorSelected = function () {
var selection = this.getSelection();
if (!selection || selection.rangeCount === 0) {
return false;
}
var range = index_1.getSelectedRange();
Iif (!range) {
return false;
}
var firstBlockElement = this.editorWrapper.firstElementChild;
var lastBlockElement = this.editorWrapper.lastElementChild;
var firstBlockContent = index_1.getBlockContentElement(firstBlockElement);
var lastBlockContent = index_1.getBlockContentElement(lastBlockElement);
var startContainer = range.startContainer;
var endContainer = range.endContainer;
var isFirstBlockEmpty = firstBlockContent.textContent.trim() === '';
var isLastBlockEmpty = lastBlockContent.textContent.trim() === '';
var firstBlockStartNode = firstBlockContent.childNodes[0];
var lastBlockEndNode = lastBlockContent.childNodes[lastBlockContent.childNodes.length - 1];
if (startContainer.nodeType === Node.ELEMENT_NODE && endContainer.nodeType === Node.ELEMENT_NODE &&
startContainer.classList.contains(constants.BLOCK_CONTAINER_CLS) &&
endContainer.classList.contains(constants.BLOCK_CONTAINER_CLS)) {
return true;
}
var isEqualsStartContainer = (firstBlockStartNode && firstBlockStartNode.contains(startContainer) ||
isFirstBlockEmpty && firstBlockElement.contains(startContainer));
var isEqualsEndContainer = (lastBlockEndNode && lastBlockEndNode.contains(endContainer) ||
isLastBlockEmpty && lastBlockElement.contains(endContainer));
return (isEqualsStartContainer &&
isEqualsEndContainer &&
range.startOffset === 0 &&
range.endOffset === endContainer.textContent.length);
};
return NodeSelection;
}());
exports.NodeSelection = NodeSelection;
});
|