Skip to content

Commit dde9e8e

Browse files
authored
feat: read template and style json for jscard (#677)
* feat: read template and style json for jscard Signed-off-by: lileirjyb <[email protected]> * feat: support flatten i18n json format for js widget Signed-off-by: lileirjyb <[email protected]> * feat: fit for const value in for directive in new js widget Signed-off-by: lileirjyb <[email protected]> * fix: i18n result fail to update when calling setLocale Signed-off-by: lileirjyb <[email protected]> * feat: 修改framework版本号为1300 Signed-off-by: lilei946 <[email protected]> --------- Signed-off-by: lileirjyb <[email protected]> Signed-off-by: lilei946 <[email protected]>
1 parent 48a586d commit dde9e8e

File tree

10 files changed

+227
-8
lines changed

10 files changed

+227
-8
lines changed

core/framework/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "framework",
3-
"version": "1.10.0",
3+
"version": "1.30.0",
44
"description": "",
55
"scripts": {
66
"format": "prettier \"{src,test,config}/**/*.{md,js,json}\" --write",

core/framework/src/dsls/xvm/page/interface.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ function initPage(page, code, query, globals) {
6161

6262
const instEvaluate = context.quickapp.dock.makeEvaluateBuildScript(globals)
6363

64+
const jsonRequire = (jsonModuleName, options) => {
65+
return context.quickapp.platform.requireJson(jsonModuleName, options)
66+
}
67+
6468
// 处理代码
6569
let functionBody
6670
if (typeof code === 'function') {
@@ -84,7 +88,8 @@ function initPage(page, code, query, globals) {
8488
$app_bootstrap$: instBootstrap,
8589
$app_require$: instRequireModule,
8690
$app_define_wrap$: instDefineWrap,
87-
$app_evaluate$: instEvaluate
91+
$app_evaluate$: instEvaluate,
92+
$json_require$: jsonRequire
8893
},
8994
globals
9095
)
@@ -99,6 +104,7 @@ function initPage(page, code, query, globals) {
99104
global.$app_require$ = instRequireModule
100105
global.$app_define_wrap$ = instDefineWrap
101106
global.$app_evaluate$ = instEvaluate
107+
global.$json_require$ = jsonRequire
102108

103109
global.setTimeout = globals.setTimeout
104110
global.setInterval = globals.setInterval

core/framework/src/dsls/xvm/plugin-i18n/index.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,66 @@ class PluginI18n {
106106
return null
107107
}
108108
for (let i = 0, len = resources.length; i < len; i++) {
109-
const ret = this._interpolate(locale, resources[i], key, host, interpolateMode, params, [key])
109+
let ret
110+
if (global.isRpkCardMinPlatformVersionGEQ(2000, host)) {
111+
console.log(
112+
`### App Framework ### i18n:读取卡片多语言资源,key: ${key}, locale: ${locale}, resources: ${JSON.stringify(
113+
resources[i]
114+
)}`
115+
)
116+
ret = this._interpolateFlatten(locale, resources[i], key, host, interpolateMode, params, [
117+
key
118+
])
119+
} else {
120+
console.log(
121+
`### App Framework ### i18n:读取多语言资源,key: ${key}, locale: ${locale}, resources: ${JSON.stringify(
122+
resources[i]
123+
)}`
124+
)
125+
ret = this._interpolate(locale, resources[i], key, host, interpolateMode, params, [key])
126+
}
110127
if (!isNull(ret)) {
111128
return ret
112129
}
113130
}
114131
return null
115132
}
116133

134+
_interpolateFlatten(locale, jsonObject, key, host, interpolateMode, params, visitedLinkStack) {
135+
if (!jsonObject) {
136+
return null
137+
}
138+
139+
const keyRet = jsonObject[key]
140+
return this._interpolateCore(
141+
locale,
142+
key,
143+
host,
144+
interpolateMode,
145+
params,
146+
visitedLinkStack,
147+
keyRet
148+
)
149+
}
150+
117151
_interpolate(locale, jsonObject, key, host, interpolateMode, params, visitedLinkStack) {
118152
if (!jsonObject) {
119153
return null
120154
}
121155

122156
const keyRet = this._path.getPathValue(jsonObject, key)
157+
return this._interpolateCore(
158+
locale,
159+
key,
160+
host,
161+
interpolateMode,
162+
params,
163+
visitedLinkStack,
164+
keyRet
165+
)
166+
}
167+
168+
_interpolateCore(locale, key, host, interpolateMode, params, visitedLinkStack, keyRet) {
123169
if (isPlainObject(keyRet) || Array.isArray(keyRet)) {
124170
return keyRet
125171
}

core/framework/src/dsls/xvm/plugin-i18n/mixin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ export default {
3838
onConfigurationChanged(evt) {
3939
// 页面级Vm
4040
if (evt.type === 'locale') {
41-
if (this.$root() === this && this._i18n && this.$app) {
41+
if (this.$root() === this && this._i18n && this._page && this._page.app) {
4242
const i18n = this._i18n
43-
const ret = this.$app.getLocaleConfig()
43+
const ret = this._page.app.getLocaleConfig()
4444
const retNew = JSON.parse(JSON.stringify(ret))
4545
i18n.locale = retNew.locale
4646
i18n.resources = retNew.resources

core/framework/src/dsls/xvm/vm/compiler.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,13 @@ function compileFor(vm, target, dest) {
507507
const repeat = target.repeat
508508
// 获取数据源函数
509509
let getter = repeat.exp || repeat
510+
if (typeof getter === 'number') {
511+
const num = getter
512+
getter = function() {
513+
return num
514+
}
515+
}
516+
510517
// 确保getter是函数,则添加空函数
511518
if (typeof getter !== 'function') {
512519
getter = function() {

core/framework/src/dsls/xvm/vm/index.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,13 @@ export default class XVm {
405405
}
406406
options = options || {}
407407

408+
// 打包结果包含extracted字段,说明是新的js卡打包格式
409+
// template和style的前处理后,js卡option.template和option.style的格式和之前的快应用就保持一致了
410+
if (options.style && options.style.extracted) {
411+
preHandleTemplate(options.template)
412+
preHandleStyle(options)
413+
}
414+
408415
// 页面级Vm:i18n配置合并
409416
if (this._isPageVm() && this._page.app) {
410417
const ret = this._page.app.getLocaleConfig()
@@ -1193,3 +1200,64 @@ XVm.mixin = function(options) {
11931200
}
11941201
}
11951202
}
1203+
1204+
function inReservedKeys(key) {
1205+
const RESERVED_ATTRS = ['$listeners', '$attrs']
1206+
return RESERVED_ATTRS.includes(key)
1207+
}
1208+
1209+
function isFunctionStr(str) {
1210+
const pattern = /^\s*function\s*\([\w\s,$]*\)\s*\{[\s\S]*\}\s*$/
1211+
return pattern.test(str.trim())
1212+
}
1213+
1214+
/**
1215+
* 如果携带了新打包格式的参数,则先进行template和style的预处理
1216+
* 主要是将key的$符号去除
1217+
* 以及将字符串的"function"转成真正的function
1218+
* */
1219+
1220+
function preHandleTemplate(target) {
1221+
if (!target || Object.prototype.toString.call(target) !== '[object Object]') return
1222+
1223+
Object.keys(target).forEach(key => {
1224+
if (Object.prototype.toString.call(target[key]) === '[object Object]') {
1225+
preHandleTemplate(target[key])
1226+
} else if (Object.prototype.toString.call(target[key]) === '[object Array]') {
1227+
target[key].forEach(item => {
1228+
preHandleTemplate(item)
1229+
})
1230+
} else if (typeof target[key] === 'string') {
1231+
if (!isFunctionStr(target[key])) {
1232+
// value非function字符串
1233+
if (key.startsWith('$') && !inReservedKeys(key)) {
1234+
// example: "$value": "name",代表name为变量
1235+
const trimKey = key.substring(1)
1236+
const func = `function () { return this.${target[key]} }`
1237+
target[trimKey] = new Function(`return ${func}`)()
1238+
delete target[key]
1239+
}
1240+
} else {
1241+
// value为function字符串
1242+
if (key.startsWith('$')) {
1243+
// example: "$value": "function() {return this.a + '-' + this.b}",代表name为变量
1244+
const trimKey = key.substring(1)
1245+
target[trimKey] = new Function(`return ${target[key]}`)()
1246+
delete target[key]
1247+
} else {
1248+
// 理论上不存在该情况,防止打包处理遗漏,在这里兜底
1249+
target[key] = new Function(`return ${target[key]}`)()
1250+
}
1251+
}
1252+
}
1253+
})
1254+
}
1255+
1256+
function preHandleStyle(options) {
1257+
if (!options.style) return
1258+
1259+
const styleObjectId = options.style['@info'].styleObjectId
1260+
const jsonPath = options.style.jsonPath
1261+
1262+
options.style = context.quickapp.platform.requireJson(jsonPath, { styleObjectId }) || {}
1263+
}

core/framework/src/infras/dock/page/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class XPage extends ModuleHost {
3838
this._visible = false
3939
this._meta = Object.assign({ query }, intent, meta)
4040
this._orientation = intent.orientation
41+
this._currentPageName = intent.currentPageName
4142
this.nextTickCallbacks = [] // nextTick的回调函数数组
4243
}
4344

@@ -53,6 +54,10 @@ class XPage extends ModuleHost {
5354
return this._orientation
5455
}
5556

57+
get currentPageName() {
58+
return this._currentPageName
59+
}
60+
5661
get pageName() {
5762
return this._meta.name
5863
}

core/framework/src/infras/platform/chunk.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,68 @@ function requireBundleChunk(filePath) {
3737
return cont
3838
}
3939

40-
export { registerBundleChunks, requireBundleChunk }
40+
const templateJsonMap = new Map()
41+
const styleJsonMap = new Map()
42+
function registerComponentJson(templateJson, cssJson) {
43+
if (typeof templateJson === 'string') {
44+
templateJson = JSON.parse(templateJson)
45+
}
46+
for (const compPath in templateJson) {
47+
if (templateJson.hasOwnProperty(compPath)) {
48+
let pathKey = compPath
49+
if (pathKey.startsWith('/')) {
50+
pathKey = pathKey.replace(/^\/+/, '')
51+
}
52+
const templateObj = templateJson[compPath]
53+
templateJsonMap.set(pathKey, templateObj)
54+
}
55+
}
56+
57+
if (typeof cssJson === 'string') {
58+
cssJson = JSON.parse(cssJson)
59+
}
60+
for (const compPath in cssJson) {
61+
if (cssJson.hasOwnProperty(compPath)) {
62+
let pathKey = compPath
63+
if (pathKey.startsWith('/')) {
64+
pathKey = pathKey.replace(/^\/+/, '')
65+
}
66+
const styleObj = cssJson[compPath]
67+
styleJsonMap.set(pathKey, styleObj)
68+
}
69+
}
70+
}
71+
72+
function requireJson(compPath, options) {
73+
try {
74+
if (options && options.styleObjectId) {
75+
const styleObj = JSON.parse(styleJsonMap.get(compPath))
76+
if (!styleObj) {
77+
console.warn(
78+
`### App Framework ### requireJson not exist ${compPath} -- options: ${JSON.stringify(
79+
options
80+
)}`
81+
)
82+
return {}
83+
}
84+
const style = styleObj[options.styleObjectId]
85+
return style
86+
} else if (options && options.componentPath) {
87+
const templateObj = JSON.parse(templateJsonMap.get(compPath))
88+
if (!templateObj) {
89+
console.warn(
90+
`### App Framework ### requireJson not exist ${compPath} -- options: ${JSON.stringify(
91+
options
92+
)}`
93+
)
94+
return {}
95+
}
96+
const template = templateObj[options.componentPath].template
97+
return template
98+
}
99+
} catch (e) {
100+
console.error(`### App Framework ### requireJson error: ${JSON.stringify(e)}`)
101+
}
102+
}
103+
104+
export { registerBundleChunks, requireBundleChunk, registerComponentJson, requireJson }

core/framework/src/infras/platform/interface.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ import { invokeScript } from 'src/shared/function'
1010
import ModuleHost from './module/index'
1111
import { registerModules, execInvokeCallback, requireModule } from './module/interface'
1212
import Session from './session'
13-
import { registerBundleChunks, requireBundleChunk } from './chunk'
13+
import {
14+
registerBundleChunks,
15+
requireBundleChunk,
16+
registerComponentJson,
17+
requireJson
18+
} from './chunk'
1419

1520
import {
1621
registerManifest,
1722
isRpkMinPlatformVersionGEQ,
23+
isRpkCardMinPlatformVersionGEQ,
1824
getManifestField,
1925
isRpkDebugMode
2026
} from './manifest'
@@ -140,14 +146,17 @@ export default {
140146
ModuleHost,
141147
requireModule,
142148
requireScriptFile,
149+
requireJson,
143150
initInterface,
144151
exposure: {
145152
registerModules,
146153
registerBundleChunks,
154+
registerComponentJson,
147155
execInvokeCallback,
148156
registerManifest,
149157
getManifestField,
150158
isRpkMinPlatformVersionGEQ,
159+
isRpkCardMinPlatformVersionGEQ,
151160
isRpkDebugMode
152161
}
153162
}

core/framework/src/infras/platform/manifest.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ function isRpkMinPlatformVersionGEQ(val) {
5555
return manifestJSON.minPlatformVersion >= val
5656
}
5757

58+
function isRpkCardMinPlatformVersionGEQ(val, vm) {
59+
const widgetKey = vm._page.currentPageName
60+
const widgetsOption = (manifestJSON.router && manifestJSON.router.widgets) || {}
61+
const widgetManiest = widgetsOption[widgetKey] || {}
62+
63+
return widgetManiest.minCardPlatformVersion >= val
64+
}
65+
5866
let mode = null
5967

6068
function isRpkDebugMode() {
@@ -66,4 +74,10 @@ function isRpkDebugMode() {
6674
return mode
6775
}
6876

69-
export { registerManifest, isRpkMinPlatformVersionGEQ, getManifestField, isRpkDebugMode }
77+
export {
78+
registerManifest,
79+
isRpkMinPlatformVersionGEQ,
80+
isRpkCardMinPlatformVersionGEQ,
81+
getManifestField,
82+
isRpkDebugMode
83+
}

0 commit comments

Comments
 (0)