Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit f2b0339

Browse files
authored
Merge pull request #136 from onebar/unstick-styles-in-the-beginning-of-line
correct how we unstick styles in the beginning of the line
2 parents d19aec2 + d06007e commit f2b0339

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

‎src/__test__/__snapshots__/plugin-new.test.js.snap‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,35 @@ Object {
4545
"entityMap": Object {},
4646
}
4747
`;
48+
49+
exports[`markdown should not have sticky inline styles after the line ending with styles 1`] = `
50+
Object {
51+
"blocks": Array [
52+
Object {
53+
"data": Object {},
54+
"depth": 0,
55+
"entityRanges": Array [],
56+
"inlineStyleRanges": Array [
57+
Object {
58+
"length": 4,
59+
"offset": 5,
60+
"style": "BOLD",
61+
},
62+
],
63+
"key": "item1",
64+
"text": "Some text",
65+
"type": "unstyled",
66+
},
67+
Object {
68+
"data": Object {},
69+
"depth": 0,
70+
"entityRanges": Array [],
71+
"inlineStyleRanges": Array [],
72+
"key": "item2",
73+
"text": "a",
74+
"type": "unstyled",
75+
},
76+
],
77+
"entityMap": Object {},
78+
}
79+
`

‎src/__test__/plugin-new.test.js‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,49 @@ describe("markdown", () => {
111111
expect(raw.blocks[0].inlineStyleRanges[0]).toEqual(boldInlineStyleRange);
112112
expect(raw).toMatchSnapshot();
113113
});
114+
115+
it("should not have sticky inline styles after the line ending with styles", () => {
116+
const { handleBeforeInput } = createMarkdownPlugin();
117+
const setEditorState = jest.fn();
118+
const boldInlineStyleRange = {
119+
length: 4,
120+
offset: 5,
121+
style: "BOLD",
122+
};
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+
})
148+
)
149+
);
150+
expect(handleBeforeInput("a", before, { setEditorState })).toEqual(
151+
"handled"
152+
);
153+
const raw = convertToRaw(
154+
setEditorState.mock.calls[0][0].getCurrentContent()
155+
);
156+
expect(raw.blocks[0].inlineStyleRanges[0]).toEqual(boldInlineStyleRange);
157+
expect(raw).toMatchSnapshot();
158+
});
114159
});

‎src/index.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,22 @@ const unstickyInlineStyles = (character, editorState) => {
209209
const startOffset = selection.getStartOffset();
210210
const content = editorState.getCurrentContent();
211211
const block = content.getBlockForKey(selection.getStartKey());
212+
const previousBlock = content.getBlockBefore(block.getKey());
212213
const entity = block.getEntityAt(startOffset - 1);
213214
if (entity !== null) return editorState;
214215

215216
// If we're currently in a style, but the next character doesn't have a style (or doesn't exist)
216217
// 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;
218+
if (!startOffset && previousBlock) {
219+
// If we're in the beginning of the line we have to check styles of the previous block
220+
const previousBlockStyle = previousBlock.getInlineStyleAt(
221+
previousBlock.getText().length - 1
222+
);
223+
if (previousBlockStyle.size === 0) return editorState;
224+
} else {
225+
const style = block.getInlineStyleAt(startOffset - 1);
226+
if (style.size === 0) return editorState;
227+
}
219228
const nextStyle = block.getInlineStyleAt(startOffset);
220229
if (nextStyle.size !== 0) return editorState;
221230

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /