@wiajs/core 1.2.2 → 2.1.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.
- package/.oxfmtrc.jsonc +47 -0
- package/.oxlintrc.json +78 -0
- package/dist/core.cjs +272 -13
- package/dist/core.js +692 -11
- package/dist/core.min.js +5 -5
- package/dist/core.mjs +272 -14
- package/dist/jsx-runtime.js +127 -105
- package/package.json +27 -27
- package/util/log.d.ts +113 -0
- package/util/log.js +189 -0
- package/util/tool.js +94 -81
- package/.eslintignore +0 -5
- package/.eslintrc.js +0 -121
- package/.prettierignore +0 -24
- package/prettier.config.js +0 -13
package/.oxfmtrc.jsonc
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
// https://unpkg.com/oxfmt@0.35.0/configuration_schema.json
|
|
3
|
+
"$schema": "../../node_modules/oxfmt/configuration_schema.json",
|
|
4
|
+
"printWidth": 120,
|
|
5
|
+
"semi": false, // 强制去除分号,保持 JS/TS 的现代感与简洁
|
|
6
|
+
"singleQuote": true, // 推荐使用单引号,视觉干扰比双引号少
|
|
7
|
+
"arrowParens": "avoid",
|
|
8
|
+
"bracketSameLine": true,
|
|
9
|
+
"bracketSpacing": false, // 去除对象字面量中的空格,保持紧凑
|
|
10
|
+
"jsxBracketSameLine": true,
|
|
11
|
+
"jsxSingleQuote": true,
|
|
12
|
+
"trailingComma": "es5", // 去除尾逗号,保持代码简洁
|
|
13
|
+
// 导入排序:极度紧凑
|
|
14
|
+
"sortImports": {
|
|
15
|
+
"newlinesBetween": false, // 核心:去除不同组导入之间的空行,压缩垂直空间
|
|
16
|
+
"groups": ["builtin", "external", "internal", "parent", "sibling", "index"],
|
|
17
|
+
"caseSensitive": false,
|
|
18
|
+
},
|
|
19
|
+
// Package.json 排序:工程标准化
|
|
20
|
+
"sortPackageJson": {
|
|
21
|
+
"sortScripts": true, // 必须:脚本有序化,方便维护
|
|
22
|
+
},
|
|
23
|
+
// 3. Tailwind 类名排序:样式一致性
|
|
24
|
+
"sortTailwindcss": {
|
|
25
|
+
// 1. 覆盖所有类名可能出现的逻辑场景
|
|
26
|
+
"functions": ["clsx", "cn", "cva", "twMerge", "tw"],
|
|
27
|
+
// 2. 适配所有可能的属性(如某些库使用的 tw 属性)
|
|
28
|
+
"attributes": ["class", "className", "tw"],
|
|
29
|
+
// 3. 保持紧凑:自动去重,自动压缩空格
|
|
30
|
+
"preserveDuplicates": false,
|
|
31
|
+
"preserveWhitespace": false,
|
|
32
|
+
// 4. 如果是 Tailwind v4,指向你的 CSS 入口
|
|
33
|
+
// "stylesheet": "src/index.css"
|
|
34
|
+
},
|
|
35
|
+
"ignorePatterns": [
|
|
36
|
+
"apps/",
|
|
37
|
+
"assets/",
|
|
38
|
+
"dist/",
|
|
39
|
+
"docs/_layouts/",
|
|
40
|
+
"node_modules/",
|
|
41
|
+
"patches/",
|
|
42
|
+
"pnpm-lock.yaml/",
|
|
43
|
+
"Swabble/",
|
|
44
|
+
"vendor/",
|
|
45
|
+
"**/*.min.js",
|
|
46
|
+
],
|
|
47
|
+
}
|
package/.oxlintrc.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "../../node_modules/oxlint/configuration_schema.json",
|
|
3
|
+
"plugins": ["unicorn", "typescript", "oxc", "react"],
|
|
4
|
+
"env": {
|
|
5
|
+
"browser": true,
|
|
6
|
+
"node": true,
|
|
7
|
+
"es2022": true
|
|
8
|
+
},
|
|
9
|
+
"categories": {
|
|
10
|
+
"correctness": "error", // 代码正确性相关的规则,必须修复
|
|
11
|
+
"perf": "error", // 性能相关的规则,建议修复
|
|
12
|
+
"suspicious": "error", // 可能存在问题的代码模式,建议修复
|
|
13
|
+
"pedantic": "off", // 推荐设为 off:避免因为琐碎的规则干扰开发流程。
|
|
14
|
+
"style": "off", // 代码风格相关的规则,保持一致性,建议修复
|
|
15
|
+
"restriction": "off" // 可能限制开发者的规则,视项目需求开启
|
|
16
|
+
},
|
|
17
|
+
"extends": ["recommended"],
|
|
18
|
+
"rules": {
|
|
19
|
+
// --- 代码风格 ---
|
|
20
|
+
"curly": ["error", "multi-or-nest"], // 紧凑风格:单行不加大括号,多行嵌套才加
|
|
21
|
+
"eqeqeq": ["error", "always", {"null": "ignore"}], // 强制全等,但允许对 null 进行模糊匹配
|
|
22
|
+
"no-var": "error",
|
|
23
|
+
"prefer-const": "error",
|
|
24
|
+
"no-debugger": "off", // 开发阶段允许 debugger
|
|
25
|
+
|
|
26
|
+
/* --- 兼容旧版项目中的特殊处理 --- */
|
|
27
|
+
"no-console": "off", // 开发阶段允许 console
|
|
28
|
+
"eslint-plugin-unicorn/prefer-array-find": "off", // .filter()[0]
|
|
29
|
+
"eslint/no-shadow": "off", // 局部变量覆盖外层变量名
|
|
30
|
+
"eslint/no-await-in-loop": "off", // 循环中的 await
|
|
31
|
+
"eslint/no-new": "off",
|
|
32
|
+
"eslint/no-unmodified-loop-condition": "error", // 防止死循环
|
|
33
|
+
"typescript/no-unnecessary-boolean-literal-compare": "off", // 禁止不必要的布尔比较
|
|
34
|
+
"typescript/no-unsafe-type-assertion": "off", // as 给 TS 留一点“逃生舱”
|
|
35
|
+
"typescript/no-extraneous-class": "off", // 只包含静态成员的类
|
|
36
|
+
"typescript/no-explicit-any": "warn", // 禁止使用 any 类型
|
|
37
|
+
|
|
38
|
+
/* --- 性能与现代语法优化 (Unicorn & OXC) --- */
|
|
39
|
+
"oxc/no-accumulating-spread": "off", // 允许在迭代中使用解构(平衡性能与可读性)
|
|
40
|
+
"oxc/no-async-endpoint-handlers": "warn", // 异步后端处理函数中可能的未捕获错误
|
|
41
|
+
"oxc/no-map-spread": "off", // .map() 回调中直接返回解构的对象
|
|
42
|
+
"unicorn/consistent-function-scoping": "off", // 函数提升到最外层
|
|
43
|
+
"unicorn/prefer-module": "off", // 强制使用 ES 模块
|
|
44
|
+
"unicorn/require-post-message-target-origin": "warn", // 基本的安全防范
|
|
45
|
+
"unicorn/prefer-array-to-sorted": "off", // 数组排序时直接调用 .sort(),不需要先创建新数组
|
|
46
|
+
"unicorn/no-array-sort": "off", // 允许使用 .sort(),性能更好
|
|
47
|
+
"object-shorthand": ["warn", "always"], // 强制对象简写,保持代码紧凑
|
|
48
|
+
"arrow-body-style": ["warn", "as-needed"], // 只有必要时才写 return,缩短函数长度
|
|
49
|
+
|
|
50
|
+
/* --- React 适配 --- */
|
|
51
|
+
"react/jsx-no-undef": "error",
|
|
52
|
+
"react/react-in-jsx-scope": "off", // 现代 React (Next.js/Bun) 不需要 import React
|
|
53
|
+
"react/jsx-key": "error",
|
|
54
|
+
|
|
55
|
+
/* --- 变量管理 --- */
|
|
56
|
+
"no-unused-vars": [
|
|
57
|
+
"warn",
|
|
58
|
+
{
|
|
59
|
+
"vars": "local",
|
|
60
|
+
"args": "after-used",
|
|
61
|
+
"argsIgnorePattern": "^_", // 允许下划线开头的未用参数
|
|
62
|
+
"ignoreRestSiblings": true
|
|
63
|
+
}
|
|
64
|
+
]
|
|
65
|
+
},
|
|
66
|
+
// 排除检查的文件/目录
|
|
67
|
+
"ignorePatterns": [
|
|
68
|
+
"**/node_modules/**",
|
|
69
|
+
"**/dist/**",
|
|
70
|
+
"**/build/**",
|
|
71
|
+
"**/vendor/**",
|
|
72
|
+
"**/assets/**",
|
|
73
|
+
"**/*.min.js",
|
|
74
|
+
"pnpm-lock.yaml",
|
|
75
|
+
"yarn.lock",
|
|
76
|
+
"package-lock.json"
|
|
77
|
+
]
|
|
78
|
+
}
|
package/dist/core.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wia core
|
|
3
|
-
* (c) 2015-
|
|
2
|
+
* wia core v2.1.0
|
|
3
|
+
* (c) 2015-2026 Sibyl Yu and contributors
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
'use strict';
|
|
@@ -2075,6 +2075,194 @@ class Event {
|
|
|
2075
2075
|
}
|
|
2076
2076
|
}
|
|
2077
2077
|
|
|
2078
|
+
/**
|
|
2079
|
+
* @typedef {Object} LogOptions
|
|
2080
|
+
* @property {string} m - 模块名称
|
|
2081
|
+
*/
|
|
2082
|
+
|
|
2083
|
+
/**
|
|
2084
|
+
* @typedef {((...args: any[]) => void) & { debug: (...args: any[])=>void, info: (...args: any[])=>void, warn: (...args: any[])=>void, error: (...args: any[])=>void, err: (...args: any[])=>void }} ScopedLogger
|
|
2085
|
+
*/
|
|
2086
|
+
|
|
2087
|
+
/**
|
|
2088
|
+
* @typedef {((...args: any[]) => void) & { log: (...args: any[])=>void, debug: (...args: any[])=>void, info: (...args: any[])=>void, warn: (...args: any[])=>void, error: (...args: any[])=>void, err: (...args: any[])=>void, fn: (name: string) => ScopedLogger }} LoggerFn
|
|
2089
|
+
*/
|
|
2090
|
+
|
|
2091
|
+
// 1. 安全地获取开发环境状态(兼容打包工具、浏览器和 Node.js)
|
|
2092
|
+
// 如果 Rspack 注入了 __DEV__,就用注入的值;如果是在 Node.js 下,就回退读取 "production"
|
|
2093
|
+
const isDev = typeof __DEV__ !== 'undefined' ? __DEV__ : "production" !== 'production';
|
|
2094
|
+
|
|
2095
|
+
/**
|
|
2096
|
+
* 前端日志输出,封装 console 日志,简化代码
|
|
2097
|
+
* 自动识别并重排提示符与对象,保持前后端一致性
|
|
2098
|
+
*/
|
|
2099
|
+
class Log {
|
|
2100
|
+
/** @type {string} 模块名称 */
|
|
2101
|
+
m = ''
|
|
2102
|
+
|
|
2103
|
+
/** @type {string} 函数名称 */
|
|
2104
|
+
fnName = ''
|
|
2105
|
+
|
|
2106
|
+
/**
|
|
2107
|
+
* @param {string} [m=''] 模块
|
|
2108
|
+
* @param {string} [fnName=''] 函数
|
|
2109
|
+
*/
|
|
2110
|
+
constructor(m = '', fnName = '') {
|
|
2111
|
+
this.m = m;
|
|
2112
|
+
this.fnName = fnName;
|
|
2113
|
+
}
|
|
2114
|
+
|
|
2115
|
+
/**
|
|
2116
|
+
* 格式化参数:提取提示符,构建前缀,将对象后置
|
|
2117
|
+
* 格式要求:[模块名称:函数名称]提示符: {对象}
|
|
2118
|
+
* @param {any[]} args
|
|
2119
|
+
* @returns {any[]} 返回处理后传递给 console 的参数数组
|
|
2120
|
+
*/
|
|
2121
|
+
formatArgs(args) {
|
|
2122
|
+
let R = '';
|
|
2123
|
+
const _ = this;
|
|
2124
|
+
try {
|
|
2125
|
+
const msgs = [];
|
|
2126
|
+
const objs = [];
|
|
2127
|
+
|
|
2128
|
+
// 1. 将字符串(提示符)和非字符串(对象/数值等)分开
|
|
2129
|
+
for (const arg of args) {
|
|
2130
|
+
if (typeof arg === 'string') msgs.push(arg);
|
|
2131
|
+
else objs.push(arg);
|
|
2132
|
+
}
|
|
2133
|
+
|
|
2134
|
+
let prefix = '';
|
|
2135
|
+
|
|
2136
|
+
// 2. 添加模块与函数前缀
|
|
2137
|
+
if (_.m) prefix = `[${_.m}${_.fnName ? ':' + _.fnName : ''}]`;
|
|
2138
|
+
else if (_.fnName) prefix = `[:${_.fnName}]`;
|
|
2139
|
+
|
|
2140
|
+
// 3. 拼接提示符
|
|
2141
|
+
const msg = msgs.join(' ');
|
|
2142
|
+
if (msg) prefix += msg;
|
|
2143
|
+
|
|
2144
|
+
R = [];
|
|
2145
|
+
if (prefix) R.push(prefix);
|
|
2146
|
+
R.push(...objs);
|
|
2147
|
+
} catch (e) {
|
|
2148
|
+
console.error(`formatArgs exp:${e.message}`);
|
|
2149
|
+
}
|
|
2150
|
+
|
|
2151
|
+
return R
|
|
2152
|
+
}
|
|
2153
|
+
|
|
2154
|
+
/** @param {...any} args */
|
|
2155
|
+
log(...args) {
|
|
2156
|
+
// 生产环境安全剔除 (需 Rspack DefinePlugin 注入 __DEV__)
|
|
2157
|
+
if (isDev) console.log(...this.formatArgs(args));
|
|
2158
|
+
}
|
|
2159
|
+
|
|
2160
|
+
/** @param {...any} args */
|
|
2161
|
+
debug(...args) {
|
|
2162
|
+
if (isDev) console.debug(...this.formatArgs(args));
|
|
2163
|
+
}
|
|
2164
|
+
|
|
2165
|
+
/** @param {...any} args */
|
|
2166
|
+
info(...args) {
|
|
2167
|
+
if (isDev) console.info(...this.formatArgs(args));
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
/** @param {...any} args */
|
|
2171
|
+
warn(...args) {
|
|
2172
|
+
if (isDev) console.warn(...this.formatArgs(args));
|
|
2173
|
+
}
|
|
2174
|
+
|
|
2175
|
+
/** @param {...any} args */
|
|
2176
|
+
trace(...args) {
|
|
2177
|
+
if (isDev) console.trace(...this.formatArgs(args));
|
|
2178
|
+
}
|
|
2179
|
+
|
|
2180
|
+
/** * error 会在生产环境保留
|
|
2181
|
+
* @param {...any} args
|
|
2182
|
+
*/
|
|
2183
|
+
error(...args) {
|
|
2184
|
+
console.error(...this.formatArgs(args));
|
|
2185
|
+
}
|
|
2186
|
+
|
|
2187
|
+
/**
|
|
2188
|
+
* 用于 catch(e) log.err(e) 或 log.err("提示符", e) 或 log.err(e, "提示符")
|
|
2189
|
+
* 自动对齐前缀 [模块:函数] 提示符: Error对象,并保留堆栈
|
|
2190
|
+
* @param {...any} args
|
|
2191
|
+
*/
|
|
2192
|
+
err(...args) {
|
|
2193
|
+
// 生产环境安全输出,不会被 Tree-shaking 摇掉
|
|
2194
|
+
// formatArgs 会自动分离字符串与 Error 对象,并补全冒号
|
|
2195
|
+
const formattedArgs = this.formatArgs(args);
|
|
2196
|
+
|
|
2197
|
+
// 原生打印,保持 stack trace 可点击跳转!
|
|
2198
|
+
console.error(...formattedArgs);
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
/**
|
|
2202
|
+
* 派生函数级别日志实例 (完美对齐后端 log.fn)
|
|
2203
|
+
* @param {string} name 函数名称
|
|
2204
|
+
* @returns {ScopedLogger}
|
|
2205
|
+
*/
|
|
2206
|
+
fn(name) {
|
|
2207
|
+
const scopedLog = new Log(this.m, name);
|
|
2208
|
+
|
|
2209
|
+
/** @type {any} */
|
|
2210
|
+
const R = (...args) => scopedLog.log(...args);
|
|
2211
|
+
|
|
2212
|
+
R.debug = scopedLog.debug.bind(scopedLog);
|
|
2213
|
+
R.info = scopedLog.info.bind(scopedLog);
|
|
2214
|
+
R.warn = scopedLog.warn.bind(scopedLog);
|
|
2215
|
+
R.trace = scopedLog.trace.bind(scopedLog);
|
|
2216
|
+
R.error = scopedLog.error.bind(scopedLog);
|
|
2217
|
+
R.err = scopedLog.err.bind(scopedLog);
|
|
2218
|
+
|
|
2219
|
+
return /** @type {ScopedLogger} */ (R)
|
|
2220
|
+
}
|
|
2221
|
+
}
|
|
2222
|
+
|
|
2223
|
+
// 实例化唯一的全局日志单例
|
|
2224
|
+
const _log = new Log();
|
|
2225
|
+
|
|
2226
|
+
/**
|
|
2227
|
+
* 标准日志输出或构建模块日志类实例,用于模块中带[m:xxx]标记日志输出
|
|
2228
|
+
* 传入 { m: '模块名' } 构建实例,或直接传入参数输出全局日志
|
|
2229
|
+
* @param {...any} args - params
|
|
2230
|
+
* returns {*}
|
|
2231
|
+
*/
|
|
2232
|
+
function log(...args) {
|
|
2233
|
+
const last = args.at(-1);
|
|
2234
|
+
|
|
2235
|
+
// 1. 如果只有一个参数且带 m 属性,说明是构造 Logger 实例 (如: const log = Log({m: 'User'}))
|
|
2236
|
+
if (args.length === 1 && typeof last === 'object' && last !== null && 'm' in last) {
|
|
2237
|
+
const lg = new Log(last.m);
|
|
2238
|
+
|
|
2239
|
+
/** @type {any} */
|
|
2240
|
+
const R = (...args2) => lg.log(...args2);
|
|
2241
|
+
|
|
2242
|
+
R.log = lg.log.bind(lg);
|
|
2243
|
+
R.debug = lg.debug.bind(lg);
|
|
2244
|
+
R.info = lg.info.bind(lg);
|
|
2245
|
+
R.warn = lg.warn.bind(lg);
|
|
2246
|
+
R.trace = lg.trace.bind(lg);
|
|
2247
|
+
R.error = lg.error.bind(lg);
|
|
2248
|
+
R.err = lg.err.bind(lg);
|
|
2249
|
+
R.fn = lg.fn.bind(lg); // 暴露派生函数作用域方法
|
|
2250
|
+
|
|
2251
|
+
return /** @type {LoggerFn} */ (R)
|
|
2252
|
+
}
|
|
2253
|
+
|
|
2254
|
+
// 2. 否则作为全局普通日志直接输出
|
|
2255
|
+
_log.log(...args);
|
|
2256
|
+
}
|
|
2257
|
+
|
|
2258
|
+
// 绑定全局快捷方法,方便直接调用 log.err(e) 等
|
|
2259
|
+
log.err = _log.err.bind(_log);
|
|
2260
|
+
log.error = _log.error.bind(_log);
|
|
2261
|
+
log.warn = _log.warn.bind(_log);
|
|
2262
|
+
log.info = _log.info.bind(_log);
|
|
2263
|
+
log.debug = _log.debug.bind(_log);
|
|
2264
|
+
log.trace = _log.trace.bind(_log);
|
|
2265
|
+
|
|
2078
2266
|
/**
|
|
2079
2267
|
* 所有页面从该类继承,并必须实现 load 事件!
|
|
2080
2268
|
* 事件
|
|
@@ -2103,7 +2291,6 @@ class Event {
|
|
|
2103
2291
|
*
|
|
2104
2292
|
*/
|
|
2105
2293
|
|
|
2106
|
-
|
|
2107
2294
|
class Page extends Event {
|
|
2108
2295
|
constructor(app, name, title, style) {
|
|
2109
2296
|
super(null, [app]);
|
|
@@ -2145,16 +2332,16 @@ class Page extends Event {
|
|
|
2145
2332
|
* 在已经加载就绪的视图上操作
|
|
2146
2333
|
* @param {*} view 页面层的 Dom 对象,已经使用`$(#page-name)`,做了处理
|
|
2147
2334
|
* @param {*} param go 函数的参数,或 网址中 url 中的参数
|
|
2148
|
-
* @param {*}
|
|
2335
|
+
* @param {*} lastPath 前路由的路径,go 函数的参数,或 网址中 url 中的参数
|
|
2149
2336
|
*/
|
|
2150
|
-
ready(view, param,
|
|
2337
|
+
ready(view, param, lastPath) {
|
|
2151
2338
|
// $.assign(this, {page, param, back});
|
|
2152
2339
|
// $.assign(this.data, param);
|
|
2153
2340
|
// 隐藏所有模板
|
|
2154
2341
|
this.init();
|
|
2155
|
-
this.emit('local::ready', view, param,
|
|
2342
|
+
this.emit('local::ready', view, param, lastPath);
|
|
2156
2343
|
// 向上触发跨页面事件,存在安全问题
|
|
2157
|
-
this.emit('pageReady', this, view, param,
|
|
2344
|
+
this.emit('pageReady', this, view, param, lastPath);
|
|
2158
2345
|
}
|
|
2159
2346
|
|
|
2160
2347
|
/**
|
|
@@ -2168,29 +2355,29 @@ class Page extends Event {
|
|
|
2168
2355
|
|
|
2169
2356
|
// 显示已加载的页面
|
|
2170
2357
|
// view:页面Dom层,param:参数
|
|
2171
|
-
show(view, param) {
|
|
2358
|
+
show(view, param, lastPath) {
|
|
2172
2359
|
// 隐藏所有模板
|
|
2173
2360
|
view.qus('[name$=-tp]').hide();
|
|
2174
2361
|
// 防止空链接,刷新页面
|
|
2175
2362
|
view.qus('a[href=""]').attr('href', 'javascript:;');
|
|
2176
2363
|
// this.init();
|
|
2177
2364
|
if (this.reset) this.reset();
|
|
2178
|
-
this.emit('local::show', view, param);
|
|
2365
|
+
this.emit('local::show', view, param, lastPath);
|
|
2179
2366
|
// 向上触发跨页面事件,存在安全问题
|
|
2180
|
-
this.emit('pageShow', this, view, param);
|
|
2367
|
+
this.emit('pageShow', this, view, param, lastPath);
|
|
2181
2368
|
}
|
|
2182
2369
|
|
|
2183
2370
|
// 回退显示已加载的页面
|
|
2184
2371
|
// view:页面Dom层,param:参数
|
|
2185
|
-
back(view, param) {
|
|
2372
|
+
back(view, param, lastPath) {
|
|
2186
2373
|
// 隐藏所有模板
|
|
2187
2374
|
view.qus('[name$=-tp]').hide();
|
|
2188
2375
|
// 防止空链接,刷新页面
|
|
2189
2376
|
view.qus('a[href=""]').attr('href', 'javascript:;');
|
|
2190
2377
|
|
|
2191
|
-
this.emit('local::back', view, param);
|
|
2378
|
+
this.emit('local::back', view, param, lastPath);
|
|
2192
2379
|
// 向上触发跨页面事件,存在安全问题
|
|
2193
|
-
this.emit('pageBack', this, view, param);
|
|
2380
|
+
this.emit('pageBack', this, view, param, lastPath);
|
|
2194
2381
|
}
|
|
2195
2382
|
|
|
2196
2383
|
change(view, param, lastParam) {
|
|
@@ -2212,6 +2399,77 @@ class Page extends Event {
|
|
|
2212
2399
|
}
|
|
2213
2400
|
}
|
|
2214
2401
|
|
|
2402
|
+
/**
|
|
2403
|
+
* 页面工厂函数
|
|
2404
|
+
* 自动处理类的继承、日志注入、生命周期事件绑定
|
|
2405
|
+
* @param {Object} def - 页面默认配置
|
|
2406
|
+
* @param {Object} hooks - 业务钩子函数集合 { init, bind, show... }
|
|
2407
|
+
*/
|
|
2408
|
+
function page(def, hooks = {}) {
|
|
2409
|
+
// 自动根据配置创建当前页面的专属日志实例
|
|
2410
|
+
const _log = log({m: `${def.name}`});
|
|
2411
|
+
|
|
2412
|
+
return class extends Page {
|
|
2413
|
+
constructor(opts = {}) {
|
|
2414
|
+
const opt = {...def, ...opts};
|
|
2415
|
+
super(opt.app, opt.name, opt.title);
|
|
2416
|
+
this.opt = opt;
|
|
2417
|
+
|
|
2418
|
+
// ✨ 魔法 1:利用 page.js 内置的 Event 机制,全自动挂载无侵入日志!
|
|
2419
|
+
this.on('local::load', param => _log({param}, 'load'));
|
|
2420
|
+
this.on('local::ready', (v, param, lastPath) => _log({v, param, lastPath, id: this.id}, 'ready'));
|
|
2421
|
+
this.on('local::show', (v, param, lastPath) => _log({v, param, lastPath, id: this.id}, 'show'));
|
|
2422
|
+
this.on('local::change', (v, param, lastParam) => _log({v, param, lastParam}, 'change'));
|
|
2423
|
+
this.on('local::back', (v, param, lastPath) => _log({v, param, lastPath, id: this.id}, 'back'));
|
|
2424
|
+
this.on('local::hide', v => _log({v, id: this.id}, 'hide'));
|
|
2425
|
+
this.on('local::unload', v => _log({v, id: this.id}, 'unload'));
|
|
2426
|
+
}
|
|
2427
|
+
|
|
2428
|
+
// 映射其他生命周期到业务 hooks
|
|
2429
|
+
load(param) {
|
|
2430
|
+
super.load(param);
|
|
2431
|
+
if (hooks.load) hooks.load({param, pg: this, log: _log});
|
|
2432
|
+
}
|
|
2433
|
+
|
|
2434
|
+
// ✨ 魔法 2:统一调度业务代码的 init 和 bind,提供优雅的上下文
|
|
2435
|
+
async ready(v, param, lastPath) {
|
|
2436
|
+
super.ready(v, param, lastPath);
|
|
2437
|
+
|
|
2438
|
+
// 组装上下文对象供业务使用
|
|
2439
|
+
const ctx = {v, pg: this, param, lastPath, log: _log};
|
|
2440
|
+
|
|
2441
|
+
// 自动按顺序执行业务层的初始化和事件绑定
|
|
2442
|
+
if (hooks.init) await hooks.init(ctx);
|
|
2443
|
+
if (hooks.bind) hooks.bind(ctx);
|
|
2444
|
+
}
|
|
2445
|
+
|
|
2446
|
+
show(v, param, lastPath) {
|
|
2447
|
+
super.show(v, param, lastPath);
|
|
2448
|
+
if (hooks.show) hooks.show({v, pg: this, param, lastPath, log: _log});
|
|
2449
|
+
}
|
|
2450
|
+
|
|
2451
|
+
change(v, param, lastParam) {
|
|
2452
|
+
super.change(v, param, lastParam);
|
|
2453
|
+
if (hooks.change) hooks.change({v, pg: this, param, lastParam, log: _log});
|
|
2454
|
+
}
|
|
2455
|
+
|
|
2456
|
+
back(v, param, lastPath) {
|
|
2457
|
+
super.back(v, param, lastPath);
|
|
2458
|
+
if (hooks.back) hooks.back({v, pg: this, param, lastPath, log: _log});
|
|
2459
|
+
}
|
|
2460
|
+
|
|
2461
|
+
hide(v, param) {
|
|
2462
|
+
super.hide(v, param);
|
|
2463
|
+
if (hooks.hide) hooks.hide({v, pg: this, param, log: _log});
|
|
2464
|
+
}
|
|
2465
|
+
|
|
2466
|
+
unload(v) {
|
|
2467
|
+
super.unload(v);
|
|
2468
|
+
if (hooks.unload) hooks.unload({v, pg: this, log: _log});
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2215
2473
|
/**
|
|
2216
2474
|
* Wia app、router等继承类,通过模块化扩展类功能
|
|
2217
2475
|
* 使用 use 装载,注解可能完成类似功能
|
|
@@ -4420,3 +4678,4 @@ exports.Support = Support;
|
|
|
4420
4678
|
exports.Utils = Utils;
|
|
4421
4679
|
exports.jsx = jsx;
|
|
4422
4680
|
exports.loadModule = loadModule;
|
|
4681
|
+
exports.page = page;
|