forked from opentiny/tiny-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlowcode.js
More file actions
104 lines (92 loc) · 3.18 KB
/
Copy pathlowcode.js
File metadata and controls
104 lines (92 loc) · 3.18 KB
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
/**
* 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 { getCurrentInstance, nextTick, provide, inject } from 'vue'
import { I18nInjectionKey } from 'vue-i18n'
import { api } from './components/render/RenderMain'
import { collectionMethodsMap, generateFn, globalNotify } from './components/render/render'
export const lowcodeWrap = (props, context) => {
const global = {}
const instance = getCurrentInstance()
const router = ''
const route = ''
const { t, locale } = inject(I18nInjectionKey).global
const emit = context.emit
const ref = (ref) => instance.refs[ref]
const setState = (newState, callback) => {
Object.assign(global.state, newState)
nextTick(() => callback?.apply(global))
}
const getLocale = () => locale.value
const setLocale = (val) => {
locale.value = val
}
const location = () => window.location
const history = () => window.history
Object.defineProperties(global, {
props: { get: () => props },
emit: { get: () => emit },
setState: { get: () => setState },
router: { get: () => router },
route: { get: () => route },
i18n: { get: () => t },
getLocale: { get: () => getLocale },
setLocale: { get: () => setLocale },
location: { get: location },
history: { get: history },
utils: {
get: api.getUtils
},
bridge: { get: () => ({}) },
dataSourceMap: { get: api.getDataSourceMap },
$: { get: () => ref }
})
const wrap = (fn) => {
if (typeof fn === 'function') {
const fnName = fn.name
if (fn.toString().includes('return this')) {
return () => global
} else if (fnName && collectionMethodsMap[fnName.slice(0, -1)]) {
// 这里区块打包的时候会在方法名称后面多加一个字符串,所以此处需要截取下函数名称
fn.realName = fnName.slice(0, -1)
return generateFn(fn)
} else if (fn.name === 'setter' || fn.name === 'getter') {
// 这里需要保证在消费区块时,区块中的访问器函数可以正常执行
return (...args) => {
try {
fn.apply(global, args)
} catch (error) {
globalNotify({
type: 'warning',
title: `访问器函数:${fn.name}执行报错`,
message: error?.message || `访问器函数:${fn.name}执行报错,请检查语法`
})
}
}
} else {
return () => Promise.resolve({ result: [], page: { total: 100 } })
}
}
Object.entries(fn).forEach(([name, value]) => {
Object.defineProperty(global, name, {
get: () => value
})
})
fn.t = t
return fn
}
return wrap
}
export default () => {
const i18n = inject(I18nInjectionKey)
provide(I18nInjectionKey, i18n)
return { t: i18n.global.t, lowcodeWrap }
}