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

Commit c5927d1

Browse files
authored
Merge pull request #141 from onebar/dont-unstick-inlinestyles-done-by-other-plugins
if inline style was applied by other plugin md plugin should not unst…
2 parents 3546027 + 1e229e9 commit c5927d1

File tree

5 files changed

+86
-40
lines changed

5 files changed

+86
-40
lines changed

src/__test__/plugin-new.test.js

+66-36
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
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 "../";
3+
import { applyMDtoInlineStyleChange } from "./utils";
84

95
describe("markdown", () => {
106
it("should convert asteriks to bold text", () => {
@@ -76,15 +72,14 @@ describe("markdown", () => {
7672
);
7773
});
7874

79-
it("should not have sticky inline styles", () => {
75+
it("should not unstick inline styles if they were not added by md-to-inline-style change", () => {
8076
const { handleBeforeInput } = createMarkdownPlugin();
81-
const setEditorState = jest.fn();
8277
const boldInlineStyleRange = {
8378
length: 4,
8479
offset: 5,
8580
style: "BOLD",
8681
};
87-
const before = EditorState.moveSelectionToEnd(
82+
const editorState = EditorState.moveSelectionToEnd(
8883
EditorState.createWithContent(
8984
Draft.convertFromRaw({
9085
entityMap: {},
@@ -102,7 +97,39 @@ describe("markdown", () => {
10297
})
10398
)
10499
);
105-
expect(handleBeforeInput("a", before, { setEditorState })).toEqual(
100+
expect(handleBeforeInput("a", editorState, {})).toEqual("not-handled");
101+
});
102+
103+
it("should not have sticky inline styles", () => {
104+
const { handleBeforeInput } = createMarkdownPlugin();
105+
const setEditorState = jest.fn();
106+
const boldInlineStyleRange = {
107+
length: 4,
108+
offset: 5,
109+
style: "BOLD",
110+
};
111+
const editorState = applyMDtoInlineStyleChange(
112+
EditorState.moveSelectionToEnd(
113+
EditorState.createWithContent(
114+
Draft.convertFromRaw({
115+
entityMap: {},
116+
blocks: [
117+
{
118+
key: "item1",
119+
text: "Some text",
120+
type: "unstyled",
121+
depth: 0,
122+
inlineStyleRanges: [boldInlineStyleRange],
123+
entityRanges: [],
124+
data: {},
125+
},
126+
],
127+
})
128+
)
129+
)
130+
);
131+
132+
expect(handleBeforeInput("a", editorState, { setEditorState })).toEqual(
106133
"handled"
107134
);
108135
const raw = convertToRaw(
@@ -120,34 +147,37 @@ describe("markdown", () => {
120147
offset: 5,
121148
style: "BOLD",
122149
};
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-
})
150+
const editorState = applyMDtoInlineStyleChange(
151+
EditorState.moveSelectionToEnd(
152+
EditorState.createWithContent(
153+
Draft.convertFromRaw({
154+
entityMap: {},
155+
blocks: [
156+
{
157+
key: "item1",
158+
text: "Some text",
159+
type: "unstyled",
160+
depth: 0,
161+
inlineStyleRanges: [boldInlineStyleRange],
162+
entityRanges: [],
163+
data: {},
164+
},
165+
{
166+
key: "item2",
167+
text: "",
168+
type: "unstyled",
169+
depth: 0,
170+
inlineStyleRanges: [],
171+
entityRanges: [],
172+
data: {},
173+
},
174+
],
175+
})
176+
)
148177
)
149178
);
150-
expect(handleBeforeInput("a", before, { setEditorState })).toEqual(
179+
180+
expect(handleBeforeInput("a", editorState, { setEditorState })).toEqual(
151181
"handled"
152182
);
153183
const raw = convertToRaw(

src/__test__/plugin.test.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CheckableListItem,
44
CheckableListItemUtils,
55
} from "draft-js-checkable-list-item";
6+
import { applyMDtoInlineStyleChange } from "./utils";
67

78
import {
89
defaultInlineWhitelist,
@@ -474,8 +475,12 @@ describe("draft-js-markdown-plugin", () => {
474475
let character;
475476
beforeEach(() => {
476477
character = " ";
477-
subject = () =>
478-
plugin.handleBeforeInput(character, store.getEditorState(), store);
478+
subject = editorState =>
479+
plugin.handleBeforeInput(
480+
character,
481+
editorState || store.getEditorState(),
482+
store
483+
);
479484
currentRawContentState = {
480485
entityMap: {},
481486
blocks: [
@@ -572,7 +577,9 @@ describe("draft-js-markdown-plugin", () => {
572577
anchorOffset: 5,
573578
});
574579

575-
expect(subject()).toBe("handled");
580+
expect(
581+
subject(applyMDtoInlineStyleChange(store.getEditorState()))
582+
).toBe("handled");
576583
expect(store.setEditorState).toHaveBeenCalled();
577584
newEditorState = store.setEditorState.mock.calls[0][0];
578585
const block = newEditorState.getCurrentContent().getLastBlock();

src/__test__/utils.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { EditorState } from "draft-js";
2+
3+
export const applyMDtoInlineStyleChange = editorState =>
4+
EditorState.set(editorState, {
5+
lastChangeType: "md-to-inline-style",
6+
});

src/index.js

+3
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

+1-1
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)