forked from opentiny/tiny-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.js
905 lines (759 loc) · 22.5 KB
/
container.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
import { reactive, toRaw, nextTick, shallowReactive } from 'vue'
import {
addScript as appendScript,
addStyle as appendStyle,
copyObject,
NODE_UID,
NODE_TAG,
NODE_LOOP,
NODE_INACTIVE_UID
} from '../../common'
import { useCanvas, useLayout, useTranslate, useMaterial } from '@opentiny/tiny-engine-meta-register'
import { utils } from '@opentiny/tiny-engine-utils'
import { isVsCodeEnv } from '@opentiny/tiny-engine-common/js/environments'
import Builtin from '../../render/src/builtin/builtin.json' //TODO 画布内外应该分开
export const POSITION = Object.freeze({
TOP: 'top',
BOTTOM: 'bottom',
LEFT: 'left',
RIGHT: 'right',
IN: 'in'
})
const initialDragState = {
keydown: false,
draging: false,
data: null,
position: null, // ghost位置
mouse: null, // iframe里鼠标位置
element: null,
offset: {}
}
export const canvasState = shallowReactive({
type: 'normal',
schema: null,
renderer: null, // 存放画布内的api
iframe: null,
loading: true,
current: null,
parent: null,
loopId: null
})
export const getRenderer = () => canvasState.renderer
export const getController = () => canvasState.controller
export const getDocument = () => canvasState.iframe.contentDocument
export const getWindow = () => canvasState.iframe.contentWindow
export const getCurrent = () => {
return {
schema: canvasState.current,
parent: canvasState.parent,
loopId: canvasState.loopId
}
}
export const getDesignMode = () => getRenderer()?.getDesignMode()
export const setDesignMode = (mode) => getRenderer()?.setDesignMode(mode)
export const getSchema = () => useCanvas().getPageSchema()
// 记录拖拽状态
export const dragState = reactive({
...initialDragState
})
export const initialRectState = {
top: 0,
height: 0,
width: 0,
left: 0,
schema: null,
configure: null,
componentName: ''
}
const initialLineState = {
top: 0,
height: 0,
width: 0,
left: 0,
position: '',
forbidden: false,
id: '',
config: null,
doc: null
}
// 选中画布中元素时的状态
export const selectState = reactive({
...initialRectState
})
// 鼠标移入画布中元素时的状态
export const hoverState = reactive({
...initialRectState
})
export const inactiveHoverState = reactive({
...initialRectState
})
// 拖拽时的位置状态
export const lineState = reactive({
...initialLineState
})
export const clearHover = () => {
Object.assign(hoverState, initialRectState, { slot: null })
Object.assign(inactiveHoverState, initialRectState, { slot: null })
}
export const clearSelect = () => {
canvasState.current = null
canvasState.parent = null
Object.assign(selectState, initialRectState)
// 临时借用 remote 事件出发 currentSchema 更新
canvasState?.emit?.('remove')
}
const smoothScroll = {
timmer: null,
/**
*
* @param {*} up 方向
* @param {*} step 每次滚动距离
* @param {*} time 滚动延时(不得大于系统滚动时长,否则可能出现卡顿效果)
*/
start(up, step = 40, time = 100) {
const dom = getDocument().documentElement
const fn = () => {
const top = up ? dom.scrollTop + step : dom.scrollTop - step
dom.scrollTo({ top, behavior: 'smooth' })
this.timmer = setTimeout(fn, time)
}
this.timmer || fn()
},
stop() {
clearTimeout(this.timmer)
this.timmer = null
}
}
export const dragStart = (
data,
element,
{ offsetX = 0, offsetY = 0, horizontal, vertical, width, height, x, y } = {}
) => {
// 表示鼠标按下开始拖拽
dragState.keydown = true
dragState.data = data || {}
// 记录上次一开始拖拽的时间
dragState.timer = Date.now()
// 如果element存在表示在iframe内部拖拽
dragState.element = element
dragState.offset = { offsetX, offsetY, horizontal, vertical, width, height, x, y }
clearHover()
}
export const clearLineState = () => {
Object.assign(lineState, initialLineState)
}
export const dragEnd = () => {
const { element, data } = dragState
if (element && canvasState.type === 'absolute') {
data.props = data.props || {}
data.props.style = element.style.cssText
getController().addHistory()
}
// 重置拖拽状态
Object.assign(dragState, initialDragState)
// 重置拖拽插入位置状态
clearLineState()
smoothScroll.stop()
}
export const getOffset = (element) => {
if (element.ownerDocument === document) {
return { x: 0, y: 0 }
}
const { x, y, bottom, top } = canvasState.iframe.getBoundingClientRect()
return { x, y, bottom, top }
}
export const getElement = (element) => {
// 如果当前元素是body
if (element === element.ownerDocument.body) {
return element
}
// 如果当前元素是画布的html,返回画布的body
if (element === element.ownerDocument.documentElement) {
return element.ownerDocument.body
}
if (!element || element.nodeType !== 1) {
return undefined
}
if (element.getAttribute(NODE_UID)) {
return element
} else if (element.parentElement) {
return getElement(element.parentElement)
}
return undefined
}
export const getInactiveElement = (element) => {
if (
!element ||
element.nodeType !== 1 ||
// 如果当前元素是body或者html,需要排除
element === element.ownerDocument.body ||
element === element.ownerDocument.documentElement ||
// 如果当前元素是RouterView, 则有可能是激活元素处于非激活元素里面,需要排除
element.getAttribute(NODE_TAG) === 'RouterView'
) {
return undefined
}
if (element.getAttribute(NODE_INACTIVE_UID)) {
return element
} else if (element.parentElement) {
return getInactiveElement(element.parentElement)
}
return undefined
}
const getRect = (element) => {
if (element === getDocument().body) {
const { innerWidth: width, innerHeight: height } = getWindow()
return {
left: 0,
top: 0,
right: width,
bottom: height,
width,
height,
x: 0,
y: 0
}
}
return element.getBoundingClientRect()
}
const inserAfter = ({ parent, node, data }) => {
if (!data.id) {
data.id = utils.guid()
}
useCanvas().operateNode({
type: 'insert',
parentId: parent.id,
newNodeData: data,
position: 'after',
referTargetNodeId: node.id
})
}
const insertBefore = ({ parent, node, data }) => {
if (!data.id) {
data.id = utils.guid()
}
useCanvas().operateNode({
type: 'insert',
parentId: parent.id,
newNodeData: data,
position: 'before',
referTargetNodeId: node.id
})
}
const insertInner = ({ node, data }, position) => {
if (!data.id) {
data.id = utils.guid()
}
useCanvas().operateNode({
type: 'insert',
parentId: node.id,
newNodeData: data,
position: [POSITION.TOP, POSITION.LEFT].includes(position) ? 'before' : 'after'
})
}
export const removeNode = (id) => {
useCanvas().operateNode({
type: 'delete',
id
})
}
export const removeNodeById = (id) => {
if (!id) {
return
}
removeNode(id)
clearSelect()
getController().addHistory()
canvasState.emit('remove')
}
export const querySelectById = (id) => {
let selector = `[${NODE_UID}="${id}"]`
const doc = canvasState.iframe.contentDocument
let element = doc.querySelector(selector)
const loopId = element?.getAttribute('loop-id')
if (element && loopId) {
const currentLoopId = getCurrent().loopId
selector = `[${NODE_UID}="${id}"][${NODE_LOOP}="${currentLoopId}"]`
element = doc.querySelector(selector)
}
return element
}
export const getCurrentElement = () => querySelectById(getCurrent().schema?.id)
// 滚动页面后,目标元素与页面边界至少保留的边距
const SCROLL_MARGIN = 15
export const scrollToNode = (element) => {
if (element) {
const container = getDocument().documentElement
const { clientWidth, clientHeight } = container
const { left, right, top, bottom, width, height } = element.getBoundingClientRect()
const option = {}
if (right < 0) {
option.left = container.scrollLeft + left - SCROLL_MARGIN
} else if (left > clientWidth) {
option.left = container.scrollLeft + left - clientWidth + width + SCROLL_MARGIN
}
if (bottom < 0) {
option.top = container.scrollTop + top - SCROLL_MARGIN
} else if (top > clientHeight) {
option.top = container.scrollTop + top - clientHeight + height + SCROLL_MARGIN
}
if (typeof option.left === 'number' || typeof option.top === 'number') {
container.scrollTo(option)
}
}
return nextTick()
}
const setSelectRect = (element) => {
element = element || getDocument().body
const { left, height, top, width } = getRect(element)
const componentName = getCurrent().schema?.componentName || ''
clearHover()
Object.assign(selectState, {
width,
height,
top,
left,
componentName,
doc: getDocument()
})
}
export const updateRect = (id) => {
id = (typeof id === 'string' && id) || getCurrent().schema?.id
clearHover()
if (id) {
setTimeout(() => setSelectRect(querySelectById(id)))
} else {
// 如果选中的是body,不清除选中框
if (!selectState.componentName && selectState.width > 0) {
return
}
clearSelect()
}
}
export const getConfigure = (targetName) => {
const material = getController().getMaterial(targetName)
// 这里如果是区块插槽,则返回标识为容器的对象
if (targetName === 'Template') {
return {
isContainer: true
}
}
return material?.content?.configure || material.configure || {}
}
/**
* 是否允许插入
* @param {*} configure 当前放置目标的 configure,比如getConfigure(componentName)
* @param {*} data 当前插入目标的schame数据
* @returns
*/
export const allowInsert = (configure = hoverState.configure || {}, data = dragState.data || {}) => {
const { nestingRule = {} } = configure
const { childWhitelist = [], descendantBlacklist = [] } = nestingRule
// 要插入的父节点必须是容器
if (!configure.isContainer) {
return false
}
let flag = true
// 白名单
flag = childWhitelist.length ? childWhitelist.includes(data?.componentName) : true
// 黑名单
if (descendantBlacklist.length) {
flag = !descendantBlacklist.includes(data?.componentName)
}
return flag
}
const isAncestor = (ancestor, descendant) => {
const ancestorId = typeof ancestor === 'string' ? ancestor : ancestor.id
let descendantId = typeof descendant === 'string' ? descendant : descendant.id
while (descendantId) {
const { parent } = useCanvas().getNodeWithParentById(descendantId) || {}
if (parent?.id === ancestorId) {
return true
}
descendantId = parent?.id
}
return false
}
// 获取位置信息,返回状态
const lineAbs = 20
const getPosLine = (rect, configure) => {
const mousePos = dragState.mouse
const yAbs = Math.min(lineAbs, rect.height / 3)
const xAbs = Math.min(lineAbs, rect.width / 3)
let type
let forbidden = false
if (mousePos.y < rect.top + yAbs) {
type = POSITION.TOP
} else if (mousePos.y > rect.bottom - yAbs) {
type = POSITION.BOTTOM
} else if (mousePos.x < rect.left + xAbs) {
type = POSITION.LEFT
} else if (mousePos.x > rect.right - xAbs) {
type = POSITION.RIGHT
} else if (configure.isContainer) {
type = POSITION.IN
if (!allowInsert()) {
forbidden = true
}
} else {
type = POSITION.BOTTOM
}
// 如果被拖拽的节点不是新增的,并且是放置的节点的祖先节点,则禁止插入
const draggedId = dragState.data?.id
if (draggedId && isAncestor(draggedId, lineState.id)) {
forbidden = true
}
return { type, forbidden }
}
const isBodyEl = (element) => element.nodeName === 'BODY'
const setHoverRect = (element, data) => {
if (!element) {
return clearHover()
}
const componentName = element.getAttribute(NODE_TAG)
const id = element.getAttribute(NODE_UID)
const configure = getConfigure(componentName)
const rect = getRect(element)
const { left, height, top, width } = rect
const { getSchema, getNodeWithParentById } = useCanvas()
hoverState.configure = configure
if (data) {
let childEle = null
lineState.id = id
lineState.configure = configure
const rectType = isBodyEl(element) ? POSITION.IN : getPosLine(rect, configure).type
// 如果拖拽经过的元素是body或者是带有容器属性的盒子,并且在元素内部插入,则需要特殊处理
if ((isBodyEl(element) || configure?.isContainer) && rectType === POSITION.IN) {
const { node } = isBodyEl(element) ? { node: getSchema() } : getNodeWithParentById(id) || {}
const children = node?.children || []
if (children.length > 0) {
// 如果容器盒子有子节点,则以最后一个子节点为拖拽参照物
const lastNode = children[children.length - 1]
childEle = querySelectById(lastNode.id)
const childComponentName = element.getAttribute(childEle)
const Childconfigure = getConfigure(childComponentName)
lineState.id = lastNode.id
lineState.configure = Childconfigure
}
}
// 如果容器盒子有子元素
if (childEle) {
const childRect = getRect(childEle)
const { left, height, top, width } = childRect
const posLine = getPosLine(childRect, lineState.configure)
Object.assign(lineState, {
width,
height,
top,
left,
position: canvasState.type === 'absolute' || posLine.type,
forbidden: posLine.forbidden
})
} else {
const posLine = getPosLine(rect, configure)
Object.assign(lineState, {
width,
height,
top,
left,
position: canvasState.type === 'absolute' || posLine.type,
forbidden: posLine.forbidden
})
}
useLayout().closePlugin()
}
// 设置元素hover状态
Object.assign(hoverState, {
width,
height,
top,
left,
element,
componentName
})
return undefined
}
const setInactiveHoverRect = (element) => {
if (!element) {
Object.assign(inactiveHoverState, initialRectState, { slot: null })
return
}
const componentName = element.getAttribute(NODE_TAG)
const id = element.getAttribute(NODE_INACTIVE_UID)
const configure = getConfigure(componentName)
const rect = getRect(element)
const { left, height, top, width } = rect
inactiveHoverState.configure = configure
// 设置元素hover状态
Object.assign(inactiveHoverState, {
id,
width,
height,
top,
left,
element,
componentName
})
}
let moveUpdateTimer = null
// 绝对布局
const absoluteMove = (event, element) => {
const { clientX, clientY } = event
const { offsetX, offsetY, horizontal, vertical, height, width, x, y } = dragState.offset
element.style.position = 'absolute'
if (!horizontal) {
// 未传方向信息时判断为移动元素位置
element.style.top = `${clientY - offsetY}px`
element.style.left = `${clientX - offsetX}px`
} else {
// 调整元素大小
if (horizontal === 'start') {
element.style.left = `${clientX}px`
element.style.width = `${width + (x - clientX)}px`
}
if (horizontal === 'end') {
element.style.width = `${clientX - x}px`
}
if (vertical === 'start') {
element.style.top = `${clientY}px`
element.style.height = `${height + (y - clientY)}px`
}
if (vertical === 'end') {
element.style.height = `${clientY - y}px`
}
}
clearTimeout(moveUpdateTimer)
const { data } = dragState
data.props = data.props || {}
// 防抖更新位置信息到 schema
moveUpdateTimer = setTimeout(() => {
data.props.style = element.style.cssText
getController().addHistory()
}, 100)
updateRect()
}
const setDragPosition = ({ clientX, x, clientY, y, offsetBottom, offsetTop }) => {
const left = clientX + x
const top = clientY + y
if (clientY < 20) {
smoothScroll.start(false)
} else if (offsetBottom - clientY - offsetTop < 20) {
smoothScroll.start(true)
} else {
smoothScroll.stop()
}
dragState.position = { left, top }
}
export const dragMove = (event, isHover) => {
if (!dragState.draging && dragState.keydown && new Date().getTime() - dragState.timer < 200) {
return
}
const { x, y, bottom: offsetBottom, top: offsetTop } = getOffset(event.target)
const { clientX, clientY } = event
const { element } = dragState
const absolute = canvasState.type === 'absolute'
dragState.draging = dragState.keydown
dragState.mouse = { x: clientX, y: clientY }
// 如果仅仅是mouseover事件直接return,并重置拖拽位置状态,优化性能
if (isHover) {
lineState.position = ''
setHoverRect(getElement(event.target), null)
setInactiveHoverRect(getInactiveElement(event.target))
return
}
setHoverRect(getElement(event.target), dragState.data)
if (dragState.draging) {
// 绝对布局时走的逻辑
if (element && absolute) {
absoluteMove(event, element)
}
setDragPosition({ clientX, x, clientY, y, offsetBottom, offsetTop })
}
}
// type == clickTree, 为点击大纲; type == loop-id=xxx ,为点击循环数据
export const selectNode = async (id, type) => {
if (type && type.indexOf('loop-id') > -1) {
const loopId = type.split('=')[1]
canvasState.loopId = loopId
}
const { node, parent } = useCanvas().getNodeWithParentById(id) || {}
let element = querySelectById(id, type)
if (element && node) {
const { rootSelector } = getConfigure(node.componentName)
element = rootSelector ? element.querySelector(rootSelector) : element
}
canvasState.current = node
canvasState.parent = parent
await scrollToNode(element)
setSelectRect(element)
canvasState.emit('selected', node, parent, type, id)
return node
}
export const hoverNode = (id, data) => {
const element = querySelectById(id)
element && setHoverRect(element, data)
}
export const insertNode = (node, position = POSITION.IN, select = true) => {
if (!node.parent) {
insertInner({ node: useCanvas().pageState.pageSchema, data: node.data }, position)
} else {
switch (position) {
case POSITION.TOP:
case POSITION.LEFT:
insertBefore(node)
break
case POSITION.BOTTOM:
case POSITION.RIGHT:
inserAfter(node)
break
case POSITION.IN:
insertInner(node)
break
default:
insertInner(node)
break
}
}
select && setTimeout(() => selectNode(node.data.id))
getController().addHistory()
}
export const addComponent = (data, position) => {
const { schema, parent } = getCurrent()
insertNode({ node: schema, parent, data }, position)
}
export const copyNode = (id) => {
if (!id) {
return
}
const { node, parent } = useCanvas().getNodeWithParentById(id)
inserAfter({ parent, node, data: copyObject(node) })
getController().addHistory()
}
export const onMouseUp = () => {
const { draging, data } = dragState
const { position, forbidden } = lineState
const absolute = canvasState.type === 'absolute'
const sourceId = data?.id
const lineId = lineState.id
const { getNodeWithParentById, getSchema } = useCanvas()
if (draging && !forbidden) {
const { parent, node } = getNodeWithParentById(lineId) || {} // target
const insertData = toRaw(data)
const targetNode = { parent, node, data: { ...insertData, children: insertData.children || [] } }
if (sourceId) {
// 内部拖拽
if (sourceId !== lineId && !absolute) {
removeNode(sourceId)
insertNode(targetNode, position)
}
} else {
// 从外部拖拽进来的无ID,insert
if (absolute) {
targetNode.node = getSchema()
data.props = data.props || {}
data.props.style = `position: absolute; top: ${dragState.mouse.y}px; left: ${dragState.mouse.x}px`
}
insertNode(targetNode, position)
}
}
// 重置拖拽状态
dragEnd()
}
export const addStyle = (href) => appendStyle(href, getDocument())
export const addScript = (src) => appendScript(src, getDocument())
/**
*
* @param {*} messages
* @param {*} merge 是否合并,默认是重置所有数据
*/
export const setLocales = (messages, merge) => {
const i18n = getRenderer().getI18n()
Object.keys(messages).forEach((lang) => {
const fn = merge ? 'mergeLocaleMessage' : 'setLocaleMessage'
i18n.global[fn](lang, messages[lang])
})
}
export const setConfigure = (configure) => {
getRenderer().setConfigure(configure)
}
export const setI18n = (data) => {
const messages = data || useTranslate().getData()
const i18n = getRenderer().getI18n()
Object.keys(messages).forEach((lang) => {
i18n.global.mergeLocaleMessage(lang, messages[lang])
})
}
export const setCanvasType = (type) => {
canvasState.type = type || 'normal'
getDocument().body.className = type === 'absolute' ? 'canvas-grid-bg' : ''
}
export const getCanvasType = () => canvasState.type
/**
* 画布派发事件
* @param {string} name 事件名称
* @param {any} data 派发的数据
*/
export const canvasDispatch = (name, data, doc = getDocument()) => {
if (!doc) return
doc.dispatchEvent(new CustomEvent(name, data))
}
export const canvasApi = {
dragStart,
updateRect,
dragMove,
setLocales,
getRenderer,
clearSelect,
selectNode,
hoverNode,
insertNode,
removeNode,
addComponent,
addScript,
addStyle,
getCurrent,
setI18n,
getCanvasType,
setCanvasType,
getDesignMode,
setDesignMode,
getDocument,
canvasDispatch,
getConfigure,
allowInsert,
Builtin,
removeBlockCompsCache: (...args) => {
return canvasState.renderer.removeBlockCompsCache(...args)
},
updateCanvas: (...args) => {
return canvasState.renderer.updateCanvas(...args)
}
}
export const initCanvas = ({ renderer, iframe, emit, controller }) => {
canvasState.iframe = iframe
canvasState.emit = emit
// 存放画布外层传进来的插件api
canvasState.controller = controller
canvasState.renderer = renderer
renderer.setController(controller)
setLocales(useTranslate().getData(), true)
if (isVsCodeEnv) {
const parent = window.parent
const senterMessage = parent.postMessage
// 发消息给webview
senterMessage({ type: 'i18nReady', value: true }, '*')
}
setConfigure(useMaterial().getConfigureMap())
canvasState.loading = false
}