Skip to content

Commit e385e93

Browse files
committed
fix: prevent creating empty editorjs-style span
1 parent 945e8c0 commit e385e93

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/index.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ class EditorJSStyle implements InlineTool {
2727
span.firstChild?.nodeName !== '#text' ||
2828
span.firstChild?.textContent?.slice(0, 1) !== '\u200b'
2929
) {
30-
span.prepend(document.createTextNode('\u200b'));
30+
span.prepend('\u200b');
3131
}
3232

3333
if (
3434
span.lastChild?.nodeName !== '#text' ||
3535
span.lastChild?.textContent?.slice(-1) !== '\u200b'
3636
) {
37-
span.append(document.createTextNode('\u200b'));
37+
span.append('\u200b');
3838
}
3939
});
4040

@@ -169,6 +169,7 @@ class EditorJSStyle implements InlineTool {
169169

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

172+
// To input line breaks
172173
styleTextarea.addEventListener('keydown', (event) =>
173174
event.stopPropagation()
174175
);
@@ -216,7 +217,8 @@ class EditorJSStyle implements InlineTool {
216217
.forEach((element) => {
217218
EditorJSStyle.initializeSpan({ span: element as HTMLSpanElement });
218219

219-
element.appendChild(document.createTextNode(''));
220+
// To dispatch mutation observer
221+
element.append('');
220222
});
221223

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

238240
EditorJSStyle.initializeSpan({ span });
239241

240-
span.append(range.extractContents());
242+
span.append(range.collapsed ? 'new style' : range.extractContents());
241243

242-
range.insertNode(span);
243-
this.api.selection.expandToTag(span);
244+
setTimeout(() => {
245+
range.insertNode(span);
246+
this.api.selection.expandToTag(span);
247+
});
244248
}
245249
}
246250

0 commit comments

Comments
 (0)