Skip to content

Commit

Permalink
merge with vanooo
Browse files Browse the repository at this point in the history
  • Loading branch information
Csonka Bálint committed Oct 13, 2019
2 parents df2cf16 + 9019e7f commit b8101f8
Show file tree
Hide file tree
Showing 11 changed files with 37,571 additions and 9 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.*
/dist
/node_modules
/selenium
/docs/_site
Expand Down
27 changes: 25 additions & 2 deletions core/emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import logger from './logger';

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

EVENTS.forEach(eventName => {
document.addEventListener(eventName, (...args) => {
Expand All @@ -13,13 +15,17 @@ EVENTS.forEach(eventName => {
quill.emitter.handleDOM(...args);
}
});
// EMITTERS.forEach((em) => {
// em.handleDOM(...args);
// });
});
});

class Emitter extends EventEmitter {
constructor() {
super();
this.listeners = {};
EMITTERS.push(this);
this.on('error', debug.error);
}

Expand All @@ -29,8 +35,25 @@ class Emitter extends EventEmitter {
}

handleDOM(event, ...args) {
(this.listeners[event.type] || []).forEach(({ node, handler }) => {
if (event.target === node || node.contains(event.target)) {
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 (target === node || containsNode(node, target)) {
handler(event, ...args);
}
});
Expand Down
14 changes: 9 additions & 5 deletions core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ class Selection {
this.mouseDown = false;
this.root = this.scroll.domNode;
this.cursor = this.scroll.create('cursor', this);
this.rootDocument = (this.root.getRootNode ? this.root.getRootNode() : document);
// console.log('Selection', this.rootDocument);
// savedRange is last non-null range
this.savedRange = new Range(0, 0);
this.lastRange = this.savedRange;
this.handleComposition();
this.handleDragging();
this.emitter.listenDOM('selectionchange', document, () => {
this.emitter.listenDOM('selectionchange', this.rootDocument, () => {
if (!this.mouseDown && !this.composing) {
setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
}
Expand Down Expand Up @@ -175,7 +177,8 @@ class Selection {
}

getNativeRange() {
const selection = document.getSelection();
// const selection = document.getSelection();
const selection = this.rootDocument.getSelection();
if (selection == null || selection.rangeCount <= 0) return null;
const nativeRange = selection.getRangeAt(0);
if (nativeRange == null) return null;
Expand All @@ -193,8 +196,8 @@ class Selection {

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

Expand Down Expand Up @@ -316,7 +319,8 @@ class Selection {
) {
return;
}
const selection = document.getSelection();
// const selection = document.getSelection();
const selection = this.rootDocument.getSelection();
if (selection == null) return;
if (startNode != null) {
if (!this.hasFocus()) this.root.focus();
Expand Down
Loading

0 comments on commit b8101f8

Please sign in to comment.