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-07-30 21:42:19 +08:00
|
|
|
import {CustomListItemNode} from "./custom-list-item";
|
2024-08-09 18:24:25 +08:00
|
|
|
import {CustomTableCellNode} from "./custom-table-cell";
|
|
|
|
import {CustomTableRowNode} from "./custom-table-row";
|
2024-08-11 23:08:51 +08:00
|
|
|
import {CustomHeadingNode} from "./custom-heading";
|
|
|
|
import {CustomQuoteNode} from "./custom-quote";
|
|
|
|
import {CustomListNode} from "./custom-list";
|
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-08-11 23:08:51 +08:00
|
|
|
CalloutNode,
|
|
|
|
CustomHeadingNode,
|
|
|
|
CustomQuoteNode,
|
|
|
|
CustomListNode,
|
2024-08-18 23:51:08 +08:00
|
|
|
CustomListItemNode, // TODO - Alignment?
|
2024-06-25 03:50:17 +08:00
|
|
|
CustomTableNode,
|
2024-08-09 18:24:25 +08:00
|
|
|
CustomTableRowNode,
|
2024-08-05 22:08:52 +08:00
|
|
|
CustomTableCellNode,
|
2024-08-18 23:51:08 +08:00
|
|
|
ImageNode, // TODO - Alignment
|
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-08-18 23:51:08 +08:00
|
|
|
MediaNode, // TODO - Alignment
|
|
|
|
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-08-11 23:08:51 +08:00
|
|
|
{
|
|
|
|
replace: HeadingNode,
|
|
|
|
with: (node: HeadingNode) => {
|
|
|
|
return new CustomHeadingNode(node.__tag);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
replace: QuoteNode,
|
|
|
|
with: (node: QuoteNode) => {
|
|
|
|
return new CustomQuoteNode();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
replace: ListNode,
|
|
|
|
with: (node: ListNode) => {
|
|
|
|
return new CustomListNode(node.getListType(), node.getStart());
|
|
|
|
}
|
|
|
|
},
|
2024-08-09 18:24:25 +08:00
|
|
|
{
|
|
|
|
replace: ListItemNode,
|
|
|
|
with: (node: ListItemNode) => {
|
|
|
|
return new CustomListItemNode(node.__value, node.__checked);
|
|
|
|
}
|
|
|
|
},
|
2024-06-25 03:50:17 +08:00
|
|
|
{
|
|
|
|
replace: TableNode,
|
|
|
|
with(node: TableNode) {
|
|
|
|
return new CustomTableNode();
|
|
|
|
}
|
|
|
|
},
|
2024-07-30 21:42:19 +08:00
|
|
|
{
|
2024-08-09 18:24:25 +08:00
|
|
|
replace: TableRowNode,
|
|
|
|
with(node: TableRowNode) {
|
|
|
|
return new CustomTableRowNode();
|
2024-07-30 21:42:19 +08:00
|
|
|
}
|
2024-08-05 22:08:52 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
replace: TableCellNode,
|
|
|
|
with: (node: TableCellNode) => {
|
|
|
|
const cell = new CustomTableCellNode(
|
|
|
|
node.__headerState,
|
|
|
|
node.__colSpan,
|
|
|
|
node.__width,
|
|
|
|
);
|
|
|
|
cell.__rowSpan = node.__rowSpan;
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
},
|
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;
|