phecda-vue 1.0.1
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/LICENSE +21 -0
- package/dist/index.d.ts +115 -0
- package/dist/index.js +650 -0
- package/dist/index.mjs +588 -0
- package/package.json +26 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 fgsreally
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import * as mitt from 'mitt';
|
|
2
|
+
import { EventType, Handler, Emitter } from 'mitt';
|
|
3
|
+
import * as vue from 'vue';
|
|
4
|
+
import { Ref, App, UnwrapNestedRefs, Component, DefineComponent, ExtractPropTypes } from 'vue';
|
|
5
|
+
import { Phecda } from 'phecda-core';
|
|
6
|
+
export * from 'phecda-core';
|
|
7
|
+
|
|
8
|
+
type Vret<I> = {
|
|
9
|
+
[P in keyof I]: I[P] extends Function ? I[P] : Ref<I[P]>;
|
|
10
|
+
};
|
|
11
|
+
type SchemaToObj<S> = {
|
|
12
|
+
[P in keyof S]: S[P] extends object ? SchemaToObj<S[P]> : (S[P] extends string ? any : S[P]);
|
|
13
|
+
};
|
|
14
|
+
interface PhecdaEvents extends Record<EventType, unknown> {
|
|
15
|
+
[key: EventType]: any;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
declare const emitter: mitt.Emitter<PhecdaEvents>;
|
|
19
|
+
declare const emit: {
|
|
20
|
+
<Key extends keyof PhecdaEvents>(type: Key, event: PhecdaEvents[Key]): void;
|
|
21
|
+
<Key_1 extends keyof PhecdaEvents>(type: undefined extends PhecdaEvents[Key_1] ? Key_1 : never): void;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
declare function Watcher(eventName: string): (obj: any, key: string) => void;
|
|
25
|
+
|
|
26
|
+
declare const phecdaSymbol: unique symbol;
|
|
27
|
+
declare function createPhecda(): vue.Raw<{
|
|
28
|
+
install(app: App): void;
|
|
29
|
+
uesVMap: WeakMap<object, any>;
|
|
30
|
+
uesOMap: WeakMap<object, any>;
|
|
31
|
+
uesRMap: WeakMap<object, any>;
|
|
32
|
+
}>;
|
|
33
|
+
interface PhecdaInstance {
|
|
34
|
+
uesVMap: WeakMap<any, any>;
|
|
35
|
+
uesOMap: WeakMap<any, any>;
|
|
36
|
+
uesRMap: WeakMap<any, any>;
|
|
37
|
+
}
|
|
38
|
+
declare function setActivePhecda(phecda: PhecdaInstance): void;
|
|
39
|
+
declare function getActivePhecda(): PhecdaInstance;
|
|
40
|
+
|
|
41
|
+
type _DeepPartial<T> = {
|
|
42
|
+
[K in keyof T]?: _DeepPartial<T[K]>;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
declare function useO<T extends new (...args: any) => any>(Model: T): UnwrapNestedRefs<InstanceType<T>>;
|
|
46
|
+
declare function usePatch<T extends new (...args: any) => any>(Model: T, Data: _DeepPartial<InstanceType<T>>): void;
|
|
47
|
+
declare function useR<T extends new (...args: any) => any>(Model: T): UnwrapNestedRefs<InstanceType<T>>;
|
|
48
|
+
declare function useV<T extends new (...args: any) => any>(Model: T): Vret<InstanceType<T>>;
|
|
49
|
+
declare function useOn<Key extends keyof PhecdaEvents>(eventName: Key, cb: Handler<PhecdaEvents[Key]>): () => void;
|
|
50
|
+
declare function initalize<M extends new (...args: any) => any>(Model: M): InstanceType<M> | void;
|
|
51
|
+
declare function clearStorage<M extends new (...args: any) => any>(Model: M, isForceUpdate?: boolean): void;
|
|
52
|
+
declare function deleteStorage(tag: string): void;
|
|
53
|
+
|
|
54
|
+
declare const EXPRESS_RE: RegExp;
|
|
55
|
+
declare const FN_RE: RegExp;
|
|
56
|
+
declare function createFilter<Data extends Record<string, any>>(initState?: Object, option?: {
|
|
57
|
+
expressionRE?: RegExp;
|
|
58
|
+
fnRE?: RegExp;
|
|
59
|
+
exclude?: string[];
|
|
60
|
+
}): {
|
|
61
|
+
filter: <Schema>(obj: Schema) => SchemaToObj<Schema>;
|
|
62
|
+
data: [Data] extends [vue.Ref<any>] ? Data : vue.Ref<vue.UnwrapRef<Data>>;
|
|
63
|
+
init: (params?: Data) => void;
|
|
64
|
+
setState: <Key extends string>(key: Key, value: Data[Key]) => void;
|
|
65
|
+
storeState: (key: string, params?: Data) => void;
|
|
66
|
+
store: {
|
|
67
|
+
[key: string]: Data;
|
|
68
|
+
};
|
|
69
|
+
applyStore: (key: string) => void;
|
|
70
|
+
dispose: () => void;
|
|
71
|
+
clearStore: (key: string) => void;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
declare class P {
|
|
75
|
+
protected static emitter: Emitter<PhecdaEvents>;
|
|
76
|
+
protected _namespace: Phecda['_namespace'];
|
|
77
|
+
constructor();
|
|
78
|
+
get tag(): string;
|
|
79
|
+
on<Key extends keyof PhecdaEvents>(type: Key, handler: Handler<PhecdaEvents[Key]>): void;
|
|
80
|
+
emit(type: keyof PhecdaEvents, event: PhecdaEvents[keyof PhecdaEvents]): void;
|
|
81
|
+
off<Key extends keyof PhecdaEvents>(type: Key, handler?: Handler<PhecdaEvents[Key]>): void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
declare function createForm<P extends {
|
|
85
|
+
$props: any;
|
|
86
|
+
}>(compSet: Record<string, Component> | any, form: Component<P>, formItem: Component | false, modelKey?: string): DefineComponent<{
|
|
87
|
+
config: Object;
|
|
88
|
+
data: Object;
|
|
89
|
+
} & P['$props']>;
|
|
90
|
+
|
|
91
|
+
declare function createFormData<Schema extends object, Data extends object>(schema: Schema, initData?: Data, options?: {
|
|
92
|
+
expressionRE?: RegExp;
|
|
93
|
+
fnRE?: RegExp;
|
|
94
|
+
exclude?: string[];
|
|
95
|
+
}): {
|
|
96
|
+
config: SchemaToObj<Schema>;
|
|
97
|
+
data: vue.Ref<Record<string, any>>;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
declare function getElementPlusRules<M, O extends object>(Model: M, options?: O): any;
|
|
101
|
+
declare const GetDevUIRules: typeof getElementPlusRules;
|
|
102
|
+
declare function getNaiveUIRules<M, O extends object>(Model: M, options?: O): any;
|
|
103
|
+
declare const getAntDRules: typeof getNaiveUIRules;
|
|
104
|
+
declare function getNutUIRules<M, O extends object>(Model: M, options?: O): any;
|
|
105
|
+
declare const getVantRules: typeof getNutUIRules;
|
|
106
|
+
|
|
107
|
+
declare const createModal: <P>(w: Component, k: string) => (comp: DefineComponent<P, any, any>, props?: ExtractPropTypes<P>) => void;
|
|
108
|
+
|
|
109
|
+
declare function createTable<P extends {
|
|
110
|
+
$props: any;
|
|
111
|
+
}>(compSet: Record<string, Component> | any, table: Component<P>, tableColumn: Component | false, data?: any): DefineComponent<{
|
|
112
|
+
config: Object;
|
|
113
|
+
} & P['$props']>;
|
|
114
|
+
|
|
115
|
+
export { EXPRESS_RE, FN_RE, GetDevUIRules, P, PhecdaEvents, SchemaToObj, Vret, Watcher, clearStorage, createFilter, createForm, createFormData, createModal, createPhecda, createTable, deleteStorage, emit, emitter, getActivePhecda, getAntDRules, getElementPlusRules, getNaiveUIRules, getNutUIRules, getVantRules, initalize, phecdaSymbol, setActivePhecda, useO, useOn, usePatch, useR, useV };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
+
mod
|
|
24
|
+
));
|
|
25
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
26
|
+
|
|
27
|
+
// src/index.ts
|
|
28
|
+
var src_exports = {};
|
|
29
|
+
__export(src_exports, {
|
|
30
|
+
EXPRESS_RE: () => EXPRESS_RE,
|
|
31
|
+
FN_RE: () => FN_RE,
|
|
32
|
+
GetDevUIRules: () => GetDevUIRules,
|
|
33
|
+
P: () => P,
|
|
34
|
+
Watcher: () => Watcher,
|
|
35
|
+
clearStorage: () => clearStorage,
|
|
36
|
+
createFilter: () => createFilter,
|
|
37
|
+
createForm: () => createForm,
|
|
38
|
+
createFormData: () => createFormData,
|
|
39
|
+
createModal: () => createModal,
|
|
40
|
+
createPhecda: () => createPhecda,
|
|
41
|
+
createTable: () => createTable,
|
|
42
|
+
deleteStorage: () => deleteStorage,
|
|
43
|
+
emit: () => emit,
|
|
44
|
+
emitter: () => emitter,
|
|
45
|
+
getActivePhecda: () => getActivePhecda,
|
|
46
|
+
getAntDRules: () => getAntDRules,
|
|
47
|
+
getElementPlusRules: () => getElementPlusRules,
|
|
48
|
+
getNaiveUIRules: () => getNaiveUIRules,
|
|
49
|
+
getNutUIRules: () => getNutUIRules,
|
|
50
|
+
getVantRules: () => getVantRules,
|
|
51
|
+
initalize: () => initalize,
|
|
52
|
+
phecdaSymbol: () => phecdaSymbol,
|
|
53
|
+
setActivePhecda: () => setActivePhecda,
|
|
54
|
+
useO: () => useO,
|
|
55
|
+
useOn: () => useOn,
|
|
56
|
+
usePatch: () => usePatch,
|
|
57
|
+
useR: () => useR,
|
|
58
|
+
useV: () => useV
|
|
59
|
+
});
|
|
60
|
+
module.exports = __toCommonJS(src_exports);
|
|
61
|
+
|
|
62
|
+
// src/emitter.ts
|
|
63
|
+
var import_mitt = __toESM(require("mitt"));
|
|
64
|
+
var emitter = (0, import_mitt.default)();
|
|
65
|
+
var emit = emitter.emit.bind(emitter);
|
|
66
|
+
|
|
67
|
+
// src/decorators.ts
|
|
68
|
+
var import_phecda_core = require("phecda-core");
|
|
69
|
+
function Watcher(eventName) {
|
|
70
|
+
return (obj, key) => {
|
|
71
|
+
(0, import_phecda_core.setModalState)(obj, key);
|
|
72
|
+
(0, import_phecda_core.regisHandler)(obj, key, {
|
|
73
|
+
init(instance) {
|
|
74
|
+
emitter.on(eventName, instance[key].bind(instance));
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/vue/phecda.ts
|
|
81
|
+
var import_vue = require("vue");
|
|
82
|
+
var phecdaSymbol = Symbol("phecda");
|
|
83
|
+
function createPhecda() {
|
|
84
|
+
const phecda = (0, import_vue.markRaw)({
|
|
85
|
+
install(app) {
|
|
86
|
+
app.provide(phecdaSymbol, phecda);
|
|
87
|
+
app.config.globalProperties.$phecda = phecda;
|
|
88
|
+
},
|
|
89
|
+
uesVMap: /* @__PURE__ */ new WeakMap(),
|
|
90
|
+
uesOMap: /* @__PURE__ */ new WeakMap(),
|
|
91
|
+
uesRMap: /* @__PURE__ */ new WeakMap()
|
|
92
|
+
});
|
|
93
|
+
return phecda;
|
|
94
|
+
}
|
|
95
|
+
var activePhecda = {
|
|
96
|
+
uesVMap: /* @__PURE__ */ new WeakMap(),
|
|
97
|
+
uesOMap: /* @__PURE__ */ new WeakMap(),
|
|
98
|
+
uesRMap: /* @__PURE__ */ new WeakMap()
|
|
99
|
+
};
|
|
100
|
+
function setActivePhecda(phecda) {
|
|
101
|
+
activePhecda = phecda;
|
|
102
|
+
}
|
|
103
|
+
function getActivePhecda() {
|
|
104
|
+
return activePhecda;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/vue/composable.ts
|
|
108
|
+
var import_vue3 = require("vue");
|
|
109
|
+
var import_phecda_core2 = require("phecda-core");
|
|
110
|
+
|
|
111
|
+
// src/vue/utils.ts
|
|
112
|
+
var import_vue2 = require("vue");
|
|
113
|
+
function isObject(o) {
|
|
114
|
+
return Object.prototype.toString.call(o) === "[object Object]";
|
|
115
|
+
}
|
|
116
|
+
function mergeReactiveObjects(target, patchToApply) {
|
|
117
|
+
for (const key in patchToApply) {
|
|
118
|
+
if (!patchToApply.hasOwnProperty(key))
|
|
119
|
+
continue;
|
|
120
|
+
const subPatch = patchToApply[key];
|
|
121
|
+
const targetValue = target[key];
|
|
122
|
+
if (isObject(targetValue) && isObject(subPatch) && target.hasOwnProperty(key) && !(0, import_vue2.isRef)(subPatch) && !(0, import_vue2.isReactive)(subPatch)) {
|
|
123
|
+
target[key] = mergeReactiveObjects(targetValue, subPatch);
|
|
124
|
+
} else {
|
|
125
|
+
target[key] = subPatch;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return target;
|
|
129
|
+
}
|
|
130
|
+
function wrapError(target, key, errorHandler) {
|
|
131
|
+
if (isAsyncFunc(target[key])) {
|
|
132
|
+
return (...args) => {
|
|
133
|
+
return target[key].apply(target, args).catch(errorHandler);
|
|
134
|
+
};
|
|
135
|
+
} else {
|
|
136
|
+
return (...args) => {
|
|
137
|
+
try {
|
|
138
|
+
return target[key].apply(target, args);
|
|
139
|
+
} catch (e) {
|
|
140
|
+
return errorHandler(e);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function isAsyncFunc(fn) {
|
|
146
|
+
return fn[Symbol.toStringTag] === "AsyncFunction";
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// src/vue/composable.ts
|
|
150
|
+
function useO(Model) {
|
|
151
|
+
if ((0, import_vue3.getCurrentInstance)()) {
|
|
152
|
+
const cur = (0, import_vue3.inject)(phecdaSymbol, null);
|
|
153
|
+
if (cur)
|
|
154
|
+
setActivePhecda(cur);
|
|
155
|
+
}
|
|
156
|
+
const { uesOMap } = getActivePhecda();
|
|
157
|
+
if (!uesOMap.has(Model)) {
|
|
158
|
+
const instance = (0, import_vue3.reactive)(new Model());
|
|
159
|
+
uesOMap.set(Model, instance);
|
|
160
|
+
(0, import_phecda_core2.register)(instance);
|
|
161
|
+
}
|
|
162
|
+
return uesOMap.get(Model);
|
|
163
|
+
}
|
|
164
|
+
function usePatch(Model, Data) {
|
|
165
|
+
useO(Model);
|
|
166
|
+
const { uesOMap } = getActivePhecda();
|
|
167
|
+
const target = uesOMap.get(Model);
|
|
168
|
+
mergeReactiveObjects(target, Data);
|
|
169
|
+
}
|
|
170
|
+
function useR(Model) {
|
|
171
|
+
useO(Model);
|
|
172
|
+
const { uesRMap, uesOMap } = getActivePhecda();
|
|
173
|
+
if (uesRMap.has(Model))
|
|
174
|
+
return uesRMap.get(Model);
|
|
175
|
+
const instance = uesOMap.get(Model);
|
|
176
|
+
const proxy = new Proxy(instance, {
|
|
177
|
+
get(target, key) {
|
|
178
|
+
if (typeof target[key] === "function") {
|
|
179
|
+
const errorHandler = (0, import_phecda_core2.getHandler)(target, key).find((item) => item.error)?.error;
|
|
180
|
+
if (!errorHandler)
|
|
181
|
+
return target[key].bind(target);
|
|
182
|
+
return wrapError(target, key, errorHandler);
|
|
183
|
+
}
|
|
184
|
+
return target[key];
|
|
185
|
+
},
|
|
186
|
+
set(target, key, v) {
|
|
187
|
+
target[key] = v;
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
uesRMap.set(Model, proxy);
|
|
192
|
+
return proxy;
|
|
193
|
+
}
|
|
194
|
+
function useV(Model) {
|
|
195
|
+
useO(Model);
|
|
196
|
+
const { uesVMap, uesOMap } = getActivePhecda();
|
|
197
|
+
if (uesVMap.has(Model))
|
|
198
|
+
return uesVMap.get(Model);
|
|
199
|
+
const instance = uesOMap.get(Model);
|
|
200
|
+
const proxy = new Proxy(instance, {
|
|
201
|
+
get(target, key) {
|
|
202
|
+
if (typeof target[key] === "function") {
|
|
203
|
+
const errorHandler = (0, import_phecda_core2.getHandler)(target, key).find((item) => item.error)?.error;
|
|
204
|
+
if (!errorHandler)
|
|
205
|
+
return target[key].bind(target);
|
|
206
|
+
return wrapError(target, key, errorHandler);
|
|
207
|
+
}
|
|
208
|
+
return (0, import_vue3.computed)({
|
|
209
|
+
get() {
|
|
210
|
+
return target[key];
|
|
211
|
+
},
|
|
212
|
+
set(v) {
|
|
213
|
+
return target[key] = v;
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
},
|
|
217
|
+
set() {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
uesVMap.set(Model, proxy);
|
|
222
|
+
return proxy;
|
|
223
|
+
}
|
|
224
|
+
function useOn(eventName, cb) {
|
|
225
|
+
(0, import_vue3.onUnmounted)(() => {
|
|
226
|
+
emitter.off(eventName, cb);
|
|
227
|
+
});
|
|
228
|
+
emitter.on(eventName, cb);
|
|
229
|
+
return () => emitter.off(eventName, cb);
|
|
230
|
+
}
|
|
231
|
+
function initalize(Model) {
|
|
232
|
+
const instance = useO(Model);
|
|
233
|
+
if (instance) {
|
|
234
|
+
Object.assign(instance, new Model());
|
|
235
|
+
return instance;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function clearStorage(Model, isForceUpdate = true) {
|
|
239
|
+
localStorage.removeItem(`_phecda_${useO(Model)._symbol}`);
|
|
240
|
+
isForceUpdate && initalize(Model);
|
|
241
|
+
}
|
|
242
|
+
function deleteStorage(tag) {
|
|
243
|
+
localStorage.removeItem(`_phecda_${tag}`);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// src/filter.ts
|
|
247
|
+
var import_vue4 = require("vue");
|
|
248
|
+
var EXPRESS_RE = /^{{(.*)}}$/;
|
|
249
|
+
var FN_RE = /^\[\[(.*)\]\]$/;
|
|
250
|
+
function createFilter(initState = {}, option = {}) {
|
|
251
|
+
const resolveOption = Object.assign(
|
|
252
|
+
{
|
|
253
|
+
expressionRE: EXPRESS_RE,
|
|
254
|
+
fnRE: FN_RE,
|
|
255
|
+
exclude: []
|
|
256
|
+
},
|
|
257
|
+
option
|
|
258
|
+
);
|
|
259
|
+
let data = (0, import_vue4.ref)(initState);
|
|
260
|
+
let store = {};
|
|
261
|
+
function traverse(obj) {
|
|
262
|
+
for (const i in obj) {
|
|
263
|
+
if (Array.isArray(obj[i]) || resolveOption.exclude.includes(i))
|
|
264
|
+
continue;
|
|
265
|
+
if (typeof obj[i] === "object" && obj[i])
|
|
266
|
+
traverse(obj[i]);
|
|
267
|
+
if (typeof obj[i] === "string") {
|
|
268
|
+
if (resolveOption.expressionRE.test(obj[i])) {
|
|
269
|
+
const body = obj[i].match(resolveOption.expressionRE)[1];
|
|
270
|
+
Object.defineProperty(obj, i, {
|
|
271
|
+
get() {
|
|
272
|
+
return new Function(...Object.keys(data.value), `return ${body}`)(
|
|
273
|
+
...Object.values(data.value)
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
if (resolveOption.fnRE.test(obj[i])) {
|
|
279
|
+
const body = obj[i].match(resolveOption.fnRE)[1];
|
|
280
|
+
Object.defineProperty(obj, i, {
|
|
281
|
+
get() {
|
|
282
|
+
return new Function(...Object.keys(data.value), `${body}`)(
|
|
283
|
+
...Object.values(data.value)
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function filter(obj) {
|
|
292
|
+
obj = (0, import_vue4.reactive)(obj);
|
|
293
|
+
traverse(obj);
|
|
294
|
+
return obj;
|
|
295
|
+
}
|
|
296
|
+
function setState(key, value) {
|
|
297
|
+
data.value[key] = value;
|
|
298
|
+
}
|
|
299
|
+
function storeState(key, params) {
|
|
300
|
+
store[key] = data.value;
|
|
301
|
+
init(params);
|
|
302
|
+
}
|
|
303
|
+
function applyStore(key) {
|
|
304
|
+
if (!store[key])
|
|
305
|
+
return;
|
|
306
|
+
data.value = store[key];
|
|
307
|
+
}
|
|
308
|
+
function init(params) {
|
|
309
|
+
data.value = params || initState || {};
|
|
310
|
+
}
|
|
311
|
+
function clearStore(key) {
|
|
312
|
+
delete store[key];
|
|
313
|
+
}
|
|
314
|
+
function dispose() {
|
|
315
|
+
data = null;
|
|
316
|
+
store = null;
|
|
317
|
+
}
|
|
318
|
+
return { filter, data, init, setState, storeState, store, applyStore, dispose, clearStore };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// src/index.ts
|
|
322
|
+
__reExport(src_exports, require("phecda-core"), module.exports);
|
|
323
|
+
|
|
324
|
+
// src/wrapper.ts
|
|
325
|
+
var _P = class {
|
|
326
|
+
constructor() {
|
|
327
|
+
}
|
|
328
|
+
get tag() {
|
|
329
|
+
return this._namespace.__TAG__;
|
|
330
|
+
}
|
|
331
|
+
on(type, handler) {
|
|
332
|
+
_P.emitter.on(type, handler);
|
|
333
|
+
}
|
|
334
|
+
emit(type, event) {
|
|
335
|
+
_P.emitter.emit(type, event);
|
|
336
|
+
}
|
|
337
|
+
off(type, handler) {
|
|
338
|
+
_P.emitter.off(type, handler);
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
var P = _P;
|
|
342
|
+
P.emitter = emitter;
|
|
343
|
+
|
|
344
|
+
// src/components/createForm.ts
|
|
345
|
+
var import_vue5 = require("vue");
|
|
346
|
+
function createForm(compSet, form, formItem, modelKey = "modelValue") {
|
|
347
|
+
function generateChildVNode(props) {
|
|
348
|
+
return props._children?.map(
|
|
349
|
+
(item) => (0, import_vue5.h)(compSet[item._component], item)
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
function generateVNode(props) {
|
|
353
|
+
return (0, import_vue5.h)(
|
|
354
|
+
compSet[props.config[props.property]._component],
|
|
355
|
+
{
|
|
356
|
+
...props.config[props.property],
|
|
357
|
+
[`${modelKey}`]: props.data[props.property],
|
|
358
|
+
[`onUpdate:${modelKey}`]: (v) => {
|
|
359
|
+
props.data[props.property] = v;
|
|
360
|
+
}
|
|
361
|
+
},
|
|
362
|
+
{
|
|
363
|
+
default: () => generateChildVNode(props.config[props.property])
|
|
364
|
+
}
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
const FormItem = (0, import_vue5.defineComponent)({
|
|
368
|
+
name: "CustomFormItem",
|
|
369
|
+
props: {
|
|
370
|
+
formItem: { type: Object },
|
|
371
|
+
config: {
|
|
372
|
+
type: Object,
|
|
373
|
+
required: true
|
|
374
|
+
},
|
|
375
|
+
data: {
|
|
376
|
+
type: Object,
|
|
377
|
+
required: true
|
|
378
|
+
},
|
|
379
|
+
property: {
|
|
380
|
+
type: String,
|
|
381
|
+
required: true
|
|
382
|
+
}
|
|
383
|
+
},
|
|
384
|
+
setup(props) {
|
|
385
|
+
return () => {
|
|
386
|
+
return formItem ? (0, import_vue5.h)(
|
|
387
|
+
formItem,
|
|
388
|
+
{
|
|
389
|
+
...props.formItem
|
|
390
|
+
},
|
|
391
|
+
{
|
|
392
|
+
default: () => {
|
|
393
|
+
return generateVNode(props);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
) : generateVNode(props);
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
return (0, import_vue5.defineComponent)({
|
|
401
|
+
name: "CustomForm",
|
|
402
|
+
props: {
|
|
403
|
+
config: {
|
|
404
|
+
type: Object,
|
|
405
|
+
required: true
|
|
406
|
+
},
|
|
407
|
+
data: {
|
|
408
|
+
type: Object,
|
|
409
|
+
required: true
|
|
410
|
+
}
|
|
411
|
+
},
|
|
412
|
+
setup(props, ctx) {
|
|
413
|
+
const dom = (0, import_vue5.ref)();
|
|
414
|
+
(0, import_vue5.onMounted)(() => {
|
|
415
|
+
ctx.expose({ ...dom.value });
|
|
416
|
+
});
|
|
417
|
+
return () => {
|
|
418
|
+
return (0, import_vue5.h)(form, Object.assign({ ref: dom }, ctx.attrs), {
|
|
419
|
+
default: () => Object.keys(props.config).map((item) => {
|
|
420
|
+
return (0, import_vue5.h)(FormItem, {
|
|
421
|
+
formItem: props.config[item]._formItem,
|
|
422
|
+
config: props.config,
|
|
423
|
+
property: item,
|
|
424
|
+
data: props.data
|
|
425
|
+
});
|
|
426
|
+
})
|
|
427
|
+
});
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// src/components/formFilter.ts
|
|
434
|
+
function createFormData(schema, initData = {}, options = {}) {
|
|
435
|
+
const { data, filter } = createFilter(initData, options);
|
|
436
|
+
initlize(schema, data.value);
|
|
437
|
+
const filterRet = filter(schema);
|
|
438
|
+
function initlize(obj1, obj2) {
|
|
439
|
+
for (const i in obj1) {
|
|
440
|
+
if (obj1[i]._default)
|
|
441
|
+
obj2[i] = obj1[i]._default;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return { config: filterRet, data };
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// src/components/formResolve.ts
|
|
448
|
+
var import_phecda_core3 = require("phecda-core");
|
|
449
|
+
function getElementPlusRules(Model, options = {}) {
|
|
450
|
+
const stateVars = (0, import_phecda_core3.getModelState)(Model);
|
|
451
|
+
const ret = {};
|
|
452
|
+
for (const item of stateVars) {
|
|
453
|
+
const handlers = (0, import_phecda_core3.getHandler)(Model, item);
|
|
454
|
+
if (handlers) {
|
|
455
|
+
for (const handler of handlers) {
|
|
456
|
+
const { rule, meta, info } = handler;
|
|
457
|
+
if (rule) {
|
|
458
|
+
if (!ret[item])
|
|
459
|
+
ret[item] = [];
|
|
460
|
+
ret[item].push({
|
|
461
|
+
validator: async (_, value, callback) => {
|
|
462
|
+
if (!await (0, import_phecda_core3.validate)(rule, value))
|
|
463
|
+
callback(new Error(info || ""));
|
|
464
|
+
else
|
|
465
|
+
callback();
|
|
466
|
+
},
|
|
467
|
+
...options,
|
|
468
|
+
...meta || {}
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return ret;
|
|
475
|
+
}
|
|
476
|
+
var GetDevUIRules = getElementPlusRules;
|
|
477
|
+
function getNaiveUIRules(Model, options = {}) {
|
|
478
|
+
const stateVars = (0, import_phecda_core3.getModelState)(Model);
|
|
479
|
+
const ret = {};
|
|
480
|
+
for (const item of stateVars) {
|
|
481
|
+
const handlers = (0, import_phecda_core3.getHandler)(Model, item);
|
|
482
|
+
if (handlers) {
|
|
483
|
+
for (const handler of handlers) {
|
|
484
|
+
const { rule, meta, info } = handler;
|
|
485
|
+
if (rule) {
|
|
486
|
+
if (!ret[item])
|
|
487
|
+
ret[item] = [];
|
|
488
|
+
ret[item].push({
|
|
489
|
+
validator: async (_, value) => {
|
|
490
|
+
if (!await (0, import_phecda_core3.validate)(rule, value))
|
|
491
|
+
return Promise.reject(info);
|
|
492
|
+
else
|
|
493
|
+
return Promise.resolve();
|
|
494
|
+
},
|
|
495
|
+
...options,
|
|
496
|
+
...meta || {}
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return ret;
|
|
503
|
+
}
|
|
504
|
+
var getAntDRules = getNaiveUIRules;
|
|
505
|
+
function getNutUIRules(Model, options = {}) {
|
|
506
|
+
const stateVars = (0, import_phecda_core3.getModelState)(Model);
|
|
507
|
+
const ret = {};
|
|
508
|
+
for (const item of stateVars) {
|
|
509
|
+
const handlers = (0, import_phecda_core3.getHandler)(Model, item);
|
|
510
|
+
if (handlers) {
|
|
511
|
+
for (const handler of handlers) {
|
|
512
|
+
const { rule, meta, info } = handler;
|
|
513
|
+
if (rule) {
|
|
514
|
+
if (!ret[item])
|
|
515
|
+
ret[item] = [];
|
|
516
|
+
ret[item].push({
|
|
517
|
+
validator: async (_, value) => {
|
|
518
|
+
if (!await (0, import_phecda_core3.validate)(rule, value))
|
|
519
|
+
return false;
|
|
520
|
+
else
|
|
521
|
+
return true;
|
|
522
|
+
},
|
|
523
|
+
message: info,
|
|
524
|
+
...options,
|
|
525
|
+
...meta || {}
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
return ret;
|
|
532
|
+
}
|
|
533
|
+
var getVantRules = getNutUIRules;
|
|
534
|
+
|
|
535
|
+
// src/components/createModal.ts
|
|
536
|
+
var import_vue6 = require("vue");
|
|
537
|
+
var createModal = function(modalWrapper, modelKey = "modelValue") {
|
|
538
|
+
let isMounted = false;
|
|
539
|
+
const isShow = (0, import_vue6.ref)(true);
|
|
540
|
+
const content = (0, import_vue6.shallowRef)();
|
|
541
|
+
const propsRef = (0, import_vue6.ref)({});
|
|
542
|
+
const wrapper = (0, import_vue6.defineComponent)({
|
|
543
|
+
setup() {
|
|
544
|
+
return () => (0, import_vue6.h)(modalWrapper, {
|
|
545
|
+
[modelKey]: isShow.value,
|
|
546
|
+
[`onUpdate:${modelKey}`]: (v) => {
|
|
547
|
+
isShow.value = v;
|
|
548
|
+
}
|
|
549
|
+
}, {
|
|
550
|
+
default: () => content.value && (0, import_vue6.h)(content.value, propsRef.value)
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
return (comp, props) => {
|
|
555
|
+
content.value = comp;
|
|
556
|
+
propsRef.value = props;
|
|
557
|
+
if (!isMounted) {
|
|
558
|
+
const el = document.createElement("div");
|
|
559
|
+
const vnode = (0, import_vue6.h)(wrapper);
|
|
560
|
+
document.body.appendChild(((0, import_vue6.render)(vnode, el), el));
|
|
561
|
+
isMounted = true;
|
|
562
|
+
} else {
|
|
563
|
+
isShow.value = true;
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
// src/components/createTable.ts
|
|
569
|
+
var import_vue7 = require("vue");
|
|
570
|
+
function createTable(compSet, table, tableColumn, data) {
|
|
571
|
+
const TableColumn = (0, import_vue7.defineComponent)({
|
|
572
|
+
name: "PhecdaTableColumn",
|
|
573
|
+
props: {
|
|
574
|
+
tableColumn: {
|
|
575
|
+
type: Object,
|
|
576
|
+
required: true
|
|
577
|
+
}
|
|
578
|
+
},
|
|
579
|
+
setup(props) {
|
|
580
|
+
const compName = props.tableColumn._component;
|
|
581
|
+
return () => (0, import_vue7.h)(
|
|
582
|
+
tableColumn,
|
|
583
|
+
{
|
|
584
|
+
...props.tableColumn
|
|
585
|
+
},
|
|
586
|
+
{
|
|
587
|
+
default: (scope) => {
|
|
588
|
+
return compName ? (0, import_vue7.h)(compSet[compName], { scope, data, ...props.tableColumn._props || {} }) : null;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
return (0, import_vue7.defineComponent)({
|
|
595
|
+
name: "PhecdaTable",
|
|
596
|
+
props: {
|
|
597
|
+
config: {
|
|
598
|
+
type: Array,
|
|
599
|
+
required: true
|
|
600
|
+
}
|
|
601
|
+
},
|
|
602
|
+
setup(props, ctx) {
|
|
603
|
+
const dom = (0, import_vue7.ref)();
|
|
604
|
+
(0, import_vue7.onMounted)(() => {
|
|
605
|
+
ctx.expose({ ...dom.value });
|
|
606
|
+
});
|
|
607
|
+
return () => {
|
|
608
|
+
return (0, import_vue7.h)(table, Object.assign({ ref: dom }, ctx.attrs), {
|
|
609
|
+
default: () => props.config.map((item) => {
|
|
610
|
+
return (0, import_vue7.h)(TableColumn, {
|
|
611
|
+
tableColumn: item
|
|
612
|
+
});
|
|
613
|
+
})
|
|
614
|
+
});
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
620
|
+
0 && (module.exports = {
|
|
621
|
+
EXPRESS_RE,
|
|
622
|
+
FN_RE,
|
|
623
|
+
GetDevUIRules,
|
|
624
|
+
P,
|
|
625
|
+
Watcher,
|
|
626
|
+
clearStorage,
|
|
627
|
+
createFilter,
|
|
628
|
+
createForm,
|
|
629
|
+
createFormData,
|
|
630
|
+
createModal,
|
|
631
|
+
createPhecda,
|
|
632
|
+
createTable,
|
|
633
|
+
deleteStorage,
|
|
634
|
+
emit,
|
|
635
|
+
emitter,
|
|
636
|
+
getActivePhecda,
|
|
637
|
+
getAntDRules,
|
|
638
|
+
getElementPlusRules,
|
|
639
|
+
getNaiveUIRules,
|
|
640
|
+
getNutUIRules,
|
|
641
|
+
getVantRules,
|
|
642
|
+
initalize,
|
|
643
|
+
phecdaSymbol,
|
|
644
|
+
setActivePhecda,
|
|
645
|
+
useO,
|
|
646
|
+
useOn,
|
|
647
|
+
usePatch,
|
|
648
|
+
useR,
|
|
649
|
+
useV
|
|
650
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
// src/emitter.ts
|
|
2
|
+
import mitt from "mitt";
|
|
3
|
+
var emitter = mitt();
|
|
4
|
+
var emit = emitter.emit.bind(emitter);
|
|
5
|
+
|
|
6
|
+
// src/decorators.ts
|
|
7
|
+
import { regisHandler, setModalState } from "phecda-core";
|
|
8
|
+
function Watcher(eventName) {
|
|
9
|
+
return (obj, key) => {
|
|
10
|
+
setModalState(obj, key);
|
|
11
|
+
regisHandler(obj, key, {
|
|
12
|
+
init(instance) {
|
|
13
|
+
emitter.on(eventName, instance[key].bind(instance));
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/vue/phecda.ts
|
|
20
|
+
import { markRaw } from "vue";
|
|
21
|
+
var phecdaSymbol = Symbol("phecda");
|
|
22
|
+
function createPhecda() {
|
|
23
|
+
const phecda = markRaw({
|
|
24
|
+
install(app) {
|
|
25
|
+
app.provide(phecdaSymbol, phecda);
|
|
26
|
+
app.config.globalProperties.$phecda = phecda;
|
|
27
|
+
},
|
|
28
|
+
uesVMap: /* @__PURE__ */ new WeakMap(),
|
|
29
|
+
uesOMap: /* @__PURE__ */ new WeakMap(),
|
|
30
|
+
uesRMap: /* @__PURE__ */ new WeakMap()
|
|
31
|
+
});
|
|
32
|
+
return phecda;
|
|
33
|
+
}
|
|
34
|
+
var activePhecda = {
|
|
35
|
+
uesVMap: /* @__PURE__ */ new WeakMap(),
|
|
36
|
+
uesOMap: /* @__PURE__ */ new WeakMap(),
|
|
37
|
+
uesRMap: /* @__PURE__ */ new WeakMap()
|
|
38
|
+
};
|
|
39
|
+
function setActivePhecda(phecda) {
|
|
40
|
+
activePhecda = phecda;
|
|
41
|
+
}
|
|
42
|
+
function getActivePhecda() {
|
|
43
|
+
return activePhecda;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/vue/composable.ts
|
|
47
|
+
import { computed, getCurrentInstance, inject, onUnmounted, reactive } from "vue";
|
|
48
|
+
import { getHandler, register } from "phecda-core";
|
|
49
|
+
|
|
50
|
+
// src/vue/utils.ts
|
|
51
|
+
import { isReactive, isRef } from "vue";
|
|
52
|
+
function isObject(o) {
|
|
53
|
+
return Object.prototype.toString.call(o) === "[object Object]";
|
|
54
|
+
}
|
|
55
|
+
function mergeReactiveObjects(target, patchToApply) {
|
|
56
|
+
for (const key in patchToApply) {
|
|
57
|
+
if (!patchToApply.hasOwnProperty(key))
|
|
58
|
+
continue;
|
|
59
|
+
const subPatch = patchToApply[key];
|
|
60
|
+
const targetValue = target[key];
|
|
61
|
+
if (isObject(targetValue) && isObject(subPatch) && target.hasOwnProperty(key) && !isRef(subPatch) && !isReactive(subPatch)) {
|
|
62
|
+
target[key] = mergeReactiveObjects(targetValue, subPatch);
|
|
63
|
+
} else {
|
|
64
|
+
target[key] = subPatch;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return target;
|
|
68
|
+
}
|
|
69
|
+
function wrapError(target, key, errorHandler) {
|
|
70
|
+
if (isAsyncFunc(target[key])) {
|
|
71
|
+
return (...args) => {
|
|
72
|
+
return target[key].apply(target, args).catch(errorHandler);
|
|
73
|
+
};
|
|
74
|
+
} else {
|
|
75
|
+
return (...args) => {
|
|
76
|
+
try {
|
|
77
|
+
return target[key].apply(target, args);
|
|
78
|
+
} catch (e) {
|
|
79
|
+
return errorHandler(e);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function isAsyncFunc(fn) {
|
|
85
|
+
return fn[Symbol.toStringTag] === "AsyncFunction";
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/vue/composable.ts
|
|
89
|
+
function useO(Model) {
|
|
90
|
+
if (getCurrentInstance()) {
|
|
91
|
+
const cur = inject(phecdaSymbol, null);
|
|
92
|
+
if (cur)
|
|
93
|
+
setActivePhecda(cur);
|
|
94
|
+
}
|
|
95
|
+
const { uesOMap } = getActivePhecda();
|
|
96
|
+
if (!uesOMap.has(Model)) {
|
|
97
|
+
const instance = reactive(new Model());
|
|
98
|
+
uesOMap.set(Model, instance);
|
|
99
|
+
register(instance);
|
|
100
|
+
}
|
|
101
|
+
return uesOMap.get(Model);
|
|
102
|
+
}
|
|
103
|
+
function usePatch(Model, Data) {
|
|
104
|
+
useO(Model);
|
|
105
|
+
const { uesOMap } = getActivePhecda();
|
|
106
|
+
const target = uesOMap.get(Model);
|
|
107
|
+
mergeReactiveObjects(target, Data);
|
|
108
|
+
}
|
|
109
|
+
function useR(Model) {
|
|
110
|
+
useO(Model);
|
|
111
|
+
const { uesRMap, uesOMap } = getActivePhecda();
|
|
112
|
+
if (uesRMap.has(Model))
|
|
113
|
+
return uesRMap.get(Model);
|
|
114
|
+
const instance = uesOMap.get(Model);
|
|
115
|
+
const proxy = new Proxy(instance, {
|
|
116
|
+
get(target, key) {
|
|
117
|
+
if (typeof target[key] === "function") {
|
|
118
|
+
const errorHandler = getHandler(target, key).find((item) => item.error)?.error;
|
|
119
|
+
if (!errorHandler)
|
|
120
|
+
return target[key].bind(target);
|
|
121
|
+
return wrapError(target, key, errorHandler);
|
|
122
|
+
}
|
|
123
|
+
return target[key];
|
|
124
|
+
},
|
|
125
|
+
set(target, key, v) {
|
|
126
|
+
target[key] = v;
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
uesRMap.set(Model, proxy);
|
|
131
|
+
return proxy;
|
|
132
|
+
}
|
|
133
|
+
function useV(Model) {
|
|
134
|
+
useO(Model);
|
|
135
|
+
const { uesVMap, uesOMap } = getActivePhecda();
|
|
136
|
+
if (uesVMap.has(Model))
|
|
137
|
+
return uesVMap.get(Model);
|
|
138
|
+
const instance = uesOMap.get(Model);
|
|
139
|
+
const proxy = new Proxy(instance, {
|
|
140
|
+
get(target, key) {
|
|
141
|
+
if (typeof target[key] === "function") {
|
|
142
|
+
const errorHandler = getHandler(target, key).find((item) => item.error)?.error;
|
|
143
|
+
if (!errorHandler)
|
|
144
|
+
return target[key].bind(target);
|
|
145
|
+
return wrapError(target, key, errorHandler);
|
|
146
|
+
}
|
|
147
|
+
return computed({
|
|
148
|
+
get() {
|
|
149
|
+
return target[key];
|
|
150
|
+
},
|
|
151
|
+
set(v) {
|
|
152
|
+
return target[key] = v;
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
set() {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
uesVMap.set(Model, proxy);
|
|
161
|
+
return proxy;
|
|
162
|
+
}
|
|
163
|
+
function useOn(eventName, cb) {
|
|
164
|
+
onUnmounted(() => {
|
|
165
|
+
emitter.off(eventName, cb);
|
|
166
|
+
});
|
|
167
|
+
emitter.on(eventName, cb);
|
|
168
|
+
return () => emitter.off(eventName, cb);
|
|
169
|
+
}
|
|
170
|
+
function initalize(Model) {
|
|
171
|
+
const instance = useO(Model);
|
|
172
|
+
if (instance) {
|
|
173
|
+
Object.assign(instance, new Model());
|
|
174
|
+
return instance;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function clearStorage(Model, isForceUpdate = true) {
|
|
178
|
+
localStorage.removeItem(`_phecda_${useO(Model)._symbol}`);
|
|
179
|
+
isForceUpdate && initalize(Model);
|
|
180
|
+
}
|
|
181
|
+
function deleteStorage(tag) {
|
|
182
|
+
localStorage.removeItem(`_phecda_${tag}`);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// src/filter.ts
|
|
186
|
+
import { reactive as reactive2, ref } from "vue";
|
|
187
|
+
var EXPRESS_RE = /^{{(.*)}}$/;
|
|
188
|
+
var FN_RE = /^\[\[(.*)\]\]$/;
|
|
189
|
+
function createFilter(initState = {}, option = {}) {
|
|
190
|
+
const resolveOption = Object.assign(
|
|
191
|
+
{
|
|
192
|
+
expressionRE: EXPRESS_RE,
|
|
193
|
+
fnRE: FN_RE,
|
|
194
|
+
exclude: []
|
|
195
|
+
},
|
|
196
|
+
option
|
|
197
|
+
);
|
|
198
|
+
let data = ref(initState);
|
|
199
|
+
let store = {};
|
|
200
|
+
function traverse(obj) {
|
|
201
|
+
for (const i in obj) {
|
|
202
|
+
if (Array.isArray(obj[i]) || resolveOption.exclude.includes(i))
|
|
203
|
+
continue;
|
|
204
|
+
if (typeof obj[i] === "object" && obj[i])
|
|
205
|
+
traverse(obj[i]);
|
|
206
|
+
if (typeof obj[i] === "string") {
|
|
207
|
+
if (resolveOption.expressionRE.test(obj[i])) {
|
|
208
|
+
const body = obj[i].match(resolveOption.expressionRE)[1];
|
|
209
|
+
Object.defineProperty(obj, i, {
|
|
210
|
+
get() {
|
|
211
|
+
return new Function(...Object.keys(data.value), `return ${body}`)(
|
|
212
|
+
...Object.values(data.value)
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
if (resolveOption.fnRE.test(obj[i])) {
|
|
218
|
+
const body = obj[i].match(resolveOption.fnRE)[1];
|
|
219
|
+
Object.defineProperty(obj, i, {
|
|
220
|
+
get() {
|
|
221
|
+
return new Function(...Object.keys(data.value), `${body}`)(
|
|
222
|
+
...Object.values(data.value)
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function filter(obj) {
|
|
231
|
+
obj = reactive2(obj);
|
|
232
|
+
traverse(obj);
|
|
233
|
+
return obj;
|
|
234
|
+
}
|
|
235
|
+
function setState(key, value) {
|
|
236
|
+
data.value[key] = value;
|
|
237
|
+
}
|
|
238
|
+
function storeState(key, params) {
|
|
239
|
+
store[key] = data.value;
|
|
240
|
+
init(params);
|
|
241
|
+
}
|
|
242
|
+
function applyStore(key) {
|
|
243
|
+
if (!store[key])
|
|
244
|
+
return;
|
|
245
|
+
data.value = store[key];
|
|
246
|
+
}
|
|
247
|
+
function init(params) {
|
|
248
|
+
data.value = params || initState || {};
|
|
249
|
+
}
|
|
250
|
+
function clearStore(key) {
|
|
251
|
+
delete store[key];
|
|
252
|
+
}
|
|
253
|
+
function dispose() {
|
|
254
|
+
data = null;
|
|
255
|
+
store = null;
|
|
256
|
+
}
|
|
257
|
+
return { filter, data, init, setState, storeState, store, applyStore, dispose, clearStore };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// src/index.ts
|
|
261
|
+
export * from "phecda-core";
|
|
262
|
+
|
|
263
|
+
// src/wrapper.ts
|
|
264
|
+
var _P = class {
|
|
265
|
+
constructor() {
|
|
266
|
+
}
|
|
267
|
+
get tag() {
|
|
268
|
+
return this._namespace.__TAG__;
|
|
269
|
+
}
|
|
270
|
+
on(type, handler) {
|
|
271
|
+
_P.emitter.on(type, handler);
|
|
272
|
+
}
|
|
273
|
+
emit(type, event) {
|
|
274
|
+
_P.emitter.emit(type, event);
|
|
275
|
+
}
|
|
276
|
+
off(type, handler) {
|
|
277
|
+
_P.emitter.off(type, handler);
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
var P = _P;
|
|
281
|
+
P.emitter = emitter;
|
|
282
|
+
|
|
283
|
+
// src/components/createForm.ts
|
|
284
|
+
import { defineComponent, h, onMounted, ref as ref2 } from "vue";
|
|
285
|
+
function createForm(compSet, form, formItem, modelKey = "modelValue") {
|
|
286
|
+
function generateChildVNode(props) {
|
|
287
|
+
return props._children?.map(
|
|
288
|
+
(item) => h(compSet[item._component], item)
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
function generateVNode(props) {
|
|
292
|
+
return h(
|
|
293
|
+
compSet[props.config[props.property]._component],
|
|
294
|
+
{
|
|
295
|
+
...props.config[props.property],
|
|
296
|
+
[`${modelKey}`]: props.data[props.property],
|
|
297
|
+
[`onUpdate:${modelKey}`]: (v) => {
|
|
298
|
+
props.data[props.property] = v;
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
{
|
|
302
|
+
default: () => generateChildVNode(props.config[props.property])
|
|
303
|
+
}
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
const FormItem = defineComponent({
|
|
307
|
+
name: "CustomFormItem",
|
|
308
|
+
props: {
|
|
309
|
+
formItem: { type: Object },
|
|
310
|
+
config: {
|
|
311
|
+
type: Object,
|
|
312
|
+
required: true
|
|
313
|
+
},
|
|
314
|
+
data: {
|
|
315
|
+
type: Object,
|
|
316
|
+
required: true
|
|
317
|
+
},
|
|
318
|
+
property: {
|
|
319
|
+
type: String,
|
|
320
|
+
required: true
|
|
321
|
+
}
|
|
322
|
+
},
|
|
323
|
+
setup(props) {
|
|
324
|
+
return () => {
|
|
325
|
+
return formItem ? h(
|
|
326
|
+
formItem,
|
|
327
|
+
{
|
|
328
|
+
...props.formItem
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
default: () => {
|
|
332
|
+
return generateVNode(props);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
) : generateVNode(props);
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
return defineComponent({
|
|
340
|
+
name: "CustomForm",
|
|
341
|
+
props: {
|
|
342
|
+
config: {
|
|
343
|
+
type: Object,
|
|
344
|
+
required: true
|
|
345
|
+
},
|
|
346
|
+
data: {
|
|
347
|
+
type: Object,
|
|
348
|
+
required: true
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
setup(props, ctx) {
|
|
352
|
+
const dom = ref2();
|
|
353
|
+
onMounted(() => {
|
|
354
|
+
ctx.expose({ ...dom.value });
|
|
355
|
+
});
|
|
356
|
+
return () => {
|
|
357
|
+
return h(form, Object.assign({ ref: dom }, ctx.attrs), {
|
|
358
|
+
default: () => Object.keys(props.config).map((item) => {
|
|
359
|
+
return h(FormItem, {
|
|
360
|
+
formItem: props.config[item]._formItem,
|
|
361
|
+
config: props.config,
|
|
362
|
+
property: item,
|
|
363
|
+
data: props.data
|
|
364
|
+
});
|
|
365
|
+
})
|
|
366
|
+
});
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// src/components/formFilter.ts
|
|
373
|
+
function createFormData(schema, initData = {}, options = {}) {
|
|
374
|
+
const { data, filter } = createFilter(initData, options);
|
|
375
|
+
initlize(schema, data.value);
|
|
376
|
+
const filterRet = filter(schema);
|
|
377
|
+
function initlize(obj1, obj2) {
|
|
378
|
+
for (const i in obj1) {
|
|
379
|
+
if (obj1[i]._default)
|
|
380
|
+
obj2[i] = obj1[i]._default;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return { config: filterRet, data };
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// src/components/formResolve.ts
|
|
387
|
+
import { getHandler as getHandler2, getModelState, validate } from "phecda-core";
|
|
388
|
+
function getElementPlusRules(Model, options = {}) {
|
|
389
|
+
const stateVars = getModelState(Model);
|
|
390
|
+
const ret = {};
|
|
391
|
+
for (const item of stateVars) {
|
|
392
|
+
const handlers = getHandler2(Model, item);
|
|
393
|
+
if (handlers) {
|
|
394
|
+
for (const handler of handlers) {
|
|
395
|
+
const { rule, meta, info } = handler;
|
|
396
|
+
if (rule) {
|
|
397
|
+
if (!ret[item])
|
|
398
|
+
ret[item] = [];
|
|
399
|
+
ret[item].push({
|
|
400
|
+
validator: async (_, value, callback) => {
|
|
401
|
+
if (!await validate(rule, value))
|
|
402
|
+
callback(new Error(info || ""));
|
|
403
|
+
else
|
|
404
|
+
callback();
|
|
405
|
+
},
|
|
406
|
+
...options,
|
|
407
|
+
...meta || {}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return ret;
|
|
414
|
+
}
|
|
415
|
+
var GetDevUIRules = getElementPlusRules;
|
|
416
|
+
function getNaiveUIRules(Model, options = {}) {
|
|
417
|
+
const stateVars = getModelState(Model);
|
|
418
|
+
const ret = {};
|
|
419
|
+
for (const item of stateVars) {
|
|
420
|
+
const handlers = getHandler2(Model, item);
|
|
421
|
+
if (handlers) {
|
|
422
|
+
for (const handler of handlers) {
|
|
423
|
+
const { rule, meta, info } = handler;
|
|
424
|
+
if (rule) {
|
|
425
|
+
if (!ret[item])
|
|
426
|
+
ret[item] = [];
|
|
427
|
+
ret[item].push({
|
|
428
|
+
validator: async (_, value) => {
|
|
429
|
+
if (!await validate(rule, value))
|
|
430
|
+
return Promise.reject(info);
|
|
431
|
+
else
|
|
432
|
+
return Promise.resolve();
|
|
433
|
+
},
|
|
434
|
+
...options,
|
|
435
|
+
...meta || {}
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return ret;
|
|
442
|
+
}
|
|
443
|
+
var getAntDRules = getNaiveUIRules;
|
|
444
|
+
function getNutUIRules(Model, options = {}) {
|
|
445
|
+
const stateVars = getModelState(Model);
|
|
446
|
+
const ret = {};
|
|
447
|
+
for (const item of stateVars) {
|
|
448
|
+
const handlers = getHandler2(Model, item);
|
|
449
|
+
if (handlers) {
|
|
450
|
+
for (const handler of handlers) {
|
|
451
|
+
const { rule, meta, info } = handler;
|
|
452
|
+
if (rule) {
|
|
453
|
+
if (!ret[item])
|
|
454
|
+
ret[item] = [];
|
|
455
|
+
ret[item].push({
|
|
456
|
+
validator: async (_, value) => {
|
|
457
|
+
if (!await validate(rule, value))
|
|
458
|
+
return false;
|
|
459
|
+
else
|
|
460
|
+
return true;
|
|
461
|
+
},
|
|
462
|
+
message: info,
|
|
463
|
+
...options,
|
|
464
|
+
...meta || {}
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return ret;
|
|
471
|
+
}
|
|
472
|
+
var getVantRules = getNutUIRules;
|
|
473
|
+
|
|
474
|
+
// src/components/createModal.ts
|
|
475
|
+
import { defineComponent as defineComponent2, h as h2, ref as ref3, render, shallowRef } from "vue";
|
|
476
|
+
var createModal = function(modalWrapper, modelKey = "modelValue") {
|
|
477
|
+
let isMounted = false;
|
|
478
|
+
const isShow = ref3(true);
|
|
479
|
+
const content = shallowRef();
|
|
480
|
+
const propsRef = ref3({});
|
|
481
|
+
const wrapper = defineComponent2({
|
|
482
|
+
setup() {
|
|
483
|
+
return () => h2(modalWrapper, {
|
|
484
|
+
[modelKey]: isShow.value,
|
|
485
|
+
[`onUpdate:${modelKey}`]: (v) => {
|
|
486
|
+
isShow.value = v;
|
|
487
|
+
}
|
|
488
|
+
}, {
|
|
489
|
+
default: () => content.value && h2(content.value, propsRef.value)
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
return (comp, props) => {
|
|
494
|
+
content.value = comp;
|
|
495
|
+
propsRef.value = props;
|
|
496
|
+
if (!isMounted) {
|
|
497
|
+
const el = document.createElement("div");
|
|
498
|
+
const vnode = h2(wrapper);
|
|
499
|
+
document.body.appendChild((render(vnode, el), el));
|
|
500
|
+
isMounted = true;
|
|
501
|
+
} else {
|
|
502
|
+
isShow.value = true;
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
// src/components/createTable.ts
|
|
508
|
+
import { defineComponent as defineComponent3, h as h3, onMounted as onMounted2, ref as ref4 } from "vue";
|
|
509
|
+
function createTable(compSet, table, tableColumn, data) {
|
|
510
|
+
const TableColumn = defineComponent3({
|
|
511
|
+
name: "PhecdaTableColumn",
|
|
512
|
+
props: {
|
|
513
|
+
tableColumn: {
|
|
514
|
+
type: Object,
|
|
515
|
+
required: true
|
|
516
|
+
}
|
|
517
|
+
},
|
|
518
|
+
setup(props) {
|
|
519
|
+
const compName = props.tableColumn._component;
|
|
520
|
+
return () => h3(
|
|
521
|
+
tableColumn,
|
|
522
|
+
{
|
|
523
|
+
...props.tableColumn
|
|
524
|
+
},
|
|
525
|
+
{
|
|
526
|
+
default: (scope) => {
|
|
527
|
+
return compName ? h3(compSet[compName], { scope, data, ...props.tableColumn._props || {} }) : null;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
return defineComponent3({
|
|
534
|
+
name: "PhecdaTable",
|
|
535
|
+
props: {
|
|
536
|
+
config: {
|
|
537
|
+
type: Array,
|
|
538
|
+
required: true
|
|
539
|
+
}
|
|
540
|
+
},
|
|
541
|
+
setup(props, ctx) {
|
|
542
|
+
const dom = ref4();
|
|
543
|
+
onMounted2(() => {
|
|
544
|
+
ctx.expose({ ...dom.value });
|
|
545
|
+
});
|
|
546
|
+
return () => {
|
|
547
|
+
return h3(table, Object.assign({ ref: dom }, ctx.attrs), {
|
|
548
|
+
default: () => props.config.map((item) => {
|
|
549
|
+
return h3(TableColumn, {
|
|
550
|
+
tableColumn: item
|
|
551
|
+
});
|
|
552
|
+
})
|
|
553
|
+
});
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
export {
|
|
559
|
+
EXPRESS_RE,
|
|
560
|
+
FN_RE,
|
|
561
|
+
GetDevUIRules,
|
|
562
|
+
P,
|
|
563
|
+
Watcher,
|
|
564
|
+
clearStorage,
|
|
565
|
+
createFilter,
|
|
566
|
+
createForm,
|
|
567
|
+
createFormData,
|
|
568
|
+
createModal,
|
|
569
|
+
createPhecda,
|
|
570
|
+
createTable,
|
|
571
|
+
deleteStorage,
|
|
572
|
+
emit,
|
|
573
|
+
emitter,
|
|
574
|
+
getActivePhecda,
|
|
575
|
+
getAntDRules,
|
|
576
|
+
getElementPlusRules,
|
|
577
|
+
getNaiveUIRules,
|
|
578
|
+
getNutUIRules,
|
|
579
|
+
getVantRules,
|
|
580
|
+
initalize,
|
|
581
|
+
phecdaSymbol,
|
|
582
|
+
setActivePhecda,
|
|
583
|
+
useO,
|
|
584
|
+
useOn,
|
|
585
|
+
usePatch,
|
|
586
|
+
useR,
|
|
587
|
+
useV
|
|
588
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "phecda-vue",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"author": "",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"mitt": "^3.0.0",
|
|
16
|
+
"vue": "^3.2.45",
|
|
17
|
+
"phecda-core": "1.0.1"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"tsup": "^6.5.0"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsup",
|
|
24
|
+
"dev": "tsup --watch"
|
|
25
|
+
}
|
|
26
|
+
}
|