Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit 935fe75

Browse files
committed
Unsticky inline styles
This "unstickies" inline styles, so that when you type *bold* text, backspace to the ending edge of the bold text and start typing again the bold doesn't "stick" to the text, only when typing within it. This feels much better than before. Recorded demo: ![](https://i.imgur.com/gWSLp06.gif)
1 parent d0c5dcb commit 935fe75

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,27 @@ function checkReturnForState(config, editorState, ev) {
202202
return newEditorState;
203203
}
204204

205+
const unstickyInlineStyles = (character, editorState) => {
206+
const selection = editorState.getSelection();
207+
if (!selection.isCollapsed()) return editorState;
208+
209+
const startOffset = selection.getStartOffset();
210+
const content = editorState.getCurrentContent();
211+
const block = content.getBlockForKey(selection.getStartKey());
212+
const entity = block.getEntityAt(startOffset);
213+
if (entity !== null) return editorState;
214+
215+
// If we're currently in a style, but the next character doesn't have a style (or doesn't exist)
216+
// we insert the characters manually without the inline style to "unsticky" them
217+
const style = block.getInlineStyleAt(startOffset - 1);
218+
if (style.size === 0) return editorState;
219+
const nextStyle = block.getInlineStyleAt(startOffset);
220+
if (nextStyle.size !== 0) return editorState;
221+
222+
const newContent = Modifier.insertText(content, selection, character);
223+
return EditorState.push(editorState, newContent, "insert-characters");
224+
};
225+
205226
const defaultConfig = {
206227
renderLanguageSelect: defaultRenderSelect,
207228
languages: defaultLanguages,
@@ -347,6 +368,12 @@ const createMarkdownPlugin = (_config = {}) => {
347368
// If we're in a link - don't transform markdown
348369
if (inLink(editorState)) return "not-handled";
349370

371+
const unsticky = unstickyInlineStyles(character, editorState);
372+
if (editorState !== unsticky) {
373+
setEditorState(unsticky);
374+
return "handled";
375+
}
376+
350377
const newEditorState = checkCharacterForState(
351378
config,
352379
editorState,

0 commit comments

Comments
 (0)