Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions core/emitter.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import EventEmitter from 'eventemitter3';
import logger from './logger';
import { SHADOW_SELECTIONCHANGE } from './shadow-selection-polyfill';

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);

EVENTS.forEach(function(eventName) {
document.addEventListener(eventName, (...args) => {
[].slice.call(document.querySelectorAll('.ql-container')).forEach((node) => {
// TODO use WeakMap
if (node.__quill && node.__quill.emitter) {
node.__quill.emitter.handleDOM(...args);
}
EMITTERS.forEach((em) => {
em.handleDOM(...args);
});
});
});
Expand All @@ -21,6 +21,7 @@ class Emitter extends EventEmitter {
constructor() {
super();
this.listeners = {};
EMITTERS.push(this);
this.on('error', debug.error);
}

Expand All @@ -30,8 +31,25 @@ class Emitter extends EventEmitter {
}

handleDOM(event, ...args) {
const target = (event.composedPath ? event.composedPath()[0] : event.target);
const containsNode = (node, target) => {
if (!supportsRootNode || target.getRootNode() === document) {
return node.contains(target);
}

while (!node.contains(target)) {
const root = target.getRootNode();
if (!root || !root.host) {
return false;
}
target = root.host;
}

return true;
};

(this.listeners[event.type] || []).forEach(function({ node, handler }) {
if (event.target === node || node.contains(event.target)) {
if (target === node || containsNode(node, target)) {
handler(event, ...args);
}
});
Expand Down
12 changes: 6 additions & 6 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 } from './shadow-selection-polyfill';

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

Expand All @@ -22,12 +23,13 @@ class Selection {
this.composing = false;
this.mouseDown = false;
this.root = this.scroll.domNode;
this.rootDocument = (this.root.getRootNode ? this.root.getRootNode() : document);
this.cursor = Parchment.create('cursor', this);
// savedRange is last non-null range
this.lastRange = this.savedRange = new Range(0, 0);
this.handleComposition();
this.handleDragging();
this.emitter.listenDOM('selectionchange', document, () => {
this.emitter.listenDOM(SHADOW_SELECTIONCHANGE, document, () => {
if (!this.mouseDown) {
setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
}
Expand Down Expand Up @@ -157,9 +159,7 @@ class Selection {
}

getNativeRange() {
let selection = document.getSelection();
if (selection == null || selection.rangeCount <= 0) return null;
let nativeRange = selection.getRangeAt(0);
let nativeRange = getRange(this.rootDocument);
if (nativeRange == null) return null;
let range = this.normalizeNative(nativeRange);
debug.info('getNativeRange', range);
Expand All @@ -174,7 +174,7 @@ class Selection {
}

hasFocus() {
return document.activeElement === this.root;
return this.rootDocument.activeElement === this.root;
}

normalizedToRange(range) {
Expand Down Expand Up @@ -268,7 +268,7 @@ class Selection {
if (startNode != null && (this.root.parentNode == null || startNode.parentNode == null || endNode.parentNode == null)) {
return;
}
let selection = document.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
Loading