@harbor-design/proform 1.0.5 → 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 b=Object.defineProperty;var k=(u,e,t)=>e in u?b(u,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):u[e]=t;var m=(u,e,t)=>(k(u,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("vue");class R{constructor(e){m(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 a{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,f]of i)o.set(t(s),t(f));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 c=Object.create(Object.getPrototypeOf(i));e.set(i,c);for(let o in i)i.hasOwnProperty(o)&&(c[o]=t(i[o]));return c}return t(u)}class W{constructor(e,t){m(this,"rawSchemas",[]);m(this,"rawModel",{});m(this,"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}});m(this,"componentPropsPreset",{options:{defaultValueWhenAsync:[]}});m(this,"uniqueEffectMap",{});m(this,"schemaEffect",new F);m(this,"modelEffect",new F);m(this,"stopWatchEffect",new F);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,c,o){for(let s=0;s<e.length;s++){let f=e[s];this.schemaProcessor(f,s,(p,v,y)=>{y?i[s][y]=p[y]:(t[s]=p,this.modelProcessor(p,c&&this.processedModel.value[c][0]),(!i[s]||v)&&(i[s]=V(p)),this.schemaEffect.triggerEffects(),this.modelEffect.triggerEffects())},o,c)}}schemaProcessor(e,t,i,c,o){const s={},f=this;function p(v=!1){var y,w;if(s.componentProps){const h={};f.propsProcessor(s.componentProps,f.componentPropsPreset,h,E=>{s.componentProps=h,i({...s},E,"componentProps")},t,c);return}if(s.children){f.processedSchemas.value[t]=s,f.rawSchemas[t]=s,i({...s},v),f.schemaAnalyzer(s.children,(y=f.processedSchemas.value[t])==null?void 0:y.children,(w=f.rawSchemas[t])==null?void 0:w.children,s.field,t);return}i({...s},v),f.rawModel=V(f.processedModel.value)}this.propsProcessor(e,this.schemaPreset,s,p,t,c,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,c,o,s,f){const p=Object.keys(e),v=Array.from({length:p.length}).fill(!1);function y(){return v.every(w=>w)}for(let w=0;w<p.length;w++){const h=p[w],E=e[h];if(a.isFunction(E)){const P=E(this.runtimeMeta());h!=="defaultValue"?this.schemaEffect.trackEffect(()=>{let n=E(this.runtimeMeta());n instanceof Promise?n.then(r=>{s===void 0?a.isFunction(r)||(typeof r=="string"&&r.includes("undefined")&&(r=r.replace(/undefined/g,"")),this.processedSchemas.value[o][h]=r):a.isFunction(r)||(typeof r=="string"&&r.includes("undefined")&&(r=r.replace(/undefined/g,"")),this.processedSchemas.value[s].children[o][h]=r)}):s===void 0?a.isFunction(n)||(typeof n=="string"&&n.includes("undefined")&&(n=n.replace(/undefined/g,"")),this.processedSchemas.value[o][h]=n):a.isFunction(n)||(typeof n=="string"&&n.includes("undefined")&&(n=n.replace(/undefined/g,"")),this.processedSchemas.value[s].children[o][h]=n)}):this.modelEffect.trackEffect(()=>{let n=E(this.runtimeMeta());this.stopWatchEffect.trackEffect(l.watchEffect(()=>{if(n=E(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||f===void 0){if(a.isFunction(e.field)){const d=e.field(this.runtimeMeta());d instanceof Promise?d.then(g=>{this.processedModel.value[g]=r}):this.processedModel.value[d]=r;return}this.processedModel.value[e.field]=r}else{if(a.isFunction(e.field)){const d=e.field(this.runtimeMeta());d instanceof Promise?d.then(g=>{this.processedModel.value[f][o][g]=r}):this.processedModel.value[f][o]&&(this.processedModel.value[f][o][d]=r);return}this.processedModel.value[f]&&this.processedModel.value[f].forEach(d=>{d[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,""),a.isFunction(e.field)){const r=e.field(this.runtimeMeta());r instanceof Promise?r.then(d=>{this.processedModel.value[d]=n}):this.processedModel.value[r]=n;return}if(this.processedModel.value[e.field]=n,o===void 0||f===void 0){if(a.isFunction(e.field)){const r=e.field(this.runtimeMeta());r instanceof Promise?r.then(d=>{this.processedModel.value[d]=n}):this.processedModel.value[r]=n;return}this.processedModel.value[e.field]=n}else for(let r=0;r<this.processedModel.value[f].length;r++){const d=this.processedModel.value[f][r];if(!a.isFunction(e.field))d[e.field]=n;else{const g=e.field(this.runtimeMeta());g instanceof Promise?g.then(S=>{d[S]=n}):d[g]=n;return}}this.rawModel=V(this.processedModel.value),this.modelEffect.clearEffects()}}))}),P instanceof Promise?(v[w]=!0,i[h]=t[h].defaultValueWhenAsync,y()&&c(),P.then(n=>{v[w]=!0,i[h]=n,y()&&c(!0)})):(v[w]=!0,i[h]=P,y()&&c())}else v[w]=!0,l.isRef(E)?l.watch(()=>E.value,P=>{i[h]=P,y()&&c()},{immediate:!0,deep:!0}):l.isReactive(E)?l.watch(()=>E,P=>{i[h]=P,y()&&c()},{immediate:!0,deep:!0}):(i[h]=E,y()&&c())}}modelProcessor(e,t=this.processedModel.value){if(a.isListSchema(e)){if(a.isFunction(e.field))return;t[e.field]||(t[e.field]=[{}]),e.children.forEach(i=>{this.modelProcessor(i,t[e.field][0])});return}if(a.isGroupSchema(e)){e.children.forEach(i=>{this.modelProcessor(i,t)});return}if(a.isItemSchema(e)){if(a.isFunction(e.field)||a.isUndefined(e.field)||!Number.isNaN(Number(e.field))||a.isFunction(e.defaultValue))return;t[e.field]=e.defaultValue}}}function A(u){return typeof u=="function"||Object.prototype.toString.call(u)==="[object Object]"&&!l.isVNode(u)}class N{constructor(e){m(this,"schemas",l.ref([]));m(this,"model",l.ref({}));m(this,"processorBySchemaType",{item:this.runtimeItemProcessor.bind(this),group:this.runtimeGroupProcessor.bind(this),list:this.runtimeListProcessor.bind(this)});m(this,"formRef",l.ref(null));this.setup=e,this.processors=new W(this.schemas,this.model),this.analyze(this.setup(this))}analyze(e){this.processors.schemaAnalyzer(e.schemas)}runtimeItemProcessor(e,t,i=this.model.value,c){const o=c?`${c.field}.${t}.${e.field}`:e.field,s=l.toRaw(e.component),f=e.componentProps??{};let p=e.show;return p===void 0&&(p=!0),p||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":v=>i[e.field]=v},f),null)]}),[[l.vShow,p]])}})}runtimeGroupProcessor(e){let t;return l.createVNode(M.runtimeDoms.Group,{schema:e},A(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,c)=>l.createVNode(M.runtimeDoms.ListItem,null,{default(){return e.children.map((o,s)=>t.runtimeItemProcessor(o,s,i,e))},delete({container:o}={}){var f;let s=o??l.createVNode("button",null,null);return l.withDirectives(l.createVNode(s,{onClick:()=>t.deleteListItem(e,c)},null),[[l.vShow,((f=t.model.value[e.field])==null?void 0:f.length)>1]])}}))},add({container:i}={}){let c=i??l.createVNode("button",null,[l.createTextVNode("添加")]);return l.createVNode(c,{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},A(e=this.runtimeProcessor(this.schemas.value))?e:{default:()=>[e]})}}class M{}m(M,"runtimeDoms");class F{constructor(){m(this,"effects",new Set)}clearEffects(){this.effects.clear()}triggerEffects(){Array.from(this.effects).forEach(e=>e())}trackEffect(e){this.effects.add(e)}}const D=l.defineComponent({props:{setup:{type:Function,required:!0}},setup(u){const e=new N(u.setup);return()=>e.exec()}});function C(u){const e=new R(u);return[e.setup.bind(e),{submit:e.submit.bind(e)}]}function _(u){return{install(){M.runtimeDoms=u}}}exports.ProForm=D;exports.useForm=C;exports.useFormRenderer=_;
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 D = Object.defineProperty;
2
- var _ = (c, e, t) => e in c ? D(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t;
3
- var h = (c, e, t) => (_(c, typeof e != "symbol" ? e + "" : e, t), t);
4
- import { toRaw as S, watch as F, watchEffect as j, isRef as L, isReactive as z, ref as b, createVNode as M, withDirectives as V, mergeProps as N, vShow as W, createTextVNode as G, isVNode as U, defineComponent as $ } from "vue";
5
- class B {
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
- h(this, "runtimeCore");
7
+ c(this, "runtimeCore");
8
8
  this.formCustomization = e;
9
9
  }
10
10
  // happy path, 后续可以完善更多的 fallback 处理,fallback 处理是为了不卡住异步时的首次渲染做的优化
@@ -18,15 +18,15 @@ class B {
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
- S(this.runtimeCore.processors.processedModel.value)
23
+ S(this.runtimeCore.processor.processedModel.value)
24
24
  )
25
25
  ));
26
26
  });
27
27
  }
28
28
  }
29
- class d {
29
+ class f {
30
30
  static typeChecker(e) {
31
31
  return {}.toString.call(e);
32
32
  }
@@ -34,11 +34,16 @@ class d {
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,387 +63,363 @@ class d {
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;
97
122
  }
98
- return t(c);
123
+ return t(o);
99
124
  }
100
- class O {
101
- constructor(e, t) {
102
- h(this, "rawSchemas", []);
103
- h(this, "rawModel", {});
104
- h(this, "schemaPreset", {
105
- type: {
106
- defaultValueWhenAsync: "item"
107
- },
108
- component: {
109
- defaultValueWhenAsync: void 0
110
- },
111
- componentProps: {
112
- defaultValueWhenAsync: void 0
113
- },
114
- defaultValue: {
115
- defaultValueWhenAsync: void 0
116
- },
117
- label: {
118
- defaultValueWhenAsync: ""
119
- },
120
- field: {
121
- defaultValueWhenAsync: "__yiwwhl_async_field_fallback"
122
- },
123
- rules: {
124
- defaultValueWhenAsync: []
125
- },
126
- show: {
127
- defaultValueWhenAsync: !0
128
- }
129
- });
130
- h(this, "componentPropsPreset", {
131
- options: {
132
- defaultValueWhenAsync: []
133
- }
134
- });
135
- h(this, "uniqueEffectMap", {});
136
- h(this, "schemaEffect", new k());
137
- h(this, "modelEffect", new k());
138
- h(this, "stopWatchEffect", new k());
139
- this.processedSchemas = e, this.processedModel = t, F(
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);
137
+ }
138
+ }
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(
140
154
  () => this.processedModel.value,
141
155
  () => {
142
- this.schemaEffect.triggerEffects(), this.modelEffect.triggerEffects();
156
+ this.schemaEffect.triggerEffects();
143
157
  },
144
158
  {
145
159
  deep: !0
146
160
  }
147
161
  );
148
162
  }
149
- schemaAnalyzer(e, t = this.processedSchemas.value, i = this.rawSchemas, f, o) {
150
- for (let s = 0; s < e.length; s++) {
151
- let l = e[s];
152
- this.schemaProcessor(
153
- l,
154
- s,
155
- (m, y, p) => {
156
- p ? i[s][p] = m[p] : (t[s] = m, this.modelProcessor(
157
- m,
158
- f && this.processedModel.value[f][0]
159
- ), (!i[s] || y) && (i[s] = A(m)), this.schemaEffect.triggerEffects(), this.modelEffect.triggerEffects());
160
- },
161
- o,
162
- f
163
- );
163
+ // 核心处理过程,接收一个初始的代理数据结构,由其衍生的有 parseSchema
164
+ parse(e, t) {
165
+ e.forEach((s, r) => {
166
+ this.parseItem(s, r, t);
167
+ });
168
+ }
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
+ });
175
+ }
176
+ // 派生过程,用于外部应用
177
+ parseSchemas(e, t) {
178
+ f.isArrayEmpty(this.processedSchemas.value) && (this.processedSchemas.value = this.initSchemas(e)), this.parse(e, t);
179
+ }
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;
187
+ }
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));
164
195
  }
165
196
  }
166
- schemaProcessor(e, t, i, f, o) {
167
- const s = {}, l = this;
168
- function m(y = !1) {
169
- var p, E;
170
- if (s.componentProps) {
171
- const a = {};
172
- l.propsProcessor(
173
- s.componentProps,
174
- l.componentPropsPreset,
175
- a,
176
- (v) => {
177
- s.componentProps = a, i({ ...s }, v, "componentProps");
178
- },
179
- t,
180
- f
181
- );
182
- return;
183
- }
184
- if (s.children) {
185
- l.processedSchemas.value[t] = s, l.rawSchemas[t] = s, i({ ...s }, y), l.schemaAnalyzer(
186
- s.children,
187
- // @ts-expect-error 此处已经守卫为非 ItemSchema
188
- (p = l.processedSchemas.value[t]) == null ? void 0 : p.children,
189
- // @ts-expect-error 此处已经守卫为非 ItemSchema
190
- (E = l.rawSchemas[t]) == null ? void 0 : E.children,
191
- s.field,
192
- t
193
- );
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)
194
206
  return;
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);
195
215
  }
196
- i({ ...s }, y), l.rawModel = A(l.processedModel.value);
197
216
  }
198
- this.propsProcessor(
199
- e,
200
- this.schemaPreset,
201
- s,
202
- m,
203
- t,
204
- f,
205
- o
206
- );
207
217
  }
208
- replaceFunctionsWithUndefined(e) {
209
- if (typeof e != "object" || e === null)
210
- return e;
211
- for (let t in e)
212
- if (e.hasOwnProperty(t)) {
213
- let i = e[t];
214
- typeof i == "function" || typeof i == "object" && this.replaceFunctionsWithUndefined(i);
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);
215
264
  }
216
- return e;
217
- }
218
- runtimeMeta() {
219
- return {
220
- model: this.replaceFunctionsWithUndefined(
221
- S(A(this.processedModel.value))
222
- )
223
- };
224
- }
225
- propsProcessor(e, t, i, f, o, s, l) {
226
- const m = Object.keys(e), y = Array.from({
227
- length: m.length
228
- }).fill(!1);
229
- function p() {
230
- return y.every((E) => E);
231
- }
232
- for (let E = 0; E < m.length; E++) {
233
- const a = m[E], v = e[a];
234
- if (d.isFunction(v)) {
235
- const P = v(this.runtimeMeta());
236
- a !== "defaultValue" ? this.schemaEffect.trackEffect(() => {
237
- let n = v(this.runtimeMeta());
238
- n instanceof Promise ? n.then((r) => {
239
- s === void 0 ? d.isFunction(r) || (typeof r == "string" && r.includes("undefined") && (r = r.replace(/undefined/g, "")), this.processedSchemas.value[o][a] = r) : d.isFunction(r) || (typeof r == "string" && r.includes("undefined") && (r = r.replace(/undefined/g, "")), this.processedSchemas.value[s].children[o][a] = r);
240
- }) : s === void 0 ? d.isFunction(n) || (typeof n == "string" && n.includes("undefined") && (n = n.replace(/undefined/g, "")), this.processedSchemas.value[o][a] = n) : d.isFunction(n) || (typeof n == "string" && n.includes("undefined") && (n = n.replace(/undefined/g, "")), this.processedSchemas.value[s].children[o][a] = n);
241
- }) : this.modelEffect.trackEffect(() => {
242
- let n = v(this.runtimeMeta());
243
- this.stopWatchEffect.trackEffect(
244
- j(() => {
245
- if (n = v(this.runtimeMeta()), n instanceof Promise)
246
- n.then((r) => {
247
- if (typeof r == "string" && !r.includes("undefined") ? this.stopWatchEffect.triggerEffects() : r = r.replace(/undefined/g, ""), o === void 0 || l === void 0) {
248
- if (d.isFunction(e.field)) {
249
- const u = e.field(
250
- this.runtimeMeta()
251
- );
252
- u instanceof Promise ? u.then((g) => {
253
- this.processedModel.value[g] = r;
254
- }) : this.processedModel.value[u] = r;
255
- return;
256
- }
257
- this.processedModel.value[e.field] = r;
258
- } else {
259
- if (d.isFunction(e.field)) {
260
- const u = e.field(
261
- this.runtimeMeta()
262
- );
263
- u instanceof Promise ? u.then((g) => {
264
- this.processedModel.value[l][o][g] = r;
265
- }) : this.processedModel.value[l][o] && (this.processedModel.value[l][o][u] = r);
266
- return;
267
- }
268
- this.processedModel.value[l] && this.processedModel.value[l].forEach(
269
- // @ts-expect-error
270
- (u) => {
271
- u[e.field] = r;
272
- }
273
- );
274
- }
275
- this.rawModel = A(this.processedModel.value), this.modelEffect.clearEffects();
276
- });
277
- else {
278
- if (typeof n == "string" && !n.includes("undefined") ? this.stopWatchEffect.triggerEffects() : n = n.replace(/undefined/g, ""), d.isFunction(e.field)) {
279
- const r = e.field(this.runtimeMeta());
280
- r instanceof Promise ? r.then((u) => {
281
- this.processedModel.value[u] = n;
282
- }) : this.processedModel.value[r] = n;
283
- return;
284
- }
285
- if (this.processedModel.value[e.field] = n, o === void 0 || l === void 0) {
286
- if (d.isFunction(e.field)) {
287
- const r = e.field(this.runtimeMeta());
288
- r instanceof Promise ? r.then((u) => {
289
- this.processedModel.value[u] = n;
290
- }) : this.processedModel.value[r] = n;
291
- return;
292
- }
293
- this.processedModel.value[e.field] = n;
294
- } else
295
- for (let r = 0; r < this.processedModel.value[l].length; r++) {
296
- const u = this.processedModel.value[l][r];
297
- if (!d.isFunction(e.field))
298
- u[e.field] = n;
299
- else {
300
- const g = e.field(
301
- this.runtimeMeta()
302
- );
303
- g instanceof Promise ? g.then((C) => {
304
- u[C] = n;
305
- }) : u[g] = n;
306
- return;
307
- }
308
- }
309
- this.rawModel = A(this.processedModel.value), this.modelEffect.clearEffects();
310
- }
311
- })
312
- );
313
- }), P instanceof Promise ? (y[E] = !0, i[a] = t[a].defaultValueWhenAsync, p() && f(), P.then((n) => {
314
- y[E] = !0, i[a] = n, p() && f(!0);
315
- })) : (y[E] = !0, i[a] = P, p() && f());
316
- } else
317
- y[E] = !0, L(v) ? F(
318
- () => v.value,
319
- (P) => {
320
- i[a] = P, p() && f();
321
- },
322
- {
323
- immediate: !0,
324
- deep: !0
325
- }
326
- ) : z(v) ? F(
327
- () => v,
328
- (P) => {
329
- i[a] = P, p() && f();
330
- },
331
- {
332
- immediate: !0,
333
- deep: !0
334
- }
335
- ) : (i[a] = v, p() && f());
336
- }
265
+ });
337
266
  }
338
- modelProcessor(e, t = this.processedModel.value) {
339
- if (d.isListSchema(e)) {
340
- if (d.isFunction(e.field))
341
- return;
342
- t[e.field] || (t[e.field] = [{}]), e.children.forEach((i) => {
343
- this.modelProcessor(i, t[e.field][0]);
344
- });
345
- return;
346
- }
347
- if (d.isGroupSchema(e)) {
348
- e.children.forEach((i) => {
349
- this.modelProcessor(i, t);
350
- });
351
- return;
352
- }
353
- if (d.isItemSchema(e)) {
354
- if (d.isFunction(e.field) || d.isUndefined(e.field) || !Number.isNaN(Number(e.field)) || d.isFunction(e.defaultValue))
355
- return;
356
- t[e.field] = e.defaultValue;
357
- }
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);
358
324
  }
359
325
  }
360
- function R(c) {
361
- return typeof c == "function" || Object.prototype.toString.call(c) === "[object Object]" && !U(c);
326
+ function x(o) {
327
+ return typeof o == "function" || Object.prototype.toString.call(o) === "[object Object]" && !B(o);
362
328
  }
363
- class T {
329
+ class _ {
364
330
  constructor(e) {
365
- h(this, "schemas", b([]));
366
- h(this, "model", b({}));
367
- h(this, "processorBySchemaType", {
331
+ c(this, "schemas", I([]));
332
+ c(this, "model", I({}));
333
+ c(this, "processorBySchemaType", {
368
334
  item: this.runtimeItemProcessor.bind(this),
369
335
  group: this.runtimeGroupProcessor.bind(this),
370
336
  list: this.runtimeListProcessor.bind(this)
371
337
  });
372
- h(this, "formRef", b(null));
373
- this.setup = e, this.processors = new O(this.schemas, this.model), this.analyze(this.setup(this));
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);
374
342
  }
375
- analyze(e) {
376
- this.processors.schemaAnalyzer(e.schemas);
343
+ getRuntimeMeta() {
344
+ return {
345
+ model: S(V(this.model.value))
346
+ };
377
347
  }
378
- runtimeItemProcessor(e, t, i = this.model.value, f) {
379
- const o = f ? `${f.field}.${t}.${e.field}` : e.field, s = S(e.component), l = e.componentProps ?? {};
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({
357
+ required: !0,
358
+ message: `${e.label}是必填项`
359
+ }));
380
360
  let m = e.show;
381
- return m === void 0 && (m = !0), m || delete i[e.field], M(w.runtimeDoms.Item, null, {
361
+ return m === void 0 && (m = !0), m || delete s[e.field], d(h.runtimeDoms.Item, null, {
382
362
  default() {
383
- return V(M(w.runtimeDoms.FormItem, {
363
+ return O(d(h.runtimeDoms.FormItem, {
384
364
  label: `${e.label}:`,
385
365
  rules: e.rules,
386
- field: o
366
+ field: n
387
367
  }, {
388
- default: () => [M(s, N({
389
- modelValue: i[e.field],
390
- "onUpdate:modelValue": (y) => i[e.field] = y
391
- }, l), null)]
392
- }), [[W, m]]);
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]]);
393
374
  }
394
375
  });
395
376
  }
396
377
  runtimeGroupProcessor(e) {
397
378
  let t;
398
- return M(w.runtimeDoms.Group, {
379
+ return d(h.runtimeDoms.Group, {
399
380
  schema: e
400
- }, R(t = e.children.map((i) => this.runtimeItemProcessor(i))) ? t : {
381
+ }, x(t = e.children.map((s) => this.runtimeItemProcessor(s))) ? t : {
401
382
  default: () => [t]
402
383
  });
403
384
  }
404
385
  addListItem(e) {
405
- var t, i;
406
- 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]))
407
388
  return Promise.reject({
408
389
  code: "0001",
409
390
  message: "异步默认值数据正在处理中,请您耐心等待... "
410
391
  });
411
- (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]));
412
393
  }
413
394
  deleteListItem(e, t) {
414
395
  this.model.value[e.field].splice(t, 1);
415
396
  }
416
397
  runtimeListProcessor(e) {
417
398
  const t = this;
418
- return M(w.runtimeDoms.List, {
399
+ return t.model.value[e.field] || (t.model.value[e.field] = [{}]), d(h.runtimeDoms.List, {
419
400
  schema: e
420
401
  }, {
421
402
  default() {
422
- return t.model.value[e.field].map((i, f) => M(w.runtimeDoms.ListItem, null, {
403
+ return t.model.value[e.field].map((s, r) => d(h.runtimeDoms.ListItem, null, {
423
404
  default() {
424
- 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));
425
406
  },
426
407
  delete({
427
- container: o
408
+ container: n
428
409
  } = {}) {
429
410
  var l;
430
- let s = o ?? M("button", null, null);
431
- return V(M(s, {
432
- onClick: () => t.deleteListItem(e, f)
433
- }, null), [[W, ((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]]);
434
415
  }
435
416
  }));
436
417
  },
437
418
  add({
438
- container: i
419
+ container: s
439
420
  } = {}) {
440
- let f = i ?? M("button", null, [G("添加")]);
441
- return M(f, {
421
+ let r = s ?? d("button", null, [D("添加")]);
422
+ return d(r, {
442
423
  onClick: () => t.addListItem(e)
443
424
  }, null);
444
425
  }
@@ -449,45 +430,85 @@ class T {
449
430
  }
450
431
  exec() {
451
432
  let e;
452
- return M(w.runtimeDoms.Form, {
433
+ return d(h.runtimeDoms.Form, {
453
434
  ref: this.formRef,
454
435
  model: this.model.value
455
- }, R(e = this.runtimeProcessor(this.schemas.value)) ? e : {
436
+ }, x(e = this.runtimeProcessor(this.schemas.value)) ? e : {
456
437
  default: () => [e]
457
438
  });
458
439
  }
459
440
  }
460
- class w {
441
+ class h {
461
442
  }
462
- h(w, "runtimeDoms");
463
- class k {
464
- constructor() {
465
- h(this, "effects", /* @__PURE__ */ new Set());
466
- }
467
- clearEffects() {
468
- this.effects.clear();
443
+ c(h, "runtimeDoms");
444
+ const P = class P {
445
+ static getPlaceholderPrefixPresetByComponentName() {
446
+ const e = {
447
+ 请选择: ["Select", "Tree", "TreeSelect"],
448
+ 请输入: ["Input"]
449
+ }, t = {};
450
+ for (let s in e)
451
+ e[s].forEach((r) => {
452
+ t[r] = s;
453
+ });
454
+ return t;
469
455
  }
470
- triggerEffects() {
471
- Array.from(this.effects).forEach((e) => e());
456
+ };
457
+ c(P, "schemaPreset", {
458
+ type: {
459
+ defaultValue: "item"
460
+ },
461
+ component: {
462
+ defaultValue: void 0
463
+ },
464
+ componentProps: {
465
+ defaultValue: void 0
466
+ },
467
+ defaultValue: {
468
+ defaultValue: void 0
469
+ },
470
+ label: {
471
+ defaultValue: ""
472
+ },
473
+ field: {
474
+ defaultValue: "__yiwwhl_async_field_fallback"
475
+ },
476
+ rules: {
477
+ defaultValue: []
478
+ },
479
+ show: {
480
+ defaultValue: !0
481
+ },
482
+ required: {
483
+ defaultValue: !1
484
+ },
485
+ placeholder: {
486
+ defaultValue: void 0
487
+ },
488
+ children: {
489
+ defaultValue: []
472
490
  }
473
- trackEffect(e) {
474
- this.effects.add(e);
491
+ }), c(P, "componentPropsPreset", {
492
+ options: {
493
+ defaultValue: []
475
494
  }
476
- }
477
- const J = /* @__PURE__ */ $({
495
+ }), // 基于基本功能提出基本预设
496
+ c(P, "placeholderPresetByComponentName", P.getPlaceholderPrefixPresetByComponentName());
497
+ let j = P;
498
+ const z = /* @__PURE__ */ L({
478
499
  props: {
479
500
  setup: {
480
501
  type: Function,
481
502
  required: !0
482
503
  }
483
504
  },
484
- setup(c) {
485
- const e = new T(c.setup);
505
+ setup(o) {
506
+ const e = new _(o.setup);
486
507
  return () => e.exec();
487
508
  }
488
509
  });
489
- function K(c) {
490
- const e = new B(c);
510
+ function F(o) {
511
+ const e = new N(o);
491
512
  return [
492
513
  e.setup.bind(e),
493
514
  {
@@ -495,15 +516,15 @@ function K(c) {
495
516
  }
496
517
  ];
497
518
  }
498
- function Q(c) {
519
+ function G(o) {
499
520
  return {
500
521
  install() {
501
- w.runtimeDoms = c;
522
+ h.runtimeDoms = o;
502
523
  }
503
524
  };
504
525
  }
505
526
  export {
506
- J as ProForm,
507
- K as useForm,
508
- Q as useFormRenderer
527
+ z as ProForm,
528
+ F as useForm,
529
+ G as useFormRenderer
509
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
  }
@@ -0,0 +1,9 @@
1
+ import { AnyObject, ItemSchema } from "../types";
2
+ export default class Preset {
3
+ static schemaPreset: Record<keyof ItemSchema, any> & {
4
+ children: any;
5
+ };
6
+ static componentPropsPreset: AnyObject;
7
+ static placeholderPresetByComponentName: {};
8
+ static getPlaceholderPrefixPresetByComponentName(): {};
9
+ }
@@ -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,5 +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";
5
+ export { default as Preset } from "./Preset";
@@ -27,6 +27,8 @@ export interface ItemSchema {
27
27
  type?: "item";
28
28
  rules?: FieldRule[];
29
29
  show?: boolean;
30
+ required?: boolean;
31
+ placeholder?: string;
30
32
  label: string;
31
33
  field: string;
32
34
  component: DomType;
@@ -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.5",
3
+ "version": "1.0.7",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -19,16 +19,11 @@
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",
28
26
  "scripts": {
29
- "build": "vue-tsc && vite build",
30
- "prerelease": "pnpm build",
31
- "release": "pnpm prerelease && changeset && pnpm postrelease",
32
- "postrelease": "changeset version && changeset publish && git push --follow-tags"
27
+ "build": "vue-tsc && vite build"
33
28
  }
34
29
  }
package/CHANGELOG.md DELETED
@@ -1,31 +0,0 @@
1
- # @harbor-design/proform
2
-
3
- ## 1.0.5
4
-
5
- ### Patch Changes
6
-
7
- - update pack sourcecode
8
-
9
- ## 1.0.4
10
-
11
- ### Patch Changes
12
-
13
- - remove console
14
-
15
- ## 1.0.3
16
-
17
- ### Patch Changes
18
-
19
- - fix error dist bug
20
-
21
- ## 1.0.2
22
-
23
- ### Patch Changes
24
-
25
- - fix bugs of list item add error
26
-
27
- ## 1.0.1
28
-
29
- ### Patch Changes
30
-
31
- - ProForm Release 1.0.1
@@ -1,24 +0,0 @@
1
- import { Ref } from "vue";
2
- import { AnyObject, Schema, ItemSchema, 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 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
- }