Skip to content

Commit

Permalink
Do not add global update listener if polyfill is used
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan committed Nov 15, 2018
1 parent 45ff970 commit 95b8374
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 8 additions & 6 deletions core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import clone from 'clone';
import equal from 'deep-equal';
import Emitter from './emitter';
import logger from './logger';
import { SHADOW_SELECTIONCHANGE, getRange, addRange } from './shadow-selection-polyfill';
import { SHADOW_SELECTIONCHANGE, getRange, addRange, usePolyfill } from './shadow-selection-polyfill';

let debug = logger('quill:selection');

Expand All @@ -29,11 +29,13 @@ class Selection {
this.lastRange = this.savedRange = new Range(0, 0);
this.handleComposition();
this.handleDragging();
this.emitter.listenDOM(SHADOW_SELECTIONCHANGE, document, () => {
if (!this.mouseDown) {
setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
}
});
if (!usePolyfill) {
this.emitter.listenDOM(SHADOW_SELECTIONCHANGE, document, () => {
if (!this.mouseDown) {
setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
}
});
}
this.emitter.on(Emitter.events.EDITOR_CHANGE, (type, delta) => {
if (type === Emitter.events.TEXT_CHANGE && delta.length() > 0) {
this.update(Emitter.sources.SILENT);
Expand Down
8 changes: 5 additions & 3 deletions core/shadow-selection-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent) ||
/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
const useDocument = !hasShadow || hasShady || (!hasSelection && !isSafari);

export const usePolyfill = !(hasSelection || useDocument);

const validNodeTypes = [Node.ELEMENT_NODE, Node.TEXT_NODE, Node.DOCUMENT_FRAGMENT_NODE];
function isValidNode(node) {
return validNodeTypes.includes(node.nodeType);
Expand Down Expand Up @@ -57,7 +59,7 @@ function findNode(s, parentNode, isLeft) {
* @param {function(!Event)} fn to add to selectionchange internals
*/
const addInternalListener = (() => {
if (hasSelection || useDocument) {
if (!usePolyfill) {
// getSelection exists or document API can be used
document.addEventListener('selectionchange', () => {
document.dispatchEvent(new CustomEvent(SHADOW_SELECTIONCHANGE));
Expand All @@ -76,7 +78,7 @@ const addInternalListener = (() => {
withinInternals = true;
window.setTimeout(() => {
withinInternals = false;
}, 2); // FIXME: should be > 1 to prevent infinite Selection.update() loop
}, 0);
handlers.forEach((fn) => fn(ev));
});

Expand Down Expand Up @@ -234,7 +236,7 @@ export function addRange(root, selection, range) {
}

export function getRange(root) {
if (hasSelection || useDocument) {
if (!usePolyfill) {
const s = (useDocument ? document : root).getSelection();
return s.rangeCount ? s.getRangeAt(0) : null;
}
Expand Down

0 comments on commit 95b8374

Please sign in to comment.