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

Commit 65fee79

Browse files
authored
Merge pull request #83 from withspectrum/unsticky
Unsticky inline styles
2 parents 1ceb937 + 2a98941 commit 65fee79

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/__test__/plugin.test.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ describe("draft-js-markdown-plugin", () => {
180180
expect(subject()).toBe("not-handled");
181181
});
182182

183-
it("resets curent inline style", () => {
183+
it("resets current inline style", () => {
184184
currentRawContentState = {
185185
entityMap: {},
186186
blocks: [
@@ -545,6 +545,39 @@ describe("draft-js-markdown-plugin", () => {
545545
" "
546546
);
547547
});
548+
it("unstickys inline style", () => {
549+
currentRawContentState = {
550+
entityMap: {},
551+
blocks: [
552+
{
553+
key: "item1",
554+
text: "item1",
555+
type: "unstyled",
556+
depth: 0,
557+
inlineStyleRanges: [
558+
{ offset: 0, length: 5, style: "BOLD" },
559+
],
560+
entityRanges: [],
561+
data: {},
562+
},
563+
],
564+
};
565+
566+
currentSelectionState = currentSelectionState.merge({
567+
focusOffset: 5,
568+
anchorOffset: 5,
569+
});
570+
571+
expect(subject()).toBe("handled");
572+
expect(store.setEditorState).toHaveBeenCalled();
573+
newEditorState = store.setEditorState.mock.calls[0][0];
574+
const block = newEditorState.getCurrentContent().getLastBlock();
575+
const length = block.getLength();
576+
expect(block.getInlineStyleAt(length - 1).toJS()).toEqual([]);
577+
expect(block.getInlineStyleAt(length - 2).toJS()).toEqual([
578+
"BOLD",
579+
]);
580+
});
548581
});
549582
});
550583
describe("character is not a space", () => {

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 - 1);
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,
@@ -348,6 +369,12 @@ const createMarkdownPlugin = (_config = {}) => {
348369
// If we're in a link - don't transform markdown
349370
if (inLink(editorState)) return "not-handled";
350371

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

0 commit comments

Comments
 (0)