2024-05-28 03:23:45 +08:00
|
|
|
import {HeadingNode, QuoteNode} from '@lexical/rich-text';
|
2024-05-28 22:09:50 +08:00
|
|
|
import {CalloutNode} from './callout';
|
2024-07-16 23:36:08 +08:00
|
|
|
import {
|
|
|
|
ElementNode,
|
|
|
|
KlassConstructor,
|
|
|
|
LexicalNode,
|
|
|
|
LexicalNodeReplacement, NodeMutation,
|
|
|
|
ParagraphNode
|
|
|
|
} from "lexical";
|
2024-05-28 22:09:50 +08:00
|
|
|
import {CustomParagraphNode} from "./custom-paragraph";
|
2024-05-30 03:38:31 +08:00
|
|
|
import {LinkNode} from "@lexical/link";
|
2024-06-03 23:56:31 +08:00
|
|
|
import {ImageNode} from "./image";
|
2024-06-06 21:43:50 +08:00
|
|
|
import {DetailsNode, SummaryNode} from "./details";
|
2024-06-19 23:14:20 +08:00
|
|
|
import {ListItemNode, ListNode} from "@lexical/list";
|
2024-06-21 20:47:47 +08:00
|
|
|
import {TableCellNode, TableNode, TableRowNode} from "@lexical/table";
|
2024-06-25 03:50:17 +08:00
|
|
|
import {CustomTableNode} from "./custom-table";
|
2024-06-27 22:48:06 +08:00
|
|
|
import {HorizontalRuleNode} from "./horizontal-rule";
|
2024-07-02 21:46:30 +08:00
|
|
|
import {CodeBlockNode} from "./code-block";
|
2024-07-03 17:28:04 +08:00
|
|
|
import {DiagramNode} from "./diagram";
|
2024-07-16 23:36:08 +08:00
|
|
|
import {EditorUiContext} from "../ui/framework/core";
|
2024-07-25 23:25:08 +08:00
|
|
|
import {MediaNode} from "./media";
|
2024-05-28 03:23:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the nodes for lexical.
|
|
|
|
*/
|
2024-05-28 22:09:50 +08:00
|
|
|
export function getNodesForPageEditor(): (KlassConstructor<typeof LexicalNode> | LexicalNodeReplacement)[] {
|
2024-05-28 03:23:45 +08:00
|
|
|
return [
|
2024-05-28 22:09:50 +08:00
|
|
|
CalloutNode, // Todo - Create custom
|
|
|
|
HeadingNode, // Todo - Create custom
|
|
|
|
QuoteNode, // Todo - Create custom
|
2024-06-19 23:14:20 +08:00
|
|
|
ListNode, // Todo - Create custom
|
|
|
|
ListItemNode,
|
2024-06-25 03:50:17 +08:00
|
|
|
CustomTableNode,
|
2024-06-21 20:47:47 +08:00
|
|
|
TableRowNode,
|
|
|
|
TableCellNode,
|
2024-06-03 23:56:31 +08:00
|
|
|
ImageNode,
|
2024-06-27 22:48:06 +08:00
|
|
|
HorizontalRuleNode,
|
2024-06-06 21:43:50 +08:00
|
|
|
DetailsNode, SummaryNode,
|
2024-07-02 21:46:30 +08:00
|
|
|
CodeBlockNode,
|
2024-07-03 17:28:04 +08:00
|
|
|
DiagramNode,
|
2024-07-25 23:25:08 +08:00
|
|
|
MediaNode,
|
2024-05-28 22:09:50 +08:00
|
|
|
CustomParagraphNode,
|
2024-06-25 03:50:17 +08:00
|
|
|
LinkNode,
|
2024-05-28 22:09:50 +08:00
|
|
|
{
|
|
|
|
replace: ParagraphNode,
|
|
|
|
with: (node: ParagraphNode) => {
|
|
|
|
return new CustomParagraphNode();
|
|
|
|
}
|
2024-05-30 03:38:31 +08:00
|
|
|
},
|
2024-06-25 03:50:17 +08:00
|
|
|
{
|
|
|
|
replace: TableNode,
|
|
|
|
with(node: TableNode) {
|
|
|
|
return new CustomTableNode();
|
|
|
|
}
|
|
|
|
},
|
2024-05-28 03:23:45 +08:00
|
|
|
];
|
|
|
|
}
|
2024-05-29 01:04:48 +08:00
|
|
|
|
2024-07-16 23:36:08 +08:00
|
|
|
export function registerCommonNodeMutationListeners(context: EditorUiContext): void {
|
|
|
|
const decorated = [ImageNode, CodeBlockNode, DiagramNode];
|
|
|
|
|
|
|
|
const decorationDestroyListener = (mutations: Map<string, NodeMutation>): void => {
|
|
|
|
for (let [nodeKey, mutation] of mutations) {
|
|
|
|
if (mutation === "destroyed") {
|
|
|
|
const decorator = context.manager.getDecoratorByNodeKey(nodeKey);
|
|
|
|
if (decorator) {
|
|
|
|
decorator.destroy(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let decoratedNode of decorated) {
|
|
|
|
// Have to pass a unique function here since they are stored by lexical keyed on listener function.
|
|
|
|
context.editor.registerMutationListener(decoratedNode, (mutations) => decorationDestroyListener(mutations));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 01:04:48 +08:00
|
|
|
export type LexicalNodeMatcher = (node: LexicalNode|null|undefined) => boolean;
|
|
|
|
export type LexicalElementNodeCreator = () => ElementNode;
|