@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
53 lines (47 loc) • 1.57 kB
JavaScript
/**
* 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 { TRANSFORMERS, registerMarkdownShortcuts } from '@lexical/markdown';
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { HorizontalRuleNode, $isHorizontalRuleNode, $createHorizontalRuleNode } from '@lexical/react/LexicalHorizontalRuleNode';
import { useEffect } from 'react';
/**
* 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.
*
*/
const HR = {
dependencies: [HorizontalRuleNode],
export: node => {
return $isHorizontalRuleNode(node) ? '***' : null;
},
regExp: /^(---|\*\*\*|___)\s?$/,
replace: (parentNode, _1, _2, isImport) => {
const line = $createHorizontalRuleNode();
// TODO: Get rid of isImport flag
if (isImport || parentNode.getNextSibling() != null) {
parentNode.replace(line);
} else {
parentNode.insertBefore(line);
}
line.selectNext();
},
type: 'element'
};
const DEFAULT_TRANSFORMERS = [HR, ...TRANSFORMERS];
function MarkdownShortcutPlugin({
transformers = DEFAULT_TRANSFORMERS
}) {
const [editor] = useLexicalComposerContext();
useEffect(() => {
return registerMarkdownShortcuts(editor, transformers);
}, [editor, transformers]);
return null;
}
export { DEFAULT_TRANSFORMERS, MarkdownShortcutPlugin };