Skip to content

Commit

Permalink
Command priorities as constants (facebook#1775)
Browse files Browse the repository at this point in the history
  • Loading branch information
zurfyx authored Apr 16, 2022
1 parent 5ab8cd3 commit 682fbfb
Show file tree
Hide file tree
Showing 31 changed files with 231 additions and 274 deletions.
9 changes: 5 additions & 4 deletions packages/lexical-code/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
KEY_ARROW_DOWN_COMMAND,
KEY_ARROW_UP_COMMAND,
OUTDENT_CONTENT_COMMAND,
COMMAND_PRIORITY_LOW,
} from 'lexical';

const DEFAULT_CODE_LANGUAGE = 'javascript';
Expand Down Expand Up @@ -871,22 +872,22 @@ export function registerCodeHighlighting(editor: LexicalEditor): () => void {
editor.registerCommand(
INDENT_CONTENT_COMMAND,
(payload): boolean => handleMultilineIndent(INDENT_CONTENT_COMMAND),
1,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
OUTDENT_CONTENT_COMMAND,
(payload): boolean => handleMultilineIndent(OUTDENT_CONTENT_COMMAND),
1,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
KEY_ARROW_UP_COMMAND,
(payload): boolean => handleShiftLines(KEY_ARROW_UP_COMMAND, payload),
1,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
KEY_ARROW_DOWN_COMMAND,
(payload): boolean => handleShiftLines(KEY_ARROW_DOWN_COMMAND, payload),
1,
COMMAND_PRIORITY_LOW,
),
);
}
12 changes: 5 additions & 7 deletions packages/lexical-history/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import type {
CommandListenerEditorPriority,
EditorState,
GridSelection,
IntentionallyMarkedAsDirtyElement,
Expand All @@ -29,6 +28,7 @@ import {
CAN_UNDO_COMMAND,
CLEAR_EDITOR_COMMAND,
CLEAR_HISTORY_COMMAND,
COMMAND_PRIORITY_EDITOR,
REDO_COMMAND,
UNDO_COMMAND,
} from 'lexical';
Expand All @@ -45,8 +45,6 @@ const INSERT_CHARACTER_AFTER_SELECTION = 2;
const DELETE_CHARACTER_BEFORE_SELECTION = 3;
const DELETE_CHARACTER_AFTER_SELECTION = 4;

const EditorPriority: CommandListenerEditorPriority = 0;

export type HistoryStateEntry = {
editor: LexicalEditor,
editorState: EditorState,
Expand Down Expand Up @@ -376,31 +374,31 @@ export function registerHistory(
undo(editor, historyState);
return true;
},
EditorPriority,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
REDO_COMMAND,
() => {
redo(editor, historyState);
return true;
},
EditorPriority,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
CLEAR_EDITOR_COMMAND,
() => {
clearHistory(historyState);
return false;
},
EditorPriority,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
CLEAR_HISTORY_COMMAND,
() => {
clearHistory(historyState);
return true;
},
EditorPriority,
COMMAND_PRIORITY_EDITOR,
),
editor.registerUpdateListener(applyChange),
);
Expand Down
35 changes: 18 additions & 17 deletions packages/lexical-plain-text/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
$getRoot,
$getSelection,
$isRangeSelection,
COMMAND_PRIORITY_EDITOR,
COPY_COMMAND,
CUT_COMMAND,
DELETE_CHARACTER_COMMAND,
Expand Down Expand Up @@ -155,7 +156,7 @@ export function registerPlainText(
selection.deleteCharacter(isBackward);
return true;
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
DELETE_WORD_COMMAND,
Expand All @@ -167,7 +168,7 @@ export function registerPlainText(
selection.deleteWord(isBackward);
return true;
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
DELETE_LINE_COMMAND,
Expand All @@ -179,7 +180,7 @@ export function registerPlainText(
selection.deleteLine(isBackward);
return true;
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
INSERT_TEXT_COMMAND,
Expand All @@ -203,7 +204,7 @@ export function registerPlainText(
}
return true;
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
REMOVE_TEXT_COMMAND,
Expand All @@ -215,7 +216,7 @@ export function registerPlainText(
selection.removeText();
return true;
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
INSERT_LINE_BREAK_COMMAND,
Expand All @@ -227,7 +228,7 @@ export function registerPlainText(
selection.insertLineBreak(selectStart);
return true;
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
INSERT_PARAGRAPH_COMMAND,
Expand All @@ -239,7 +240,7 @@ export function registerPlainText(
selection.insertLineBreak();
return true;
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
KEY_ARROW_LEFT_COMMAND,
Expand All @@ -257,7 +258,7 @@ export function registerPlainText(
}
return false;
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
KEY_ARROW_RIGHT_COMMAND,
Expand All @@ -275,7 +276,7 @@ export function registerPlainText(
}
return false;
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
KEY_BACKSPACE_COMMAND,
Expand All @@ -287,7 +288,7 @@ export function registerPlainText(
event.preventDefault();
return editor.dispatchCommand(DELETE_CHARACTER_COMMAND, true);
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
KEY_DELETE_COMMAND,
Expand All @@ -299,7 +300,7 @@ export function registerPlainText(
event.preventDefault();
return editor.dispatchCommand(DELETE_CHARACTER_COMMAND, false);
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
KEY_ENTER_COMMAND,
Expand All @@ -313,7 +314,7 @@ export function registerPlainText(
}
return editor.dispatchCommand(INSERT_LINE_BREAK_COMMAND);
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
COPY_COMMAND,
Expand All @@ -325,7 +326,7 @@ export function registerPlainText(
onCopyForPlainText(event, editor);
return true;
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
CUT_COMMAND,
Expand All @@ -337,7 +338,7 @@ export function registerPlainText(
onCutForPlainText(event, editor);
return true;
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
PASTE_COMMAND,
Expand All @@ -349,7 +350,7 @@ export function registerPlainText(
onPasteForPlainText(event, editor);
return true;
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
DROP_COMMAND,
Expand All @@ -362,7 +363,7 @@ export function registerPlainText(
event.preventDefault();
return true;
},
0,
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
DRAGSTART_COMMAND,
Expand All @@ -375,7 +376,7 @@ export function registerPlainText(
event.preventDefault();
return true;
},
0,
COMMAND_PRIORITY_EDITOR,
),
);
initializeEditor(editor, initialEditorState);
Expand Down
14 changes: 4 additions & 10 deletions packages/lexical-playground/src/nodes/EquationNode.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@
* @flow strict
*/

import type {
CommandListenerHighPriority,
EditorConfig,
LexicalNode,
NodeKey,
} from 'lexical';
import type {EditorConfig, LexicalNode, NodeKey} from 'lexical';

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {mergeRegister} from '@lexical/utils';
import {
$getNodeByKey,
COMMAND_PRIORITY_HIGH,
DecoratorNode,
KEY_ESCAPE_COMMAND,
SELECTION_CHANGE_COMMAND,
Expand All @@ -28,8 +24,6 @@ import {useCallback, useEffect, useRef, useState} from 'react';
import EquationEditor from '../ui/EquationEditor';
import KatexRenderer from '../ui/KatexRenderer';

const HighPriority: CommandListenerHighPriority = 3;

type EquationComponentProps = {
equation: string,
inline: boolean,
Expand Down Expand Up @@ -75,7 +69,7 @@ function EquationComponent({
}
return false;
},
HighPriority,
COMMAND_PRIORITY_HIGH,
),
editor.registerCommand(
KEY_ESCAPE_COMMAND,
Expand All @@ -88,7 +82,7 @@ function EquationComponent({
}
return false;
},
HighPriority,
COMMAND_PRIORITY_HIGH,
),
);
}
Expand Down
28 changes: 14 additions & 14 deletions packages/lexical-playground/src/nodes/ExcalidrawNode/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@
* @flow strict
*/

import type {
CommandListenerLowPriority,
EditorConfig,
LexicalEditor,
LexicalNode,
NodeKey,
} from 'lexical';
import type {EditorConfig, LexicalEditor, LexicalNode, NodeKey} from 'lexical';

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import useLexicalNodeSelection from '@lexical/react/useLexicalNodeSelection';
Expand All @@ -23,6 +17,7 @@ import {
$getSelection,
$isNodeSelection,
CLICK_COMMAND,
COMMAND_PRIORITY_LOW,
DecoratorNode,
KEY_BACKSPACE_COMMAND,
KEY_DELETE_COMMAND,
Expand All @@ -33,8 +28,6 @@ import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import ExcalidrawImage from './ExcalidrawImage';
import ExcalidrawModal from './ExcalidrawModal';

const LowPriority: CommandListenerLowPriority = 1;

function ExcalidrawComponent({
nodeKey,
data,
Expand Down Expand Up @@ -87,10 +80,18 @@ function ExcalidrawComponent({

return false;
},
LowPriority,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
KEY_DELETE_COMMAND,
onDelete,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
KEY_BACKSPACE_COMMAND,
onDelete,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(KEY_DELETE_COMMAND, onDelete, LowPriority),
editor.registerCommand(KEY_BACKSPACE_COMMAND, onDelete, LowPriority),
);
}, [clearSelection, editor, isSelected, onDelete, setSelected]);

Expand Down Expand Up @@ -131,8 +132,7 @@ function ExcalidrawComponent({
/>
<button
ref={buttonRef}
className={`excalidraw-button ${isSelected ? 'selected' : ''}`}
>
className={`excalidraw-button ${isSelected ? 'selected' : ''}`}>
<ExcalidrawImage className="image" elements={elements} />
</button>
</>
Expand Down
Loading

0 comments on commit 682fbfb

Please sign in to comment.