From 5ae8f4764ce26f3260735c482056f1060a3c5d24 Mon Sep 17 00:00:00 2001 From: iseulde Date: Tue, 20 Nov 2018 00:29:54 +0100 Subject: [PATCH] Add comment --- packages/editor/src/components/rich-text/index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/editor/src/components/rich-text/index.js b/packages/editor/src/components/rich-text/index.js index 8d35ac3e7aeb2e..cfedef9af0e35f 100644 --- a/packages/editor/src/components/rich-text/index.js +++ b/packages/editor/src/components/rich-text/index.js @@ -177,6 +177,20 @@ export class RichText extends Component { createRecord() { const range = getSelection().getRangeAt( 0 ); + // If the range is shallowly equal to the last, return the last + // calculated value. This is useful when creating values on the + // `selectionchange` event, which fires more often than it should + // without a change in selection. + // + // Shallowly checking equality for ranges is fine: + // + // > Thus subsequent calls of this method returns the same range object + // > if nothing has removed the context object's range in the meantime. + // > In particular, getSelection().getRangeAt( 0 ) === + // > getSelection().getRangeAt( 0 ) evaluates to true if the selection + // > is not empty. + // > + // > https://w3c.github.io/selection-api/#dom-selection-getrangeat if ( range === this.createRecord.lastRange ) { return this.createRecord.lastRecord; }