import { DocumentModel as InnerDocumentModel, Node as InnerNode, getConvertedExtraKey, } from '@alilc/lowcode-designer'; import { CompositeValue, NodeSchema, TransformStage } from '@alilc/lowcode-types'; import Prop from './prop'; import Props from './props'; import DocumentModel from './document-model'; import NodeChildren from './node-children'; import ComponentMeta from './component-meta'; import SettingTopEntry from './setting-top-entry'; import { documentSymbol, nodeSymbol } from './symbols'; const shellNodeSymbol = Symbol('shellNodeSymbol'); export default class Node { private readonly [documentSymbol]: InnerDocumentModel; private readonly [nodeSymbol]: InnerNode; private _id: string; constructor(node: InnerNode) { this[nodeSymbol] = node; this[documentSymbol] = node.document; this._id = this[nodeSymbol].id; } static create(node: InnerNode | null | undefined) { if (!node) return null; // @ts-ignore ç´æ¥è¿åå·²æè½½ç shell node å®ä¾ if (node[shellNodeSymbol]) return node[shellNodeSymbol]; const shellNode = new Node(node); // @ts-ignore æè½½ shell node å®ä¾ node[shellNodeSymbol] = shellNode; return shellNode; } /** * èç¹ id */ get id() { return this._id; } /** * set id */ set id(id: string) { this._id = id; } /** * èç¹æ é¢ */ get title() { return this[nodeSymbol].title; } /** * æ¯å¦ä¸ºã容å¨åãèç¹ */ get isContainer() { return this[nodeSymbol].isContainer(); } /** * æ¯å¦ä¸ºæ ¹èç¹ */ get isRoot() { return this[nodeSymbol].isRoot(); } /** * æ¯å¦ä¸ºç©ºèç¹ï¼æ children æè children ä¸ºç©ºï¼ */ get isEmpty() { return this[nodeSymbol].isEmpty(); } /** * æ¯å¦ä¸º Page èç¹ */ get isPage() { return this[nodeSymbol].isPage(); } /** * æ¯å¦ä¸º Component èç¹ */ get isComponent() { return this[nodeSymbol].isComponent(); } /** * æ¯å¦ä¸ºãæ¨¡ææ¡ãèç¹ */ get isModal() { return this[nodeSymbol].isModal(); } /** * æ¯å¦ä¸ºææ§½èç¹ */ get isSlot() { return this[nodeSymbol].isSlot(); } /** * æ¯å¦ä¸ºç¶ç±»/忝èç¹ */ get isParental() { return this[nodeSymbol].isParental(); } /** * æ¯å¦ä¸ºå¶åèç¹ */ get isLeaf() { return this[nodeSymbol].isLeaf(); } /** * judge if it is a node or not */ get isNode() { return true; } /** * 䏿 */ get index() { return this[nodeSymbol].index; } /** * 徿 */ get icon() { return this[nodeSymbol].icon; } /** * èç¹æå¨æ çå±çº§æ·±åº¦ï¼æ ¹èç¹æ·±åº¦ä¸º 0 */ get zLevel() { return this[nodeSymbol].zLevel; } /** * èç¹ componentName */ get componentName() { return this[nodeSymbol].componentName; } /** * èç¹çç©æå æ°æ® */ get componentMeta() { return ComponentMeta.create(this[nodeSymbol].componentMeta); } /** * è·åèç¹æå±çææ¡£æ¨¡å对象 * @returns */ get document() { return DocumentModel.create(this[documentSymbol]); } /** * è·åå½åèç¹çåä¸ä¸ªå å¼èç¹ * @returns */ get prevSibling(): Node | null { return Node.create(this[nodeSymbol].prevSibling); } /** * è·åå½åèç¹çåä¸ä¸ªå å¼èç¹ * @returns */ get nextSibling(): Node | null { return Node.create(this[nodeSymbol].nextSibling); } /** * è·åå½åèç¹çç¶äº²èç¹ * @returns */ get parent(): Node | null { return Node.create(this[nodeSymbol].parent); } /** * è·åå½åèç¹çå©åèç¹æ¨¡å * @returns */ get children() { return NodeChildren.create(this[nodeSymbol].children); } /** * èç¹ä¸æè½½çææ§½èç¹ä»¬ */ get slots(): Node[] { return this[nodeSymbol].slots.map((node: InnerNode) => Node.create(node)!); } /** * å½åèç¹ä¸ºææ§½èç¹æ¶ï¼è¿åèç¹å¯¹åºç屿§å®ä¾ */ get slotFor() { return Prop.create(this[nodeSymbol].slotFor); } /** * è¿åèç¹ç屿§é */ get props() { return Props.create(this[nodeSymbol].props); } /** * è¿åèç¹ç屿§é */ get propsData() { return this[nodeSymbol].propsData; } /** * è·å符åæå»ºåè®®-èç¹ schema ç»æ */ get schema(): any { return this[nodeSymbol].schema; } get settingEntry(): any { return SettingTopEntry.create(this[nodeSymbol].settingEntry as any); } /** * @deprecated use .children instead */ getChildren() { return this.children; } /** * è·åèç¹å®ä¾å¯¹åºç dom èç¹ */ getDOMNode() { return this[nodeSymbol].getDOMNode(); } /** * æ§è¡æ°å¢ãå é¤ãæåºçæä½ * @param remover * @param adder * @param sorter */ mergeChildren( remover: (node: Node, idx: number) => boolean, adder: (children: Node[]) => any, sorter: (firstNode: Node, secondNode: Node) => number, ) { return this.children?.mergeChildren(remover, adder, sorter); } /** * è¿åèç¹ç尺寸ãä½ç½®ä¿¡æ¯ * @returns */ getRect() { return this[nodeSymbol].getRect(); } /** * æ¯å¦ææè½½ææ§½èç¹ * @returns */ hasSlots() { return this[nodeSymbol].hasSlots(); } /** * æ¯å¦è®¾å®äºæ¸²ææ¡ä»¶ * @returns */ hasCondition() { return this[nodeSymbol].hasCondition(); } /** * æ¯å¦è®¾å®äºå¾ªç¯æ°æ® * @returns */ hasLoop() { return this[nodeSymbol].hasLoop(); } getVisible() { return this[nodeSymbol].getVisible(); } isConditionalVisible() { return this[nodeSymbol].isConditionalVisible(); } /** * @deprecated use .props instead */ getProps() { return this.props; } contains(node: Node) { return this[nodeSymbol].contains(node[nodeSymbol]); } /** * è·åæå® path ç屿§æ¨¡åå®ä¾ * @param path 屿§è·¯å¾ï¼æ¯æ a / a.b / a.0 çæ ¼å¼ * @returns */ getProp(path: string): Prop | null { return Prop.create(this[nodeSymbol].getProp(path)); } /** * è·åæå® path ç屿§æ¨¡åå®ä¾å¼ * @param path 屿§è·¯å¾ï¼æ¯æ a / a.b / a.0 çæ ¼å¼ * @returns */ getPropValue(path: string) { return this.getProp(path)?.getValue(); } /** * è·åæå® path ç屿§æ¨¡åå®ä¾ï¼ * 注ï¼å¯¼åºæ¶ï¼ä¸åäºæ®é屿§ï¼è¯¥å±æ§å¹¶ä¸æè½½å¨ props ä¹ä¸ï¼èæ¯ä¸ props å级 * @param path 屿§è·¯å¾ï¼æ¯æ a / a.b / a.0 çæ ¼å¼ * @returns */ getExtraProp(path: string): Prop | null { return Prop.create(this[nodeSymbol].getProp(getConvertedExtraKey(path))); } /** * è·åæå® path ç屿§æ¨¡åå®ä¾ï¼ * 注ï¼å¯¼åºæ¶ï¼ä¸åäºæ®é屿§ï¼è¯¥å±æ§å¹¶ä¸æè½½å¨ props ä¹ä¸ï¼èæ¯ä¸ props å级 * @param path 屿§è·¯å¾ï¼æ¯æ a / a.b / a.0 çæ ¼å¼ * @returns */ getExtraPropValue(path: string) { return this.getExtraProp(path)?.getValue(); } /** * 设置æå® path ç屿§æ¨¡åå®ä¾å¼ * @param path 屿§è·¯å¾ï¼æ¯æ a / a.b / a.0 çæ ¼å¼ * @param value å¼ * @returns */ setPropValue(path: string, value: CompositeValue) { return this.getProp(path)?.setValue(value); } /** * 设置æå® path ç屿§æ¨¡åå®ä¾å¼ * @param path 屿§è·¯å¾ï¼æ¯æ a / a.b / a.0 çæ ¼å¼ * @param value å¼ * @returns */ setExtraPropValue(path: string, value: CompositeValue) { return this.getExtraProp(path)?.setValue(value); } /** * å¯¼å ¥èç¹æ°æ® * @param data */ importSchema(data: NodeSchema) { this[nodeSymbol].import(data); } /** * 导åºèç¹æ°æ® * @param stage * @param options * @returns */ exportSchema(stage: TransformStage = TransformStage.Render, options?: any) { return this[nodeSymbol].export(stage, options); } /** * 卿å®ä½ç½®ä¹åæå ¥ä¸ä¸ªèç¹ * @param node * @param ref * @param useMutator */ insertBefore(node: Node, ref?: Node | undefined, useMutator?: boolean) { this[nodeSymbol].insertBefore(node[nodeSymbol] || node, ref?.[nodeSymbol], useMutator); } /** * 卿å®ä½ç½®ä¹åæå ¥ä¸ä¸ªèç¹ * @param node * @param ref * @param useMutator */ insertAfter(node: Node, ref?: Node | undefined, useMutator?: boolean) { this[nodeSymbol].insertAfter(node[nodeSymbol] || node, ref?.[nodeSymbol], useMutator); } /** * æ¿æ¢æå®èç¹ * @param node å¾ æ¿æ¢çåèç¹ * @param data ç¨ä½æ¿æ¢çèç¹å¯¹è±¡æè èç¹æè¿° * @returns */ replaceChild(node: Node, data: any) { return Node.create(this[nodeSymbol].replaceChild(node[nodeSymbol], data)); } /** * å°å½åèç¹æ¿æ¢ææå®èç¹æè¿° * @param schema */ replaceWith(schema: NodeSchema) { this[nodeSymbol].replaceWith(schema); } /** * éä¸å½åèç¹å®ä¾ */ select() { this[nodeSymbol].select(); } /** * 设置æ¬åæ * @param flag */ hover(flag = true) { this[nodeSymbol].hover(flag); } /** * å é¤å½åèç¹å®ä¾ */ remove() { this[nodeSymbol].remove(); } }