Skip to content

Commit

Permalink
handle typing around inline embed
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Jun 20, 2017
1 parent c98a6a6 commit 4e874c4
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions blots/embed.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Parchment from 'parchment';
import TextBlot from './text';

const GUARD_TEXT = "\uFEFF";


class Embed extends Parchment.Embed { }
Expand All @@ -12,8 +15,8 @@ class InlineEmbed extends Embed {
[].slice.call(this.domNode.childNodes).forEach(function(childNode) {
wrapper.appendChild(childNode);
});
this.leftGuard = document.createTextNode("\uFEFF");
this.rightGuard = document.createTextNode("\uFEFF");
this.leftGuard = document.createTextNode(GUARD_TEXT);
this.rightGuard = document.createTextNode(GUARD_TEXT);
this.domNode.appendChild(this.leftGuard);
this.domNode.appendChild(wrapper);
this.domNode.appendChild(this.rightGuard);
Expand All @@ -24,6 +27,38 @@ class InlineEmbed extends Embed {
if (node === this.rightGuard) return 1;
return super.index(node, offset);
}

restore(node) {
let text, textNode;
if (node === this.leftGuard) {
text = this.leftGuard.data.split(GUARD_TEXT).join('');
if (this.prev instanceof TextBlot) {
this.prev.insertAt(this.prev.length(), text);
} else {
textNode = document.createTextNode(text);
this.parent.insertBefore(Parchment.create(textNode), this);
}
this.leftGuard.data = GUARD_TEXT;
} else if (node === this.rightGuard) {
text = this.rightGuard.data.split(GUARD_TEXT).join('');
if (this.next instanceof TextBlot) {
this.next.insertAt(0, text);
} else {
textNode = document.createTextNode(text);
this.parent.insertBefore(Parchment.create(textNode), this.next);
}
this.rightGuard.data = GUARD_TEXT;
}
}

update(mutations) {
mutations.forEach((mutation) => {
if (mutation.type === 'characterData' &&
(mutation.target === this.leftGuard || mutation.target === this.rightGuard)) {
this.restore(mutation.target);
}
});
}
}


Expand Down

0 comments on commit 4e874c4

Please sign in to comment.