Skip to content

Commit b37c8e5

Browse files
authored
Fix keyboard shortcuts regression introduced in v1.2.0 (#189)
1 parent ff5bfa6 commit b37c8e5

File tree

6 files changed

+84
-34
lines changed

6 files changed

+84
-34
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
> Documentation: [draftail.org/docs/next/getting-started](https://www.draftail.org/docs/next/getting-started)
88
9+
### Fixed
10+
11+
- Fix regression introduced in v1.2.0 where Draft.js-defined keyboard shortcuts were available unconditionally ([#189](https://github.com/springload/draftail/pull/189)).
12+
913
## [[v1.2.0]](https://github.com/springload/draftail/releases/tag/v1.2.0)
1014

1115
> Documentation: [draftail.org/docs/getting-started](https://www.draftail.org/docs/getting-started)

lib/api/constants.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,21 @@ export const INLINE_STYLE = {
4343
SUBSCRIPT: "SUBSCRIPT",
4444
};
4545

46-
export const BLOCK_TYPES = Object.values(BLOCK_TYPE);
47-
export const ENTITY_TYPES = Object.values(ENTITY_TYPE);
48-
export const INLINE_STYLES = Object.values(INLINE_STYLE);
46+
const BLOCK_TYPES = Object.values(BLOCK_TYPE);
47+
const ENTITY_TYPES = Object.values(ENTITY_TYPE);
48+
const INLINE_STYLES = Object.values(INLINE_STYLE);
49+
50+
export const KEY_COMMANDS = [
51+
...BLOCK_TYPES,
52+
...ENTITY_TYPES,
53+
...INLINE_STYLES,
54+
// Lowercase identifiers used by Draft.js
55+
// See https://github.com/facebook/draft-js/blob/585af35c3a8c31fefb64bc884d4001faa96544d3/src/model/constants/DraftEditorCommand.js#L58-L61.
56+
"bold",
57+
"italic",
58+
"underline",
59+
"code",
60+
];
4961

5062
export const FONT_FAMILY_MONOSPACE =
5163
"Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, sans-serif";

lib/api/constants.test.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import {
22
BLOCK_TYPE,
33
ENTITY_TYPE,
44
INLINE_STYLE,
5-
BLOCK_TYPES,
6-
ENTITY_TYPES,
7-
INLINE_STYLES,
5+
KEY_COMMANDS,
86
FONT_FAMILY_MONOSPACE,
97
CUSTOM_STYLE_MAP,
108
BR_TYPE,
@@ -24,9 +22,7 @@ describe("constants", () => {
2422
it("#BLOCK_TYPE", () => expect(BLOCK_TYPE).toBeDefined());
2523
it("#ENTITY_TYPE", () => expect(ENTITY_TYPE).toBeDefined());
2624
it("#INLINE_STYLE", () => expect(INLINE_STYLE).toBeDefined());
27-
it("#BLOCK_TYPES", () => expect(BLOCK_TYPES).toBeDefined());
28-
it("#ENTITY_TYPES", () => expect(ENTITY_TYPES).toBeDefined());
29-
it("#INLINE_STYLES", () => expect(INLINE_STYLES).toBeDefined());
25+
it("#KEY_COMMANDS", () => expect(KEY_COMMANDS).toBeDefined());
3026
it("#FONT_FAMILY_MONOSPACE", () =>
3127
expect(FONT_FAMILY_MONOSPACE).toBeDefined());
3228
it("#CUSTOM_STYLE_MAP", () => expect(CUSTOM_STYLE_MAP).toBeDefined());

lib/components/DraftailEditor.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ import decorateComponentWithProps from "decorate-component-with-props";
1818
import {
1919
ENTITY_TYPE,
2020
BLOCK_TYPE,
21-
ENTITY_TYPES,
22-
BLOCK_TYPES,
23-
INLINE_STYLES,
21+
KEY_COMMANDS,
2422
HANDLED,
2523
NOT_HANDLED,
2624
UNDO_TYPE,
@@ -557,19 +555,20 @@ class DraftailEditor extends Component<Props, State> {
557555

558556
/* :: handleKeyCommand: (command: DraftEditorCommand) => 'handled' | 'not-handled'; */
559557
handleKeyCommand(command: DraftEditorCommand) {
558+
const { entityTypes, blockTypes, inlineStyles } = this.props;
560559
const { editorState } = this.state;
561560

562-
if (ENTITY_TYPES.includes(command)) {
561+
if (entityTypes.some((t) => t.type === command)) {
563562
this.onRequestSource(command);
564563
return HANDLED;
565564
}
566565

567-
if (BLOCK_TYPES.includes(command)) {
566+
if (blockTypes.some((t) => t.type === command)) {
568567
this.toggleBlockType(command);
569568
return HANDLED;
570569
}
571570

572-
if (INLINE_STYLES.includes(command)) {
571+
if (inlineStyles.some((t) => t.type === command)) {
573572
this.toggleInlineStyle(command);
574573
return HANDLED;
575574
}
@@ -584,6 +583,11 @@ class DraftailEditor extends Component<Props, State> {
584583
}
585584
}
586585

586+
// If the command is known but not whitelisted for this editor, treat it as handled but don't do anything.
587+
if (KEY_COMMANDS.includes(command)) {
588+
return HANDLED;
589+
}
590+
587591
const newState = RichUtils.handleKeyCommand(editorState, command);
588592
if (newState) {
589593
this.onChange(newState);

lib/components/DraftailEditor.test.js

+52-18
Original file line numberDiff line numberDiff line change
@@ -651,28 +651,62 @@ describe("DraftailEditor", () => {
651651
RichUtils.handleKeyCommand.mockRestore();
652652
});
653653

654-
it("entity type", () => {
655-
expect(
656-
shallowNoLifecycle(<DraftailEditor />)
657-
.instance()
658-
.handleKeyCommand("LINK"),
659-
).toBe("handled");
654+
it("entity type - active", () => {
655+
const wrapper = shallowNoLifecycle(
656+
<DraftailEditor entityTypes={[{ type: "LINK", source: () => null }]} />,
657+
).instance();
658+
jest.spyOn(wrapper, "onRequestSource");
659+
expect(wrapper.handleKeyCommand("LINK")).toBe("handled");
660+
expect(wrapper.onRequestSource).toHaveBeenCalled();
660661
});
661662

662-
it("block type", () => {
663-
expect(
664-
shallowNoLifecycle(<DraftailEditor />)
665-
.instance()
666-
.handleKeyCommand("header-one"),
667-
).toBe("handled");
663+
it("entity type - inactive", () => {
664+
const wrapper = shallowNoLifecycle(<DraftailEditor />).instance();
665+
jest.spyOn(wrapper, "onRequestSource");
666+
expect(wrapper.handleKeyCommand("LINK")).toBe("handled");
667+
expect(wrapper.onRequestSource).not.toHaveBeenCalled();
668668
});
669669

670-
it("inline style", () => {
671-
expect(
672-
shallowNoLifecycle(<DraftailEditor />)
673-
.instance()
674-
.handleKeyCommand("BOLD"),
675-
).toBe("handled");
670+
it("block type - active", () => {
671+
const wrapper = shallowNoLifecycle(
672+
<DraftailEditor blockTypes={[{ type: "header-one" }]} />,
673+
).instance();
674+
jest.spyOn(wrapper, "toggleBlockType");
675+
expect(wrapper.handleKeyCommand("header-one")).toBe("handled");
676+
expect(wrapper.toggleBlockType).toHaveBeenCalled();
677+
});
678+
679+
it("block type - inactive", () => {
680+
const wrapper = shallowNoLifecycle(<DraftailEditor />).instance();
681+
jest.spyOn(wrapper, "toggleBlockType");
682+
expect(wrapper.handleKeyCommand("header-one")).toBe("handled");
683+
expect(wrapper.toggleBlockType).not.toHaveBeenCalled();
684+
});
685+
686+
it("inline style - active", () => {
687+
const wrapper = shallowNoLifecycle(
688+
<DraftailEditor inlineStyles={[{ type: "BOLD" }]} />,
689+
).instance();
690+
jest.spyOn(wrapper, "toggleInlineStyle");
691+
expect(wrapper.handleKeyCommand("BOLD")).toBe("handled");
692+
expect(wrapper.toggleInlineStyle).toHaveBeenCalled();
693+
});
694+
695+
it("inline style - inactive", () => {
696+
const wrapper = shallowNoLifecycle(<DraftailEditor />).instance();
697+
jest.spyOn(wrapper, "toggleInlineStyle");
698+
expect(wrapper.handleKeyCommand("BOLD")).toBe("handled");
699+
expect(wrapper.toggleInlineStyle).not.toHaveBeenCalled();
700+
});
701+
702+
it("draft-js defaults", () => {
703+
const wrapper = shallowNoLifecycle(<DraftailEditor />).instance();
704+
jest.spyOn(wrapper, "toggleInlineStyle");
705+
expect(wrapper.handleKeyCommand("bold")).toBe("handled");
706+
expect(wrapper.handleKeyCommand("italic")).toBe("handled");
707+
expect(wrapper.handleKeyCommand("underline")).toBe("handled");
708+
expect(wrapper.handleKeyCommand("code")).toBe("handled");
709+
expect(wrapper.toggleInlineStyle).not.toHaveBeenCalled();
676710
});
677711

678712
describe("delete", () => {

tests/performance/markov_draftjs_41.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe("performance", () => {
4848
expect(results.mean).toBeLessThan(2 * PERFORMANCE_BUFFER);
4949
expect(results.min).toBeLessThan(1 * PERFORMANCE_BUFFER);
5050
expect(results.median).toBeLessThan(2 * PERFORMANCE_BUFFER);
51-
expect(results.max).toBeLessThan(8 * PERFORMANCE_BUFFER);
51+
expect(results.max).toBeLessThan(12 * PERFORMANCE_BUFFER);
5252
});
5353

5454
it("markov_draftjs[41] update", () => {

0 commit comments

Comments
 (0)