@opentiny/tiny-engine-canvas 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/.eslintrc.js +42 -0
  2. package/README.md +7 -0
  3. package/canvas.html +212 -0
  4. package/dist/index.js +48919 -0
  5. package/index.html +13 -0
  6. package/package.json +30 -0
  7. package/public/favicon.ico +0 -0
  8. package/src/Design.vue +53 -0
  9. package/src/assets/logo.png +0 -0
  10. package/src/canvas.js +34 -0
  11. package/src/components/builtin/CanvasBox.vue +22 -0
  12. package/src/components/builtin/CanvasCol.vue +89 -0
  13. package/src/components/builtin/CanvasCollection.js +278 -0
  14. package/src/components/builtin/CanvasCollection.vue +106 -0
  15. package/src/components/builtin/CanvasIcon.vue +30 -0
  16. package/src/components/builtin/CanvasImg.vue +18 -0
  17. package/src/components/builtin/CanvasPlaceholder.vue +26 -0
  18. package/src/components/builtin/CanvasRow.vue +67 -0
  19. package/src/components/builtin/CanvasRowColContainer.vue +42 -0
  20. package/src/components/builtin/CanvasSlot.vue +22 -0
  21. package/src/components/builtin/CanvasText.vue +18 -0
  22. package/src/components/builtin/builtin.json +955 -0
  23. package/src/components/builtin/helper.js +46 -0
  24. package/src/components/builtin/index.js +33 -0
  25. package/src/components/common/index.js +158 -0
  26. package/src/components/container/CanvasAction.vue +554 -0
  27. package/src/components/container/CanvasContainer.vue +244 -0
  28. package/src/components/container/CanvasDivider.vue +246 -0
  29. package/src/components/container/CanvasDragItem.vue +38 -0
  30. package/src/components/container/CanvasFooter.vue +86 -0
  31. package/src/components/container/CanvasMenu.vue +214 -0
  32. package/src/components/container/CanvasResize.vue +195 -0
  33. package/src/components/container/CanvasResizeBorder.vue +219 -0
  34. package/src/components/container/container.js +791 -0
  35. package/src/components/container/keyboard.js +147 -0
  36. package/src/components/container/shortCutPopover.vue +181 -0
  37. package/src/components/render/CanvasEmpty.vue +14 -0
  38. package/src/components/render/RenderMain.js +408 -0
  39. package/src/components/render/context.js +53 -0
  40. package/src/components/render/render.js +689 -0
  41. package/src/components/render/runner.js +140 -0
  42. package/src/i18n/en.json +5 -0
  43. package/src/i18n/zh.json +5 -0
  44. package/src/i18n.js +21 -0
  45. package/src/index.js +96 -0
  46. package/src/locale.js +19 -0
  47. package/src/lowcode.js +104 -0
  48. package/src/main.js +17 -0
  49. package/test/form.json +690 -0
  50. package/test/group.json +99 -0
  51. package/test/jsslot.json +427 -0
  52. package/vite.config.js +73 -0
@@ -0,0 +1,140 @@
1
+ /**
2
+ * Copyright (c) 2023 - present TinyEngine Authors.
3
+ * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license.
6
+ *
7
+ * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8
+ * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9
+ * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10
+ *
11
+ */
12
+
13
+ import * as Vue from 'vue'
14
+ import * as VueI18n from 'vue-i18n'
15
+ import { addScript, addStyle, dynamicImportComponents, updateDependencies } from '../common'
16
+ import TinyI18nHost, { I18nInjectionKey } from '@opentiny/tiny-engine-common/js/i18n'
17
+ import * as TinyWebcomponentCore from '@opentiny/tiny-engine-webcomponent-core'
18
+ import TinyVue from '@opentiny/vue'
19
+ import * as TinyVueIcon from '@opentiny/vue-icon'
20
+ import Main, { api } from './RenderMain'
21
+ import { getComponent, blockSlotDataMap } from './render'
22
+ import lowcode from '../../lowcode'
23
+ import { camelize, capitalize } from '@vue/shared'
24
+
25
+ const dispatch = (name, data) => {
26
+ window.parent.document.dispatchEvent(new CustomEvent(name, data))
27
+ }
28
+
29
+ dispatch('beforeCanvasReady')
30
+
31
+ TinyI18nHost.lowcode = lowcode
32
+
33
+ // 目前先兼容老区块发布的代码,后期区块发布整改后再删除
34
+ window.React = {}
35
+ window.React.createElement = Vue.h
36
+
37
+ // 不能采用new Proxy代理Vue的方案,在编译后的vue会报错警告,采用一下方案扩展用于注入一些区块加载逻辑
38
+ window.Vue = {
39
+ ...Vue,
40
+ resolveComponent(...args) {
41
+ // 此处先执行vue内部的解析组件的方法,如果可以拿到组件对象则直接返回,反之则去注册区块
42
+ const component = Vue.resolveComponent(args[0])
43
+ if (component && typeof component === 'string') {
44
+ return getComponent(capitalize(camelize(args[0])))
45
+ } else {
46
+ return component
47
+ }
48
+ },
49
+ // renderSlot方法第三个参数是作用域插槽传递的数据,格式{ data: vue.unref(state).componentData }
50
+ renderSlot(...args) {
51
+ // 获取当前vue的实例
52
+ const instance = Vue.getCurrentInstance()
53
+
54
+ // 获取当前区块名称
55
+ const blockName = instance.attrs.dataTag
56
+
57
+ const [, slotName, slotData] = args
58
+
59
+ // 如果是作用域插槽,则获取作用域插槽传递过来的参数
60
+ if (slotData) {
61
+ if (blockSlotDataMap[blockName]) {
62
+ blockSlotDataMap[blockName][slotName] = slotData
63
+ } else {
64
+ blockSlotDataMap[blockName] = { [slotName]: slotData }
65
+ }
66
+ }
67
+
68
+ /**
69
+ * vue源码中的renderSlot会忽略default插槽的名称,所以这里必须手动添加args第三个参数的name值
70
+ * vue源码如右所示:if (name !== 'default') props.name = name; return createVNode('slot', props, fallback && fallback());
71
+ **/
72
+ if (slotName === 'default') {
73
+ args[2] = args[2] || {}
74
+ args[2].name = slotName
75
+ }
76
+
77
+ return Vue.renderSlot(...args)
78
+ }
79
+ }
80
+
81
+ window.VueI18n = VueI18n
82
+ window.TinyVue = TinyVue
83
+ window.TinyVueIcon = TinyVueIcon
84
+ window.TinyWebcomponentCore = TinyWebcomponentCore
85
+ window.TinyI18nHost = TinyI18nHost
86
+ window.TinyLowcodeComponent = {}
87
+ window.TinyComponentLibs = {}
88
+
89
+ Object.entries(TinyVue).forEach(([_key, component]) => {
90
+ const { name } = component
91
+ if (name) {
92
+ window.TinyLowcodeComponent[name] = component
93
+ }
94
+ })
95
+
96
+ let App = null
97
+
98
+ const renderer = {
99
+ getApp() {
100
+ return App
101
+ },
102
+ getI18n() {
103
+ return TinyI18nHost
104
+ },
105
+ ...api
106
+ }
107
+
108
+ const create = () => {
109
+ App && App.unmount()
110
+ App = null
111
+
112
+ document.body.remove()
113
+ document.body = document.createElement('body')
114
+ dispatch('canvasReady', { detail: renderer })
115
+
116
+ App = Vue.createApp(Main).use(TinyI18nHost).provide(I18nInjectionKey, TinyI18nHost)
117
+ App.config.globalProperties.lowcodeConfig = window.parent.TinyGlobalConfig
118
+ App.mount(document.body)
119
+
120
+ new ResizeObserver(() => {
121
+ dispatch('canvasResize')
122
+ }).observe(document.body)
123
+
124
+ App.config.errorHandler = () => {}
125
+ }
126
+
127
+ export const createRender = (config) => {
128
+ const { dslMode, canvasOptions } = config
129
+ const { styles = [], scripts = [] } = canvasOptions[dslMode]
130
+ const { styles: thirdStyles = [], scripts: thirdScripts = [] } = window.thirdPartyDeps || {}
131
+
132
+ Promise.all([
133
+ ...thirdScripts.map(dynamicImportComponents),
134
+ ...scripts.map((src) => addScript(src)).concat([...thirdStyles, ...styles].map((src) => addStyle(src)))
135
+ ]).finally(create)
136
+ }
137
+
138
+ createRender(window.parent.TinyGlobalConfig)
139
+
140
+ document.addEventListener('updateDependencies', updateDependencies)
@@ -0,0 +1,5 @@
1
+ {
2
+ "tip": {
3
+ "ok": "are you ok"
4
+ }
5
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "tip": {
3
+ "ok": "准备好了吗?"
4
+ }
5
+ }
package/src/i18n.js ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Copyright (c) 2023 - present TinyEngine Authors.
3
+ * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license.
6
+ *
7
+ * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8
+ * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9
+ * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10
+ *
11
+ */
12
+
13
+ import i18n from '@opentiny/tiny-engine-i18n-host'
14
+ import lowcode from './lowcode'
15
+ import locale from './locale'
16
+
17
+ i18n.lowcode = lowcode
18
+ i18n.global.mergeLocaleMessage('zh_CN', locale.zh)
19
+ i18n.global.mergeLocaleMessage('en_US', locale.en)
20
+
21
+ export default i18n
package/src/index.js ADDED
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Copyright (c) 2023 - present TinyEngine Authors.
3
+ * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license.
6
+ *
7
+ * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8
+ * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9
+ * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10
+ *
11
+ */
12
+
13
+ import CanvasContainer from './components/container/CanvasContainer.vue'
14
+ import CanvasAction from './components/container/CanvasAction.vue'
15
+ import CanvasDragItem from './components/container/CanvasDragItem.vue'
16
+ import CanvasFooter from './components/container/CanvasFooter.vue'
17
+ import CanvasResize from './components/container/CanvasResize.vue'
18
+ import Builtin from './components/builtin/builtin.json'
19
+ import RenderMain, { api as renderApi } from './components/render/RenderMain'
20
+ import {
21
+ dragStart,
22
+ updateRect,
23
+ getContext,
24
+ getNodePath,
25
+ dragMove,
26
+ setLocales,
27
+ setState,
28
+ deleteState,
29
+ getRenderer,
30
+ clearSelect,
31
+ selectNode,
32
+ hoverNode,
33
+ insertNode,
34
+ removeNode,
35
+ addComponent,
36
+ setPageCss,
37
+ addScript,
38
+ addStyle,
39
+ getNode,
40
+ getCurrent,
41
+ setSchema,
42
+ setUtils,
43
+ getSchema,
44
+ setI18n,
45
+ getCanvasType,
46
+ setCanvasType,
47
+ setProps,
48
+ setGlobalState,
49
+ getGlobalState,
50
+ getDocument,
51
+ canvasDispatch
52
+ } from './components/container/container'
53
+
54
+ export {
55
+ CanvasContainer,
56
+ CanvasAction,
57
+ CanvasFooter,
58
+ CanvasDragItem,
59
+ CanvasResize,
60
+ RenderMain,
61
+ renderApi,
62
+ Builtin,
63
+ dragStart,
64
+ dragMove,
65
+ updateRect,
66
+ getContext,
67
+ getNodePath,
68
+ getNode,
69
+ getCurrent,
70
+ setSchema,
71
+ setUtils,
72
+ getSchema,
73
+ setLocales,
74
+ setState,
75
+ deleteState,
76
+ setI18n,
77
+ getRenderer,
78
+ clearSelect,
79
+ selectNode,
80
+ insertNode,
81
+ removeNode,
82
+ hoverNode,
83
+ addComponent,
84
+ setPageCss,
85
+ addScript,
86
+ addStyle,
87
+ getCanvasType,
88
+ setCanvasType,
89
+ setProps,
90
+ setGlobalState,
91
+ getGlobalState,
92
+ getDocument,
93
+ canvasDispatch
94
+ }
95
+
96
+ export default CanvasContainer
package/src/locale.js ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Copyright (c) 2023 - present TinyEngine Authors.
3
+ * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license.
6
+ *
7
+ * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8
+ * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9
+ * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10
+ *
11
+ */
12
+
13
+ import zh from './i18n/zh.json'
14
+ import en from './i18n/en.json'
15
+
16
+ export default {
17
+ en,
18
+ zh
19
+ }
package/src/lowcode.js ADDED
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Copyright (c) 2023 - present TinyEngine Authors.
3
+ * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license.
6
+ *
7
+ * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8
+ * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9
+ * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10
+ *
11
+ */
12
+
13
+ import { getCurrentInstance, nextTick, provide, inject } from 'vue'
14
+ import { I18nInjectionKey } from 'vue-i18n'
15
+ import { api } from './components/render/RenderMain'
16
+ import { collectionMethodsMap, generateFn, globalNotify } from './components/render/render'
17
+
18
+ export const lowcodeWrap = (props, context) => {
19
+ const global = {}
20
+ const instance = getCurrentInstance()
21
+ const router = ''
22
+ const route = ''
23
+ const { t, locale } = inject(I18nInjectionKey).global
24
+ const emit = context.emit
25
+ const ref = (ref) => instance.refs[ref]
26
+
27
+ const setState = (newState, callback) => {
28
+ Object.assign(global.state, newState)
29
+ nextTick(() => callback?.apply(global))
30
+ }
31
+
32
+ const getLocale = () => locale.value
33
+ const setLocale = (val) => {
34
+ locale.value = val
35
+ }
36
+
37
+ const location = () => window.location
38
+ const history = () => window.history
39
+
40
+ Object.defineProperties(global, {
41
+ props: { get: () => props },
42
+ emit: { get: () => emit },
43
+ setState: { get: () => setState },
44
+ router: { get: () => router },
45
+ route: { get: () => route },
46
+ i18n: { get: () => t },
47
+ getLocale: { get: () => getLocale },
48
+ setLocale: { get: () => setLocale },
49
+ location: { get: location },
50
+ history: { get: history },
51
+ utils: {
52
+ get: api.getUtils
53
+ },
54
+ bridge: { get: () => ({}) },
55
+ dataSourceMap: { get: api.getDataSourceMap },
56
+ $: { get: () => ref }
57
+ })
58
+
59
+ const wrap = (fn) => {
60
+ if (typeof fn === 'function') {
61
+ const fnName = fn.name
62
+ if (fn.toString().includes('return this')) {
63
+ return () => global
64
+ } else if (fnName && collectionMethodsMap[fnName.slice(0, -1)]) {
65
+ // 这里区块打包的时候会在方法名称后面多加一个字符串,所以此处需要截取下函数名称
66
+ fn.realName = fnName.slice(0, -1)
67
+ return generateFn(fn)
68
+ } else if (fn.name === 'setter' || fn.name === 'getter') {
69
+ // 这里需要保证在消费区块时,区块中的访问器函数可以正常执行
70
+ return (...args) => {
71
+ try {
72
+ fn.apply(global, args)
73
+ } catch (error) {
74
+ globalNotify({
75
+ type: 'warning',
76
+ title: `访问器函数:${fn.name}执行报错`,
77
+ message: error?.message || `访问器函数:${fn.name}执行报错,请检查语法`
78
+ })
79
+ }
80
+ }
81
+ } else {
82
+ return () => Promise.resolve({ result: [], page: { total: 100 } })
83
+ }
84
+ }
85
+
86
+ Object.entries(fn).forEach(([name, value]) => {
87
+ Object.defineProperty(global, name, {
88
+ get: () => value
89
+ })
90
+ })
91
+
92
+ fn.t = t
93
+
94
+ return fn
95
+ }
96
+
97
+ return wrap
98
+ }
99
+
100
+ export default () => {
101
+ const i18n = inject(I18nInjectionKey)
102
+ provide(I18nInjectionKey, i18n)
103
+ return { t: i18n.global.t, lowcodeWrap }
104
+ }
package/src/main.js ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Copyright (c) 2023 - present TinyEngine Authors.
3
+ * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license.
6
+ *
7
+ * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
8
+ * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
9
+ * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
10
+ *
11
+ */
12
+
13
+ import { createApp } from 'vue'
14
+
15
+ import App from './Design.vue'
16
+
17
+ createApp(App).mount('#app')