Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Aug 27, 2017
1 parent d2094ff commit ec02e8f
Showing 1 changed file with 52 additions and 40 deletions.
92 changes: 52 additions & 40 deletions core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,12 @@ class Selection {
this.scroll = scroll;
this.composing = false;
this.root = this.scroll.domNode;
this.root.addEventListener('compositionstart', () => {
this.composing = true;
});
this.root.addEventListener('compositionend', () => {
this.composing = false;
if (this.cursor.parent) {
const range = this.cursor.restore();
if (!range) return;
setTimeout(() => {
this.setNativeRange(range.startNode, range.startOffset, range.endNode, range.endOffset);
}, 1);
}
});
this.cursor = Parchment.create('cursor', this);
// savedRange is last non-null range
this.lastRange = this.savedRange = new Range(0, 0);
this.root.addEventListener('click', (e) => {
const blot = Parchment.find(e.target, true);
const selectedNode = document.querySelector('.ql-embed-selected');
if (selectedNode) {
selectedNode.classList.remove('ql-embed-selected');
}
if (blot instanceof Parchment.Embed) {
blot.domNode.classList.add('ql-embed-selected');
const range = new Range(blot.offset(scroll), blot.length());
this.setRange(range, Emitter.sources.USER);
}
});
let mouseCount = 0;
this.emitter.listenDOM('mousedown', document.body, () => {
mouseCount += 1;
});
this.emitter.listenDOM('mouseup', document.body, () => {
mouseCount -= 1;
if (mouseCount === 0) {
this.update(Emitter.sources.USER);
}
});
this.emitter.listenDOM('selectionchange', document, () => {
if (mouseCount === 0) {
setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
}
});
this.handleComposition();
this.handleDragging();
this.handleEmbedSelection();
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 Expand Up @@ -112,6 +75,55 @@ class Selection {
}
}

handleComposition() {
this.root.addEventListener('compositionstart', () => {
this.composing = true;
});
this.root.addEventListener('compositionend', () => {
this.composing = false;
if (this.cursor.parent) {
const range = this.cursor.restore();
if (!range) return;
setTimeout(() => {
this.setNativeRange(range.startNode, range.startOffset, range.endNode, range.endOffset);
}, 1);
}
});
}

handleDragging() {
let mouseCount = 0;
this.emitter.listenDOM('mousedown', document.body, () => {
mouseCount += 1;
});
this.emitter.listenDOM('mouseup', document.body, () => {
mouseCount -= 1;
if (mouseCount === 0) {
this.update(Emitter.sources.USER);
}
});
this.emitter.listenDOM('selectionchange', document, () => {
if (mouseCount === 0) {
setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
}
});
}

handleEmbedSelection() {
this.root.addEventListener('click', (e) => {
const blot = Parchment.find(e.target, true);
const selectedNode = document.querySelector('.ql-embed-selected');
if (selectedNode) {
selectedNode.classList.remove('ql-embed-selected');
}
if (blot instanceof Parchment.Embed) {
blot.domNode.classList.add('ql-embed-selected');
const range = new Range(blot.offset(scroll), blot.length());
this.setRange(range, Emitter.sources.USER);
}
});
}

focus() {
if (this.hasFocus()) return;
this.root.focus();
Expand Down

0 comments on commit ec02e8f

Please sign in to comment.