From 4e874c48d7e7b7652cdbd3294d486d71a166fb37 Mon Sep 17 00:00:00 2001 From: Jason Chen Date: Tue, 20 Jun 2017 10:41:03 -0700 Subject: [PATCH] handle typing around inline embed --- blots/embed.js | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/blots/embed.js b/blots/embed.js index a3c8fe4a42..e9dfac9f05 100644 --- a/blots/embed.js +++ b/blots/embed.js @@ -1,4 +1,7 @@ import Parchment from 'parchment'; +import TextBlot from './text'; + +const GUARD_TEXT = "\uFEFF"; class Embed extends Parchment.Embed { } @@ -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); @@ -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); + } + }); + } }