2024-05-27 15:23:45 -04:00
|
|
|
import {HeadingNode, QuoteNode} from '@lexical/rich-text';
|
2024-05-28 10:09:50 -04:00
|
|
|
import {CalloutNode} from './callout';
|
2024-07-16 11:36:08 -04:00
|
|
|
import {
|
|
|
|
ElementNode,
|
|
|
|
KlassConstructor,
|
|
|
|
LexicalNode,
|
|
|
|
LexicalNodeReplacement, NodeMutation,
|
|
|
|
ParagraphNode
|
|
|
|
} from "lexical";
|
2024-05-28 10:09:50 -04:00
|
|
|
import {CustomParagraphNode} from "./custom-paragraph";
|
2024-05-29 15:38:31 -04:00
|
|
|
import {LinkNode} from "@lexical/link";
|
2024-06-03 11:56:31 -04:00
|
|
|
import {ImageNode} from "./image";
|
2024-06-06 09:43:50 -04:00
|
|
|
import {DetailsNode, SummaryNode} from "./details";
|
2024-06-19 11:14:20 -04:00
|
|
|
import {ListItemNode, ListNode} from "@lexical/list";
|
2024-06-21 08:47:47 -04:00
|
|
|
import {TableCellNode, TableNode, TableRowNode} from "@lexical/table";
|
2024-06-24 15:50:17 -04:00
|
|
|
import {CustomTableNode} from "./custom-table";
|
2024-06-27 10:48:06 -04:00
|
|
|
import {HorizontalRuleNode} from "./horizontal-rule";
|
2024-07-02 09:46:30 -04:00
|
|
|
import {CodeBlockNode} from "./code-block";
|
2024-07-03 05:28:04 -04:00
|
|
|
import {DiagramNode} from "./diagram";
|
2024-07-16 11:36:08 -04:00
|
|
|
import {EditorUiContext} from "../ui/framework/core";
|
2024-07-25 11:25:08 -04:00
|
|
|
import {MediaNode} from "./media";
|
2024-07-30 09:42:19 -04:00
|
|
|
import {CustomListItemNode} from "./custom-list-item";
|
2024-08-09 06:24:25 -04:00
|
|
|
import {CustomTableCellNode} from "./custom-table-cell";
|
|
|
|
import {CustomTableRowNode} from "./custom-table-row";
|
2024-08-11 11:08:51 -04:00
|
|
|
import {CustomHeadingNode} from "./custom-heading";
|
|
|
|
import {CustomQuoteNode} from "./custom-quote";
|
|
|
|
import {CustomListNode} from "./custom-list";
|
2024-05-27 15:23:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the nodes for lexical.
|
|
|
|
*/
|
2024-05-28 10:09:50 -04:00
|
|
|
export function getNodesForPageEditor(): (KlassConstructor<typeof LexicalNode> | LexicalNodeReplacement)[] {
|
2024-05-27 15:23:45 -04:00
|
|
|
return [
|
2024-08-11 11:08:51 -04:00
|
|
|
CalloutNode,
|
|
|
|
CustomHeadingNode,
|
|
|
|
CustomQuoteNode,
|
|
|
|
CustomListNode,
|
2024-08-18 11:51:08 -04:00
|
|
|
CustomListItemNode, // TODO - Alignment?
|
2024-06-24 15:50:17 -04:00
|
|
|
CustomTableNode,
|
2024-08-09 06:24:25 -04:00
|
|
|
CustomTableRowNode,
|
2024-08-05 10:08:52 -04:00
|
|
|
CustomTableCellNode,
|
2024-08-18 11:51:08 -04:00
|
|
|
ImageNode, // TODO - Alignment
|
2024-06-27 10:48:06 -04:00
|
|
|
HorizontalRuleNode,
|
2024-06-06 09:43:50 -04:00
|
|
|
DetailsNode, SummaryNode,
|
2024-07-02 09:46:30 -04:00
|
|
|
CodeBlockNode,
|
2024-07-03 05:28:04 -04:00
|
|
|
DiagramNode,
|
2024-08-18 11:51:08 -04:00
|
|
|
MediaNode, // TODO - Alignment
|
|
|
|
CustomParagraphNode,
|
2024-06-24 15:50:17 -04:00
|
|
|
LinkNode,
|
2024-05-28 10:09:50 -04:00
|
|
|
{
|
|
|
|
replace: ParagraphNode,
|
|
|
|
with: (node: ParagraphNode) => {
|
|
|
|
return new CustomParagraphNode();
|
|
|
|
}
|
2024-05-29 15:38:31 -04:00
|
|
|
},
|
2024-08-11 11:08:51 -04: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 06:24:25 -04:00
|
|
|
{
|
|
|
|
replace: ListItemNode,
|
|
|
|
with: (node: ListItemNode) => {
|
|
|
|
return new CustomListItemNode(node.__value, node.__checked);
|
|
|
|
}
|
|
|
|
},
|
2024-06-24 15:50:17 -04:00
|
|
|
{
|
|
|
|
replace: TableNode,
|
|
|
|
with(node: TableNode) {
|
|
|
|
return new CustomTableNode();
|
|
|
|
}
|
|
|
|
},
|
2024-07-30 09:42:19 -04:00
|
|
|
{
|
2024-08-09 06:24:25 -04:00
|
|
|
replace: TableRowNode,
|
|
|
|
with(node: TableRowNode) {
|
|
|
|
return new CustomTableRowNode();
|
2024-07-30 09:42:19 -04:00
|
|
|
}
|
2024-08-05 10:08:52 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
replace: TableCellNode,
|
|
|
|
with: (node: TableCellNode) => {
|
|
|
|
const cell = new CustomTableCellNode(
|
|
|
|
node.__headerState,
|
|
|
|
node.__colSpan,
|
|
|
|
node.__width,
|
|
|
|
);
|
|
|
|
cell.__rowSpan = node.__rowSpan;
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
},
|
2024-05-27 15:23:45 -04:00
|
|
|
];
|
|
|
|
}
|
2024-05-28 13:04:48 -04:00
|
|
|
|
2024-07-16 11:36:08 -04: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-28 13:04:48 -04:00
|
|
|
export type LexicalNodeMatcher = (node: LexicalNode|null|undefined) => boolean;
|
|
|
|
export type LexicalElementNodeCreator = () => ElementNode;
|