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

Commit f3b06b3

Browse files
committed
- add inLink function
- add unit tests to ensure transformation is not applied when current entity is link
1 parent 65e6779 commit f3b06b3

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

src/__test__/plugin.test.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,51 @@ describe("draft-js-markdown-plugin", () => {
127127
expect(modifierSpy).not.toHaveBeenCalledTimes(1);
128128
expect(store.setEditorState).not.toHaveBeenCalled();
129129
});
130+
131+
it("does not handle if current entity is link", () => {
132+
currentRawContentState = {
133+
entityMap: {
134+
"0": {
135+
data: {
136+
href: "www.google.com",
137+
url: "http://www.google.com",
138+
},
139+
mutability: "MUTABLE",
140+
type: "LINK",
141+
},
142+
},
143+
blocks: [
144+
{
145+
key: "item1",
146+
text: "what **is** going on",
147+
type: "unstyled",
148+
depth: 0,
149+
inlineStyleRanges: [],
150+
entityRanges: [
151+
{
152+
offset: 0,
153+
key: 0,
154+
length: 20,
155+
},
156+
],
157+
data: {},
158+
},
159+
],
160+
};
161+
162+
currentSelectionState = currentEditorState.getSelection().merge({
163+
focusOffset: 19,
164+
anchorOffset: 19,
165+
});
166+
167+
currentEditorState = createEditorState(
168+
currentRawContentState,
169+
currentSelectionState
170+
);
171+
172+
expect(subject()).toBe("not-handled");
173+
});
174+
130175
it("leaves from list", () => {
131176
createMarkdownPlugin.__Rewire__("leaveList", modifierSpy); // eslint-disable-line no-underscore-dangle
132177
currentRawContentState = {
@@ -375,6 +420,51 @@ describe("draft-js-markdown-plugin", () => {
375420
expect(subject()).toBe("not-handled");
376421
});
377422
});
423+
describe("current entity is a link", () => {
424+
it("returns not-handled", () => {
425+
currentRawContentState = {
426+
entityMap: {
427+
"0": {
428+
data: {
429+
href: "www.google.com",
430+
url: "http://www.google.com",
431+
},
432+
mutability: "MUTABLE",
433+
type: "LINK",
434+
},
435+
},
436+
blocks: [
437+
{
438+
key: "item1",
439+
text: "what **is** going on",
440+
type: "unstyled",
441+
depth: 0,
442+
inlineStyleRanges: [],
443+
entityRanges: [
444+
{
445+
offset: 0,
446+
key: 0,
447+
length: 20,
448+
},
449+
],
450+
data: {},
451+
},
452+
],
453+
};
454+
455+
currentSelectionState = currentEditorState.getSelection().merge({
456+
focusOffset: 19,
457+
anchorOffset: 19,
458+
});
459+
460+
currentEditorState = createEditorState(
461+
currentRawContentState,
462+
currentSelectionState
463+
);
464+
465+
expect(subject()).toBe("not-handled");
466+
});
467+
});
378468
});
379469
describe("handlePastedText", () => {
380470
let pastedText;

src/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ import { CODE_BLOCK_REGEX } from "./constants";
2525

2626
const INLINE_STYLE_CHARACTERS = [" ", "*", "_"];
2727

28+
function inLink(editorState) {
29+
const selection = editorState.getSelection();
30+
const contentState = editorState.getCurrentContent();
31+
const block = contentState.getBlockForKey(selection.getAnchorKey());
32+
const entityKey = block.getEntityAt(selection.getFocusOffset());
33+
return (
34+
entityKey != null && contentState.getEntity(entityKey).getType() === "LINK"
35+
);
36+
}
37+
2838
function inCodeBlock(editorState) {
2939
const startKey = editorState.getSelection().getStartKey();
3040
if (startKey) {
@@ -153,6 +163,9 @@ const createMarkdownPlugin = (config = {}) => {
153163
return "not-handled";
154164
},
155165
handleReturn(ev, editorState, { setEditorState }) {
166+
if (inLink(editorState)) return "not-handled";
167+
168+
const selection = editorState.getSelection();
156169
let newEditorState = checkReturnForState(editorState, ev);
157170
if (editorState !== newEditorState) {
158171
setEditorState(newEditorState);
@@ -175,9 +188,12 @@ const createMarkdownPlugin = (config = {}) => {
175188
return "not-handled";
176189
}
177190

178-
// If we're in a code block don't add markdown to it
191+
// If we're in a code block - don't transform markdown
179192
if (inCodeBlock(editorState)) return "not-handled";
180193

194+
// If we're in a link - don't transform markdown
195+
if (inLink(editorState)) return "not-handled";
196+
181197
const newEditorState = checkCharacterForState(editorState, character);
182198
if (editorState !== newEditorState) {
183199
setEditorState(newEditorState);

0 commit comments

Comments
 (0)