-
Notifications
You must be signed in to change notification settings - Fork 40
T/629b Alternative fix infinite selection loop. #671
Changes from 13 commits
f4c0d25
b9d6bc2
1b39bfb
d2d1e73
a656829
d80b367
317d3dc
f220109
a83c2ab
6b6da0c
3768f04
3dc704c
a8e53ca
dcf65b8
0de2bf7
25b8ba2
a3690d5
b1e0e1f
adcdbdc
6a2418b
7ccaa13
bf9fc9f
bf87840
6a9306e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,25 +125,29 @@ export default class Selection { | |
} | ||
|
||
/** | ||
* Checks whether, this selection is equal to given selection. Selections equal if they have the same ranges and directions. | ||
* Checks whether this selection is equal to given selection. Selections are equal if they have same directions, | ||
* same number of ranges and all ranges from one selection equal to a range from other selection. | ||
* | ||
* @param {engine.model.Selection} otherSelection Selection to compare with. | ||
* @returns {Boolean} `true` if selections are equal, `false` otherwise. | ||
*/ | ||
isEqual( otherSelection ) { | ||
const rangeCount = this.rangeCount; | ||
|
||
if ( rangeCount != otherSelection.rangeCount ) { | ||
if ( !this.anchor.isEqual( otherSelection.anchor ) || !this.focus.isEqual( otherSelection.focus ) ) { | ||
return false; | ||
} | ||
|
||
for ( let i = 0; i < this.rangeCount; i++ ) { | ||
if ( !this._ranges[ i ].isEqual( otherSelection._ranges[ i ] ) ) { | ||
return false; | ||
} | ||
if ( this.rangeCount != otherSelection.rangeCount ) { | ||
return false; | ||
} | ||
|
||
return this.isBackward === otherSelection.isBackward; | ||
// Every range from this selection... | ||
return Array.from( this.getRanges() ).every( ( rangeA ) => { | ||
// ...Has a range in other selection... | ||
return Array.from( otherSelection.getRanges() ).some( ( rangeB ) => { | ||
// That it is equal to. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return rangeA.isEqual( rangeB ); | ||
} ); | ||
} ); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/* global setInterval */ | ||
|
||
import Observer from './observer.js'; | ||
import MutationObserver from './mutationobserver.js'; | ||
|
||
|
@@ -103,6 +105,8 @@ export default class SelectionObserver extends Observer { | |
|
||
domDocument.addEventListener( 'selectionchange', () => this._handleSelectionChange( domDocument ) ); | ||
|
||
setInterval( () => this._clearInfiniteLoop(), 2000 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This interval needs to be cleaned on |
||
|
||
this._documents.add( domDocument ); | ||
} | ||
|
||
|
@@ -151,10 +155,6 @@ export default class SelectionObserver extends Observer { | |
newSelection: newViewSelection, | ||
domSelection: domSelection | ||
} ); | ||
|
||
// If nothing changes on `selectionChange` event, at this point we have "dirty DOM" (changed) and de-synched | ||
// view (which has not been changed). In order to "reset DOM" we render the view again. | ||
this.document.render(); | ||
} | ||
|
||
/** | ||
|
@@ -179,12 +179,23 @@ export default class SelectionObserver extends Observer { | |
this._loopbackCounter = 0; | ||
} | ||
|
||
if ( this._loopbackCounter > 10 ) { | ||
if ( this._loopbackCounter > 50 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deserves some comment that you empirically tested that making more than 50 selection changes in 2s is not possible. BTW. What about making selections by dragging over the text? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The selection has to be same as previous or pre-previous selection, so you would have to drag precisly, which means that it would be probably impossible to do 50 changes in 2 secs. |
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Clears `SelectionObserver` internal properties connected with preventing infinite loop. | ||
* | ||
* @protected | ||
*/ | ||
_clearInfiniteLoop() { | ||
this._lastSelection = null; | ||
this._lastButOneSelection = null; | ||
this._loopbackCounter = 0; | ||
} | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,33 +283,39 @@ export default class Selection { | |
} | ||
|
||
/** | ||
* Checks whether, this selection is equal to given selection. Selections equal if they have the same ranges and directions. | ||
* Checks whether, this selection is equal to given selection. Selections are equal if they have same directions, | ||
* same number of ranges and all ranges from one selection equal to a range from other selection. | ||
* | ||
* @param {engine.view.Selection} otherSelection Selection to compare with. | ||
* @returns {Boolean} `true` if selections are equal, `false` otherwise. | ||
*/ | ||
isEqual( otherSelection ) { | ||
const rangeCount = this.rangeCount; | ||
|
||
if ( rangeCount != otherSelection.rangeCount ) { | ||
if ( this.isFake != otherSelection.isFake ) { | ||
return false; | ||
} | ||
|
||
if ( this.isFake != otherSelection.isFake ) { | ||
if ( this.isFake && this.fakeSelectionLabel != otherSelection.fakeSelectionLabel ) { | ||
return false; | ||
} | ||
|
||
if ( this.isFake && this.fakeSelectionLabel != otherSelection.fakeSelectionLabel ) { | ||
if ( this.rangeCount != otherSelection.rangeCount ) { | ||
return false; | ||
} else if ( this.rangeCount === 0 ) { | ||
return true; | ||
} | ||
|
||
for ( let i = 0; i < this.rangeCount; i++ ) { | ||
if ( !this._ranges[ i ].isEqual( otherSelection._ranges[ i ] ) ) { | ||
return false; | ||
} | ||
if ( !this.anchor.isEqual( otherSelection.anchor ) || !this.focus.isEqual( otherSelection.focus ) ) { | ||
return false; | ||
} | ||
|
||
return this._lastRangeBackward === otherSelection._lastRangeBackward; | ||
// Every range from this selection... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I see correctly that the ranges can be in a different order? Why so? Then, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Backward/forward selection check is indirectly implemented when we check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was rather asking why is this method trying to find a range in the other selection which matches range from this selection... for each range in this selection? Moreover, this implementation is very ineffective, cause it creates new array on every call of Therefore, I've been asking about the order of ranges (whether it shouldn't be the same in both selections) because the easiest possible implementation is to loop through both selections' ranges and compare the pairs. Not only this will be much faster, but also cleaner. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I see. Ranges are not sorted upon insertion. This means that we have to check whether every range in selection A has a matching range in selection B the way we do. If the ranges are ordered that would be easier. It is a matter of us deciding whether the order range is important. Being strict, I think that we should not care about the ranges order, because from "outside" selection: I can change the implementation but... To be honest selection will usually have just one range, except of tables where it may be more but in 99% scenarios it will will be less than 10-20 ranges. I agree with changing so the arrays are not created. If you want to change "order logic" I'm fine with that as well, just confirm please whether we are doing it or leaving it as is.
So, instead one operation we have three? And it is me who get complaints about premature optimization? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
BTW. I don't know if you haven't got too confused... For each range in selection A, we check if there is a matching range in selection B. The only way we can "speed" this up is if we already have sorted arrays... (or care about the order). |
||
return Array.from( this.getRanges() ).every( ( rangeA ) => { | ||
// ...Has a range in other selection... | ||
return Array.from( otherSelection.getRanges() ).some( ( rangeB ) => { | ||
// That it is equal to. | ||
return rangeA.isEqual( rangeB ); | ||
} ); | ||
} ); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<head> | ||
<link rel="stylesheet" href="%APPS_DIR%ckeditor/build/modules/amd/theme/ckeditor.css"> | ||
</head> | ||
|
||
<div id="editor"> | ||
<p>Lorem ipsum dolor sit amet.</p> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/* globals console, window, document */ | ||
|
||
import ClassicEditor from '/ckeditor5/editor-classic/classic.js'; | ||
import Enter from '/ckeditor5/enter/enter.js'; | ||
import Typing from '/ckeditor5/typing/typing.js'; | ||
import Paragraph from '/ckeditor5/paragraph/paragraph.js'; | ||
import Bold from '/ckeditor5/basic-styles/bold.js'; | ||
|
||
ClassicEditor.create( document.querySelector( '#editor' ), { | ||
features: [ Enter, Typing, Paragraph, Bold ], | ||
toolbar: [ 'bold' ] | ||
} ) | ||
.then( editor => { | ||
window.editor = editor; | ||
} ) | ||
.catch( err => { | ||
console.error( err.stack ); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@bender-ui: collapsed | ||
@bender-tags: ticket, 629, iteration4 | ||
|
||
### Selection infinite loop [#629](https://github.com/ckeditor/ckeditor5-engine/issues/629) | ||
|
||
Before testing open console. | ||
|
||
1. Select part of text. | ||
2. Click bold or press <kbd>ctrl + b</kbd>. | ||
3. There should be no errors or warnings in console. | ||
|
||
|
||
1. Put caret somewhere in text. | ||
2. Press <kbd>left arrow</kbd>, then <kbd>right arrow</kbd>. | ||
3. Repeat as fast as you can for 2-3 seconds. | ||
4. There should be no errors or warnings in console. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// ... has
:P