Skip to content

Commit

Permalink
Add getSelection() polyfill for Safari shadow DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan committed Oct 3, 2018
1 parent 2186896 commit c6a20bb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import logger from './logger';

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

const EVENTS = ['selectionchange', 'mousedown', 'mouseup', 'click'];
const EVENTS = ['-shadow-selectionchange', 'mousedown', 'mouseup', 'click'];
const EMITTERS = [];
const supportsRootNode = ('getRootNode' in document);

Expand Down
9 changes: 4 additions & 5 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 * as shadow from 'shadow-selection-polyfill';

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

Expand All @@ -28,7 +29,7 @@ class Selection {
this.lastRange = this.savedRange = new Range(0, 0);
this.handleComposition();
this.handleDragging();
this.emitter.listenDOM('selectionchange', this.rootDocument, () => {
this.emitter.listenDOM('-shadow-selectionchange', document, () => {
if (!this.mouseDown) {
setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
}
Expand Down Expand Up @@ -158,9 +159,7 @@ class Selection {
}

getNativeRange() {
let selection = this.rootDocument.getSelection();
if (selection == null || selection.rangeCount <= 0) return null;
let nativeRange = selection.getRangeAt(0);
let nativeRange = shadow.getRange(this.rootDocument);
if (nativeRange == null) return null;
let range = this.normalizeNative(nativeRange);
debug.info('getNativeRange', range);
Expand Down Expand Up @@ -269,7 +268,7 @@ class Selection {
if (startNode != null && (this.root.parentNode == null || startNode.parentNode == null || endNode.parentNode == null)) {
return;
}
let 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"eventemitter3": "^2.0.3",
"extend": "^3.0.1",
"parchment": "^1.1.4",
"quill-delta": "^3.6.2"
"quill-delta": "^3.6.2",
"shadow-selection-polyfill": "^1.0.0"
},
"devDependencies": {
"babel-core": "^6.26.0",
Expand Down

0 comments on commit c6a20bb

Please sign in to comment.