@harbor-design/proform 1.0.6 → 1.0.7

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/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var k=Object.defineProperty;var W=(u,e,t)=>e in u?k(u,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):u[e]=t;var a=(u,e,t)=>(W(u,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("vue");class R{constructor(e){a(this,"runtimeCore");this.formCustomization=e}cleanFallbackFields(e){return e!==null&&typeof e=="object"&&(delete e.__yiwwhl_async_field_fallback,Object.values(e).forEach(t=>{this.cleanFallbackFields(t)})),e}setup(e){return this.runtimeCore=e,this.formCustomization}submit(){return new Promise((e,t)=>{this.runtimeCore.formRef.value.validate(i=>i?t(i):e(this.cleanFallbackFields(l.toRaw(this.runtimeCore.processors.processedModel.value))))})}}class m{static typeChecker(e){return{}.toString.call(e)}static isArray(e){return this.typeChecker(e)==="[object Array]"}static isFunction(e){return this.typeChecker(e).includes("Function")}static isAsyncFunction(e){let t=!1;return this.typeChecker(e)==="[object AsyncFunction]"&&(t=!0),t}static isUndefined(e){return e===void 0}static isArrayEmpty(e){return(e==null?void 0:e.length)<1}static isObjectEmpty(e){return this.isArrayEmpty(Object.keys(e))}static isListSchema(e){return e.type==="list"}static isGroupSchema(e){return e.type==="group"}static isItemSchema(e){return this.isUndefined(e.type)||e.type==="item"}}function V(u){const e=new WeakMap;function t(i){if(i===null||typeof i!="object")return i;if(i instanceof Date)return new Date(i);if(i instanceof RegExp)return new RegExp(i);if(i instanceof Map){const o=new Map;for(let[s,c]of i)o.set(t(s),t(c));return o}if(i instanceof Set){const o=new Set;for(let s of i)o.add(t(s));return o}if(e.has(i))return e.get(i);if(Array.isArray(i)){const o=[];e.set(i,o);for(let s=0;s<i.length;s++)o[s]=t(i[s]);return o}const f=Object.create(Object.getPrototypeOf(i));e.set(i,f);for(let o in i)i.hasOwnProperty(o)&&(f[o]=t(i[o]));return f}return t(u)}class C{constructor(e,t){a(this,"rawSchemas",[]);a(this,"rawModel",{});a(this,"schemaPreset",A.schemaPreset);a(this,"componentPropsPreset",A.componentPropsPreset);a(this,"uniqueEffectMap",{});a(this,"schemaEffect",new S);a(this,"modelEffect",new S);a(this,"stopWatchEffect",new S);this.processedSchemas=e,this.processedModel=t,l.watch(()=>this.processedModel.value,()=>{this.schemaEffect.triggerEffects(),this.modelEffect.triggerEffects()},{deep:!0})}schemaAnalyzer(e,t=this.processedSchemas.value,i=this.rawSchemas,f,o){for(let s=0;s<e.length;s++){let c=e[s];this.schemaProcessor(c,s,(v,E,h)=>{h?i[s][h]=v[h]:(t[s]=v,this.modelProcessor(v,f&&this.processedModel.value[f][0]),(!i[s]||E)&&(i[s]=V(v)),this.schemaEffect.triggerEffects(),this.modelEffect.triggerEffects())},o,f)}}schemaProcessor(e,t,i,f,o){const s={},c=this;function v(E=!1){var h,P;if(s.componentProps){const d={};c.propsProcessor(s.componentProps,c.componentPropsPreset,d,y=>{s.componentProps=d,i({...s},y,"componentProps")},t,f);return}if(s.children){c.processedSchemas.value[t]=s,c.rawSchemas[t]=s,i({...s},E),c.schemaAnalyzer(s.children,(h=c.processedSchemas.value[t])==null?void 0:h.children,(P=c.rawSchemas[t])==null?void 0:P.children,s.field,t);return}i({...s},E),c.rawModel=V(c.processedModel.value)}this.propsProcessor(e,this.schemaPreset,s,v,t,f,o)}replaceFunctionsWithUndefined(e){if(typeof e!="object"||e===null)return e;for(let t in e)if(e.hasOwnProperty(t)){let i=e[t];typeof i=="function"||typeof i=="object"&&this.replaceFunctionsWithUndefined(i)}return e}runtimeMeta(){return{model:this.replaceFunctionsWithUndefined(l.toRaw(V(this.processedModel.value)))}}propsProcessor(e,t,i,f,o,s,c){const v=Object.keys(e),E=Array.from({length:v.length}).fill(!1);function h(){return E.every(P=>P)}for(let P=0;P<v.length;P++){const d=v[P],y=e[d];if(m.isFunction(y)){const w=y(this.runtimeMeta());d!=="defaultValue"?this.schemaEffect.trackEffect(()=>{let n=y(this.runtimeMeta());n instanceof Promise?n.then(r=>{s===void 0?m.isFunction(r)||(typeof r=="string"&&r.includes("undefined")&&(r=r.replace(/undefined/g,"")),this.processedSchemas.value[o][d]=r):m.isFunction(r)||(typeof r=="string"&&r.includes("undefined")&&(r=r.replace(/undefined/g,"")),this.processedSchemas.value[s].children[o][d]=r)}):s===void 0?m.isFunction(n)||(typeof n=="string"&&n.includes("undefined")&&(n=n.replace(/undefined/g,"")),this.processedSchemas.value[o][d]=n):m.isFunction(n)||(typeof n=="string"&&n.includes("undefined")&&(n=n.replace(/undefined/g,"")),this.processedSchemas.value[s].children[o][d]=n)}):this.modelEffect.trackEffect(()=>{let n=y(this.runtimeMeta());this.stopWatchEffect.trackEffect(l.watchEffect(()=>{if(n=y(this.runtimeMeta()),n instanceof Promise)n.then(r=>{if(typeof r=="string"&&!r.includes("undefined")?this.stopWatchEffect.triggerEffects():r=r.replace(/undefined/g,""),o===void 0||c===void 0){if(m.isFunction(e.field)){const p=e.field(this.runtimeMeta());p instanceof Promise?p.then(g=>{this.processedModel.value[g]=r}):this.processedModel.value[p]=r;return}this.processedModel.value[e.field]=r}else{if(m.isFunction(e.field)){const p=e.field(this.runtimeMeta());p instanceof Promise?p.then(g=>{this.processedModel.value[c][o][g]=r}):this.processedModel.value[c][o]&&(this.processedModel.value[c][o][p]=r);return}this.processedModel.value[c]&&this.processedModel.value[c].forEach(p=>{p[e.field]=r})}this.rawModel=V(this.processedModel.value),this.modelEffect.clearEffects()});else{if(typeof n=="string"&&!n.includes("undefined")?this.stopWatchEffect.triggerEffects():n=n.replace(/undefined/g,""),m.isFunction(e.field)){const r=e.field(this.runtimeMeta());r instanceof Promise?r.then(p=>{this.processedModel.value[p]=n}):this.processedModel.value[r]=n;return}if(this.processedModel.value[e.field]=n,o===void 0||c===void 0){if(m.isFunction(e.field)){const r=e.field(this.runtimeMeta());r instanceof Promise?r.then(p=>{this.processedModel.value[p]=n}):this.processedModel.value[r]=n;return}this.processedModel.value[e.field]=n}else for(let r=0;r<this.processedModel.value[c].length;r++){const p=this.processedModel.value[c][r];if(!m.isFunction(e.field))p[e.field]=n;else{const g=e.field(this.runtimeMeta());g instanceof Promise?g.then(N=>{p[N]=n}):p[g]=n;return}}this.rawModel=V(this.processedModel.value),this.modelEffect.clearEffects()}}))}),w instanceof Promise?(E[P]=!0,i[d]=t[d].defaultValueWhenAsync,h()&&f(),w.then(n=>{E[P]=!0,i[d]=n,h()&&f(!0)})):(E[P]=!0,i[d]=w,h()&&f())}else E[P]=!0,l.isRef(y)?l.watch(()=>y.value,w=>{i[d]=w,h()&&f()},{immediate:!0,deep:!0}):l.isReactive(y)?l.watch(()=>y,w=>{i[d]=w,h()&&f()},{immediate:!0,deep:!0}):(i[d]=y,h()&&f())}}modelProcessor(e,t=this.processedModel.value){if(m.isListSchema(e)){if(m.isFunction(e.field))return;t[e.field]||(t[e.field]=[{}]),e.children.forEach(i=>{this.modelProcessor(i,t[e.field][0])});return}if(m.isGroupSchema(e)){e.children.forEach(i=>{this.modelProcessor(i,t)});return}if(m.isItemSchema(e)){if(m.isFunction(e.field)||m.isUndefined(e.field)||!Number.isNaN(Number(e.field))||m.isFunction(e.defaultValue))return;t[e.field]=e.defaultValue}}}function b(u){return typeof u=="function"||Object.prototype.toString.call(u)==="[object Object]"&&!l.isVNode(u)}class D{constructor(e){a(this,"schemas",l.ref([]));a(this,"model",l.ref({}));a(this,"processorBySchemaType",{item:this.runtimeItemProcessor.bind(this),group:this.runtimeGroupProcessor.bind(this),list:this.runtimeListProcessor.bind(this)});a(this,"formRef",l.ref(null));this.setup=e,this.processors=new C(this.schemas,this.model),this.analyze(this.setup(this))}analyze(e){this.processors.schemaAnalyzer(e.schemas)}runtimeItemProcessor(e,t,i=this.model.value,f){var y;const o=f?`${f.field}.${t}.${e.field}`:e.field,s=l.toRaw(e.component),c=s.name,v=e.componentProps??{},E=A.placeholderPresetByComponentName;let h=e.placeholder;h||(h=`${E[c]??"请输入"}${e.label}`),e.required&&(e.rules||(e.rules=[]),(y=e.rules)==null||y.push({required:!0,message:`${e.label}是必填项`}));let d=e.show;return d===void 0&&(d=!0),d||delete i[e.field],l.createVNode(M.runtimeDoms.Item,null,{default(){return l.withDirectives(l.createVNode(M.runtimeDoms.FormItem,{label:`${e.label}:`,rules:e.rules,field:o},{default:()=>[l.createVNode(s,l.mergeProps({modelValue:i[e.field],"onUpdate:modelValue":w=>i[e.field]=w,placeholder:h},v),null)]}),[[l.vShow,d]])}})}runtimeGroupProcessor(e){let t;return l.createVNode(M.runtimeDoms.Group,{schema:e},b(t=e.children.map(i=>this.runtimeItemProcessor(i)))?t:{default:()=>[t]})}addListItem(e){var t,i;if(!((t=this.processors.rawModel[e.field])!=null&&t[0]))return Promise.reject({code:"0001",message:"异步默认值数据正在处理中,请您耐心等待... "});(i=this.processors.rawModel[e.field])!=null&&i[0]&&this.model.value[e.field].push(V(this.processors.rawModel[e.field][0]))}deleteListItem(e,t){this.model.value[e.field].splice(t,1)}runtimeListProcessor(e){const t=this;return l.createVNode(M.runtimeDoms.List,{schema:e},{default(){return t.model.value[e.field].map((i,f)=>l.createVNode(M.runtimeDoms.ListItem,null,{default(){return e.children.map((o,s)=>t.runtimeItemProcessor(o,s,i,e))},delete({container:o}={}){var c;let s=o??l.createVNode("button",null,null);return l.withDirectives(l.createVNode(s,{onClick:()=>t.deleteListItem(e,f)},null),[[l.vShow,((c=t.model.value[e.field])==null?void 0:c.length)>1]])}}))},add({container:i}={}){let f=i??l.createVNode("button",null,[l.createTextVNode("添加")]);return l.createVNode(f,{onClick:()=>t.addListItem(e)},null)}})}runtimeProcessor(e){return e.map(t=>(t.type||(t.type="item"),this.processorBySchemaType[t.type](t)))}exec(){let e;return l.createVNode(M.runtimeDoms.Form,{ref:this.formRef,model:this.model.value},b(e=this.runtimeProcessor(this.schemas.value))?e:{default:()=>[e]})}}class M{}a(M,"runtimeDoms");class S{constructor(){a(this,"effects",new Set)}clearEffects(){this.effects.clear()}triggerEffects(){Array.from(this.effects).forEach(e=>e())}trackEffect(e){this.effects.add(e)}}const F=class F{static getPlaceholderPrefixPresetByComponentName(){const e={请选择:["Select","Tree","TreeSelect"],请输入:["Input"]},t={};for(let i in e)e[i].forEach(f=>{t[f]=i});return t}};a(F,"schemaPreset",{type:{defaultValueWhenAsync:"item"},component:{defaultValueWhenAsync:void 0},componentProps:{defaultValueWhenAsync:void 0},defaultValue:{defaultValueWhenAsync:void 0},label:{defaultValueWhenAsync:""},field:{defaultValueWhenAsync:"__yiwwhl_async_field_fallback"},rules:{defaultValueWhenAsync:[]},show:{defaultValueWhenAsync:!0},required:{defaultValueWhenAsync:!1},placeholder:{defaultValueWhenAsync:void 0}}),a(F,"componentPropsPreset",{options:{defaultValueWhenAsync:[]}}),a(F,"placeholderPresetByComponentName",F.getPlaceholderPrefixPresetByComponentName());let A=F;const j=l.defineComponent({props:{setup:{type:Function,required:!0}},setup(u){const e=new D(u.setup);return()=>e.exec()}});function L(u){const e=new R(u);return[e.setup.bind(e),{submit:e.submit.bind(e)}]}function z(u){return{install(){M.runtimeDoms=u}}}exports.ProForm=j;exports.useForm=L;exports.useFormRenderer=z;
1
+ "use strict";var I=Object.defineProperty;var v=(l,e,t)=>e in l?I(l,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):l[e]=t;var f=(l,e,t)=>(v(l,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("vue");class O{constructor(e){f(this,"runtimeCore");this.formCustomization=e}cleanFallbackFields(e){return e!==null&&typeof e=="object"&&(delete e.__yiwwhl_async_field_fallback,Object.values(e).forEach(t=>{this.cleanFallbackFields(t)})),e}setup(e){return this.runtimeCore=e,this.formCustomization}submit(){return new Promise((e,t)=>{this.runtimeCore.formRef.value.validate(s=>s?t(s):e(this.cleanFallbackFields(i.toRaw(this.runtimeCore.processor.processedModel.value))))})}}class u{static typeChecker(e){return{}.toString.call(e)}static isArray(e){return this.typeChecker(e)==="[object Array]"}static isFunction(e){return this.typeChecker(e)==="[object Function]"}static isPromise(e){return e instanceof Promise}static isObject(e){return this.typeChecker(e)==="[object Object]"}static isAsyncFunction(e){return this.typeChecker(e)==="[object AsyncFunction]"}static isUndefined(e){return e===void 0}static isArrayEmpty(e){return(e==null?void 0:e.length)<1}static isObjectEmpty(e){return this.isArrayEmpty(Object.keys(e))}static isListSchema(e){return e.type==="list"}static isGroupSchema(e){return e.type==="group"}static isItemSchema(e){return this.isUndefined(e.type)||e.type==="item"}static isProcessInprogress(e){if(e===void 0)return!0;if(this.isObject(e)){if(e.setup&&this.isFunction(e.setup)&&e.props)return!1;if(this.isObjectEmpty(e))return!0;for(const t in e)if(e.hasOwnProperty(t)&&this.isProcessInprogress(e[t]))return!0}else if(this.isArray(e)){if(this.isArrayEmpty(e))return!0;for(const t of e)if(this.isProcessInprogress(t))return!0}return!1}}function j(l){const e=new WeakMap;function t(s){if(s===null||typeof s!="object")return s;if(s instanceof Date)return new Date(s);if(s instanceof RegExp)return new RegExp(s);if(s instanceof Map){const n=new Map;for(let[o,c]of s)n.set(t(o),t(c));return n}if(s instanceof Set){const n=new Set;for(let o of s)n.add(t(o));return n}if(e.has(s))return e.get(s);if(Array.isArray(s)){const n=[];e.set(s,n);for(let o=0;o<s.length;o++)n[o]=t(s[o]);return n}const r=Object.create(Object.getPrototypeOf(s));e.set(s,r);for(let n in s)s.hasOwnProperty(n)&&(r[n]=t(s[n]));return r}return t(l)}class E{constructor(){f(this,"effects",new Set)}clearEffects(){this.effects.clear()}triggerEffects(){Array.from(this.effects).forEach(e=>e())}trackEffect(e){return e(),this.effects.add(e),()=>this.effects.delete(e)}}class C{constructor(e){f(this,"processedSchemas");f(this,"processedModel");f(this,"getRuntimeMeta");f(this,"stableSchemas",[]);f(this,"stableModel",{});f(this,"schemaPreset",S.schemaPreset);f(this,"componentPropsPreset",S.componentPropsPreset);f(this,"stableUpdaterProcessProgress");f(this,"stableUpdaterTimes",0);f(this,"schemaEffect",new E);f(this,"defaultValueEffect",new E);f(this,"defaultValueInprogressMap",new Map);this.processedSchemas=e.schemas,this.processedModel=e.model,this.getRuntimeMeta=e.getRuntimeMeta.bind(e),i.watch(()=>this.processedModel.value,()=>{this.schemaEffect.triggerEffects()},{deep:!0})}parse(e,t){e.forEach((s,r)=>{this.parseItem(s,r,t)})}initSchemas(e){return e.map(t=>{const s={};return t.children&&(s.children=this.initSchemas(t.children)),s})}parseSchemas(e,t){u.isArrayEmpty(this.processedSchemas.value)&&(this.processedSchemas.value=this.initSchemas(e)),this.parse(e,t)}parseStable(e){const t={};if(!u.isUndefined(e.stable))t[e.key]=this.parseStable(e.stable);else return e;return t}stableUpdater(e=[]){if(e.every(Boolean)){const t=i.toRaw(this.processedSchemas.value);!u.isProcessInprogress(t)&&u.isObjectEmpty(this.stableModel)&&(this.stableUpdaterProcessProgress||(this.stableUpdaterProcessProgress=Array.from({length:t.length}).fill(!1)),this.stableUpdaterProcessProgress[this.stableUpdaterTimes]=!0,this.stableUpdaterTimes++,this.modelProcessor(t))}}parseItem(e,t,s){const r=this,n=Array.from({length:Object.keys(e).filter(c=>c!=="children").length}).fill(!1);this.objectParser({data:e,index:t,updater:o});function o(c){const a=c.index,d=c.key,y=c.keyIndex;if(!c.stable)return;const V=r.parseStable(c.stable),m=s==null?void 0:s.index,g=s==null?void 0:s.key;let p=V;if(u.isProcessInprogress(p)||(n[y]=!0),s){let b=r.processedSchemas.value[m][g][a][d];b&&u.isObject(b)&&d!=="component"&&(p=Object.assign(b,p)),r.processedSchemas.value[m][g][a][d]=p,r.stableUpdater(n)}else{let b=r.processedSchemas.value[a][d];b&&u.isObject(b)&&(p=Object.assign(b,p)),r.processedSchemas.value[a][d]=p,r.stableUpdater(n)}}}objectParser(e){const t=e.data;Object.keys(t).forEach((r,n)=>{if(r==="children")this.parseSchemas(t[r],{...e,key:r,keyIndex:n});else{const o=c=>{e.updater({...e,key:r,keyIndex:n,stable:c})};if(u.isFunction(t[r]))r!=="defaultValue"?this.schemaEffect.trackEffect(()=>{if(r==="component"){const c=t[r](this.getRuntimeMeta());this.promiseFieldParser(c,o,!1)}else this.fieldParser(t[r],o)}):this.defaultValueEffect.trackEffect(()=>{const c=this.schemaEffect.trackEffect(()=>{/\{\s*model\s*\}/.test(t[r].toString())?this.fieldParser(t[r],a=>{if(!a)return o(a);this.defaultValueInprogressMap.set(t[r],a),!u.isProcessInprogress(a)&&Array.from(this.defaultValueInprogressMap.values()).every(d=>!d.includes("undefined"))?(o(a),this.defaultValueEffect.clearEffects(),c()):o(a)}):this.fieldParser(t[r],a=>{!u.isProcessInprogress(a)&&Array.from(this.defaultValueInprogressMap.values()).every(d=>!d.includes("undefined"))?(o(a),this.defaultValueEffect.clearEffects(),c()):o(a)})})});else if(r==="component"){const c=t[r];this.promiseFieldParser(c,o,!1)}else this.fieldParser(t[r],o)}})}promiseFieldParser(e,t,s){u.isPromise(e)?e.then(r=>{s&&u.isObject(r)?this.objectParser({data:r,updater:t}):t(r)}):s&&u.isObject(e)?this.objectParser({data:e,updater:t}):t(e)}fieldParser(e,t,s=!0){if(u.isFunction(e)){const r=e(this.getRuntimeMeta());this.promiseFieldParser(r,t,s)}else i.isRef(e)?i.watch(()=>e.value,()=>{u.isUndefined(e.value)||(s&&u.isObject(e.value)?this.objectParser({data:e.value,updater:t}):t(e.value))},{immediate:!0,deep:!0}):i.isReactive(e)?i.watch(()=>e,()=>{u.isUndefined(e)||(s&&u.isObject(e)?this.objectParser({data:e,updater:t}):t(e))},{immediate:!0,deep:!0}):s&&u.isObject(e)?this.objectParser({data:e,updater:t}):t(e)}modelProcessor(e){e.map(t=>this.createModel(t,this.processedModel.value)),u.isObjectEmpty(this.stableModel)&&this.stableUpdaterProcessProgress.every(Boolean)&&this.defaultValueEffect.effects.size===0&&(this.stableModel=j(this.processedModel.value))}createModel(e,t){u.isListSchema(e)&&(t[e.field]||(t[e.field]=[{}]),e.children.forEach(s=>{this.createModel(s,t[e.field][0])})),u.isGroupSchema(e)&&e.children.forEach(s=>{this.createModel(s,t)}),u.isItemSchema(e)&&(t[e.field]=e.defaultValue)}}function w(l){return typeof l=="function"||Object.prototype.toString.call(l)==="[object Object]"&&!i.isVNode(l)}class R{constructor(e){f(this,"schemas",i.ref([]));f(this,"model",i.ref({}));f(this,"processorBySchemaType",{item:this.runtimeItemProcessor.bind(this),group:this.runtimeGroupProcessor.bind(this),list:this.runtimeListProcessor.bind(this)});f(this,"formRef",i.ref(null));this.setup=e,this.processor=new C(this);const t=this.setup(this);this.processor.parseSchemas(t.schemas)}getRuntimeMeta(){return{model:i.toRaw(j(this.model.value))}}runtimeItemProcessor(e,t,s=this.model.value,r){var g;const n=r?`${r.field}.${t}.${e.field}`:e.field,o=i.toRaw(e.component);if(!o)return;const c=o.name,a=e.componentProps??{},d=S.placeholderPresetByComponentName;let y=e.placeholder;y||(y=`${d[c]??"请输入"}${e.label}`),e.required&&(e.rules||(e.rules=[]),(g=e.rules)==null||g.push({required:!0,message:`${e.label}是必填项`}));let m=e.show;return m===void 0&&(m=!0),m||delete s[e.field],i.createVNode(h.runtimeDoms.Item,null,{default(){return i.withDirectives(i.createVNode(h.runtimeDoms.FormItem,{label:`${e.label}:`,rules:e.rules,field:n},{default:()=>[i.createVNode(o,i.mergeProps({modelValue:s[e.field],"onUpdate:modelValue":p=>s[e.field]=p,placeholder:y},a),null)]}),[[i.vShow,m]])}})}runtimeGroupProcessor(e){let t;return i.createVNode(h.runtimeDoms.Group,{schema:e},w(t=e.children.map(s=>this.runtimeItemProcessor(s)))?t:{default:()=>[t]})}addListItem(e){var t,s;if(!((t=this.processor.stableModel[e.field])!=null&&t[0]))return Promise.reject({code:"0001",message:"异步默认值数据正在处理中,请您耐心等待... "});(s=this.processor.stableModel[e.field])!=null&&s[0]&&this.model.value[e.field].push(j(this.processor.stableModel[e.field][0]))}deleteListItem(e,t){this.model.value[e.field].splice(t,1)}runtimeListProcessor(e){const t=this;return t.model.value[e.field]||(t.model.value[e.field]=[{}]),i.createVNode(h.runtimeDoms.List,{schema:e},{default(){return t.model.value[e.field].map((s,r)=>i.createVNode(h.runtimeDoms.ListItem,null,{default(){return e.children.map((n,o)=>t.runtimeItemProcessor(n,o,s,e))},delete({container:n}={}){var c;let o=n??i.createVNode("button",null,null);return i.withDirectives(i.createVNode(o,{onClick:()=>t.deleteListItem(e,r)},null),[[i.vShow,((c=t.model.value[e.field])==null?void 0:c.length)>1]])}}))},add({container:s}={}){let r=s??i.createVNode("button",null,[i.createTextVNode("添加")]);return i.createVNode(r,{onClick:()=>t.addListItem(e)},null)}})}runtimeProcessor(e){return e.map(t=>(t.type||(t.type="item"),this.processorBySchemaType[t.type](t)))}exec(){let e;return i.createVNode(h.runtimeDoms.Form,{ref:this.formRef,model:this.model.value},w(e=this.runtimeProcessor(this.schemas.value))?e:{default:()=>[e]})}}class h{}f(h,"runtimeDoms");const P=class P{static getPlaceholderPrefixPresetByComponentName(){const e={请选择:["Select","Tree","TreeSelect"],请输入:["Input"]},t={};for(let s in e)e[s].forEach(r=>{t[r]=s});return t}};f(P,"schemaPreset",{type:{defaultValue:"item"},component:{defaultValue:void 0},componentProps:{defaultValue:void 0},defaultValue:{defaultValue:void 0},label:{defaultValue:""},field:{defaultValue:"__yiwwhl_async_field_fallback"},rules:{defaultValue:[]},show:{defaultValue:!0},required:{defaultValue:!1},placeholder:{defaultValue:void 0},children:{defaultValue:[]}}),f(P,"componentPropsPreset",{options:{defaultValue:[]}}),f(P,"placeholderPresetByComponentName",P.getPlaceholderPrefixPresetByComponentName());let S=P;const N=i.defineComponent({props:{setup:{type:Function,required:!0}},setup(l){const e=new R(l.setup);return()=>e.exec()}});function x(l){const e=new O(l);return[e.setup.bind(e),{submit:e.submit.bind(e)}]}function A(l){return{install(){h.runtimeDoms=l}}}exports.ProForm=N;exports.useForm=x;exports.useFormRenderer=A;
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- var j = Object.defineProperty;
2
- var L = (c, e, t) => e in c ? j(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t;
3
- var d = (c, e, t) => (L(c, typeof e != "symbol" ? e + "" : e, t), t);
4
- import { toRaw as W, watch as S, watchEffect as z, isRef as _, isReactive as B, ref as k, createVNode as v, withDirectives as C, mergeProps as q, vShow as R, createTextVNode as $, isVNode as G, defineComponent as T } from "vue";
5
- class U {
1
+ var A = Object.defineProperty;
2
+ var U = (o, e, t) => e in o ? A(o, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : o[e] = t;
3
+ var c = (o, e, t) => (U(o, typeof e != "symbol" ? e + "" : e, t), t);
4
+ import { toRaw as S, watch as E, isRef as R, isReactive as M, ref as I, createVNode as d, withDirectives as O, mergeProps as k, vShow as v, createTextVNode as D, isVNode as B, defineComponent as L } from "vue";
5
+ class N {
6
6
  constructor(e) {
7
- d(this, "runtimeCore");
7
+ c(this, "runtimeCore");
8
8
  this.formCustomization = e;
9
9
  }
10
10
  // happy path, 后续可以完善更多的 fallback 处理,fallback 处理是为了不卡住异步时的首次渲染做的优化
@@ -18,15 +18,15 @@ class U {
18
18
  }
19
19
  submit() {
20
20
  return new Promise((e, t) => {
21
- this.runtimeCore.formRef.value.validate((i) => i ? t(i) : e(
21
+ this.runtimeCore.formRef.value.validate((s) => s ? t(s) : e(
22
22
  this.cleanFallbackFields(
23
- W(this.runtimeCore.processors.processedModel.value)
23
+ S(this.runtimeCore.processor.processedModel.value)
24
24
  )
25
25
  ));
26
26
  });
27
27
  }
28
28
  }
29
- class p {
29
+ class f {
30
30
  static typeChecker(e) {
31
31
  return {}.toString.call(e);
32
32
  }
@@ -34,11 +34,16 @@ class p {
34
34
  return this.typeChecker(e) === "[object Array]";
35
35
  }
36
36
  static isFunction(e) {
37
- return this.typeChecker(e).includes("Function");
37
+ return this.typeChecker(e) === "[object Function]";
38
+ }
39
+ static isPromise(e) {
40
+ return e instanceof Promise;
41
+ }
42
+ static isObject(e) {
43
+ return this.typeChecker(e) === "[object Object]";
38
44
  }
39
45
  static isAsyncFunction(e) {
40
- let t = !1;
41
- return this.typeChecker(e) === "[object AsyncFunction]" && (t = !0), t;
46
+ return this.typeChecker(e) === "[object AsyncFunction]";
42
47
  }
43
48
  static isUndefined(e) {
44
49
  return e === void 0;
@@ -58,366 +63,363 @@ class p {
58
63
  static isItemSchema(e) {
59
64
  return this.isUndefined(e.type) || e.type === "item";
60
65
  }
66
+ static isProcessInprogress(e) {
67
+ if (e === void 0)
68
+ return !0;
69
+ if (this.isObject(e)) {
70
+ if (e.setup && this.isFunction(e.setup) && e.props)
71
+ return !1;
72
+ if (this.isObjectEmpty(e))
73
+ return !0;
74
+ for (const t in e)
75
+ if (e.hasOwnProperty(t) && this.isProcessInprogress(e[t]))
76
+ return !0;
77
+ } else if (this.isArray(e)) {
78
+ if (this.isArrayEmpty(e))
79
+ return !0;
80
+ for (const t of e)
81
+ if (this.isProcessInprogress(t))
82
+ return !0;
83
+ }
84
+ return !1;
85
+ }
61
86
  }
62
- function A(c) {
87
+ function V(o) {
63
88
  const e = /* @__PURE__ */ new WeakMap();
64
- function t(i) {
65
- if (i === null || typeof i != "object")
66
- return i;
67
- if (i instanceof Date)
68
- return new Date(i);
69
- if (i instanceof RegExp)
70
- return new RegExp(i);
71
- if (i instanceof Map) {
72
- const o = /* @__PURE__ */ new Map();
73
- for (let [s, l] of i)
74
- o.set(t(s), t(l));
75
- return o;
89
+ function t(s) {
90
+ if (s === null || typeof s != "object")
91
+ return s;
92
+ if (s instanceof Date)
93
+ return new Date(s);
94
+ if (s instanceof RegExp)
95
+ return new RegExp(s);
96
+ if (s instanceof Map) {
97
+ const n = /* @__PURE__ */ new Map();
98
+ for (let [i, l] of s)
99
+ n.set(t(i), t(l));
100
+ return n;
76
101
  }
77
- if (i instanceof Set) {
78
- const o = /* @__PURE__ */ new Set();
79
- for (let s of i)
80
- o.add(t(s));
81
- return o;
102
+ if (s instanceof Set) {
103
+ const n = /* @__PURE__ */ new Set();
104
+ for (let i of s)
105
+ n.add(t(i));
106
+ return n;
82
107
  }
83
- if (e.has(i))
84
- return e.get(i);
85
- if (Array.isArray(i)) {
86
- const o = [];
87
- e.set(i, o);
88
- for (let s = 0; s < i.length; s++)
89
- o[s] = t(i[s]);
90
- return o;
108
+ if (e.has(s))
109
+ return e.get(s);
110
+ if (Array.isArray(s)) {
111
+ const n = [];
112
+ e.set(s, n);
113
+ for (let i = 0; i < s.length; i++)
114
+ n[i] = t(s[i]);
115
+ return n;
91
116
  }
92
- const f = Object.create(Object.getPrototypeOf(i));
93
- e.set(i, f);
94
- for (let o in i)
95
- i.hasOwnProperty(o) && (f[o] = t(i[o]));
96
- return f;
117
+ const r = Object.create(Object.getPrototypeOf(s));
118
+ e.set(s, r);
119
+ for (let n in s)
120
+ s.hasOwnProperty(n) && (r[n] = t(s[n]));
121
+ return r;
122
+ }
123
+ return t(o);
124
+ }
125
+ class C {
126
+ constructor() {
127
+ c(this, "effects", /* @__PURE__ */ new Set());
128
+ }
129
+ clearEffects() {
130
+ this.effects.clear();
131
+ }
132
+ triggerEffects() {
133
+ Array.from(this.effects).forEach((e) => e());
134
+ }
135
+ trackEffect(e) {
136
+ return e(), this.effects.add(e), () => this.effects.delete(e);
97
137
  }
98
- return t(c);
99
138
  }
100
- class O {
101
- constructor(e, t) {
102
- d(this, "rawSchemas", []);
103
- d(this, "rawModel", {});
104
- d(this, "schemaPreset", b.schemaPreset);
105
- d(this, "componentPropsPreset", b.componentPropsPreset);
106
- d(this, "uniqueEffectMap", {});
107
- d(this, "schemaEffect", new V());
108
- d(this, "modelEffect", new V());
109
- d(this, "stopWatchEffect", new V());
110
- this.processedSchemas = e, this.processedModel = t, S(
139
+ class T {
140
+ constructor(e) {
141
+ c(this, "processedSchemas");
142
+ c(this, "processedModel");
143
+ c(this, "getRuntimeMeta");
144
+ c(this, "stableSchemas", []);
145
+ c(this, "stableModel", {});
146
+ c(this, "schemaPreset", j.schemaPreset);
147
+ c(this, "componentPropsPreset", j.componentPropsPreset);
148
+ c(this, "stableUpdaterProcessProgress");
149
+ c(this, "stableUpdaterTimes", 0);
150
+ c(this, "schemaEffect", new C());
151
+ c(this, "defaultValueEffect", new C());
152
+ c(this, "defaultValueInprogressMap", /* @__PURE__ */ new Map());
153
+ this.processedSchemas = e.schemas, this.processedModel = e.model, this.getRuntimeMeta = e.getRuntimeMeta.bind(e), E(
111
154
  () => this.processedModel.value,
112
155
  () => {
113
- this.schemaEffect.triggerEffects(), this.modelEffect.triggerEffects();
156
+ this.schemaEffect.triggerEffects();
114
157
  },
115
158
  {
116
159
  deep: !0
117
160
  }
118
161
  );
119
162
  }
120
- schemaAnalyzer(e, t = this.processedSchemas.value, i = this.rawSchemas, f, o) {
121
- for (let s = 0; s < e.length; s++) {
122
- let l = e[s];
123
- this.schemaProcessor(
124
- l,
125
- s,
126
- (P, E, a) => {
127
- a ? i[s][a] = P[a] : (t[s] = P, this.modelProcessor(
128
- P,
129
- f && this.processedModel.value[f][0]
130
- ), (!i[s] || E) && (i[s] = A(P)), this.schemaEffect.triggerEffects(), this.modelEffect.triggerEffects());
131
- },
132
- o,
133
- f
134
- );
135
- }
163
+ // 核心处理过程,接收一个初始的代理数据结构,由其衍生的有 parseSchema
164
+ parse(e, t) {
165
+ e.forEach((s, r) => {
166
+ this.parseItem(s, r, t);
167
+ });
136
168
  }
137
- schemaProcessor(e, t, i, f, o) {
138
- const s = {}, l = this;
139
- function P(E = !1) {
140
- var a, y;
141
- if (s.componentProps) {
142
- const u = {};
143
- l.propsProcessor(
144
- s.componentProps,
145
- l.componentPropsPreset,
146
- u,
147
- (m) => {
148
- s.componentProps = u, i({ ...s }, m, "componentProps");
149
- },
150
- t,
151
- f
152
- );
153
- return;
154
- }
155
- if (s.children) {
156
- l.processedSchemas.value[t] = s, l.rawSchemas[t] = s, i({ ...s }, E), l.schemaAnalyzer(
157
- s.children,
158
- // @ts-expect-error 此处已经守卫为非 ItemSchema
159
- (a = l.processedSchemas.value[t]) == null ? void 0 : a.children,
160
- // @ts-expect-error 此处已经守卫为非 ItemSchema
161
- (y = l.rawSchemas[t]) == null ? void 0 : y.children,
162
- s.field,
163
- t
164
- );
165
- return;
166
- }
167
- i({ ...s }, E), l.rawModel = A(l.processedModel.value);
168
- }
169
- this.propsProcessor(
170
- e,
171
- this.schemaPreset,
172
- s,
173
- P,
174
- t,
175
- f,
176
- o
177
- );
169
+ // 初始化空数据结构,避免后续复杂的 if else
170
+ initSchemas(e) {
171
+ return e.map((t) => {
172
+ const s = {};
173
+ return t.children && (s.children = this.initSchemas(t.children)), s;
174
+ });
178
175
  }
179
- replaceFunctionsWithUndefined(e) {
180
- if (typeof e != "object" || e === null)
181
- return e;
182
- for (let t in e)
183
- if (e.hasOwnProperty(t)) {
184
- let i = e[t];
185
- typeof i == "function" || typeof i == "object" && this.replaceFunctionsWithUndefined(i);
186
- }
187
- return e;
176
+ // 派生过程,用于外部应用
177
+ parseSchemas(e, t) {
178
+ f.isArrayEmpty(this.processedSchemas.value) && (this.processedSchemas.value = this.initSchemas(e)), this.parse(e, t);
188
179
  }
189
- runtimeMeta() {
190
- return {
191
- model: this.replaceFunctionsWithUndefined(
192
- W(A(this.processedModel.value))
193
- )
194
- };
180
+ parseStable(e) {
181
+ const t = {};
182
+ if (!f.isUndefined(e.stable))
183
+ t[e.key] = this.parseStable(e.stable);
184
+ else
185
+ return e;
186
+ return t;
195
187
  }
196
- propsProcessor(e, t, i, f, o, s, l) {
197
- const P = Object.keys(e), E = Array.from({
198
- length: P.length
199
- }).fill(!1);
200
- function a() {
201
- return E.every((y) => y);
202
- }
203
- for (let y = 0; y < P.length; y++) {
204
- const u = P[y], m = e[u];
205
- if (p.isFunction(m)) {
206
- const M = m(this.runtimeMeta());
207
- u !== "defaultValue" ? this.schemaEffect.trackEffect(() => {
208
- let n = m(this.runtimeMeta());
209
- n instanceof Promise ? n.then((r) => {
210
- s === void 0 ? p.isFunction(r) || (typeof r == "string" && r.includes("undefined") && (r = r.replace(/undefined/g, "")), this.processedSchemas.value[o][u] = r) : p.isFunction(r) || (typeof r == "string" && r.includes("undefined") && (r = r.replace(/undefined/g, "")), this.processedSchemas.value[s].children[o][u] = r);
211
- }) : s === void 0 ? p.isFunction(n) || (typeof n == "string" && n.includes("undefined") && (n = n.replace(/undefined/g, "")), this.processedSchemas.value[o][u] = n) : p.isFunction(n) || (typeof n == "string" && n.includes("undefined") && (n = n.replace(/undefined/g, "")), this.processedSchemas.value[s].children[o][u] = n);
212
- }) : this.modelEffect.trackEffect(() => {
213
- let n = m(this.runtimeMeta());
214
- this.stopWatchEffect.trackEffect(
215
- z(() => {
216
- if (n = m(this.runtimeMeta()), n instanceof Promise)
217
- n.then((r) => {
218
- if (typeof r == "string" && !r.includes("undefined") ? this.stopWatchEffect.triggerEffects() : r = r.replace(/undefined/g, ""), o === void 0 || l === void 0) {
219
- if (p.isFunction(e.field)) {
220
- const h = e.field(
221
- this.runtimeMeta()
222
- );
223
- h instanceof Promise ? h.then((g) => {
224
- this.processedModel.value[g] = r;
225
- }) : this.processedModel.value[h] = r;
226
- return;
227
- }
228
- this.processedModel.value[e.field] = r;
229
- } else {
230
- if (p.isFunction(e.field)) {
231
- const h = e.field(
232
- this.runtimeMeta()
233
- );
234
- h instanceof Promise ? h.then((g) => {
235
- this.processedModel.value[l][o][g] = r;
236
- }) : this.processedModel.value[l][o] && (this.processedModel.value[l][o][h] = r);
237
- return;
238
- }
239
- this.processedModel.value[l] && this.processedModel.value[l].forEach(
240
- // @ts-expect-error
241
- (h) => {
242
- h[e.field] = r;
243
- }
244
- );
245
- }
246
- this.rawModel = A(this.processedModel.value), this.modelEffect.clearEffects();
247
- });
248
- else {
249
- if (typeof n == "string" && !n.includes("undefined") ? this.stopWatchEffect.triggerEffects() : n = n.replace(/undefined/g, ""), p.isFunction(e.field)) {
250
- const r = e.field(this.runtimeMeta());
251
- r instanceof Promise ? r.then((h) => {
252
- this.processedModel.value[h] = n;
253
- }) : this.processedModel.value[r] = n;
254
- return;
255
- }
256
- if (this.processedModel.value[e.field] = n, o === void 0 || l === void 0) {
257
- if (p.isFunction(e.field)) {
258
- const r = e.field(this.runtimeMeta());
259
- r instanceof Promise ? r.then((h) => {
260
- this.processedModel.value[h] = n;
261
- }) : this.processedModel.value[r] = n;
262
- return;
263
- }
264
- this.processedModel.value[e.field] = n;
265
- } else
266
- for (let r = 0; r < this.processedModel.value[l].length; r++) {
267
- const h = this.processedModel.value[l][r];
268
- if (!p.isFunction(e.field))
269
- h[e.field] = n;
270
- else {
271
- const g = e.field(
272
- this.runtimeMeta()
273
- );
274
- g instanceof Promise ? g.then((N) => {
275
- h[N] = n;
276
- }) : h[g] = n;
277
- return;
278
- }
279
- }
280
- this.rawModel = A(this.processedModel.value), this.modelEffect.clearEffects();
281
- }
282
- })
283
- );
284
- }), M instanceof Promise ? (E[y] = !0, i[u] = t[u].defaultValueWhenAsync, a() && f(), M.then((n) => {
285
- E[y] = !0, i[u] = n, a() && f(!0);
286
- })) : (E[y] = !0, i[u] = M, a() && f());
287
- } else
288
- E[y] = !0, _(m) ? S(
289
- () => m.value,
290
- (M) => {
291
- i[u] = M, a() && f();
292
- },
293
- {
294
- immediate: !0,
295
- deep: !0
296
- }
297
- ) : B(m) ? S(
298
- () => m,
299
- (M) => {
300
- i[u] = M, a() && f();
301
- },
302
- {
303
- immediate: !0,
304
- deep: !0
305
- }
306
- ) : (i[u] = m, a() && f());
188
+ // 对于稳定初始化更新的抽象
189
+ stableUpdater(e = []) {
190
+ if (e.every(Boolean)) {
191
+ const t = S(this.processedSchemas.value);
192
+ !f.isProcessInprogress(t) && f.isObjectEmpty(this.stableModel) && (this.stableUpdaterProcessProgress || (this.stableUpdaterProcessProgress = Array.from({
193
+ length: t.length
194
+ }).fill(!1)), this.stableUpdaterProcessProgress[this.stableUpdaterTimes] = !0, this.stableUpdaterTimes++, this.modelProcessor(t));
307
195
  }
308
196
  }
309
- modelProcessor(e, t = this.processedModel.value) {
310
- if (p.isListSchema(e)) {
311
- if (p.isFunction(e.field))
312
- return;
313
- t[e.field] || (t[e.field] = [{}]), e.children.forEach((i) => {
314
- this.modelProcessor(i, t[e.field][0]);
315
- });
316
- return;
317
- }
318
- if (p.isGroupSchema(e)) {
319
- e.children.forEach((i) => {
320
- this.modelProcessor(i, t);
321
- });
322
- return;
323
- }
324
- if (p.isItemSchema(e)) {
325
- if (p.isFunction(e.field) || p.isUndefined(e.field) || !Number.isNaN(Number(e.field)) || p.isFunction(e.defaultValue))
197
+ // 核心单 schema 处理过程,接收一个对象键值对的数据结构
198
+ parseItem(e, t, s) {
199
+ const r = this, n = Array.from({
200
+ length: Object.keys(e).filter((l) => l !== "children").length
201
+ }).fill(!1);
202
+ this.objectParser({ data: e, index: t, updater: i });
203
+ function i(l) {
204
+ const u = l.index, a = l.key, y = l.keyIndex;
205
+ if (!l.stable)
326
206
  return;
327
- t[e.field] = e.defaultValue;
207
+ const w = r.parseStable(l.stable), m = s == null ? void 0 : s.index, g = s == null ? void 0 : s.key;
208
+ let p = w;
209
+ if (f.isProcessInprogress(p) || (n[y] = !0), s) {
210
+ let b = r.processedSchemas.value[m][g][u][a];
211
+ b && f.isObject(b) && a !== "component" && (p = Object.assign(b, p)), r.processedSchemas.value[m][g][u][a] = p, r.stableUpdater(n);
212
+ } else {
213
+ let b = r.processedSchemas.value[u][a];
214
+ b && f.isObject(b) && (p = Object.assign(b, p)), r.processedSchemas.value[u][a] = p, r.stableUpdater(n);
215
+ }
328
216
  }
329
217
  }
218
+ // 只做基本的对象 parser
219
+ objectParser(e) {
220
+ const t = e.data;
221
+ Object.keys(t).forEach((r, n) => {
222
+ if (r === "children")
223
+ this.parseSchemas(t[r], {
224
+ ...e,
225
+ key: r,
226
+ keyIndex: n
227
+ });
228
+ else {
229
+ const i = (l) => {
230
+ e.updater({
231
+ ...e,
232
+ key: r,
233
+ keyIndex: n,
234
+ stable: l
235
+ });
236
+ };
237
+ if (f.isFunction(t[r]))
238
+ r !== "defaultValue" ? this.schemaEffect.trackEffect(() => {
239
+ if (r === "component") {
240
+ const l = t[r](this.getRuntimeMeta());
241
+ this.promiseFieldParser(l, i, !1);
242
+ } else
243
+ this.fieldParser(t[r], i);
244
+ }) : this.defaultValueEffect.trackEffect(() => {
245
+ const l = this.schemaEffect.trackEffect(() => {
246
+ /\{\s*model\s*\}/.test(t[r].toString()) ? this.fieldParser(t[r], (u) => {
247
+ if (!u)
248
+ return i(u);
249
+ this.defaultValueInprogressMap.set(t[r], u), !f.isProcessInprogress(u) && Array.from(this.defaultValueInprogressMap.values()).every(
250
+ (a) => !a.includes("undefined")
251
+ ) ? (i(u), this.defaultValueEffect.clearEffects(), l()) : i(u);
252
+ }) : this.fieldParser(t[r], (u) => {
253
+ !f.isProcessInprogress(u) && Array.from(this.defaultValueInprogressMap.values()).every(
254
+ (a) => !a.includes("undefined")
255
+ ) ? (i(u), this.defaultValueEffect.clearEffects(), l()) : i(u);
256
+ });
257
+ });
258
+ });
259
+ else if (r === "component") {
260
+ const l = t[r];
261
+ this.promiseFieldParser(l, i, !1);
262
+ } else
263
+ this.fieldParser(t[r], i);
264
+ }
265
+ });
266
+ }
267
+ promiseFieldParser(e, t, s) {
268
+ f.isPromise(e) ? e.then((r) => {
269
+ s && f.isObject(r) ? this.objectParser({
270
+ data: r,
271
+ updater: t
272
+ }) : t(r);
273
+ }) : s && f.isObject(e) ? this.objectParser({
274
+ data: e,
275
+ updater: t
276
+ }) : t(e);
277
+ }
278
+ // 对任意对象中单个字段的 parse: 做基本处理
279
+ fieldParser(e, t, s = !0) {
280
+ if (f.isFunction(e)) {
281
+ const r = e(this.getRuntimeMeta());
282
+ this.promiseFieldParser(r, t, s);
283
+ } else
284
+ R(e) ? E(
285
+ () => e.value,
286
+ () => {
287
+ f.isUndefined(e.value) || (s && f.isObject(e.value) ? this.objectParser({
288
+ data: e.value,
289
+ updater: t
290
+ }) : t(e.value));
291
+ },
292
+ {
293
+ immediate: !0,
294
+ deep: !0
295
+ }
296
+ ) : M(e) ? E(
297
+ () => e,
298
+ () => {
299
+ f.isUndefined(e) || (s && f.isObject(e) ? this.objectParser({
300
+ data: e,
301
+ updater: t
302
+ }) : t(e));
303
+ },
304
+ {
305
+ immediate: !0,
306
+ deep: !0
307
+ }
308
+ ) : s && f.isObject(e) ? this.objectParser({
309
+ data: e,
310
+ updater: t
311
+ }) : t(e);
312
+ }
313
+ modelProcessor(e) {
314
+ e.map(
315
+ (t) => this.createModel(t, this.processedModel.value)
316
+ ), f.isObjectEmpty(this.stableModel) && this.stableUpdaterProcessProgress.every(Boolean) && this.defaultValueEffect.effects.size === 0 && (this.stableModel = V(this.processedModel.value));
317
+ }
318
+ createModel(e, t) {
319
+ f.isListSchema(e) && (t[e.field] || (t[e.field] = [{}]), e.children.forEach((s) => {
320
+ this.createModel(s, t[e.field][0]);
321
+ })), f.isGroupSchema(e) && e.children.forEach((s) => {
322
+ this.createModel(s, t);
323
+ }), f.isItemSchema(e) && (t[e.field] = e.defaultValue);
324
+ }
330
325
  }
331
- function D(c) {
332
- return typeof c == "function" || Object.prototype.toString.call(c) === "[object Object]" && !G(c);
326
+ function x(o) {
327
+ return typeof o == "function" || Object.prototype.toString.call(o) === "[object Object]" && !B(o);
333
328
  }
334
- class H {
329
+ class _ {
335
330
  constructor(e) {
336
- d(this, "schemas", k([]));
337
- d(this, "model", k({}));
338
- d(this, "processorBySchemaType", {
331
+ c(this, "schemas", I([]));
332
+ c(this, "model", I({}));
333
+ c(this, "processorBySchemaType", {
339
334
  item: this.runtimeItemProcessor.bind(this),
340
335
  group: this.runtimeGroupProcessor.bind(this),
341
336
  list: this.runtimeListProcessor.bind(this)
342
337
  });
343
- d(this, "formRef", k(null));
344
- this.setup = e, this.processors = new O(this.schemas, this.model), this.analyze(this.setup(this));
345
- }
346
- analyze(e) {
347
- this.processors.schemaAnalyzer(e.schemas);
348
- }
349
- runtimeItemProcessor(e, t, i = this.model.value, f) {
350
- var m;
351
- const o = f ? `${f.field}.${t}.${e.field}` : e.field, s = W(e.component), l = s.name, P = e.componentProps ?? {}, E = b.placeholderPresetByComponentName;
352
- let a = e.placeholder;
353
- a || (a = `${// @ts-expect-error
354
- E[l] ?? "请输入"}${e.label}`), e.required && (e.rules || (e.rules = []), (m = e.rules) == null || m.push({
338
+ c(this, "formRef", I(null));
339
+ this.setup = e, this.processor = new T(this);
340
+ const t = this.setup(this);
341
+ this.processor.parseSchemas(t.schemas);
342
+ }
343
+ getRuntimeMeta() {
344
+ return {
345
+ model: S(V(this.model.value))
346
+ };
347
+ }
348
+ runtimeItemProcessor(e, t, s = this.model.value, r) {
349
+ var g;
350
+ const n = r ? `${r.field}.${t}.${e.field}` : e.field, i = S(e.component);
351
+ if (!i)
352
+ return;
353
+ const l = i.name, u = e.componentProps ?? {}, a = j.placeholderPresetByComponentName;
354
+ let y = e.placeholder;
355
+ y || (y = `${// @ts-expect-error
356
+ a[l] ?? "请输入"}${e.label}`), e.required && (e.rules || (e.rules = []), (g = e.rules) == null || g.push({
355
357
  required: !0,
356
358
  message: `${e.label}是必填项`
357
359
  }));
358
- let u = e.show;
359
- return u === void 0 && (u = !0), u || delete i[e.field], v(w.runtimeDoms.Item, null, {
360
+ let m = e.show;
361
+ return m === void 0 && (m = !0), m || delete s[e.field], d(h.runtimeDoms.Item, null, {
360
362
  default() {
361
- return C(v(w.runtimeDoms.FormItem, {
363
+ return O(d(h.runtimeDoms.FormItem, {
362
364
  label: `${e.label}:`,
363
365
  rules: e.rules,
364
- field: o
366
+ field: n
365
367
  }, {
366
- default: () => [v(s, q({
367
- modelValue: i[e.field],
368
- "onUpdate:modelValue": (M) => i[e.field] = M,
369
- placeholder: a
370
- }, P), null)]
371
- }), [[R, u]]);
368
+ default: () => [d(i, k({
369
+ modelValue: s[e.field],
370
+ "onUpdate:modelValue": (p) => s[e.field] = p,
371
+ placeholder: y
372
+ }, u), null)]
373
+ }), [[v, m]]);
372
374
  }
373
375
  });
374
376
  }
375
377
  runtimeGroupProcessor(e) {
376
378
  let t;
377
- return v(w.runtimeDoms.Group, {
379
+ return d(h.runtimeDoms.Group, {
378
380
  schema: e
379
- }, D(t = e.children.map((i) => this.runtimeItemProcessor(i))) ? t : {
381
+ }, x(t = e.children.map((s) => this.runtimeItemProcessor(s))) ? t : {
380
382
  default: () => [t]
381
383
  });
382
384
  }
383
385
  addListItem(e) {
384
- var t, i;
385
- if (!((t = this.processors.rawModel[e.field]) != null && t[0]))
386
+ var t, s;
387
+ if (!((t = this.processor.stableModel[e.field]) != null && t[0]))
386
388
  return Promise.reject({
387
389
  code: "0001",
388
390
  message: "异步默认值数据正在处理中,请您耐心等待... "
389
391
  });
390
- (i = this.processors.rawModel[e.field]) != null && i[0] && this.model.value[e.field].push(A(this.processors.rawModel[e.field][0]));
392
+ (s = this.processor.stableModel[e.field]) != null && s[0] && this.model.value[e.field].push(V(this.processor.stableModel[e.field][0]));
391
393
  }
392
394
  deleteListItem(e, t) {
393
395
  this.model.value[e.field].splice(t, 1);
394
396
  }
395
397
  runtimeListProcessor(e) {
396
398
  const t = this;
397
- return v(w.runtimeDoms.List, {
399
+ return t.model.value[e.field] || (t.model.value[e.field] = [{}]), d(h.runtimeDoms.List, {
398
400
  schema: e
399
401
  }, {
400
402
  default() {
401
- return t.model.value[e.field].map((i, f) => v(w.runtimeDoms.ListItem, null, {
403
+ return t.model.value[e.field].map((s, r) => d(h.runtimeDoms.ListItem, null, {
402
404
  default() {
403
- return e.children.map((o, s) => t.runtimeItemProcessor(o, s, i, e));
405
+ return e.children.map((n, i) => t.runtimeItemProcessor(n, i, s, e));
404
406
  },
405
407
  delete({
406
- container: o
408
+ container: n
407
409
  } = {}) {
408
410
  var l;
409
- let s = o ?? v("button", null, null);
410
- return C(v(s, {
411
- onClick: () => t.deleteListItem(e, f)
412
- }, null), [[R, ((l = t.model.value[e.field]) == null ? void 0 : l.length) > 1]]);
411
+ let i = n ?? d("button", null, null);
412
+ return O(d(i, {
413
+ onClick: () => t.deleteListItem(e, r)
414
+ }, null), [[v, ((l = t.model.value[e.field]) == null ? void 0 : l.length) > 1]]);
413
415
  }
414
416
  }));
415
417
  },
416
418
  add({
417
- container: i
419
+ container: s
418
420
  } = {}) {
419
- let f = i ?? v("button", null, [$("添加")]);
420
- return v(f, {
421
+ let r = s ?? d("button", null, [D("添加")]);
422
+ return d(r, {
421
423
  onClick: () => t.addListItem(e)
422
424
  }, null);
423
425
  }
@@ -428,96 +430,85 @@ class H {
428
430
  }
429
431
  exec() {
430
432
  let e;
431
- return v(w.runtimeDoms.Form, {
433
+ return d(h.runtimeDoms.Form, {
432
434
  ref: this.formRef,
433
435
  model: this.model.value
434
- }, D(e = this.runtimeProcessor(this.schemas.value)) ? e : {
436
+ }, x(e = this.runtimeProcessor(this.schemas.value)) ? e : {
435
437
  default: () => [e]
436
438
  });
437
439
  }
438
440
  }
439
- class w {
441
+ class h {
440
442
  }
441
- d(w, "runtimeDoms");
442
- class V {
443
- constructor() {
444
- d(this, "effects", /* @__PURE__ */ new Set());
445
- }
446
- clearEffects() {
447
- this.effects.clear();
448
- }
449
- triggerEffects() {
450
- Array.from(this.effects).forEach((e) => e());
451
- }
452
- trackEffect(e) {
453
- this.effects.add(e);
454
- }
455
- }
456
- const F = class F {
443
+ c(h, "runtimeDoms");
444
+ const P = class P {
457
445
  static getPlaceholderPrefixPresetByComponentName() {
458
446
  const e = {
459
447
  请选择: ["Select", "Tree", "TreeSelect"],
460
448
  请输入: ["Input"]
461
449
  }, t = {};
462
- for (let i in e)
463
- e[i].forEach((f) => {
464
- t[f] = i;
450
+ for (let s in e)
451
+ e[s].forEach((r) => {
452
+ t[r] = s;
465
453
  });
466
454
  return t;
467
455
  }
468
456
  };
469
- d(F, "schemaPreset", {
457
+ c(P, "schemaPreset", {
470
458
  type: {
471
- defaultValueWhenAsync: "item"
459
+ defaultValue: "item"
472
460
  },
473
461
  component: {
474
- defaultValueWhenAsync: void 0
462
+ defaultValue: void 0
475
463
  },
476
464
  componentProps: {
477
- defaultValueWhenAsync: void 0
465
+ defaultValue: void 0
478
466
  },
479
467
  defaultValue: {
480
- defaultValueWhenAsync: void 0
468
+ defaultValue: void 0
481
469
  },
482
470
  label: {
483
- defaultValueWhenAsync: ""
471
+ defaultValue: ""
484
472
  },
485
473
  field: {
486
- defaultValueWhenAsync: "__yiwwhl_async_field_fallback"
474
+ defaultValue: "__yiwwhl_async_field_fallback"
487
475
  },
488
476
  rules: {
489
- defaultValueWhenAsync: []
477
+ defaultValue: []
490
478
  },
491
479
  show: {
492
- defaultValueWhenAsync: !0
480
+ defaultValue: !0
493
481
  },
494
482
  required: {
495
- defaultValueWhenAsync: !1
483
+ defaultValue: !1
496
484
  },
497
485
  placeholder: {
498
- defaultValueWhenAsync: void 0
486
+ defaultValue: void 0
487
+ },
488
+ children: {
489
+ defaultValue: []
499
490
  }
500
- }), d(F, "componentPropsPreset", {
491
+ }), c(P, "componentPropsPreset", {
501
492
  options: {
502
- defaultValueWhenAsync: []
493
+ defaultValue: []
503
494
  }
504
495
  }), // 基于基本功能提出基本预设
505
- d(F, "placeholderPresetByComponentName", F.getPlaceholderPrefixPresetByComponentName());
506
- let b = F;
507
- const Q = /* @__PURE__ */ T({
496
+ c(P, "placeholderPresetByComponentName", P.getPlaceholderPrefixPresetByComponentName());
497
+ let j = P;
498
+ const z = /* @__PURE__ */ L({
508
499
  props: {
509
500
  setup: {
510
501
  type: Function,
511
502
  required: !0
512
503
  }
513
504
  },
514
- setup(c) {
515
- const e = new H(c.setup);
505
+ setup(o) {
506
+ const e = new _(o.setup);
516
507
  return () => e.exec();
517
508
  }
518
509
  });
519
- function X(c) {
520
- const e = new U(c);
510
+ function F(o) {
511
+ const e = new N(o);
521
512
  return [
522
513
  e.setup.bind(e),
523
514
  {
@@ -525,15 +516,15 @@ function X(c) {
525
516
  }
526
517
  ];
527
518
  }
528
- function Y(c) {
519
+ function G(o) {
529
520
  return {
530
521
  install() {
531
- w.runtimeDoms = c;
522
+ h.runtimeDoms = o;
532
523
  }
533
524
  };
534
525
  }
535
526
  export {
536
- Q as ProForm,
537
- X as useForm,
538
- Y as useFormRenderer
527
+ z as ProForm,
528
+ F as useForm,
529
+ G as useFormRenderer
539
530
  };
@@ -4,5 +4,5 @@ export default class Effect {
4
4
  constructor();
5
5
  clearEffects(): void;
6
6
  triggerEffects(): void;
7
- trackEffect(effect: AnyFunction): void;
7
+ trackEffect(effect: AnyFunction): () => boolean;
8
8
  }
@@ -1,6 +1,8 @@
1
1
  import { AnyObject, ItemSchema } from "../types";
2
2
  export default class Preset {
3
- static schemaPreset: Record<keyof ItemSchema, any>;
3
+ static schemaPreset: Record<keyof ItemSchema, any> & {
4
+ children: any;
5
+ };
4
6
  static componentPropsPreset: AnyObject;
5
7
  static placeholderPresetByComponentName: {};
6
8
  static getPlaceholderPrefixPresetByComponentName(): {};
@@ -0,0 +1,34 @@
1
+ import { Ref } from "vue";
2
+ import { AnyObject, Schema, ProxyedSchema, AnyFunction, ObjectParserRoot } from "../types";
3
+ import { RuntimeCore } from ".";
4
+ import Effect from "./Effect";
5
+ /**
6
+ * 基本描述
7
+ * 对于函数的命名,如果是动词相关,代表对过程的处理,如果是名词,代表一个处理器
8
+ */
9
+ export default class Processor {
10
+ processedSchemas: Ref<AnyObject[]>;
11
+ processedModel: Ref<AnyObject>;
12
+ getRuntimeMeta: AnyFunction;
13
+ stableSchemas: ProxyedSchema[];
14
+ stableModel: AnyObject;
15
+ schemaPreset: AnyObject;
16
+ componentPropsPreset: AnyObject;
17
+ stableUpdaterProcessProgress: boolean[];
18
+ stableUpdaterTimes: number;
19
+ schemaEffect: Effect;
20
+ defaultValueEffect: Effect;
21
+ defaultValueInprogressMap: Map<any, any>;
22
+ constructor(runtimeCore: RuntimeCore);
23
+ parse<T extends object = any>(data: T[], parentMeta?: AnyObject): void;
24
+ initSchemas(schemas: AnyObject[]): AnyObject[];
25
+ parseSchemas(schemas: ProxyedSchema[], parentMeta?: AnyObject): void;
26
+ parseStable(stable: AnyObject): AnyObject;
27
+ stableUpdater(parseProcess?: boolean[]): void;
28
+ parseItem(data: AnyObject, index: number, parentMeta?: AnyObject): void;
29
+ objectParser(root: ObjectParserRoot): void;
30
+ promiseFieldParser(rootField: any, updater: AnyFunction, deepProcess: boolean): void;
31
+ fieldParser(rootField: any, updater: AnyFunction, deepProcess?: boolean): void;
32
+ modelProcessor(schemas: Schema[]): void;
33
+ createModel(schema: AnyObject, baseModel: AnyObject): void;
34
+ }
@@ -1,16 +1,18 @@
1
1
  import { Ref } from "vue";
2
- import { Setup, Schema, FormCustomization, AnyObject, ItemSchema, GroupSchema, ListSchema, ProcessorBySchemaType } from "../types";
3
- import Processors from "./Processors";
2
+ import { Setup, Schema, AnyObject, ItemSchema, GroupSchema, ListSchema, ProcessorBySchemaType } from "../types";
3
+ import Processor from "./Processor";
4
4
  export default class RuntimeCore {
5
5
  setup: Setup;
6
- processors: Processors;
6
+ processor: Processor;
7
7
  schemas: Ref<Schema[]>;
8
8
  model: Ref<AnyObject>;
9
9
  processorBySchemaType: ProcessorBySchemaType;
10
10
  formRef: Ref<AnyObject>;
11
11
  constructor(setup: Setup);
12
- analyze(formCustomization: FormCustomization): void;
13
- runtimeItemProcessor(schema: ItemSchema, index?: number, baseModel?: AnyObject, parentSchema?: ListSchema): JSX.Element;
12
+ getRuntimeMeta(): {
13
+ model: AnyObject;
14
+ };
15
+ runtimeItemProcessor(schema: ItemSchema, index?: number, baseModel?: AnyObject, parentSchema?: ListSchema): JSX.Element | undefined;
14
16
  runtimeGroupProcessor(schema: GroupSchema): JSX.Element;
15
17
  addListItem(schema: AnyObject): Promise<never> | undefined;
16
18
  deleteListItem(schema: AnyObject, index: number): void;
@@ -1,6 +1,5 @@
1
1
  export { default as FormCustomizer } from "./FormCustomizer";
2
2
  export { default as RuntimeCore } from "./RuntimeCore";
3
3
  export { default as Context } from "./Context";
4
- export { default as Processors } from "./Processors";
5
- export { default as Effect } from "./Effect";
4
+ export { default as Processors } from "./Processor";
6
5
  export { default as Preset } from "./Preset";
@@ -3,3 +3,4 @@ export * from "./contextTypes";
3
3
  export * from "./formCustomizerTypes";
4
4
  export * from "./utilTypes";
5
5
  export * from "./runtimeTypes";
6
+ export * from "./processorTypes";
@@ -0,0 +1,6 @@
1
+ import { AnyFunction, AnyObject } from ".";
2
+ export interface ObjectParserRoot {
3
+ data: AnyObject;
4
+ index?: number;
5
+ updater: AnyFunction;
6
+ }
@@ -1,8 +1,10 @@
1
- import { GroupSchema, ItemSchema, ListSchema } from "../types";
1
+ import { AnyObject, GroupSchema, ItemSchema, ListSchema } from "../types";
2
2
  export default class IS {
3
3
  private static typeChecker;
4
4
  static isArray(data: any): data is any[];
5
5
  static isFunction(data: any): data is (...args: any) => any;
6
+ static isPromise(data: any): data is Promise<any>;
7
+ static isObject(data: any): data is AnyObject;
6
8
  static isAsyncFunction(data: any): data is (...args: any) => Promise<any>;
7
9
  static isUndefined(data: any): data is undefined;
8
10
  static isArrayEmpty(data: any[]): boolean;
@@ -10,4 +12,5 @@ export default class IS {
10
12
  static isListSchema(data: any): data is ListSchema;
11
13
  static isGroupSchema(data: any): data is GroupSchema;
12
14
  static isItemSchema(data: any): data is ItemSchema;
15
+ static isProcessInprogress(data: any): boolean;
13
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@harbor-design/proform",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -19,9 +19,7 @@
19
19
  }
20
20
  },
21
21
  "files": [
22
- "dist/",
23
- "./LICENSE",
24
- "./CHANGELOG.md"
22
+ "dist/"
25
23
  ],
26
24
  "author": "yiwwhl<yiwwhl@qq.com>",
27
25
  "license": "MIT",
package/CHANGELOG.md DELETED
@@ -1,37 +0,0 @@
1
- # @harbor-design/proform
2
-
3
- ## 1.0.6
4
-
5
- ### Patch Changes
6
-
7
- - support required and default placeholder
8
-
9
- ## 1.0.5
10
-
11
- ### Patch Changes
12
-
13
- - update pack sourcecode
14
-
15
- ## 1.0.4
16
-
17
- ### Patch Changes
18
-
19
- - remove console
20
-
21
- ## 1.0.3
22
-
23
- ### Patch Changes
24
-
25
- - fix error dist bug
26
-
27
- ## 1.0.2
28
-
29
- ### Patch Changes
30
-
31
- - fix bugs of list item add error
32
-
33
- ## 1.0.1
34
-
35
- ### Patch Changes
36
-
37
- - ProForm Release 1.0.1
@@ -1,24 +0,0 @@
1
- import { Ref } from "vue";
2
- import { AnyObject, Schema, ProxyedSchema, AnyFunction } from "../types";
3
- import { Effect } from "../services";
4
- export default class Processors {
5
- processedSchemas: Ref<Schema[]>;
6
- processedModel: Ref<AnyObject>;
7
- rawSchemas: ProxyedSchema[];
8
- rawModel: AnyObject;
9
- schemaPreset: Record<keyof import("../types").ItemSchema, any>;
10
- componentPropsPreset: AnyObject;
11
- uniqueEffectMap: any;
12
- schemaEffect: Effect;
13
- modelEffect: Effect;
14
- stopWatchEffect: Effect;
15
- constructor(processedSchemas: Ref<Schema[]>, processedModel: Ref<AnyObject>);
16
- schemaAnalyzer(schemas: ProxyedSchema[], baseSchema?: Schema[], baseRawSchema?: ProxyedSchema[], parentField?: string, schemaIndex?: number): void;
17
- schemaProcessor(schema: ProxyedSchema, index: number, setter: AnyFunction, schemaIndex?: number, parentField?: string): void;
18
- replaceFunctionsWithUndefined(obj: AnyObject): AnyObject;
19
- runtimeMeta(): {
20
- model: AnyObject;
21
- };
22
- propsProcessor<T extends object = any>(pendingProcess: T, preset: Record<keyof T, any>, processed: AnyObject, update: AnyFunction, schemaIndexOrChildrenIndex: number, schemaIndex?: number, parentField?: string): void;
23
- modelProcessor(schema: ProxyedSchema, baseModel?: AnyObject): void;
24
- }