Skip to content

Commit

Permalink
fix: prevent creating empty editorjs-style span
Browse files Browse the repository at this point in the history
  • Loading branch information
hata6502 committed Sep 11, 2020
1 parent 945e8c0 commit e385e93
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ class EditorJSStyle implements InlineTool {
span.firstChild?.nodeName !== '#text' ||
span.firstChild?.textContent?.slice(0, 1) !== '\u200b'
) {
span.prepend(document.createTextNode('\u200b'));
span.prepend('\u200b');
}

if (
span.lastChild?.nodeName !== '#text' ||
span.lastChild?.textContent?.slice(-1) !== '\u200b'
) {
span.append(document.createTextNode('\u200b'));
span.append('\u200b');
}
});

Expand Down Expand Up @@ -169,6 +169,7 @@ class EditorJSStyle implements InlineTool {

styleTextarea.value = span.getAttribute('style') ?? '';

// To input line breaks
styleTextarea.addEventListener('keydown', (event) =>
event.stopPropagation()
);
Expand Down Expand Up @@ -216,7 +217,8 @@ class EditorJSStyle implements InlineTool {
.forEach((element) => {
EditorJSStyle.initializeSpan({ span: element as HTMLSpanElement });

element.appendChild(document.createTextNode(''));
// To dispatch mutation observer
element.append('');
});

mutationObserver.disconnect();
Expand All @@ -237,10 +239,12 @@ class EditorJSStyle implements InlineTool {

EditorJSStyle.initializeSpan({ span });

span.append(range.extractContents());
span.append(range.collapsed ? 'new style' : range.extractContents());

range.insertNode(span);
this.api.selection.expandToTag(span);
setTimeout(() => {
range.insertNode(span);
this.api.selection.expandToTag(span);
});
}
}

Expand Down

0 comments on commit e385e93

Please sign in to comment.