95 lines
6.1 KiB
TypeScript
95 lines
6.1 KiB
TypeScript
/**
|
|
* 旧画布的元素创建类体系。类只提供内置类型、默认样式/尺寸和能力,不持有editor或DOM,
|
|
* 实际视图通过组合使用能力;新版可扩展registry与这些旧内置class并非同一个类型系统。
|
|
*/
|
|
import type { Node, XYPosition } from "@xyflow/react";
|
|
import type { ElementKind, CanvasNodeData } from "./canvas-bindings/compat-types";
|
|
export type {ElementKind,FrameLabelMode,TextStyle,CanvasNodeData} from "./canvas-bindings/compat-types";
|
|
|
|
/** 旧React Flow节点加本地pendingUpload标记;该标记在snapshot保存前会被过滤。 */
|
|
export type CanvasNode = Node<CanvasNodeData, "material" | "plainText" | "icon" | "frame" | "plainImage"> & { pendingUpload?: string };
|
|
/** 旧UI四项能力声明:缩放、旋转、连线、分支;实际组件借用它,不继承React组件类。 */
|
|
export type ElementCapabilities = { resizable: boolean; rotatable: boolean; connectable: boolean; branchable: boolean };
|
|
|
|
/** Business inheritance only: React views reuse these capabilities by composition. */
|
|
/** 所有旧元素的业务基类,默认240×72和plainText视图;子类声明kind/title并覆盖特定默认值。 */
|
|
export abstract class CanvasElement {
|
|
// 基类可缩放/旋转但不连线/分支;静态能力由类表读取,不为每个节点重复创建行为对象。
|
|
static readonly capabilities: ElementCapabilities = { resizable: true, rotatable: true, connectable: false, branchable: false };
|
|
abstract readonly kind: ElementKind;
|
|
abstract readonly title: string;
|
|
protected readonly nodeType: CanvasNode["type"] = "plainText";
|
|
protected readonly size = { width: 240, height: 72 };
|
|
readonly position: XYPosition;
|
|
readonly text: string;
|
|
readonly src?: string;
|
|
/** 构造业务创建参数;position在toNode输出时复制,尚未创建节点身份或渲染对象。 */
|
|
constructor(position: XYPosition, text = "", src?: string) {
|
|
this.position = position;
|
|
this.text = text;
|
|
this.src = src;
|
|
}
|
|
/** 每次转换都生成新UUID与独立position/data/style,字体默认随plain-text与卡片类型选择。 */
|
|
toNode(): CanvasNode {
|
|
return {
|
|
id: crypto.randomUUID(), type: this.nodeType, position: { ...this.position },
|
|
width: this.size.width, height: this.size.height,
|
|
style: { width: this.size.width, height: this.size.height },
|
|
data: { kind: this.kind, title: this.title, text: this.text, rotation: 0,
|
|
fontSize: this.kind === "plain-text" ? 24 : 13, ...(this.src ? { src: this.src } : {}) },
|
|
};
|
|
}
|
|
}
|
|
/** 文本/图片/脚本共享卡片视图、260×200尺寸和连线/分支能力。 */
|
|
export abstract class CardElement extends CanvasElement {
|
|
static override readonly capabilities: ElementCapabilities = { ...CanvasElement.capabilities, connectable: true, branchable: true };
|
|
protected override readonly nodeType = "material" as const;
|
|
protected override readonly size = { width: 260, height: 200 };
|
|
}
|
|
/** 文本卡片,只覆盖内容类型和默认标题,其余沿用CardElement。 */
|
|
export class TextCardElement extends CardElement { readonly kind = "text"; readonly title = "灵感笔记"; }
|
|
/** 图片卡片,与纯图片的不同是卡片布局及可分支能力。 */
|
|
export class ImageCardElement extends CardElement { readonly kind = "image"; readonly title = "视觉参考"; }
|
|
/** 脚本卡片,数据类型不同但复用同一卡片行为。 */
|
|
export class ScriptCardElement extends CardElement { readonly kind = "script"; readonly title = "脚本"; }
|
|
/** 无卡片外壳的文字元素,沿用基类尺寸和不连线能力。 */
|
|
export class PlainTextElement extends CanvasElement { readonly kind = "plain-text"; readonly title = "纯文本"; }
|
|
|
|
/** 图标用80×80图形视图,首次创建默认Star;具体SVG由图标加载器另读。 */
|
|
export class IconElement extends CanvasElement {
|
|
readonly kind = "icon"; readonly title = "图标";
|
|
protected override readonly nodeType = "icon" as const;
|
|
protected override readonly size = { width: 80, height: 80 };
|
|
/** 先用基类分配身份/几何,再补初始图标字段,不共享已创建节点对象。 */
|
|
override toNode(): CanvasNode { const node = super.toNode(); return { ...node, data: { ...node.data, icon: "Star" } }; }
|
|
}
|
|
|
|
/** 普通旧分区框640×400,不可旋转;子图入口/端口由其它流程按schema v2赋予。 */
|
|
export class FrameElement extends CanvasElement {
|
|
static override readonly capabilities: ElementCapabilities = { ...CanvasElement.capabilities, rotatable: false };
|
|
readonly kind = "frame"; readonly title = "未命名分区";
|
|
protected override readonly nodeType = "frame" as const;
|
|
protected override readonly size = { width: 640, height: 400 };
|
|
/** 分区置底且内容区不截获子元素指针,标题默认固定显示;这不是新建子board操作。 */
|
|
override toNode(): CanvasNode {
|
|
const node = super.toNode();
|
|
return { ...node, zIndex: -1, style: { ...node.style, pointerEvents: "none" }, data: { ...node.data, labelMode: "fixed" } };
|
|
}
|
|
}
|
|
|
|
/** 无卡片外壳的320×240图片,是否按原图比例调整由解码/创建尺寸流程负责。 */
|
|
export class PlainImageElement extends CanvasElement {
|
|
readonly kind = "plain-image"; readonly title = "图片";
|
|
protected override readonly nodeType = "plainImage" as const;
|
|
protected override readonly size = { width: 320, height: 240 };
|
|
}
|
|
|
|
/** 封闭内置kind→构造器表,是旧元素工厂入口,不接受任意未知kind动态执行。 */
|
|
export const elementClasses = { text: TextCardElement, image: ImageCardElement, script: ScriptCardElement, "plain-text": PlainTextElement, icon: IconElement, frame: FrameElement, "plain-image": PlainImageElement };
|
|
/** 按kind读取静态能力,调用方需先保证kind属于内置枚举。 */
|
|
export function elementCapabilities(kind: ElementKind) { return elementClasses[kind].capabilities; }
|
|
/** 创建指定内置类型的空内容节点,src仅作为创建参数传入;不自动上传文件或登记history。 */
|
|
export function createCanvasElement(kind: ElementKind, position: XYPosition, src?: string): CanvasNode {
|
|
return new elementClasses[kind](position, "", src).toNode();
|
|
}
|