import Stage from "./stage" import Adder from "./adder" import Undo from "./undo" import Liner, { Position } from "./line" import Toolbar, { ToolEvent } from "./toolbar" import { Text, Rect, Circle, Diamond } from "./node/index" type Node = Rect | Circle | Diamond | Text interface Link { orient: string node: Node } interface Space { width: number height: number } interface Item { title: string type?: string color?: string width?: number height?: number children?: { left?: Array top?: Array right?: Array bottom?: Array } } interface TreeItem { node: Node spaceHeight: number spaceWidth: number children?: { left: Array top: Array right: Array bottom: Array } } interface Option { type: 'tree' | 'mind' orient: 'horizontal' | 'vertical' spaceWidth: number spaceHeight: number lineSpace: number nodeWidth: number nodeHeight: number } function reverseOrientation(orient: Position): Position { switch (orient) { case 'left': return 'right' case 'right': return 'left' case 'top': return 'bottom' default: return 'top' } } export class Mind { id: string = 'mind' stage2d: Stage //图形列表 nodes: Array = [] //需要复原的动画 recoverAnimateList = [] data: any | null = [] nodeTree: TreeItem[] = [] adder: Adder toolbar: Toolbar liner: Liner // 当前选中的节点 selected: Node | null // 当前拖动的节点 dragged: Node | null // 鼠标是否按下 isMousedown: boolean rect: Space = { width: 100, height: 100 } option: Option constructor(element: HTMLDivElement | null) { if (element) { this.id = element.id || 'mind' this.stage2d = new Stage(element) this.liner = new Liner(this.stage2d) } } // 转化为 mind 格式的 data parseData(data: any[], orient: string): Item[] { const todata = (data: any[], orient: string): Item[] => { for (let i = 0; i < data.length; i++) { const item = data[i] if (item.children) { if (Array.isArray(item.children)) { item.children = { [orient]: todata(item.children, orient) } } else { if (item.children.top) { item.children.top = todata(item.children.top, 'top') } if (item.children.right) { item.children.right = todata(item.children.right, 'right') } if (item.children.bottom) { item.children.bottom = todata(item.children.bottom, 'bottom') } if (item.children.left) { item.children.left = todata(item.children.left, 'left') } } } } return data } return todata(data, orient) } initOption(option: Partial