Skip to content

Commit

Permalink
fix composing on an empty line
Browse files Browse the repository at this point in the history
- previously composing into <p><br></p> would insert a new character and
our cleanup would remove the <br> which would disrupt the selection and
composition
- the DOMNodeInserted stopped working though logically not sure why it
did work to begin with
  • Loading branch information
jhchen committed Oct 17, 2018
1 parent 9d4b827 commit 41a60fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
19 changes: 13 additions & 6 deletions blots/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ class Scroll extends ScrollBlot {
constructor(registry, domNode, { emitter }) {
super(registry, domNode);
this.emitter = emitter;
// Some reason fixes composition issues with character languages in Windows/Chrome, Safari
this.domNode.addEventListener('DOMNodeInserted', () => {});
this.batch = false;
this.optimize();
this.enable();
}

batchStart() {
this.batch = true;
if (!Array.isArray(this.batch)) {
this.batch = [];
}
}

batchEnd() {
const mutations = this.batch;
this.batch = false;
this.optimize();
this.update(mutations);
}

emitMount(blot) {
Expand Down Expand Up @@ -128,7 +130,7 @@ class Scroll extends ScrollBlot {
}

optimize(mutations = [], context = {}) {
if (this.batch === true) return;
if (this.batch) return;
super.optimize(mutations, context);
if (mutations.length > 0) {
this.emitter.emit(Emitter.events.SCROLL_OPTIMIZE, mutations, context);
Expand All @@ -144,7 +146,12 @@ class Scroll extends ScrollBlot {
}

update(mutations) {
if (this.batch === true) return;
if (this.batch) {
if (Array.isArray(mutations)) {
this.batch = this.batch.concat(mutations);
}
return;
}
let source = Emitter.sources.USER;
if (typeof mutations === 'string') {
source = mutations;
Expand Down
12 changes: 7 additions & 5 deletions core/selection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContainerBlot, LeafBlot, Scope } from 'parchment';
import { LeafBlot, Scope } from 'parchment';
import clone from 'clone';
import equal from 'deep-equal';
import Emitter from './emitter';
Expand Down Expand Up @@ -27,7 +27,7 @@ class Selection {
this.handleComposition();
this.handleDragging();
this.emitter.listenDOM('selectionchange', document, () => {
if (!this.mouseDown) {
if (!this.mouseDown && !this.composing) {
setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
}
});
Expand Down Expand Up @@ -68,8 +68,10 @@ class Selection {
handleComposition() {
this.root.addEventListener('compositionstart', () => {
this.composing = true;
this.scroll.batchStart();
});
this.root.addEventListener('compositionend', () => {
this.scroll.batchEnd();
this.composing = false;
if (this.cursor.parent) {
const range = this.cursor.restore();
Expand Down Expand Up @@ -208,10 +210,10 @@ class Selection {
if (offset === 0) {
return index;
}
if (blot instanceof ContainerBlot) {
return index + blot.length();
if (blot instanceof LeafBlot) {
return index + blot.index(node, offset);
}
return index + blot.index(node, offset);
return index + blot.length();
});
const end = Math.min(Math.max(...indexes), this.scroll.length() - 1);
const start = Math.min(end, ...indexes);
Expand Down

0 comments on commit 41a60fb

Please sign in to comment.