@harbor-design/proform 1.0.6 → 1.0.8

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=(c,e,t)=>e in c?I(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var l=(c,e,t)=>(v(c,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("vue");class O{constructor(e){l(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(n.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 S(c){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 i=new Map;for(let[o,a]of s)i.set(t(o),t(a));return i}if(s instanceof Set){const i=new Set;for(let o of s)i.add(t(o));return i}if(e.has(s))return e.get(s);if(Array.isArray(s)){const i=[];e.set(s,i);for(let o=0;o<s.length;o++)i[o]=t(s[o]);return i}const r=Object.create(Object.getPrototypeOf(s));e.set(s,r);for(let i in s)s.hasOwnProperty(i)&&(r[i]=t(s[i]));return r}return t(c)}class w{constructor(){l(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){l(this,"processedSchemas");l(this,"processedModel");l(this,"getRuntimeMeta");l(this,"stableSchemas",[]);l(this,"stableModel",{});l(this,"schemaPreset",V.schemaPreset);l(this,"componentPropsPreset",V.componentPropsPreset);l(this,"stableUpdaterProcessProgress");l(this,"stableUpdaterTimes",0);l(this,"schemaEffect",new w);l(this,"defaultValueEffect",new w);l(this,"defaultValueInprogressMap",new Map);l(this,"baseDefaultValueFunctionsLength");this.processedSchemas=e.schemas,this.processedModel=e.model,this.getRuntimeMeta=e.getRuntimeMeta.bind(e),n.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})}countFunctionDefaultValues(e){let t=0,s=new Set;function r(i){if(!s.has(i)&&(Array.isArray(i)||i!==null&&typeof i=="object")){s.add(i);for(let o in i)i.hasOwnProperty(o)&&(o==="defaultValue"&&typeof i[o]=="function"&&t++,r(i[o]))}}return r(e),t}parseSchemas(e,t){u.isArrayEmpty(this.processedSchemas.value)&&(this.baseDefaultValueFunctionsLength=this.countFunctionDefaultValues(S(e)),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=n.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,i=Array.from({length:Object.keys(e).filter(a=>a!=="children").length}).fill(!1);this.objectParser({data:e,index:t,updater:o});function o(a){const f=a.index,d=a.key,P=a.keyIndex;if(!a.stable)return;const j=r.parseStable(a.stable),m=s==null?void 0:s.index,g=s==null?void 0:s.key;let h=j;if(u.isProcessInprogress(h)||(i[P]=!0),s){let b=r.processedSchemas.value[m][g][f][d];b&&u.isObject(b)&&d!=="component"&&(h=Object.assign(b,h)),r.processedSchemas.value[m][g][f][d]=h,r.stableUpdater(i)}else{let b=r.processedSchemas.value[f][d];b&&u.isObject(b)&&(h=Object.assign(b,h)),r.processedSchemas.value[f][d]=h,r.stableUpdater(i)}}}objectParser(e){const t=e.data;Object.keys(t).forEach((r,i)=>{if(r==="children")this.parseSchemas(t[r],{...e,key:r,keyIndex:i});else{const o=a=>{e.updater({...e,key:r,keyIndex:i,stable:a})};if(u.isFunction(t[r]))r!=="defaultValue"?this.schemaEffect.trackEffect(()=>{if(r==="component"){const a=t[r](this.getRuntimeMeta());this.promiseFieldParser(a,o,!1)}else this.fieldParser(t[r],o)}):this.defaultValueEffect.trackEffect(()=>{const a=this.schemaEffect.trackEffect(()=>{/\{\s*model\s*\}/.test(t[r].toString())?this.fieldParser(t[r],f=>{if(!f)return o(f);this.defaultValueInprogressMap.set(t[r],f),!u.isProcessInprogress(f)&&this.defaultValueInprogressMap.size===this.baseDefaultValueFunctionsLength&&Array.from(this.defaultValueInprogressMap.values()).every(d=>!d.includes("undefined"))?(o(f),this.defaultValueEffect.clearEffects(),a()):o(f)}):this.fieldParser(t[r],f=>{this.defaultValueInprogressMap.set(t[r],f),!u.isProcessInprogress(f)&&this.defaultValueInprogressMap.size===this.baseDefaultValueFunctionsLength&&Array.from(this.defaultValueInprogressMap.values()).every(d=>!d.includes("undefined"))?(o(f),this.defaultValueEffect.clearEffects(),a()):o(f)})})});else if(r==="component"){const a=t[r];this.promiseFieldParser(a,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 n.isRef(e)?n.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}):n.isReactive(e)?n.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=S(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 E(c){return typeof c=="function"||Object.prototype.toString.call(c)==="[object Object]"&&!n.isVNode(c)}class R{constructor(e){l(this,"schemas",n.ref([]));l(this,"model",n.ref({}));l(this,"processorBySchemaType",{item:this.runtimeItemProcessor.bind(this),group:this.runtimeGroupProcessor.bind(this),list:this.runtimeListProcessor.bind(this)});l(this,"formRef",n.ref(null));this.setup=e,this.processor=new C(this);const t=this.setup(this);this.processor.parseSchemas(t.schemas)}getRuntimeMeta(){return{model:n.toRaw(S(this.model.value))}}runtimeItemProcessor(e,t,s=this.model.value,r){var g;const i=r?`${r.field}.${t}.${e.field}`:e.field,o=n.toRaw(e.component);if(!o)return;const a=o.name,f=e.componentProps??{},d=V.placeholderPresetByComponentName;let P=e.placeholder;P||(P=`${d[a]??"请输入"}${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],n.createVNode(p.runtimeDoms.Item,null,{default(){return n.withDirectives(n.createVNode(p.runtimeDoms.FormItem,{label:`${e.label}:`,rules:e.rules,field:i},{default:()=>[n.createVNode(o,n.mergeProps({modelValue:s[e.field],"onUpdate:modelValue":h=>s[e.field]=h,placeholder:P},f),null)]}),[[n.vShow,m]])}})}runtimeGroupProcessor(e){let t;return n.createVNode(p.runtimeDoms.Group,{schema:e},E(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(S(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]=[{}]),n.createVNode(p.runtimeDoms.List,{schema:e},{default(){return t.model.value[e.field].map((s,r)=>n.createVNode(p.runtimeDoms.ListItem,null,{default(){return e.children.map((i,o)=>t.runtimeItemProcessor(i,o,s,e))},delete({container:i}={}){var a;let o=i??n.createVNode("button",null,null);return n.withDirectives(n.createVNode(o,{onClick:()=>t.deleteListItem(e,r)},null),[[n.vShow,((a=t.model.value[e.field])==null?void 0:a.length)>1]])}}))},add({container:s}={}){let r=s??n.createVNode("button",null,[n.createTextVNode("添加")]);return n.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 n.createVNode(p.runtimeDoms.Form,{ref:this.formRef,model:this.model.value},E(e=this.runtimeProcessor(this.schemas.value))?e:{default:()=>[e]})}}class p{}l(p,"runtimeDoms");const y=class y{static getPlaceholderPrefixPresetByComponentName(){const e={请选择:["Select","Tree","TreeSelect"],请输入:["Input"]},t={};for(let s in e)e[s].forEach(r=>{t[r]=s});return t}};l(y,"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:[]}}),l(y,"componentPropsPreset",{options:{defaultValue:[]}}),l(y,"placeholderPresetByComponentName",y.getPlaceholderPrefixPresetByComponentName());let V=y;const A=n.defineComponent({props:{setup:{type:Function,required:!0}},setup(c){const e=new R(c.setup);return()=>e.exec()}});function D(c){const e=new O(c);return[e.setup.bind(e),{submit:e.submit.bind(e)}]}function M(c){return{install(){p.runtimeDoms=c}}}exports.ProForm=A;exports.useForm=D;exports.useFormRenderer=M;
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 x = Object.defineProperty;
2
+ var D = (l, e, t) => e in l ? x(l, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[e] = t;
3
+ var o = (l, e, t) => (D(l, typeof e != "symbol" ? e + "" : e, t), t);
4
+ import { toRaw as S, watch as E, isRef as M, isReactive as U, ref as I, createVNode as d, withDirectives as v, mergeProps as R, vShow as O, createTextVNode as k, isVNode as L, defineComponent as F } from "vue";
5
+ class B {
6
6
  constructor(e) {
7
- d(this, "runtimeCore");
7
+ o(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 u {
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,377 @@ 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(l) {
63
88
  const e = /* @__PURE__ */ new WeakMap();
64
- function t(i) {
65
- if (i === null || typeof i != "object")
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 i = /* @__PURE__ */ new Map();
98
+ for (let [n, c] of s)
99
+ i.set(t(n), t(c));
66
100
  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;
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 i = /* @__PURE__ */ new Set();
104
+ for (let n of s)
105
+ i.add(t(n));
106
+ return i;
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 i = [];
112
+ e.set(s, i);
113
+ for (let n = 0; n < s.length; n++)
114
+ i[n] = t(s[n]);
115
+ return i;
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 i in s)
120
+ s.hasOwnProperty(i) && (r[i] = t(s[i]));
121
+ return r;
122
+ }
123
+ return t(l);
124
+ }
125
+ class C {
126
+ constructor() {
127
+ o(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 N {
140
+ constructor(e) {
141
+ o(this, "processedSchemas");
142
+ o(this, "processedModel");
143
+ o(this, "getRuntimeMeta");
144
+ o(this, "stableSchemas", []);
145
+ o(this, "stableModel", {});
146
+ o(this, "schemaPreset", j.schemaPreset);
147
+ o(this, "componentPropsPreset", j.componentPropsPreset);
148
+ o(this, "stableUpdaterProcessProgress");
149
+ o(this, "stableUpdaterTimes", 0);
150
+ o(this, "schemaEffect", new C());
151
+ o(this, "defaultValueEffect", new C());
152
+ o(this, "defaultValueInprogressMap", /* @__PURE__ */ new Map());
153
+ o(this, "baseDefaultValueFunctionsLength");
154
+ this.processedSchemas = e.schemas, this.processedModel = e.model, this.getRuntimeMeta = e.getRuntimeMeta.bind(e), E(
111
155
  () => this.processedModel.value,
112
156
  () => {
113
- this.schemaEffect.triggerEffects(), this.modelEffect.triggerEffects();
157
+ this.schemaEffect.triggerEffects();
114
158
  },
115
159
  {
116
160
  deep: !0
117
161
  }
118
162
  );
119
163
  }
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
- }
164
+ // 核心处理过程,接收一个初始的代理数据结构,由其衍生的有 parseSchema
165
+ parse(e, t) {
166
+ e.forEach((s, r) => {
167
+ this.parseItem(s, r, t);
168
+ });
136
169
  }
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;
170
+ // 初始化空数据结构,避免后续复杂的 if else
171
+ initSchemas(e) {
172
+ return e.map((t) => {
173
+ const s = {};
174
+ return t.children && (s.children = this.initSchemas(t.children)), s;
175
+ });
176
+ }
177
+ countFunctionDefaultValues(e) {
178
+ let t = 0, s = /* @__PURE__ */ new Set();
179
+ function r(i) {
180
+ if (!s.has(i) && (Array.isArray(i) || i !== null && typeof i == "object")) {
181
+ s.add(i);
182
+ for (let n in i)
183
+ i.hasOwnProperty(n) && (n === "defaultValue" && typeof i[n] == "function" && t++, r(i[n]));
166
184
  }
167
- i({ ...s }, E), l.rawModel = A(l.processedModel.value);
168
185
  }
169
- this.propsProcessor(
170
- e,
171
- this.schemaPreset,
172
- s,
173
- P,
174
- t,
175
- f,
176
- o
177
- );
178
- }
179
- replaceFunctionsWithUndefined(e) {
180
- if (typeof e != "object" || e === null)
186
+ return r(e), t;
187
+ }
188
+ // 派生过程,用于外部应用
189
+ parseSchemas(e, t) {
190
+ u.isArrayEmpty(this.processedSchemas.value) && (this.baseDefaultValueFunctionsLength = this.countFunctionDefaultValues(
191
+ V(e)
192
+ ), this.processedSchemas.value = this.initSchemas(e)), this.parse(e, t);
193
+ }
194
+ parseStable(e) {
195
+ const t = {};
196
+ if (!u.isUndefined(e.stable))
197
+ t[e.key] = this.parseStable(e.stable);
198
+ else
181
199
  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;
188
- }
189
- runtimeMeta() {
190
- return {
191
- model: this.replaceFunctionsWithUndefined(
192
- W(A(this.processedModel.value))
193
- )
194
- };
200
+ return t;
195
201
  }
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());
202
+ // 对于稳定初始化更新的抽象
203
+ stableUpdater(e = []) {
204
+ if (e.every(Boolean)) {
205
+ const t = S(this.processedSchemas.value);
206
+ !u.isProcessInprogress(t) && u.isObjectEmpty(this.stableModel) && (this.stableUpdaterProcessProgress || (this.stableUpdaterProcessProgress = Array.from({
207
+ length: t.length
208
+ }).fill(!1)), this.stableUpdaterProcessProgress[this.stableUpdaterTimes] = !0, this.stableUpdaterTimes++, this.modelProcessor(t));
307
209
  }
308
210
  }
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))
211
+ // 核心单 schema 处理过程,接收一个对象键值对的数据结构
212
+ parseItem(e, t, s) {
213
+ const r = this, i = Array.from({
214
+ length: Object.keys(e).filter((c) => c !== "children").length
215
+ }).fill(!1);
216
+ this.objectParser({ data: e, index: t, updater: n });
217
+ function n(c) {
218
+ const f = c.index, a = c.key, P = c.keyIndex;
219
+ if (!c.stable)
326
220
  return;
327
- t[e.field] = e.defaultValue;
221
+ const w = r.parseStable(c.stable), m = s == null ? void 0 : s.index, g = s == null ? void 0 : s.key;
222
+ let p = w;
223
+ if (u.isProcessInprogress(p) || (i[P] = !0), s) {
224
+ let b = r.processedSchemas.value[m][g][f][a];
225
+ b && u.isObject(b) && a !== "component" && (p = Object.assign(b, p)), r.processedSchemas.value[m][g][f][a] = p, r.stableUpdater(i);
226
+ } else {
227
+ let b = r.processedSchemas.value[f][a];
228
+ b && u.isObject(b) && (p = Object.assign(b, p)), r.processedSchemas.value[f][a] = p, r.stableUpdater(i);
229
+ }
328
230
  }
329
231
  }
232
+ // 只做基本的对象 parser
233
+ objectParser(e) {
234
+ const t = e.data;
235
+ Object.keys(t).forEach((r, i) => {
236
+ if (r === "children")
237
+ this.parseSchemas(t[r], {
238
+ ...e,
239
+ key: r,
240
+ keyIndex: i
241
+ });
242
+ else {
243
+ const n = (c) => {
244
+ e.updater({
245
+ ...e,
246
+ key: r,
247
+ keyIndex: i,
248
+ stable: c
249
+ });
250
+ };
251
+ if (u.isFunction(t[r]))
252
+ r !== "defaultValue" ? this.schemaEffect.trackEffect(() => {
253
+ if (r === "component") {
254
+ const c = t[r](this.getRuntimeMeta());
255
+ this.promiseFieldParser(c, n, !1);
256
+ } else
257
+ this.fieldParser(t[r], n);
258
+ }) : this.defaultValueEffect.trackEffect(() => {
259
+ const c = this.schemaEffect.trackEffect(() => {
260
+ /\{\s*model\s*\}/.test(t[r].toString()) ? this.fieldParser(t[r], (f) => {
261
+ if (!f)
262
+ return n(f);
263
+ this.defaultValueInprogressMap.set(t[r], f), !u.isProcessInprogress(f) && this.defaultValueInprogressMap.size === this.baseDefaultValueFunctionsLength && Array.from(this.defaultValueInprogressMap.values()).every(
264
+ (a) => !a.includes("undefined")
265
+ ) ? (n(f), this.defaultValueEffect.clearEffects(), c()) : n(f);
266
+ }) : this.fieldParser(t[r], (f) => {
267
+ this.defaultValueInprogressMap.set(t[r], f), !u.isProcessInprogress(f) && this.defaultValueInprogressMap.size === this.baseDefaultValueFunctionsLength && Array.from(this.defaultValueInprogressMap.values()).every(
268
+ (a) => !a.includes("undefined")
269
+ ) ? (n(f), this.defaultValueEffect.clearEffects(), c()) : n(f);
270
+ });
271
+ });
272
+ });
273
+ else if (r === "component") {
274
+ const c = t[r];
275
+ this.promiseFieldParser(c, n, !1);
276
+ } else
277
+ this.fieldParser(t[r], n);
278
+ }
279
+ });
280
+ }
281
+ promiseFieldParser(e, t, s) {
282
+ u.isPromise(e) ? e.then((r) => {
283
+ s && u.isObject(r) ? this.objectParser({
284
+ data: r,
285
+ updater: t
286
+ }) : t(r);
287
+ }) : s && u.isObject(e) ? this.objectParser({
288
+ data: e,
289
+ updater: t
290
+ }) : t(e);
291
+ }
292
+ // 对任意对象中单个字段的 parse: 做基本处理
293
+ fieldParser(e, t, s = !0) {
294
+ if (u.isFunction(e)) {
295
+ const r = e(this.getRuntimeMeta());
296
+ this.promiseFieldParser(r, t, s);
297
+ } else
298
+ M(e) ? E(
299
+ () => e.value,
300
+ () => {
301
+ u.isUndefined(e.value) || (s && u.isObject(e.value) ? this.objectParser({
302
+ data: e.value,
303
+ updater: t
304
+ }) : t(e.value));
305
+ },
306
+ {
307
+ immediate: !0,
308
+ deep: !0
309
+ }
310
+ ) : U(e) ? E(
311
+ () => e,
312
+ () => {
313
+ u.isUndefined(e) || (s && u.isObject(e) ? this.objectParser({
314
+ data: e,
315
+ updater: t
316
+ }) : t(e));
317
+ },
318
+ {
319
+ immediate: !0,
320
+ deep: !0
321
+ }
322
+ ) : s && u.isObject(e) ? this.objectParser({
323
+ data: e,
324
+ updater: t
325
+ }) : t(e);
326
+ }
327
+ modelProcessor(e) {
328
+ e.map(
329
+ (t) => this.createModel(t, this.processedModel.value)
330
+ ), u.isObjectEmpty(this.stableModel) && this.stableUpdaterProcessProgress.every(Boolean) && this.defaultValueEffect.effects.size === 0 && (this.stableModel = V(this.processedModel.value));
331
+ }
332
+ createModel(e, t) {
333
+ u.isListSchema(e) && (t[e.field] || (t[e.field] = [{}]), e.children.forEach((s) => {
334
+ this.createModel(s, t[e.field][0]);
335
+ })), u.isGroupSchema(e) && e.children.forEach((s) => {
336
+ this.createModel(s, t);
337
+ }), u.isItemSchema(e) && (t[e.field] = e.defaultValue);
338
+ }
330
339
  }
331
- function D(c) {
332
- return typeof c == "function" || Object.prototype.toString.call(c) === "[object Object]" && !G(c);
340
+ function A(l) {
341
+ return typeof l == "function" || Object.prototype.toString.call(l) === "[object Object]" && !L(l);
333
342
  }
334
- class H {
343
+ class T {
335
344
  constructor(e) {
336
- d(this, "schemas", k([]));
337
- d(this, "model", k({}));
338
- d(this, "processorBySchemaType", {
345
+ o(this, "schemas", I([]));
346
+ o(this, "model", I({}));
347
+ o(this, "processorBySchemaType", {
339
348
  item: this.runtimeItemProcessor.bind(this),
340
349
  group: this.runtimeGroupProcessor.bind(this),
341
350
  list: this.runtimeListProcessor.bind(this)
342
351
  });
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({
352
+ o(this, "formRef", I(null));
353
+ this.setup = e, this.processor = new N(this);
354
+ const t = this.setup(this);
355
+ this.processor.parseSchemas(t.schemas);
356
+ }
357
+ getRuntimeMeta() {
358
+ return {
359
+ model: S(V(this.model.value))
360
+ };
361
+ }
362
+ runtimeItemProcessor(e, t, s = this.model.value, r) {
363
+ var g;
364
+ const i = r ? `${r.field}.${t}.${e.field}` : e.field, n = S(e.component);
365
+ if (!n)
366
+ return;
367
+ const c = n.name, f = e.componentProps ?? {}, a = j.placeholderPresetByComponentName;
368
+ let P = e.placeholder;
369
+ P || (P = `${// @ts-expect-error
370
+ a[c] ?? "请输入"}${e.label}`), e.required && (e.rules || (e.rules = []), (g = e.rules) == null || g.push({
355
371
  required: !0,
356
372
  message: `${e.label}是必填项`
357
373
  }));
358
- let u = e.show;
359
- return u === void 0 && (u = !0), u || delete i[e.field], v(w.runtimeDoms.Item, null, {
374
+ let m = e.show;
375
+ return m === void 0 && (m = !0), m || delete s[e.field], d(h.runtimeDoms.Item, null, {
360
376
  default() {
361
- return C(v(w.runtimeDoms.FormItem, {
377
+ return v(d(h.runtimeDoms.FormItem, {
362
378
  label: `${e.label}:`,
363
379
  rules: e.rules,
364
- field: o
380
+ field: i
365
381
  }, {
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]]);
382
+ default: () => [d(n, R({
383
+ modelValue: s[e.field],
384
+ "onUpdate:modelValue": (p) => s[e.field] = p,
385
+ placeholder: P
386
+ }, f), null)]
387
+ }), [[O, m]]);
372
388
  }
373
389
  });
374
390
  }
375
391
  runtimeGroupProcessor(e) {
376
392
  let t;
377
- return v(w.runtimeDoms.Group, {
393
+ return d(h.runtimeDoms.Group, {
378
394
  schema: e
379
- }, D(t = e.children.map((i) => this.runtimeItemProcessor(i))) ? t : {
395
+ }, A(t = e.children.map((s) => this.runtimeItemProcessor(s))) ? t : {
380
396
  default: () => [t]
381
397
  });
382
398
  }
383
399
  addListItem(e) {
384
- var t, i;
385
- if (!((t = this.processors.rawModel[e.field]) != null && t[0]))
400
+ var t, s;
401
+ if (!((t = this.processor.stableModel[e.field]) != null && t[0]))
386
402
  return Promise.reject({
387
403
  code: "0001",
388
404
  message: "异步默认值数据正在处理中,请您耐心等待... "
389
405
  });
390
- (i = this.processors.rawModel[e.field]) != null && i[0] && this.model.value[e.field].push(A(this.processors.rawModel[e.field][0]));
406
+ (s = this.processor.stableModel[e.field]) != null && s[0] && this.model.value[e.field].push(V(this.processor.stableModel[e.field][0]));
391
407
  }
392
408
  deleteListItem(e, t) {
393
409
  this.model.value[e.field].splice(t, 1);
394
410
  }
395
411
  runtimeListProcessor(e) {
396
412
  const t = this;
397
- return v(w.runtimeDoms.List, {
413
+ return t.model.value[e.field] || (t.model.value[e.field] = [{}]), d(h.runtimeDoms.List, {
398
414
  schema: e
399
415
  }, {
400
416
  default() {
401
- return t.model.value[e.field].map((i, f) => v(w.runtimeDoms.ListItem, null, {
417
+ return t.model.value[e.field].map((s, r) => d(h.runtimeDoms.ListItem, null, {
402
418
  default() {
403
- return e.children.map((o, s) => t.runtimeItemProcessor(o, s, i, e));
419
+ return e.children.map((i, n) => t.runtimeItemProcessor(i, n, s, e));
404
420
  },
405
421
  delete({
406
- container: o
422
+ container: i
407
423
  } = {}) {
408
- 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]]);
424
+ var c;
425
+ let n = i ?? d("button", null, null);
426
+ return v(d(n, {
427
+ onClick: () => t.deleteListItem(e, r)
428
+ }, null), [[O, ((c = t.model.value[e.field]) == null ? void 0 : c.length) > 1]]);
413
429
  }
414
430
  }));
415
431
  },
416
432
  add({
417
- container: i
433
+ container: s
418
434
  } = {}) {
419
- let f = i ?? v("button", null, [$("添加")]);
420
- return v(f, {
435
+ let r = s ?? d("button", null, [k("添加")]);
436
+ return d(r, {
421
437
  onClick: () => t.addListItem(e)
422
438
  }, null);
423
439
  }
@@ -428,96 +444,85 @@ class H {
428
444
  }
429
445
  exec() {
430
446
  let e;
431
- return v(w.runtimeDoms.Form, {
447
+ return d(h.runtimeDoms.Form, {
432
448
  ref: this.formRef,
433
449
  model: this.model.value
434
- }, D(e = this.runtimeProcessor(this.schemas.value)) ? e : {
450
+ }, A(e = this.runtimeProcessor(this.schemas.value)) ? e : {
435
451
  default: () => [e]
436
452
  });
437
453
  }
438
454
  }
439
- class w {
440
- }
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
+ class h {
455
456
  }
456
- const F = class F {
457
+ o(h, "runtimeDoms");
458
+ const y = class y {
457
459
  static getPlaceholderPrefixPresetByComponentName() {
458
460
  const e = {
459
461
  请选择: ["Select", "Tree", "TreeSelect"],
460
462
  请输入: ["Input"]
461
463
  }, t = {};
462
- for (let i in e)
463
- e[i].forEach((f) => {
464
- t[f] = i;
464
+ for (let s in e)
465
+ e[s].forEach((r) => {
466
+ t[r] = s;
465
467
  });
466
468
  return t;
467
469
  }
468
470
  };
469
- d(F, "schemaPreset", {
471
+ o(y, "schemaPreset", {
470
472
  type: {
471
- defaultValueWhenAsync: "item"
473
+ defaultValue: "item"
472
474
  },
473
475
  component: {
474
- defaultValueWhenAsync: void 0
476
+ defaultValue: void 0
475
477
  },
476
478
  componentProps: {
477
- defaultValueWhenAsync: void 0
479
+ defaultValue: void 0
478
480
  },
479
481
  defaultValue: {
480
- defaultValueWhenAsync: void 0
482
+ defaultValue: void 0
481
483
  },
482
484
  label: {
483
- defaultValueWhenAsync: ""
485
+ defaultValue: ""
484
486
  },
485
487
  field: {
486
- defaultValueWhenAsync: "__yiwwhl_async_field_fallback"
488
+ defaultValue: "__yiwwhl_async_field_fallback"
487
489
  },
488
490
  rules: {
489
- defaultValueWhenAsync: []
491
+ defaultValue: []
490
492
  },
491
493
  show: {
492
- defaultValueWhenAsync: !0
494
+ defaultValue: !0
493
495
  },
494
496
  required: {
495
- defaultValueWhenAsync: !1
497
+ defaultValue: !1
496
498
  },
497
499
  placeholder: {
498
- defaultValueWhenAsync: void 0
500
+ defaultValue: void 0
501
+ },
502
+ children: {
503
+ defaultValue: []
499
504
  }
500
- }), d(F, "componentPropsPreset", {
505
+ }), o(y, "componentPropsPreset", {
501
506
  options: {
502
- defaultValueWhenAsync: []
507
+ defaultValue: []
503
508
  }
504
509
  }), // 基于基本功能提出基本预设
505
- d(F, "placeholderPresetByComponentName", F.getPlaceholderPrefixPresetByComponentName());
506
- let b = F;
507
- const Q = /* @__PURE__ */ T({
510
+ o(y, "placeholderPresetByComponentName", y.getPlaceholderPrefixPresetByComponentName());
511
+ let j = y;
512
+ const $ = /* @__PURE__ */ F({
508
513
  props: {
509
514
  setup: {
510
515
  type: Function,
511
516
  required: !0
512
517
  }
513
518
  },
514
- setup(c) {
515
- const e = new H(c.setup);
519
+ setup(l) {
520
+ const e = new T(l.setup);
516
521
  return () => e.exec();
517
522
  }
518
523
  });
519
- function X(c) {
520
- const e = new U(c);
524
+ function q(l) {
525
+ const e = new B(l);
521
526
  return [
522
527
  e.setup.bind(e),
523
528
  {
@@ -525,15 +530,15 @@ function X(c) {
525
530
  }
526
531
  ];
527
532
  }
528
- function Y(c) {
533
+ function G(l) {
529
534
  return {
530
535
  install() {
531
- w.runtimeDoms = c;
536
+ h.runtimeDoms = l;
532
537
  }
533
538
  };
534
539
  }
535
540
  export {
536
- Q as ProForm,
537
- X as useForm,
538
- Y as useFormRenderer
541
+ $ as ProForm,
542
+ q as useForm,
543
+ G as useFormRenderer
539
544
  };
@@ -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,36 @@
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
+ baseDefaultValueFunctionsLength: number;
23
+ constructor(runtimeCore: RuntimeCore);
24
+ parse<T extends object = any>(data: T[], parentMeta?: AnyObject): void;
25
+ initSchemas(schemas: AnyObject[]): AnyObject[];
26
+ countFunctionDefaultValues(input: AnyObject): number;
27
+ parseSchemas(schemas: ProxyedSchema[], parentMeta?: AnyObject): void;
28
+ parseStable(stable: AnyObject): AnyObject;
29
+ stableUpdater(parseProcess?: boolean[]): void;
30
+ parseItem(data: AnyObject, index: number, parentMeta?: AnyObject): void;
31
+ objectParser(root: ObjectParserRoot): void;
32
+ promiseFieldParser(rootField: any, updater: AnyFunction, deepProcess: boolean): void;
33
+ fieldParser(rootField: any, updater: AnyFunction, deepProcess?: boolean): void;
34
+ modelProcessor(schemas: Schema[]): void;
35
+ createModel(schema: AnyObject, baseModel: AnyObject): void;
36
+ }
@@ -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.8",
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
- }