-
-
Notifications
You must be signed in to change notification settings - Fork 634
/
Engine.ts
116 lines (96 loc) · 2.75 KB
/
Engine.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { IEngineProps } from '../types'
import { ITreeNode, TreeNode } from './TreeNode'
import { Workbench } from './Workbench'
import { Cursor } from './Cursor'
import { DragSource, GlobalDragSource } from './DragSource'
import { Keyboard } from './Keyboard'
import { Screen, ScreenType } from './Screen'
import { Event, uid } from '@designable/shared'
/**
* 设计器引擎
*/
export class Engine extends Event {
id: string
source: DragSource = GlobalDragSource
props: IEngineProps<Engine>
cursor: Cursor
workbench: Workbench
keyboard: Keyboard
screen: Screen
constructor(props: IEngineProps<Engine>) {
super(props)
this.props = {
...Engine.defaultProps,
...props,
}
this.init()
this.id = uid()
}
init() {
this.workbench = new Workbench(this)
this.screen = new Screen(this)
this.cursor = new Cursor(this)
this.keyboard = new Keyboard(this)
}
setCurrentTree(tree?: ITreeNode) {
if (this.workbench.currentWorkspace) {
this.workbench.currentWorkspace.operation.tree.from(tree)
}
}
getCurrentTree() {
return this.workbench?.currentWorkspace?.operation?.tree
}
getAllSelectedNodes() {
let results: TreeNode[] = []
for (let i = 0; i < this.workbench.workspaces.length; i++) {
const workspace = this.workbench.workspaces[i]
results = results.concat(workspace.operation.getSelectedNodes())
}
return results
}
findNodeById(id: string) {
for (let i = 0; i < this.workbench.workspaces.length; i++) {
const workspace = this.workbench.workspaces[i]
const node = workspace.operation.tree.findById(id)
if (node) {
return node
}
}
}
findSourceNodeById(id: string) {
return this.source.tree.findById(id)
}
findDraggingNodes(): TreeNode[] {
const results = []
this.workbench.eachWorkspace((workspace) => {
workspace.operation.viewportDragon.dragNodes?.forEach((node) => {
if (!results.includes(node)) {
results.push(node)
}
})
})
return results
}
createNode(node: ITreeNode, parent?: TreeNode) {
return new TreeNode(node, parent)
}
mount() {
this.attachEvents(window)
}
unmount() {
this.destroy()
}
static defaultProps: IEngineProps<Engine> = {
shortcuts: [],
effects: [],
drivers: [],
sourceIdAttrName: 'data-designer-source-id',
nodeIdAttrName: 'data-designer-node-id',
contentEditableAttrName: 'data-content-editable',
contentEditableNodeIdAttrName: 'data-content-editable-node-id',
clickStopPropagationAttrName: 'data-click-stop-propagation',
nodeHelpersIdAttrName: 'data-designer-node-helpers-id',
outlineNodeIdAttrName: 'data-designer-outline-node-id',
defaultScreenType: ScreenType.PC,
}
}