@terrible-lexical/react
Version:
This package provides Lexical components and hooks for React applications.
66 lines (60 loc) • 1.62 kB
text/typescript
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type {LexicalEditor} from 'terrible-lexical';
import {
$handleListInsertParagraph,
INSERT_ORDERED_LIST_COMMAND,
INSERT_UNORDERED_LIST_COMMAND,
insertList,
REMOVE_LIST_COMMAND,
removeList,
} from '@terrible-lexical/list/src';
import {mergeRegister} from '@terrible-lexical/utils/src';
import {COMMAND_PRIORITY_LOW, INSERT_PARAGRAPH_COMMAND} from 'terrible-lexical';
import {useEffect} from 'react';
export function useList(editor: LexicalEditor): void {
useEffect(() => {
return mergeRegister(
editor.registerCommand(
INSERT_ORDERED_LIST_COMMAND,
() => {
insertList(editor, 'number');
return true;
},
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
INSERT_UNORDERED_LIST_COMMAND,
() => {
insertList(editor, 'bullet');
return true;
},
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
REMOVE_LIST_COMMAND,
() => {
removeList(editor);
return true;
},
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
INSERT_PARAGRAPH_COMMAND,
() => {
const hasHandledInsertParagraph = $handleListInsertParagraph();
if (hasHandledInsertParagraph) {
return true;
}
return false;
},
COMMAND_PRIORITY_LOW,
),
);
}, [editor]);
}