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

Commit c58601e

Browse files
committed
if inline style was applied by other plugin md plugin should not unstick it
1 parent 3546027 commit c58601e

File tree

4 files changed

+88
-40
lines changed

4 files changed

+88
-40
lines changed

src/__test__/plugin-new.test.js

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import Draft, {
2-
EditorState,
3-
SelectionState,
4-
ContentBlock,
5-
convertToRaw,
6-
} from "draft-js";
1+
import Draft, { EditorState, SelectionState, convertToRaw } from "draft-js";
72
import createMarkdownPlugin from "../";
83

4+
const applyMDtoInlineStyleChange = editorState =>
5+
EditorState.set(editorState, {
6+
lastChangeType: "md-to-inline-style",
7+
});
8+
99
describe("markdown", () => {
1010
it("should convert asteriks to bold text", () => {
1111
const { handleBeforeInput } = createMarkdownPlugin();
@@ -76,15 +76,14 @@ describe("markdown", () => {
7676
);
7777
});
7878

79-
it("should not have sticky inline styles", () => {
79+
it("should not unstick inline styles if they were not added by md-to-inline-style change", () => {
8080
const { handleBeforeInput } = createMarkdownPlugin();
81-
const setEditorState = jest.fn();
8281
const boldInlineStyleRange = {
8382
length: 4,
8483
offset: 5,
8584
style: "BOLD",
8685
};
87-
const before = EditorState.moveSelectionToEnd(
86+
const editorState = EditorState.moveSelectionToEnd(
8887
EditorState.createWithContent(
8988
Draft.convertFromRaw({
9089
entityMap: {},
@@ -102,7 +101,39 @@ describe("markdown", () => {
102101
})
103102
)
104103
);
105-
expect(handleBeforeInput("a", before, { setEditorState })).toEqual(
104+
expect(handleBeforeInput("a", editorState, {})).toEqual("not-handled");
105+
});
106+
107+
it("should not have sticky inline styles", () => {
108+
const { handleBeforeInput } = createMarkdownPlugin();
109+
const setEditorState = jest.fn();
110+
const boldInlineStyleRange = {
111+
length: 4,
112+
offset: 5,
113+
style: "BOLD",
114+
};
115+
const editorState = applyMDtoInlineStyleChange(
116+
EditorState.moveSelectionToEnd(
117+
EditorState.createWithContent(
118+
Draft.convertFromRaw({
119+
entityMap: {},
120+
blocks: [
121+
{
122+
key: "item1",
123+
text: "Some text",
124+
type: "unstyled",
125+
depth: 0,
126+
inlineStyleRanges: [boldInlineStyleRange],
127+
entityRanges: [],
128+
data: {},
129+
},
130+
],
131+
})
132+
)
133+
)
134+
);
135+
136+
expect(handleBeforeInput("a", editorState, { setEditorState })).toEqual(
106137
"handled"
107138
);
108139
const raw = convertToRaw(
@@ -120,34 +151,37 @@ describe("markdown", () => {
120151
offset: 5,
121152
style: "BOLD",
122153
};
123-
const before = EditorState.moveSelectionToEnd(
124-
EditorState.createWithContent(
125-
Draft.convertFromRaw({
126-
entityMap: {},
127-
blocks: [
128-
{
129-
key: "item1",
130-
text: "Some text",
131-
type: "unstyled",
132-
depth: 0,
133-
inlineStyleRanges: [boldInlineStyleRange],
134-
entityRanges: [],
135-
data: {},
136-
},
137-
{
138-
key: "item2",
139-
text: "",
140-
type: "unstyled",
141-
depth: 0,
142-
inlineStyleRanges: [],
143-
entityRanges: [],
144-
data: {},
145-
},
146-
],
147-
})
154+
const editorState = applyMDtoInlineStyleChange(
155+
EditorState.moveSelectionToEnd(
156+
EditorState.createWithContent(
157+
Draft.convertFromRaw({
158+
entityMap: {},
159+
blocks: [
160+
{
161+
key: "item1",
162+
text: "Some text",
163+
type: "unstyled",
164+
depth: 0,
165+
inlineStyleRanges: [boldInlineStyleRange],
166+
entityRanges: [],
167+
data: {},
168+
},
169+
{
170+
key: "item2",
171+
text: "",
172+
type: "unstyled",
173+
depth: 0,
174+
inlineStyleRanges: [],
175+
entityRanges: [],
176+
data: {},
177+
},
178+
],
179+
})
180+
)
148181
)
149182
);
150-
expect(handleBeforeInput("a", before, { setEditorState })).toEqual(
183+
184+
expect(handleBeforeInput("a", editorState, { setEditorState })).toEqual(
151185
"handled"
152186
);
153187
const raw = convertToRaw(

src/__test__/plugin.test.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import {
1313
import { Map, List } from "immutable";
1414
import createMarkdownPlugin from "../";
1515

16+
const applyMDtoInlineStyleChange = editorState =>
17+
EditorState.set(editorState, {
18+
lastChangeType: "md-to-inline-style",
19+
});
20+
1621
describe("draft-js-markdown-plugin", () => {
1722
afterEach(() => {
1823
/* eslint-disable no-underscore-dangle */
@@ -474,8 +479,12 @@ describe("draft-js-markdown-plugin", () => {
474479
let character;
475480
beforeEach(() => {
476481
character = " ";
477-
subject = () =>
478-
plugin.handleBeforeInput(character, store.getEditorState(), store);
482+
subject = editorState =>
483+
plugin.handleBeforeInput(
484+
character,
485+
editorState || store.getEditorState(),
486+
store
487+
);
479488
currentRawContentState = {
480489
entityMap: {},
481490
blocks: [
@@ -572,7 +581,9 @@ describe("draft-js-markdown-plugin", () => {
572581
anchorOffset: 5,
573582
});
574583

575-
expect(subject()).toBe("handled");
584+
expect(
585+
subject(applyMDtoInlineStyleChange(store.getEditorState()))
586+
).toBe("handled");
576587
expect(store.setEditorState).toHaveBeenCalled();
577588
newEditorState = store.setEditorState.mock.calls[0][0];
578589
const block = newEditorState.getCurrentContent().getLastBlock();

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ function checkReturnForState(config, editorState, ev) {
205205
const unstickyInlineStyles = (character, editorState) => {
206206
const selection = editorState.getSelection();
207207
if (!selection.isCollapsed()) return editorState;
208+
if (editorState.getLastChangeType() !== "md-to-inline-style") {
209+
return editorState;
210+
}
208211

209212
const startOffset = selection.getStartOffset();
210213
const content = editorState.getCurrentContent();

src/modifiers/handleInlineStyle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const handleInlineStyle = (
6161
newEditorState = EditorState.push(
6262
newEditorState,
6363
newContentState,
64-
"change-inline-style"
64+
"md-to-inline-style"
6565
);
6666

6767
return newEditorState;

0 commit comments

Comments
 (0)