Skip to content

Commit

Permalink
Add getSelection() polyfill for Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored and nelsonsilva committed Feb 21, 2020
1 parent 7552872 commit fc40d07
Show file tree
Hide file tree
Showing 3 changed files with 370 additions and 12 deletions.
3 changes: 2 additions & 1 deletion core/emitter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import EventEmitter from 'eventemitter3';
import instances from './instances';
import logger from './logger';
import { SHADOW_SELECTIONCHANGE } from './shadow-selection-polyfill';

const debug = logger('quill:events');
const EVENTS = ['selectionchange', 'mousedown', 'mouseup', 'click'];
const EVENTS = [SHADOW_SELECTIONCHANGE, 'mousedown', 'mouseup', 'click'];
const EMITTERS = [];

EVENTS.forEach(eventName => {
Expand Down
22 changes: 11 additions & 11 deletions core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import clone from 'clone';
import equal from 'deep-equal';
import Emitter from './emitter';
import logger from './logger';
import { SHADOW_SELECTIONCHANGE, getRange, addRange, usePolyfill } from './shadow-selection-polyfill';

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

Expand All @@ -27,11 +28,13 @@ class Selection {
this.lastRange = this.savedRange;
this.handleComposition();
this.handleDragging();
this.emitter.listenDOM('selectionchange', this.rootDocument, () => {
if (!this.mouseDown && !this.composing) {
setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
}
});
if (!usePolyfill) {
this.emitter.listenDOM(SHADOW_SELECTIONCHANGE, document, () => {
if (!this.mouseDown && !this.composing) {
setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
}
});
}
this.emitter.on(Emitter.events.SCROLL_BEFORE_UPDATE, () => {
if (!this.hasFocus()) return;
const native = this.getNativeRange();
Expand Down Expand Up @@ -176,9 +179,7 @@ class Selection {
}

getNativeRange() {
const selection = this.rootDocument.getSelection();
if (selection == null || selection.rangeCount <= 0) return null;
const nativeRange = selection.getRangeAt(0);
let nativeRange = getRange(this.rootDocument);
if (nativeRange == null) return null;
const range = this.normalizeNative(nativeRange);
debug.info('getNativeRange', range);
Expand Down Expand Up @@ -317,7 +318,7 @@ class Selection {
) {
return;
}
const selection = this.rootDocument.getSelection();
let selection = typeof this.rootDocument.getSelection === 'function' ? this.rootDocument.getSelection() : document.getSelection();
if (selection == null) return;
if (startNode != null) {
if (!this.hasFocus()) this.root.focus();
Expand Down Expand Up @@ -345,8 +346,7 @@ class Selection {
const range = document.createRange();
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
selection.removeAllRanges();
selection.addRange(range);
addRange(this.rootDocument, selection, range);
}
} else {
selection.removeAllRanges();
Expand Down
Loading

0 comments on commit fc40d07

Please sign in to comment.