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

Commit 9db03b0

Browse files
authored
Merge pull request #38 from juliankrispel/disable-inline-styles-in-link
Disable inline styles inside link
2 parents 5b52fde + 034db23 commit 9db03b0

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

src/__test__/plugin.test.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,50 @@ describe("draft-js-markdown-plugin", () => {
129129
expect(store.setEditorState).not.toHaveBeenCalled();
130130
});
131131

132+
it("does not handle if current entity is link", () => {
133+
currentRawContentState = {
134+
entityMap: {
135+
"0": {
136+
data: {
137+
href: "www.google.com",
138+
url: "http://www.google.com",
139+
},
140+
mutability: "MUTABLE",
141+
type: "LINK",
142+
},
143+
},
144+
blocks: [
145+
{
146+
key: "item1",
147+
text: "what **is** going on",
148+
type: "unstyled",
149+
depth: 0,
150+
inlineStyleRanges: [],
151+
entityRanges: [
152+
{
153+
offset: 0,
154+
key: 0,
155+
length: 20,
156+
},
157+
],
158+
data: {},
159+
},
160+
],
161+
};
162+
163+
currentSelectionState = currentEditorState.getSelection().merge({
164+
focusOffset: 19,
165+
anchorOffset: 19,
166+
});
167+
168+
currentEditorState = createEditorState(
169+
currentRawContentState,
170+
currentSelectionState
171+
);
172+
173+
expect(subject()).toBe("not-handled");
174+
});
175+
132176
it("resets curent inline style", () => {
133177
currentRawContentState = {
134178
entityMap: {},
@@ -144,6 +188,7 @@ describe("draft-js-markdown-plugin", () => {
144188
},
145189
],
146190
};
191+
147192
currentSelectionState = currentSelectionState.merge({
148193
focusOffset: 5,
149194
anchorOffset: 5,
@@ -435,6 +480,51 @@ describe("draft-js-markdown-plugin", () => {
435480
expect(subject()).toBe("not-handled");
436481
});
437482
});
483+
describe("current entity is a link", () => {
484+
it("returns not-handled", () => {
485+
currentRawContentState = {
486+
entityMap: {
487+
"0": {
488+
data: {
489+
href: "www.google.com",
490+
url: "http://www.google.com",
491+
},
492+
mutability: "MUTABLE",
493+
type: "LINK",
494+
},
495+
},
496+
blocks: [
497+
{
498+
key: "item1",
499+
text: "what **is** going on",
500+
type: "unstyled",
501+
depth: 0,
502+
inlineStyleRanges: [],
503+
entityRanges: [
504+
{
505+
offset: 0,
506+
key: 0,
507+
length: 20,
508+
},
509+
],
510+
data: {},
511+
},
512+
],
513+
};
514+
515+
currentSelectionState = currentEditorState.getSelection().merge({
516+
focusOffset: 19,
517+
anchorOffset: 19,
518+
});
519+
520+
currentEditorState = createEditorState(
521+
currentRawContentState,
522+
currentSelectionState
523+
);
524+
525+
expect(subject()).toBe("not-handled");
526+
});
527+
});
438528
});
439529
describe("handlePastedText", () => {
440530
let pastedText;

src/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ import { CODE_BLOCK_REGEX } from "./constants";
3232

3333
const INLINE_STYLE_CHARACTERS = [" ", "*", "_"];
3434

35+
function inLink(editorState) {
36+
const selection = editorState.getSelection();
37+
const contentState = editorState.getCurrentContent();
38+
const block = contentState.getBlockForKey(selection.getAnchorKey());
39+
const entityKey = block.getEntityAt(selection.getFocusOffset());
40+
return (
41+
entityKey != null && contentState.getEntity(entityKey).getType() === "LINK"
42+
);
43+
}
44+
3545
function inCodeBlock(editorState) {
3646
const startKey = editorState.getSelection().getStartKey();
3747
if (startKey) {
@@ -170,8 +180,10 @@ const createMarkdownPlugin = (config = {}) => {
170180
return "not-handled";
171181
},
172182
handleReturn(ev, editorState, { setEditorState }) {
183+
if (inLink(editorState)) return "not-handled";
184+
173185
let newEditorState = checkReturnForState(editorState, ev);
174-
let selection = newEditorState.getSelection();
186+
const selection = newEditorState.getSelection();
175187

176188
// exit code blocks
177189
if (
@@ -210,9 +222,12 @@ const createMarkdownPlugin = (config = {}) => {
210222
return "not-handled";
211223
}
212224

213-
// If we're in a code block don't add markdown to it
225+
// If we're in a code block - don't transform markdown
214226
if (inCodeBlock(editorState)) return "not-handled";
215227

228+
// If we're in a link - don't transform markdown
229+
if (inLink(editorState)) return "not-handled";
230+
216231
const newEditorState = checkCharacterForState(editorState, character);
217232
if (editorState !== newEditorState) {
218233
setEditorState(newEditorState);

0 commit comments

Comments
 (0)