Skip to content

Commit 1775c04

Browse files
committed
#9 - Range with trim refinements + log cleanup
1 parent 91deb60 commit 1775c04

File tree

4 files changed

+22
-36
lines changed

4 files changed

+22
-36
lines changed

src/scripts/modules/ContentEditable.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ const ContentEditable = Module({
152152
} else {
153153
let currentSelection = mediator.get('selection:current');
154154
let currentRange = mediator.get('selection:range');
155-
if (!currentSelection) {
156-
console.log('insertHTML:currentSelection');
157-
}
158155
currentRange.deleteContents();
159156

160157
let tmpContainer = document.createElement('container');

src/scripts/modules/Selection.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,6 @@ const Selection = Module({
187187
const { props } = this;
188188
const currentSelection = this.getCurrentSelection();
189189
let currentRange;
190-
if (!currentSelection) {
191-
console.log('getCurrentRange:currentSelection', currentSelection);
192-
}
193190

194191
if (this.validateSelection(currentSelection)) {
195192
currentRange = currentSelection.getRangeAt(0);
@@ -204,17 +201,11 @@ const Selection = Module({
204201

205202
getAnchorNode () {
206203
const currentSelection = this.getCurrentSelection();
207-
if (!currentSelection) {
208-
console.log('getAnchorNode:currentSelection', currentSelection);
209-
}
210204
return currentSelection && currentSelection.anchorNode;
211205
},
212206

213207
getCommonAncestor () {
214208
const currentSelection = this.getCurrentSelection();
215-
if (!currentSelection) {
216-
console.log('getCommonAncestor:currentSelection', currentSelection);
217-
}
218209
if (currentSelection.rangeCount > 0) {
219210
const selectionRange = currentSelection.getRangeAt(0);
220211
return selectionRange.commonAncestorContainer;
@@ -310,10 +301,14 @@ const Selection = Module({
310301
const endTrimmableSuffix = endContainer.textContent.match(/(\r?\n|\r)?(\s+)?$/);
311302
const startElement = DOM.closestElement(startContainer);
312303
const endElement = DOM.closestElement(endContainer);
304+
let startPrefixTrimLength = 0;
305+
let endPrefixTrimLength = 0;
306+
let endSuffixTrimLength = 0;
313307

314308
console.log({
315309
startTrimmablePrefix,
316310
endTrimmablePrefix,
311+
endTrimmableSuffix,
317312
startPrefixLength: startTrimmablePrefix && startTrimmablePrefix[0].length,
318313
endPrefixLength: endTrimmablePrefix && endTrimmablePrefix[0].length,
319314
startContent: startContainer.textContent,
@@ -324,24 +319,32 @@ const Selection = Module({
324319
});
325320

326321
if (startTrimmablePrefix && startTrimmablePrefix[0].length) {
327-
startOffset -= startTrimmablePrefix[0].length;
322+
startPrefixTrimLength += startTrimmablePrefix[0].length;
328323
if (DOM.nodeIsInline(startElement)) {
329-
startOffset -= startTrimmablePrefix[0].match(/\s/) ? 1 : 0;
324+
startPrefixTrimLength -= startTrimmablePrefix[0].match(/\s/) ? 1 : 0;
325+
}
326+
startOffset -= startPrefixTrimLength;
327+
if (endContainer === startContainer) {
328+
endOffset -= startPrefixTrimLength;
330329
}
331330
}
332331

333-
if (endTrimmablePrefix && endTrimmablePrefix[0].length) {
334-
endOffset -= endTrimmablePrefix[0].length;
332+
if (endTrimmablePrefix && endTrimmablePrefix[0].length && endContainer !== startContainer) {
333+
endPrefixTrimLength += endTrimmablePrefix[0].length;
335334
if (DOM.nodeIsInline(endElement)) {
336-
endOffset -= endTrimmablePrefix[0].match(/\s/) ? 1 : 0;
335+
endPrefixTrimLength -= endTrimmablePrefix[0].match(/\s/) ? 1 : 0;
337336
}
337+
endOffset -= endPrefixTrimLength;
338338
}
339339

340340
if (endTrimmableSuffix && endTrimmableSuffix[0].length) {
341-
// endOffset -= endTrimmableSuffix[0].length;
341+
endSuffixTrimLength += endTrimmableSuffix[0].length;
342342
if (DOM.nodeIsInline(endElement)) {
343-
endOffset -= endTrimmableSuffix[0].match(/\s/) ? 1 : 0;
343+
endSuffixTrimLength -= endTrimmableSuffix[0].match(/\s/) ? 1 : 0;
344344
}
345+
let trimmedTextLength = endContainer.textContent.length - endPrefixTrimLength - endSuffixTrimLength;
346+
console.log('>>', { endOffset, trimmedTextLength, textLength: endContainer.textContent.length, endPrefixTrimLength, endSuffixTrimLength });
347+
endOffset = Math.min(trimmedTextLength, endOffset);
345348
}
346349

347350
console.log({
@@ -402,9 +405,7 @@ const Selection = Module({
402405
const currentSelection = this.getCurrentSelection();
403406
let { anchorNode, focusNode } = currentSelection;
404407
const selectionContainsNode = currentSelection.containsNode(node, true);
405-
if (!currentSelection) {
406-
console.log('containsNode:currentSelection', currentSelection);
407-
}
408+
408409
if (!currentSelection.rangeCount) {
409410
return false;
410411
}
@@ -416,6 +417,7 @@ const Selection = Module({
416417
if (anchorNode.nodeType !== Node.ELEMENT_NODE) {
417418
anchorNode = anchorNode.parentNode;
418419
}
420+
419421
if (focusNode.nodeType !== Node.ELEMENT_NODE) {
420422
focusNode = focusNode.parentNode;
421423
}
@@ -538,9 +540,7 @@ const Selection = Module({
538540
updateRange (range, opts={}) {
539541
const { mediator, props } = this;
540542
const currentSelection = this.getCurrentSelection();
541-
if (!currentSelection) {
542-
console.log('updateRange:currentSelection', currentSelection);
543-
}
543+
544544
if (opts.silent) {
545545
props.silenceChanges.push(true); // silence removeAllRanges
546546
props.silenceChanges.push(true); // silence addRange

src/scripts/utils/DOM.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,9 @@ const DOM = {
253253

254254
closestElement(node) {
255255
let returnNode = node;
256-
console.log('closestElement', node);
257256

258257
while (returnNode && returnNode.nodeType !== 1) {
259258
returnNode = returnNode.parentNode;
260-
console.log('returnNode', returnNode);
261259
}
262260

263261
return returnNode;

test/unit/modules/ListFormatter.spec.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,22 @@ describe('modules/ListFormatter', function () {
4040
afterEach(() => {
4141
editableEl.innerHTML = '';
4242
mediator.emit('app:destroy');
43-
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
4443
});
4544

4645
it('should toggle ordered lists', () => {
4746
setEditableElHTML();
4847
expect(editableEl.getElementsByTagName('ol').length).toBe(0);
4948
expect(editableEl.getElementsByTagName('li').length).toBe(0);
5049

51-
console.log('---------------------------------------------------------------------------------------[2:start]')
5250
selectionHelper.selectFirstAndLastTextNodes(editableEl);
53-
console.log(document.getSelection().toString());
5451
mediator.exec('format:list', orderedListOpts);
55-
console.log('---------------------------------------------------------------------------------------[2:end]')
5652
expect(editableEl.getElementsByTagName('ol').length).toBe(1);
5753
expect(editableEl.getElementsByTagName('li').length).toBe(5);
5854

59-
console.log('---------------------------------------------------------------------------------------[2:start]')
6055
selectionHelper.selectFirstAndLastTextNodes(editableEl);
61-
console.log(document.getSelection().toString());
6256
mediator.exec('format:list', orderedListOpts);
63-
console.log('---------------------------------------------------------------------------------------[2:end]')
6457
expect(editableEl.getElementsByTagName('ol').length).toBe(0);
6558
expect(editableEl.getElementsByTagName('li').length).toBe(0);
66-
console.log('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
67-
6859
});
6960

7061
it('should toggle unordered lists', () => {

0 commit comments

Comments
 (0)