@vtj/core 0.7.2 → 0.7.3

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
@@ -2,6 +2,6 @@
2
2
  * Copyright (c) 2024, VTJ.PRO All rights reserved.
3
3
  * @name @vtj/core
4
4
  * @author CHC chenhuachun1549@dingtalk.com
5
- * @version 0.7.1
5
+ * @version 0.7.2
6
6
  * @license <a href="https://vtj.pro/license.html">MIT License</a>
7
- */const k="0.7.1";class b{}function J(o){return{all:o=o||new Map,on:function(e,t){var i=o.get(e);i?i.push(t):o.set(e,[t])},off:function(e,t){var i=o.get(e);i&&(t?i.splice(i.indexOf(t)>>>0,1):o.set(e,[]))},emit:function(e,t){var i=o.get(e);i&&i.slice().map(function(s){s(t)}),(i=o.get("*"))&&i.slice().map(function(s){s(e,t)})}}}class B{listeners=[];isReady=!1;triggerReady(){this.isReady=!0;for(const e of this.listeners)e();this.listeners=[]}ready(e){this.isReady?e():this.listeners.push(e)}resetReady(){this.isReady=!1}}class x{constructor(e,t,i){this.name=e,this.value=t,this.defaultValue=i,this.setValue(t)}isUnset=!1;setValue(e){this.value=e,this.isUnset=this.value===this.defaultValue}getValue(){return this.value??this.defaultValue}static toDsl(e={}){return Object.entries(e).reduce((t,[i,s])=>(s.isUnset||(t[i]=s.getValue()),t),{})}static parse(e={}){return Object.entries(e).reduce((t,[i,s])=>(t[i]=new x(i,s),t),{})}}class N{constructor(e){this.schema=e;const{name:t,handler:i}=this.schema;this.name=t,this.handler=i,this.update(e)}name;handler;modifiers={};update(e){Object.assign(this.schema,e);const{handler:t,modifiers:i={}}=this.schema;this.handler=t,this.modifiers=i}static toDsl(e){return Object.entries(e).reduce((t,[i,s])=>{const{handler:r,modifiers:d}=s;return t[i]={name:i,handler:r,modifiers:d},t},{})}static parse(e={}){return Object.entries(e).reduce((t,[i,s])=>(t[i]=new N(s),t),{})}}class E{constructor(e){this.schema=e,this.id=e.id||h.uid(),this.update(e)}id;name="";arg;modifiers;value;iterator;update(e){Object.assign(this.schema,e);const{name:t,arg:i,modifiers:s,value:r,iterator:d}=this.schema;this.name=t,this.arg=i,this.modifiers=s,this.value=r,this.iterator=d}static parse(e=[]){return e.map(t=>new E(t))}static toDsl(e=[]){return e.map(t=>{const{name:i,arg:s,modifiers:r,value:d,iterator:c,id:m}=t;return{id:m,name:i,arg:s,modifiers:r,value:d,iterator:c}})}}const l="EVENT_NODE_CHANGE";class p{constructor(e,t=null){this.parent=t;const{id:i=h.uid(),name:s,from:r=""}=e;this.id=i,this.name=s,this.from=r,this.update(e,!0),p.nodes[this.id]=this}__VTJ_NODE__=!0;locked=!1;static nodes={};id;name;from;invisible=!1;children="";slot;props={};events={};directives=[];disposed=!1;update(e,t=!1){const{invisible:i=!1,locked:s=!1,children:r=[],slot:d,props:c={},events:m={},directives:_=[]}=e;this.invisible=i,this.locked=s,this.setChildren(r,!0),this.setSlot(d,!0),this.props=x.parse(c),this.events=N.parse(m),this.directives=E.parse(_),t||n.emit(l,this)}setChildren(e="",t=!1){Array.isArray(e)?this.children=e.map(i=>new p(i,this)):this.children=e,t||n.emit(l,this)}setSlot(e,t=!1){this.slot=typeof e=="string"?{name:e,params:[]}:e,t||n.emit(l,this)}setProp(e,t,i,s=!1){const r=this.props[e];r?r.setValue(t):this.props[e]=new x(e,t,i),s||n.emit(l,this)}removeProp(e,t=!1){delete this.props[e],t||n.emit(l,this)}getPropValue(e){const t=this.props[e];if(t)return t.getValue()}setEvent(e,t=!1){const i=this.events[e.name];i?i.update(e):this.events[e.name]=new N(e),t||n.emit(l,this)}removeEvent(e,t=!1){delete this.events[e],t||n.emit(l,this)}setDirective(e,t=!1){const i=this.directives.findIndex(s=>s.id===e.id);i>=0?this.directives.splice(i,1,new E(e)):this.directives.push(new E(e)),t||n.emit(l,this)}removeDirective(e,t=!1){const i=this.directives.findIndex(s=>s===e||s.id===e.id);i>=0&&this.directives.splice(i,1),t||n.emit(l,this)}removeChild(e,t=!1){const{children:i,disposed:s}=this;if(s||!Array.isArray(i))return;const r=i.findIndex(d=>d===e);e.parent=null,i.splice(r,1),t||n.emit(l,this)}appendChild(e,t=!1){const{children:i,disposed:s}=this;s||(e.parent=this,Array.isArray(i)?i.push(e):this.children=[e],t||n.emit(l,this))}insertAfter(e,t=!1){if(!this.parent)return;const i=this.parent.children;if(Array.isArray(i)){e.parent=this.parent;const s=i.indexOf(this);i.splice(s+1,0,e),t||n.emit(l,this.parent)}}insertBefore(e,t=!1){if(!this.parent)return;const i=this.parent.children;if(Array.isArray(i)){e.parent=this.parent;const s=i.indexOf(this);i.splice(s,0,e),t||n.emit(l,this.parent)}}movePrev(e=!1){const t=this.parent;if(!t)return;const i=t.children;if(Array.isArray(i)){const s=i.indexOf(this);s>0&&(i.splice(s,1),i.splice(s-1,0,this),e||n.emit(l,t))}}moveNext(e=!1){const t=this.parent;if(!t)return;const i=t.children;if(Array.isArray(i)){const s=i.indexOf(this);s>-1&&s<i.length-1&&(i.splice(s,1),i.splice(s+1,0,this),e||n.emit(l,t))}}toDsl(){const{id:e,name:t,from:i,invisible:s,locked:r,slot:d,children:c,props:m,directives:_,events:P}=this,v=Array.isArray(c)?c.map(H=>H.toDsl()):c;return{id:e,name:t,from:i,invisible:s,locked:r,slot:d,children:v,props:x.toDsl(m),directives:E.toDsl(_),events:N.toDsl(P)}}dispose(e=!1){const{children:t,disposed:i}=this;i||(Array.isArray(t)&&t.forEach(s=>s.dispose(!0)),this.parent?this.parent.removeChild(this,e):e||n.emit(l,this),this.parent=null,this.disposed=!0,delete p.nodes[this.id])}lock(e=!1){if(this.locked=!0,Array.isArray(this.children))for(const t of this.children)t.lock(!0);e||n.emit(l,this)}unlock(e=!1){if(this.locked=!1,Array.isArray(this.children))for(const t of this.children)t.unlock(!0);e||n.emit(l,this)}setVisible(e,t=!1){if(this.invisible=!e,Array.isArray(this.children))for(const i of this.children)i.setVisible(e,!0);t||n.emit(l,this)}isChild(e){let t=!1;if(Array.isArray(this.children)){for(const i of this.children)if(e===i||e.id===i.id){t=!0;break}else if(t=i.isChild(e),t)break}return t}}const a="EVENT_BLOCK_CHANGE";class u{__VTJ_BLOCK__=!0;id;name="";inject=[];state={};lifeCycles={};methods={};computed={};watch=[];css="";props=[];emits=[];slots=[];dataSources={};nodes=[];locked=!1;disposed=!1;static normalAttrs=["name","locked","inject","state","lifeCycles","methods","computed","watch","css","props","emits","slots","dataSources"];constructor(e){const{id:t}=e;this.id=t||h.uid(),this.update(e,!0)}update(e,t=!1){for(const s of u.normalAttrs){const r=e[s];r&&(this[s]=r)}const{nodes:i=[]}=e;i.length&&(this.nodes=i.map(s=>new p(s))),t||n.emit(a,this)}toDsl(){const{__VTJ_BLOCK__:e,id:t,nodes:i}=this;return{...u.normalAttrs.reduce((r,d)=>(r[d]=this[d],r),{}),__VTJ_BLOCK__:e,__VERSION__:h.timestamp().toString(),id:t,nodes:i.map(r=>r.toDsl())}}dispose(){this.nodes.map(e=>e.dispose(!0)),this.nodes=[],this.disposed=!0}setFunction(e,t,i,s=!1){this[e][t]=i,s||n.emit(a,this)}removeFunction(e,t,i=!1){delete this[e][t],i||n.emit(a,this)}setState(e,t,i=!1){this.state[e]=t,i||n.emit(a,this)}removeState(e,t=!1){delete this.state[e],t||n.emit(a,this)}setCss(e,t=!1){this.css=e,t||n.emit(a,this)}setWatch(e,t=!1){e.id=e.id||h.uid();const i=this.watch.findIndex(s=>s.id&&s.id===e.id||s===e);i>-1?this.watch.splice(i,1,e):this.watch.push(e),t||n.emit(a,this)}removeWatch(e,t=!1){const i=this.watch.findIndex(s=>s.id&&s.id===e.id||s===e);i>-1&&(this.watch.splice(i,1),t||n.emit(a,this))}setProp(e,t=!1){const i=this.props.findIndex(s=>typeof s=="string"?s===e.name:s.name===e.name);i>-1?this.props.splice(i,1,e):this.props.push(e),t||n.emit(a,this)}removeProp(e,t=!1){const i=this.props.findIndex(s=>typeof s=="string"?s===e.name:s.name===e.name);i>-1&&(this.props.splice(i,1),t||n.emit(a,this))}setEmit(e,t=!1){const i=this.emits.findIndex(s=>s===e);i>-1?this.emits.splice(i,1,e):this.emits.push(e),t||n.emit(a,this)}removeEmit(e,t=!1){const i=this.emits.findIndex(s=>s===e);i>-1&&(this.emits.splice(i,1),t||n.emit(a,this))}setSlot(e,t=!1){const i=this.slots.findIndex(s=>s===e);i>-1?this.slots.splice(i,1,e):this.slots.push(e),t||n.emit(a,this)}removeSlot(e,t=!1){const i=this.slots.findIndex(s=>s===e);i>-1&&(this.slots.splice(i,1),t||n.emit(a,this))}setInject(e,t=!1){const i=this.inject.findIndex(s=>s.name===e.name);i>-1?this.inject.splice(i,1,e):this.inject.push(e),t||n.emit(a,this)}removeInject(e,t=!1){const i=this.inject.findIndex(s=>s.name===e.name);i>-1&&(this.inject.splice(i,1),t||n.emit(a,this))}setDataSource(e,t=!1){this.dataSources[e.name]=e,t||n.emit(a,this)}removeDataSource(e,t=!1){delete this.dataSources[e],t||n.emit(a,this)}insertAfter(e,t,i=!1){e.parent=null;const s=this.nodes.indexOf(t);this.nodes.splice(s+1,0,e),i||n.emit(a,this)}insertBefore(e,t,i=!1){e.parent=null;const s=this.nodes.indexOf(t);this.nodes.splice(s,0,e),i||n.emit(a,this)}appendNode(e,t=!1){e.parent=null,this.nodes.push(e),t||n.emit(a,this)}addNode(e,t,i="inner",s=!1){t?["left","top"].includes(i)?t.parent?t.insertAfter(e,s):this.insertBefore(e,t,s):["right","bottom"].includes(i)?t.parent?t.insertAfter(e,s):this.insertAfter(e,t,s):t.appendChild(e,s):this.appendNode(e,s)}__removeNode(e,t=!1){const i=this.nodes.findIndex(s=>s.id===e.id);i>-1&&(this.nodes.splice(i,1),t||n.emit(a,this))}removeNode(e,t=!1){e.parent?e.dispose(t):(e.dispose(!0),this.__removeNode(e,t))}move(e,t,i="inner",s=!1){e.parent?e.parent.removeChild(e,!0):this.__removeNode(e,!0),this.addNode(e,t,i,s)}movePrev(e,t=!1){if(e.parent)e.movePrev(t);else{const i=this.nodes,s=i.indexOf(e);s>0&&(i.splice(s,1),i.splice(s-1,0,e),t||n.emit(a,this))}}moveNext(e,t=!1){if(e.parent)e.moveNext(t);else{const i=this.nodes,s=i.indexOf(e);s>-1&&s<i.length-1&&(i.splice(s,1),i.splice(s+1,0,e),t||n.emit(a,this))}}cloneNode(e,t=!1){const i=I(e.toDsl()),s=new p(i);return this.addNode(s,e,"bottom",t),s}lock(e=!1){this.locked=!0;for(const t of this.nodes)t.lock(!0);e||n.emit(a,this)}unlock(e=!1){this.locked=!1;for(const t of this.nodes)t.unlock(!0);e||n.emit(a,this)}isChild(e){let t=!1;for(const i of this.nodes)if(e===i||e.id===i.id){t=!0;break}else if(t=i.isChild(e),t)break;return t}}const f="EVENT_PROJECT_CHANGE",g="EVENT_PROJECT_ACTIVED",V="EVENT_PROJECT_DEPS_CHANGE",C="EVENT_PROJECT_PAGES_CHANGE",A="EVENT_PROJECT_BLOCKS_CHANGE",S="EVENT_PROJECT_APIS_CHANGE",R="EVENT_PROJECT_PUBLISH",D="EVENT_PROJECT_FILE_PUBLISH";class O{id="";name="";description="";homepage="";dependencies=[];pages=[];blocks=[];apis=[];currentFile=null;static attrs=["name","homepage","description","dependencies","pages","blocks","apis"];constructor(e){const{id:t}=e;this.id=t||h.uid(),this.update(e,!0)}update(e,t=!1){for(const i of O.attrs){const s=e[i];s&&(this[i]=s)}t||n.emit(f,{model:this,type:"update",data:e})}isPageFile(e){return e.type==="page"}toDsl(){const{id:e}=this,t=O.attrs.reduce((i,s)=>(i[s]=this[s],i),{});return t.pages&&(t.pages=t.pages.map(i=>(delete i.dsl,i))),t.blocks&&(t.blocks=t.blocks.map(i=>(delete i.dsl,i))),{__VTJ_PROJECT__:!0,__VERSION__:h.timestamp().toString(),id:e,...t}}active(e,t=!1){this.currentFile=e,t||n.emit(g,{model:this,type:"update",data:e})}deactivate(e=!1){this.currentFile=null,e||n.emit(g,{model:this,type:"update",data:null})}setDeps(e,t=!1){const i=this.dependencies,s=i.findIndex(d=>d.package===e.package);let r;if(s>-1?(r="update",i.splice(s,1,{...i[s],...e})):(r="create",i.push(e)),!t){const d={model:this,type:r,data:e};n.emit(V,d),n.emit(f,d)}}removeDeps(e,t=!1){const i=this.dependencies,s=i.findIndex(r=>r.package===e.package);if(s>-1&&i.splice(s,1),!t){const r={model:this,type:"delete",data:e};n.emit(V,r),n.emit(f,r)}}getPage(e){const t=(i,s=[])=>{for(const r of s){if(r.id===i)return r;if(r.children&&r.children.length){const d=t(i,r.children);if(d)return d}}};return t(e,this.pages)}getPages(){const e=(t=[])=>{let i=[];for(const s of t)s.dir?s.children&&s.children.length&&(i=i.concat(e(s.children))):i.push(s);return i};return e(this.pages)}createPage(e,t,i=!1){if(e.id=e.id||h.uid(),e.type="page",e.dir?e.children=[]:e.dsl=e.dsl||new u({id:e.id,name:h.upperFirstCamelCase(e.name)}).toDsl(),t){const s=this.getPage(t);s?s.children?s.children.push(e):s.children=[e]:console.warn(`not found PageFile for id: ${t} `)}else this.pages.push(e);if(this.currentFile||this.active(e,i),!i){const s={model:this,type:"create",data:e};n.emit(C,s),n.emit(f,s)}}updatePage(e,t=!1){const i=this.getPage(e.id);if(i?Object.assign(i,e):console.warn(`not found PageFile for id: ${e.id} `),!t){const s={model:this,type:"update",data:e};n.emit(C,s),n.emit(f,s)}}clonePage(e,t,i=!1){const s=h.uid(),r=`${e.name}Copy`,d=`${e.title}_副本`,c=new u({id:s,name:r}).toDsl(),m=h.merge({},e,{id:s,name:r,title:d,dsl:c}),_=t?this.getPage(t)?.children||[]:this.pages,P=_.findIndex(v=>v.id===e.id);if(_.splice(P+1,0,m),!i){const v={model:this,type:"clone",data:{page:e,newPage:m}};n.emit(C,v),n.emit(f,v)}}removePage(e,t=!1){const i=(s,r)=>{const d=r.findIndex(c=>c.id===s);if(d>=0){r.splice(d,1);return}for(const c of r)if(c.children&&c.children.length)return i(s,c.children)};if(i(e,this.pages),e===this.homepage&&(this.homepage=""),this.currentFile?.id===e&&this.deactivate(t),!t){const s={model:this,type:"delete",data:e};n.emit(C,s),n.emit(f,s)}}getBlock(e){return this.blocks.find(t=>t.id===e)}createBlock(e,t=!1){const i=e.id||h.uid(),s=h.upperFirstCamelCase(e.name);if(e.id=i,e.type="block",e.dsl=new u({id:i,name:s}).toDsl(),this.blocks.push(e),this.currentFile||this.active(e,t),!t){const r={model:this,type:"create",data:e};n.emit(A,r),n.emit(f,r)}}updateBlock(e,t=!1){const i=this.getBlock(e.id);if(i?(Object.assign(i,e),i.dsl&&(i.dsl.name=e.name)):console.warn(`not found PageFile for id: ${e.id} `),!t){const s={model:this,type:"update",data:e};n.emit(A,s),n.emit(f,s)}}removeBlock(e,t=!1){const i=this.blocks,s=i.findIndex(r=>r.id===e);if(s>-1?(i.splice(s,1),this.currentFile?.id===e&&this.deactivate(t)):console.warn(`not found PageFile for id: ${e} `),!t){const r={model:this,type:"delete",data:e};n.emit(A,r),n.emit(f,r)}}existBlockName(e,t=[]){return this.blocks.some(i=>i.name===e&&!t.includes(i.id))}existPageName(e,t=[]){return this.getPages().some(s=>s.name===e&&!t.includes(s.id))}setApi(e,t=!1){const i=this.apis.find(r=>r.name===e.name||r.id===e.id);let s;if(i?(s="update",Object.assign(i,e)):(s="create",e.id=h.uid(),this.apis.push(e)),!t){const r={model:this,type:s,data:e};n.emit(S,r),n.emit(f,r)}}removeApi(e,t=!1){const i=this.apis.findIndex(s=>s.name===e||s.id===e);if(i>-1?this.apis.splice(i,1):console.warn(`not found Api for name: ${e} `),!t){const s={model:this,type:"delete",data:e};n.emit(S,s),n.emit(f,s)}}existApiName(e,t=[]){return this.apis.some(i=>i.name===e&&!t.includes(i.id))}setHomepage(e,t=!1){if(this.homepage=e,!t){const i={model:this,type:"update",data:e};n.emit(f,i)}}publish(e){const t={model:this,type:"publish",data:e||this};e?n.emit(D,t):n.emit(R,t)}}const T="EVENT_HISTORY_CHANGE",y="EVENT_HISTORY_LOAD";class G{options={max:50};index=-1;id;items;constructor(e,t={}){Object.assign(this.options,t);const{id:i,items:s=[]}=e;this.id=i,this.items=s}toDsl(){const{id:e,items:t}=this;return{id:e,items:t.map(i=>({id:i.id,label:i.label}))}}get(e){return this.items.find(t=>t.id===e)}add(e,t=!1){const{max:i}=this.options,s={id:h.uid(),label:new Date().toLocaleString(),dsl:h.cloneDeep(e)};if(this.items.unshift(s),this.items.length>i){const r=this.items.splice(i);t||n.emit(T,{model:this,type:"delete",data:r.map(d=>d.id)})}this.index=-1,t||n.emit(T,{model:this,type:"create",data:s})}remove(e,t=!1){const i=this.items.findIndex(s=>s.id===e);i>-1?(this.items.splice(i,1),i===this.index?this.index=-1:this.index>=this.items.length&&(this.index=this.items.length-1)):console.warn(`not found HistoryItem for id: ${e} `),t||n.emit(T,{model:this,type:"delete",data:[e]})}forward(e=!1){const{index:t,items:i}=this;if(t<0)return;--this.index;const s=i[this.index];s&&!e&&n.emit(y,{model:this,type:"load",data:s})}backward(e=!1){const{index:t,items:i}=this;if(t>=i.length-1)return;t<0&&(this.index=0),++this.index;const s=i[this.index];s&&!e&&n.emit(y,{model:this,type:"load",data:s})}load(e,t=!1){const i=this.items.findIndex(s=>s.id===e);i>=0&&(this.index=i,t||n.emit(y,{model:this,type:"load",data:this.items[i]}))}clear(e=!1){this.index=-1;const t=this.items.map(i=>i.id);this.items=[],e||n.emit(T,{model:this,type:"clear",data:t})}}const n=J();function w(o){return o instanceof u}function L(o){return o instanceof p}function F(o){return!!o.__VTJ_BLOCK__}function I(o){return delete o.id,Array.isArray(o.children)&&(o.children=o.children.map(e=>I(e))),o}exports.Base=B;exports.BlockModel=u;exports.DirectiveModel=E;exports.EVENT_BLOCK_CHANGE=a;exports.EVENT_HISTORY_CHANGE=T;exports.EVENT_HISTORY_LOAD=y;exports.EVENT_NODE_CHANGE=l;exports.EVENT_PROJECT_ACTIVED=g;exports.EVENT_PROJECT_APIS_CHANGE=S;exports.EVENT_PROJECT_BLOCKS_CHANGE=A;exports.EVENT_PROJECT_CHANGE=f;exports.EVENT_PROJECT_DEPS_CHANGE=V;exports.EVENT_PROJECT_FILE_PUBLISH=D;exports.EVENT_PROJECT_PAGES_CHANGE=C;exports.EVENT_PROJECT_PUBLISH=R;exports.EventModel=N;exports.HistoryModel=G;exports.NodeModel=p;exports.ProjectModel=O;exports.PropModel=x;exports.Service=b;exports.VTJ_CORE_VERSION=k;exports.cloneDsl=I;exports.emitter=n;exports.isBlock=w;exports.isBlockSchema=F;exports.isNode=L;
7
+ */const H="0.7.2";class b{}function B(o){return{all:o=o||new Map,on:function(e,t){var i=o.get(e);i?i.push(t):o.set(e,[t])},off:function(e,t){var i=o.get(e);i&&(t?i.splice(i.indexOf(t)>>>0,1):o.set(e,[]))},emit:function(e,t){var i=o.get(e);i&&i.slice().map(function(s){s(t)}),(i=o.get("*"))&&i.slice().map(function(s){s(e,t)})}}}class J{listeners=[];isReady=!1;triggerReady(){this.isReady=!0;for(const e of this.listeners)e();this.listeners=[]}ready(e){this.isReady?e():this.listeners.push(e)}resetReady(){this.isReady=!1}}class x{constructor(e,t,i){this.name=e,this.value=t,this.defaultValue=i,this.setValue(t)}isUnset=!1;setValue(e){this.value=e,this.isUnset=this.value===this.defaultValue}getValue(){return this.value??this.defaultValue}static toDsl(e={}){return Object.entries(e).reduce((t,[i,s])=>(s.isUnset||(t[i]=s.getValue()),t),{})}static parse(e={}){return Object.entries(e).reduce((t,[i,s])=>(t[i]=new x(i,s),t),{})}}class N{constructor(e){this.schema=e;const{name:t,handler:i}=this.schema;this.name=t,this.handler=i,this.update(e)}name;handler;modifiers={};update(e){Object.assign(this.schema,e);const{handler:t,modifiers:i={}}=this.schema;this.handler=t,this.modifiers=i}static toDsl(e){return Object.entries(e).reduce((t,[i,s])=>{const{handler:r,modifiers:a}=s;return t[i]={name:i,handler:r,modifiers:a},t},{})}static parse(e={}){return Object.entries(e).reduce((t,[i,s])=>(t[i]=new N(s),t),{})}}class E{constructor(e){this.schema=e,this.id=e.id||h.uid(),this.update(e)}id;name="";arg;modifiers;value;iterator;update(e){Object.assign(this.schema,e);const{name:t,arg:i,modifiers:s,value:r,iterator:a}=this.schema;this.name=t,this.arg=i,this.modifiers=s,this.value=r,this.iterator=a}static parse(e=[]){return e.map(t=>new E(t))}static toDsl(e=[]){return e.map(t=>{const{name:i,arg:s,modifiers:r,value:a,iterator:c,id:f}=t;return{id:f,name:i,arg:s,modifiers:r,value:a,iterator:c}})}}const l="EVENT_NODE_CHANGE";class p{constructor(e,t=null){this.parent=t;const{id:i=h.uid(),name:s,from:r=""}=e;this.id=i,this.name=s,this.from=r,this.update(e,!0),p.nodes[this.id]=this}__VTJ_NODE__=!0;locked=!1;static nodes={};id;name;from;invisible=!1;children="";slot;props={};events={};directives=[];disposed=!1;update(e,t=!1){const{invisible:i=!1,locked:s=!1,children:r=[],slot:a,props:c={},events:f={},directives:_=[]}=e;this.invisible=i,this.locked=s,this.setChildren(r,!0),this.setSlot(a,!0),this.props=x.parse(c),this.events=N.parse(f),this.directives=E.parse(_),t||n.emit(l,this)}setChildren(e="",t=!1){Array.isArray(e)?this.children=e.map(i=>new p(i,this)):this.children=e,t||n.emit(l,this)}setSlot(e,t=!1){this.slot=typeof e=="string"?{name:e,params:[]}:e,t||n.emit(l,this)}setProp(e,t,i,s=!1){const r=this.props[e];r?r.setValue(t):this.props[e]=new x(e,t,i),s||n.emit(l,this)}removeProp(e,t=!1){delete this.props[e],t||n.emit(l,this)}getPropValue(e){const t=this.props[e];if(t)return t.getValue()}setEvent(e,t=!1){const i=this.events[e.name];i?i.update(e):this.events[e.name]=new N(e),t||n.emit(l,this)}removeEvent(e,t=!1){delete this.events[e],t||n.emit(l,this)}setDirective(e,t=!1){const i=this.directives.findIndex(s=>s.id===e.id);i>=0?this.directives.splice(i,1,new E(e)):this.directives.push(new E(e)),t||n.emit(l,this)}removeDirective(e,t=!1){const i=this.directives.findIndex(s=>s===e||s.id===e.id);i>=0&&this.directives.splice(i,1),t||n.emit(l,this)}removeChild(e,t=!1){const{children:i,disposed:s}=this;if(s||!Array.isArray(i))return;const r=i.findIndex(a=>a===e);e.parent=null,i.splice(r,1),t||n.emit(l,this)}appendChild(e,t=!1){const{children:i,disposed:s}=this;s||(e.parent=this,Array.isArray(i)?i.push(e):this.children=[e],t||n.emit(l,this))}insertAfter(e,t=!1){if(!this.parent)return;const i=this.parent.children;if(Array.isArray(i)){e.parent=this.parent;const s=i.indexOf(this);i.splice(s+1,0,e),t||n.emit(l,this.parent)}}insertBefore(e,t=!1){if(!this.parent)return;const i=this.parent.children;if(Array.isArray(i)){e.parent=this.parent;const s=i.indexOf(this);i.splice(s,0,e),t||n.emit(l,this.parent)}}movePrev(e=!1){const t=this.parent;if(!t)return;const i=t.children;if(Array.isArray(i)){const s=i.indexOf(this);s>0&&(i.splice(s,1),i.splice(s-1,0,this),e||n.emit(l,t))}}moveNext(e=!1){const t=this.parent;if(!t)return;const i=t.children;if(Array.isArray(i)){const s=i.indexOf(this);s>-1&&s<i.length-1&&(i.splice(s,1),i.splice(s+1,0,this),e||n.emit(l,t))}}toDsl(){const{id:e,name:t,from:i,invisible:s,locked:r,slot:a,children:c,props:f,directives:_,events:g}=this,v=Array.isArray(c)?c.map(D=>D.toDsl()):c;return{id:e,name:t,from:i,invisible:s,locked:r,slot:a,children:v,props:x.toDsl(f),directives:E.toDsl(_),events:N.toDsl(g)}}dispose(e=!1){const{children:t,disposed:i}=this;i||(Array.isArray(t)&&t.forEach(s=>s.dispose(!0)),this.parent?this.parent.removeChild(this,e):e||n.emit(l,this),this.parent=null,this.disposed=!0,delete p.nodes[this.id])}lock(e=!1){if(this.locked=!0,Array.isArray(this.children))for(const t of this.children)t.lock(!0);e||n.emit(l,this)}unlock(e=!1){if(this.locked=!1,Array.isArray(this.children))for(const t of this.children)t.unlock(!0);e||n.emit(l,this)}setVisible(e,t=!1){if(this.invisible=!e,Array.isArray(this.children))for(const i of this.children)i.setVisible(e,!0);t||n.emit(l,this)}isChild(e){let t=!1;if(Array.isArray(this.children)){for(const i of this.children)if(e===i||e.id===i.id){t=!0;break}else if(t=i.isChild(e),t)break}return t}}const d="EVENT_BLOCK_CHANGE";class u{__VTJ_BLOCK__=!0;id;name="";inject=[];state={};lifeCycles={};methods={};computed={};watch=[];css="";props=[];emits=[];slots=[];dataSources={};nodes=[];locked=!1;disposed=!1;static normalAttrs=["name","locked","inject","state","lifeCycles","methods","computed","watch","css","props","emits","slots","dataSources"];constructor(e){const{id:t}=e;this.id=t||h.uid(),this.update(e,!0)}update(e,t=!1){for(const s of u.normalAttrs){const r=e[s];r&&(this[s]=r)}const{nodes:i=[]}=e;i.length&&(this.nodes=i.map(s=>new p(s))),t||n.emit(d,this)}toDsl(e){const{__VTJ_BLOCK__:t,id:i,nodes:s}=this;return{...u.normalAttrs.reduce((a,c)=>(a[c]=this[c],a),{}),__VTJ_BLOCK__:t,__VERSION__:e||h.timestamp().toString(),id:i,nodes:s.map(a=>a.toDsl())}}dispose(){this.nodes.map(e=>e.dispose(!0)),this.nodes=[],this.disposed=!0}setFunction(e,t,i,s=!1){this[e][t]=i,s||n.emit(d,this)}removeFunction(e,t,i=!1){delete this[e][t],i||n.emit(d,this)}setState(e,t,i=!1){this.state[e]=t,i||n.emit(d,this)}removeState(e,t=!1){delete this.state[e],t||n.emit(d,this)}setCss(e,t=!1){this.css=e,t||n.emit(d,this)}setWatch(e,t=!1){e.id=e.id||h.uid();const i=this.watch.findIndex(s=>s.id&&s.id===e.id||s===e);i>-1?this.watch.splice(i,1,e):this.watch.push(e),t||n.emit(d,this)}removeWatch(e,t=!1){const i=this.watch.findIndex(s=>s.id&&s.id===e.id||s===e);i>-1&&(this.watch.splice(i,1),t||n.emit(d,this))}setProp(e,t=!1){const i=this.props.findIndex(s=>typeof s=="string"?s===e.name:s.name===e.name);i>-1?this.props.splice(i,1,e):this.props.push(e),t||n.emit(d,this)}removeProp(e,t=!1){const i=this.props.findIndex(s=>typeof s=="string"?s===e.name:s.name===e.name);i>-1&&(this.props.splice(i,1),t||n.emit(d,this))}setEmit(e,t=!1){const i=this.emits.findIndex(s=>s===e);i>-1?this.emits.splice(i,1,e):this.emits.push(e),t||n.emit(d,this)}removeEmit(e,t=!1){const i=this.emits.findIndex(s=>s===e);i>-1&&(this.emits.splice(i,1),t||n.emit(d,this))}setSlot(e,t=!1){const i=this.slots.findIndex(s=>s===e);i>-1?this.slots.splice(i,1,e):this.slots.push(e),t||n.emit(d,this)}removeSlot(e,t=!1){const i=this.slots.findIndex(s=>s===e);i>-1&&(this.slots.splice(i,1),t||n.emit(d,this))}setInject(e,t=!1){const i=this.inject.findIndex(s=>s.name===e.name);i>-1?this.inject.splice(i,1,e):this.inject.push(e),t||n.emit(d,this)}removeInject(e,t=!1){const i=this.inject.findIndex(s=>s.name===e.name);i>-1&&(this.inject.splice(i,1),t||n.emit(d,this))}setDataSource(e,t=!1){this.dataSources[e.name]=e,t||n.emit(d,this)}removeDataSource(e,t=!1){delete this.dataSources[e],t||n.emit(d,this)}insertAfter(e,t,i=!1){e.parent=null;const s=this.nodes.indexOf(t);this.nodes.splice(s+1,0,e),i||n.emit(d,this)}insertBefore(e,t,i=!1){e.parent=null;const s=this.nodes.indexOf(t);this.nodes.splice(s,0,e),i||n.emit(d,this)}appendNode(e,t=!1){e.parent=null,this.nodes.push(e),t||n.emit(d,this)}addNode(e,t,i="inner",s=!1){t?["left","top"].includes(i)?t.parent?t.insertAfter(e,s):this.insertBefore(e,t,s):["right","bottom"].includes(i)?t.parent?t.insertAfter(e,s):this.insertAfter(e,t,s):t.appendChild(e,s):this.appendNode(e,s)}__removeNode(e,t=!1){const i=this.nodes.findIndex(s=>s.id===e.id);i>-1&&(this.nodes.splice(i,1),t||n.emit(d,this))}removeNode(e,t=!1){e.parent?e.dispose(t):(e.dispose(!0),this.__removeNode(e,t))}move(e,t,i="inner",s=!1){e.parent?e.parent.removeChild(e,!0):this.__removeNode(e,!0),this.addNode(e,t,i,s)}movePrev(e,t=!1){if(e.parent)e.movePrev(t);else{const i=this.nodes,s=i.indexOf(e);s>0&&(i.splice(s,1),i.splice(s-1,0,e),t||n.emit(d,this))}}moveNext(e,t=!1){if(e.parent)e.moveNext(t);else{const i=this.nodes,s=i.indexOf(e);s>-1&&s<i.length-1&&(i.splice(s,1),i.splice(s+1,0,e),t||n.emit(d,this))}}cloneNode(e,t=!1){const i=I(e.toDsl()),s=new p(i);return this.addNode(s,e,"bottom",t),s}lock(e=!1){this.locked=!0;for(const t of this.nodes)t.lock(!0);e||n.emit(d,this)}unlock(e=!1){this.locked=!1;for(const t of this.nodes)t.unlock(!0);e||n.emit(d,this)}isChild(e){let t=!1;for(const i of this.nodes)if(e===i||e.id===i.id){t=!0;break}else if(t=i.isChild(e),t)break;return t}}const m="EVENT_PROJECT_CHANGE",P="EVENT_PROJECT_ACTIVED",V="EVENT_PROJECT_DEPS_CHANGE",C="EVENT_PROJECT_PAGES_CHANGE",A="EVENT_PROJECT_BLOCKS_CHANGE",S="EVENT_PROJECT_APIS_CHANGE",R="EVENT_PROJECT_PUBLISH",k="EVENT_PROJECT_FILE_PUBLISH";class O{id="";name="";description="";homepage="";dependencies=[];pages=[];blocks=[];apis=[];currentFile=null;static attrs=["name","homepage","description","dependencies","pages","blocks","apis"];constructor(e){const{id:t}=e;this.id=t||h.uid(),this.update(e,!0)}update(e,t=!1){for(const i of O.attrs){const s=e[i];s&&(this[i]=s)}t||n.emit(m,{model:this,type:"update",data:e})}isPageFile(e){return e.type==="page"}toDsl(e){const{id:t}=this,i=O.attrs.reduce((s,r)=>(s[r]=this[r],s),{});return i.pages&&(i.pages=i.pages.map(s=>(delete s.dsl,s))),i.blocks&&(i.blocks=i.blocks.map(s=>(delete s.dsl,s))),{__VTJ_PROJECT__:!0,__VERSION__:e||h.timestamp().toString(),id:t,...i}}active(e,t=!1){this.currentFile=e,t||n.emit(P,{model:this,type:"update",data:e})}deactivate(e=!1){this.currentFile=null,e||n.emit(P,{model:this,type:"update",data:null})}setDeps(e,t=!1){const i=this.dependencies,s=i.findIndex(a=>a.package===e.package);let r;if(s>-1?(r="update",i.splice(s,1,{...i[s],...e})):(r="create",i.push(e)),!t){const a={model:this,type:r,data:e};n.emit(V,a),n.emit(m,a)}}removeDeps(e,t=!1){const i=this.dependencies,s=i.findIndex(r=>r.package===e.package);if(s>-1&&i.splice(s,1),!t){const r={model:this,type:"delete",data:e};n.emit(V,r),n.emit(m,r)}}getPage(e){const t=(i,s=[])=>{for(const r of s){if(r.id===i)return r;if(r.children&&r.children.length){const a=t(i,r.children);if(a)return a}}};return t(e,this.pages)}getPages(){const e=(t=[])=>{let i=[];for(const s of t)s.dir?s.children&&s.children.length&&(i=i.concat(e(s.children))):i.push(s);return i};return e(this.pages)}createPage(e,t,i=!1){if(e.id=e.id||h.uid(),e.type="page",e.dir?e.children=[]:e.dsl=e.dsl||new u({id:e.id,name:h.upperFirstCamelCase(e.name)}).toDsl(),t){const s=this.getPage(t);s?s.children?s.children.push(e):s.children=[e]:console.warn(`not found PageFile for id: ${t} `)}else this.pages.push(e);if(this.currentFile||this.active(e,i),!i){const s={model:this,type:"create",data:e};n.emit(C,s),n.emit(m,s)}}updatePage(e,t=!1){const i=this.getPage(e.id);if(i?Object.assign(i,e):console.warn(`not found PageFile for id: ${e.id} `),!t){const s={model:this,type:"update",data:e};n.emit(C,s),n.emit(m,s)}}clonePage(e,t,i=!1){const s=h.uid(),r=`${e.name}Copy`,a=`${e.title}_副本`,c=new u({id:s,name:r}).toDsl(),f=h.merge({},e,{id:s,name:r,title:a,dsl:c}),_=t?this.getPage(t)?.children||[]:this.pages,g=_.findIndex(v=>v.id===e.id);if(_.splice(g+1,0,f),!i){const v={model:this,type:"clone",data:{page:e,newPage:f}};n.emit(C,v),n.emit(m,v)}}removePage(e,t=!1){const i=this.getPage(e),s=(r,a)=>{const c=a.findIndex(f=>f.id===r);if(c>=0){a.splice(c,1);return}for(const f of a)if(f.children&&f.children.length)return s(r,f.children)};if(s(e,this.pages),e===this.homepage&&(this.homepage=""),this.currentFile?.id===e&&this.deactivate(t),!t){const r={model:this,type:"delete",data:i};n.emit(C,r),n.emit(m,r)}}getBlock(e){return this.blocks.find(t=>t.id===e)}createBlock(e,t=!1){const i=e.id||h.uid(),s=h.upperFirstCamelCase(e.name);if(e.id=i,e.type="block",e.dsl=new u({id:i,name:s}).toDsl(),this.blocks.push(e),this.currentFile||this.active(e,t),!t){const r={model:this,type:"create",data:e};n.emit(A,r),n.emit(m,r)}}updateBlock(e,t=!1){const i=this.getBlock(e.id);if(i?(Object.assign(i,e),i.dsl&&(i.dsl.name=e.name)):console.warn(`not found PageFile for id: ${e.id} `),!t){const s={model:this,type:"update",data:e};n.emit(A,s),n.emit(m,s)}}removeBlock(e,t=!1){const i=this.getBlock(e),s=this.blocks,r=s.findIndex(a=>a.id===e);if(r>-1?(s.splice(r,1),this.currentFile?.id===e&&this.deactivate(t)):console.warn(`not found PageFile for id: ${e} `),!t){const a={model:this,type:"delete",data:i};n.emit(A,a),n.emit(m,a)}}existBlockName(e,t=[]){return this.blocks.some(i=>i.name===e&&!t.includes(i.id))}existPageName(e,t=[]){return this.getPages().some(s=>s.name===e&&!t.includes(s.id))}setApi(e,t=!1){const i=this.apis.find(r=>r.name===e.name||r.id===e.id);let s;if(i?(s="update",Object.assign(i,e)):(s="create",e.id=h.uid(),this.apis.push(e)),!t){const r={model:this,type:s,data:e};n.emit(S,r),n.emit(m,r)}}removeApi(e,t=!1){const i=this.apis.findIndex(s=>s.name===e||s.id===e);if(i>-1?this.apis.splice(i,1):console.warn(`not found Api for name: ${e} `),!t){const s={model:this,type:"delete",data:e};n.emit(S,s),n.emit(m,s)}}existApiName(e,t=[]){return this.apis.some(i=>i.name===e&&!t.includes(i.id))}setHomepage(e,t=!1){if(this.homepage=e,!t){const i={model:this,type:"update",data:e};n.emit(m,i)}}publish(e){const t={model:this,type:"publish",data:e||this};e?n.emit(k,t):n.emit(R,t)}}const T="EVENT_HISTORY_CHANGE",y="EVENT_HISTORY_LOAD";class G{options={max:50};index=-1;id;items;constructor(e,t={}){Object.assign(this.options,t);const{id:i,items:s=[]}=e;this.id=i,this.items=s}toDsl(){const{id:e,items:t}=this;return{id:e,items:t.map(i=>({id:i.id,label:i.label}))}}get(e){return this.items.find(t=>t.id===e)}add(e,t=!1){const{max:i}=this.options,s={id:h.uid(),label:new Date().toLocaleString(),dsl:h.cloneDeep(e)};if(this.items.unshift(s),this.items.length>i){const r=this.items.splice(i);t||n.emit(T,{model:this,type:"delete",data:r.map(a=>a.id)})}this.index=-1,t||n.emit(T,{model:this,type:"create",data:s})}remove(e,t=!1){const i=this.items.findIndex(s=>s.id===e);i>-1?(this.items.splice(i,1),i===this.index?this.index=-1:this.index>=this.items.length&&(this.index=this.items.length-1)):console.warn(`not found HistoryItem for id: ${e} `),t||n.emit(T,{model:this,type:"delete",data:[e]})}forward(e=!1){const{index:t,items:i}=this;if(t<0)return;--this.index;const s=i[this.index];s&&!e&&n.emit(y,{model:this,type:"load",data:s})}backward(e=!1){const{index:t,items:i}=this;if(t>=i.length-1)return;t<0&&(this.index=0),++this.index;const s=i[this.index];s&&!e&&n.emit(y,{model:this,type:"load",data:s})}load(e,t=!1){const i=this.items.findIndex(s=>s.id===e);i>=0&&(this.index=i,t||n.emit(y,{model:this,type:"load",data:this.items[i]}))}clear(e=!1){this.index=-1;const t=this.items.map(i=>i.id);this.items=[],e||n.emit(T,{model:this,type:"clear",data:t})}}const n=B();function w(o){return o instanceof u}function L(o){return o instanceof p}function F(o){return!!o.__VTJ_BLOCK__}function I(o){return delete o.id,Array.isArray(o.children)&&(o.children=o.children.map(e=>I(e))),o}exports.Base=J;exports.BlockModel=u;exports.DirectiveModel=E;exports.EVENT_BLOCK_CHANGE=d;exports.EVENT_HISTORY_CHANGE=T;exports.EVENT_HISTORY_LOAD=y;exports.EVENT_NODE_CHANGE=l;exports.EVENT_PROJECT_ACTIVED=P;exports.EVENT_PROJECT_APIS_CHANGE=S;exports.EVENT_PROJECT_BLOCKS_CHANGE=A;exports.EVENT_PROJECT_CHANGE=m;exports.EVENT_PROJECT_DEPS_CHANGE=V;exports.EVENT_PROJECT_FILE_PUBLISH=k;exports.EVENT_PROJECT_PAGES_CHANGE=C;exports.EVENT_PROJECT_PUBLISH=R;exports.EventModel=N;exports.HistoryModel=G;exports.NodeModel=p;exports.ProjectModel=O;exports.PropModel=x;exports.Service=b;exports.VTJ_CORE_VERSION=H;exports.cloneDsl=I;exports.emitter=n;exports.isBlock=w;exports.isBlockSchema=F;exports.isNode=L;
package/dist/index.mjs CHANGED
@@ -1,12 +1,12 @@
1
- import { uid as f, timestamp as k, upperFirstCamelCase as P, merge as b, cloneDeep as H } from "@vtj/base";
1
+ import { uid as m, timestamp as S, upperFirstCamelCase as P, merge as b, cloneDeep as H } from "@vtj/base";
2
2
  /**!
3
3
  * Copyright (c) 2024, VTJ.PRO All rights reserved.
4
4
  * @name @vtj/core
5
5
  * @author CHC chenhuachun1549@dingtalk.com
6
- * @version 0.7.1
6
+ * @version 0.7.2
7
7
  * @license <a href="https://vtj.pro/license.html">MIT License</a>
8
8
  */
9
- const F = "0.7.1";
9
+ const G = "0.7.2";
10
10
  class j {
11
11
  }
12
12
  function w(o) {
@@ -91,7 +91,7 @@ class y {
91
91
  }
92
92
  class _ {
93
93
  constructor(e) {
94
- this.schema = e, this.id = e.id || f(), this.update(e);
94
+ this.schema = e, this.id = e.id || m(), this.update(e);
95
95
  }
96
96
  /**
97
97
  * 标识
@@ -127,9 +127,9 @@ class _ {
127
127
  }
128
128
  static toDsl(e = []) {
129
129
  return e.map((t) => {
130
- const { name: i, arg: s, modifiers: r, value: a, iterator: h, id: m } = t;
130
+ const { name: i, arg: s, modifiers: r, value: a, iterator: h, id: c } = t;
131
131
  return {
132
- id: m,
132
+ id: c,
133
133
  name: i,
134
134
  arg: s,
135
135
  modifiers: r,
@@ -143,7 +143,7 @@ const l = "EVENT_NODE_CHANGE";
143
143
  class p {
144
144
  constructor(e, t = null) {
145
145
  this.parent = t;
146
- const { id: i = f(), name: s, from: r = "" } = e;
146
+ const { id: i = m(), name: s, from: r = "" } = e;
147
147
  this.id = i, this.name = s, this.from = r, this.update(e, !0), p.nodes[this.id] = this;
148
148
  }
149
149
  /**
@@ -210,10 +210,10 @@ class p {
210
210
  children: r = [],
211
211
  slot: a,
212
212
  props: h = {},
213
- events: m = {},
213
+ events: c = {},
214
214
  directives: E = []
215
215
  } = e;
216
- this.invisible = i, this.locked = s, this.setChildren(r, !0), this.setSlot(a, !0), this.props = x.parse(h), this.events = y.parse(m), this.directives = _.parse(E), t || n.emit(l, this);
216
+ this.invisible = i, this.locked = s, this.setChildren(r, !0), this.setSlot(a, !0), this.props = x.parse(h), this.events = y.parse(c), this.directives = _.parse(E), t || n.emit(l, this);
217
217
  }
218
218
  /**
219
219
  * 设置子节点
@@ -385,7 +385,7 @@ class p {
385
385
  locked: r,
386
386
  slot: a,
387
387
  children: h,
388
- props: m,
388
+ props: c,
389
389
  directives: E,
390
390
  events: N
391
391
  } = this, v = Array.isArray(h) ? h.map((R) => R.toDsl()) : h;
@@ -397,7 +397,7 @@ class p {
397
397
  locked: r,
398
398
  slot: a,
399
399
  children: v,
400
- props: x.toDsl(m),
400
+ props: x.toDsl(c),
401
401
  directives: _.toDsl(E),
402
402
  events: y.toDsl(N)
403
403
  };
@@ -478,7 +478,7 @@ class u {
478
478
  ];
479
479
  constructor(e) {
480
480
  const { id: t } = e;
481
- this.id = t || f(), this.update(e, !0);
481
+ this.id = t || m(), this.update(e, !0);
482
482
  }
483
483
  update(e, t = !1) {
484
484
  for (const s of u.normalAttrs) {
@@ -492,14 +492,17 @@ class u {
492
492
  * 获取DSL
493
493
  * @returns
494
494
  */
495
- toDsl() {
496
- const { __VTJ_BLOCK__: e, id: t, nodes: i } = this;
495
+ toDsl(e) {
496
+ const { __VTJ_BLOCK__: t, id: i, nodes: s } = this;
497
497
  return {
498
- ...u.normalAttrs.reduce((r, a) => (r[a] = this[a], r), {}),
499
- __VTJ_BLOCK__: e,
500
- __VERSION__: k().toString(),
501
- id: t,
502
- nodes: i.map((r) => r.toDsl())
498
+ ...u.normalAttrs.reduce(
499
+ (a, h) => (a[h] = this[h], a),
500
+ {}
501
+ ),
502
+ __VTJ_BLOCK__: t,
503
+ __VERSION__: e || S().toString(),
504
+ id: i,
505
+ nodes: s.map((a) => a.toDsl())
503
506
  };
504
507
  }
505
508
  /**
@@ -558,7 +561,7 @@ class u {
558
561
  * @param silent
559
562
  */
560
563
  setWatch(e, t = !1) {
561
- e.id = e.id || f();
564
+ e.id = e.id || m();
562
565
  const i = this.watch.findIndex(
563
566
  (s) => s.id && s.id === e.id || s === e
564
567
  );
@@ -617,7 +620,7 @@ class u {
617
620
  }
618
621
  /**
619
622
  * 设置插槽
620
- * @param emit
623
+ * @param slot
621
624
  * @param silent
622
625
  */
623
626
  setSlot(e, t = !1) {
@@ -626,7 +629,7 @@ class u {
626
629
  }
627
630
  /**
628
631
  * 删除插槽
629
- * @param emit
632
+ * @param slot
630
633
  * @param silent
631
634
  */
632
635
  removeSlot(e, t = !1) {
@@ -771,7 +774,7 @@ class u {
771
774
  return t;
772
775
  }
773
776
  }
774
- const c = "EVENT_PROJECT_CHANGE", V = "EVENT_PROJECT_ACTIVED", I = "EVENT_PROJECT_DEPS_CHANGE", A = "EVENT_PROJECT_PAGES_CHANGE", C = "EVENT_PROJECT_BLOCKS_CHANGE", S = "EVENT_PROJECT_APIS_CHANGE", J = "EVENT_PROJECT_PUBLISH", B = "EVENT_PROJECT_FILE_PUBLISH";
777
+ const f = "EVENT_PROJECT_CHANGE", V = "EVENT_PROJECT_ACTIVED", I = "EVENT_PROJECT_DEPS_CHANGE", g = "EVENT_PROJECT_PAGES_CHANGE", C = "EVENT_PROJECT_BLOCKS_CHANGE", k = "EVENT_PROJECT_APIS_CHANGE", B = "EVENT_PROJECT_PUBLISH", J = "EVENT_PROJECT_FILE_PUBLISH";
775
778
  class T {
776
779
  id = "";
777
780
  name = "";
@@ -793,14 +796,14 @@ class T {
793
796
  ];
794
797
  constructor(e) {
795
798
  const { id: t } = e;
796
- this.id = t || f(), this.update(e, !0);
799
+ this.id = t || m(), this.update(e, !0);
797
800
  }
798
801
  update(e, t = !1) {
799
802
  for (const i of T.attrs) {
800
803
  const s = e[i];
801
804
  s && (this[i] = s);
802
805
  }
803
- t || n.emit(c, {
806
+ t || n.emit(f, {
804
807
  model: this,
805
808
  type: "update",
806
809
  data: e
@@ -809,16 +812,16 @@ class T {
809
812
  isPageFile(e) {
810
813
  return e.type === "page";
811
814
  }
812
- toDsl() {
813
- const { id: e } = this, t = T.attrs.reduce(
814
- (i, s) => (i[s] = this[s], i),
815
+ toDsl(e) {
816
+ const { id: t } = this, i = T.attrs.reduce(
817
+ (s, r) => (s[r] = this[r], s),
815
818
  {}
816
819
  );
817
- return t.pages && (t.pages = t.pages.map((i) => (delete i.dsl, i))), t.blocks && (t.blocks = t.blocks.map((i) => (delete i.dsl, i))), {
820
+ return i.pages && (i.pages = i.pages.map((s) => (delete s.dsl, s))), i.blocks && (i.blocks = i.blocks.map((s) => (delete s.dsl, s))), {
818
821
  __VTJ_PROJECT__: !0,
819
- __VERSION__: k().toString(),
820
- id: e,
821
- ...t
822
+ __VERSION__: e || S().toString(),
823
+ id: t,
824
+ ...i
822
825
  };
823
826
  }
824
827
  /**
@@ -861,7 +864,7 @@ class T {
861
864
  type: r,
862
865
  data: e
863
866
  };
864
- n.emit(I, a), n.emit(c, a);
867
+ n.emit(I, a), n.emit(f, a);
865
868
  }
866
869
  }
867
870
  /**
@@ -877,7 +880,7 @@ class T {
877
880
  type: "delete",
878
881
  data: e
879
882
  };
880
- n.emit(I, r), n.emit(c, r);
883
+ n.emit(I, r), n.emit(f, r);
881
884
  }
882
885
  }
883
886
  /**
@@ -919,7 +922,7 @@ class T {
919
922
  * @param silent
920
923
  */
921
924
  createPage(e, t, i = !1) {
922
- if (e.id = e.id || f(), e.type = "page", e.dir ? e.children = [] : e.dsl = e.dsl || new u({
925
+ if (e.id = e.id || m(), e.type = "page", e.dir ? e.children = [] : e.dsl = e.dsl || new u({
923
926
  id: e.id,
924
927
  name: P(e.name)
925
928
  }).toDsl(), t) {
@@ -933,7 +936,7 @@ class T {
933
936
  type: "create",
934
937
  data: e
935
938
  };
936
- n.emit(A, s), n.emit(c, s);
939
+ n.emit(g, s), n.emit(f, s);
937
940
  }
938
941
  }
939
942
  /**
@@ -949,7 +952,7 @@ class T {
949
952
  type: "update",
950
953
  data: e
951
954
  };
952
- n.emit(A, s), n.emit(c, s);
955
+ n.emit(g, s), n.emit(f, s);
953
956
  }
954
957
  }
955
958
  /**
@@ -959,20 +962,20 @@ class T {
959
962
  * @param silent
960
963
  */
961
964
  clonePage(e, t, i = !1) {
962
- const s = f(), r = `${e.name}Copy`, a = `${e.title}_副本`, h = new u({
965
+ const s = m(), r = `${e.name}Copy`, a = `${e.title}_副本`, h = new u({
963
966
  id: s,
964
967
  name: r
965
- }).toDsl(), m = b({}, e, { id: s, name: r, title: a, dsl: h }), E = t ? this.getPage(t)?.children || [] : this.pages, N = E.findIndex((v) => v.id === e.id);
966
- if (E.splice(N + 1, 0, m), !i) {
968
+ }).toDsl(), c = b({}, e, { id: s, name: r, title: a, dsl: h }), E = t ? this.getPage(t)?.children || [] : this.pages, N = E.findIndex((v) => v.id === e.id);
969
+ if (E.splice(N + 1, 0, c), !i) {
967
970
  const v = {
968
971
  model: this,
969
972
  type: "clone",
970
973
  data: {
971
974
  page: e,
972
- newPage: m
975
+ newPage: c
973
976
  }
974
977
  };
975
- n.emit(A, v), n.emit(c, v);
978
+ n.emit(g, v), n.emit(f, v);
976
979
  }
977
980
  }
978
981
  /**
@@ -981,23 +984,23 @@ class T {
981
984
  * @param silent
982
985
  */
983
986
  removePage(e, t = !1) {
984
- const i = (s, r) => {
985
- const a = r.findIndex((h) => h.id === s);
986
- if (a >= 0) {
987
- r.splice(a, 1);
987
+ const i = this.getPage(e), s = (r, a) => {
988
+ const h = a.findIndex((c) => c.id === r);
989
+ if (h >= 0) {
990
+ a.splice(h, 1);
988
991
  return;
989
992
  }
990
- for (const h of r)
991
- if (h.children && h.children.length)
992
- return i(s, h.children);
993
+ for (const c of a)
994
+ if (c.children && c.children.length)
995
+ return s(r, c.children);
993
996
  };
994
- if (i(e, this.pages), e === this.homepage && (this.homepage = ""), this.currentFile?.id === e && this.deactivate(t), !t) {
995
- const s = {
997
+ if (s(e, this.pages), e === this.homepage && (this.homepage = ""), this.currentFile?.id === e && this.deactivate(t), !t) {
998
+ const r = {
996
999
  model: this,
997
1000
  type: "delete",
998
- data: e
1001
+ data: i
999
1002
  };
1000
- n.emit(A, s), n.emit(c, s);
1003
+ n.emit(g, r), n.emit(f, r);
1001
1004
  }
1002
1005
  }
1003
1006
  /**
@@ -1014,14 +1017,14 @@ class T {
1014
1017
  * @param silent
1015
1018
  */
1016
1019
  createBlock(e, t = !1) {
1017
- const i = e.id || f(), s = P(e.name);
1020
+ const i = e.id || m(), s = P(e.name);
1018
1021
  if (e.id = i, e.type = "block", e.dsl = new u({ id: i, name: s }).toDsl(), this.blocks.push(e), this.currentFile || this.active(e, t), !t) {
1019
1022
  const r = {
1020
1023
  model: this,
1021
1024
  type: "create",
1022
1025
  data: e
1023
1026
  };
1024
- n.emit(C, r), n.emit(c, r);
1027
+ n.emit(C, r), n.emit(f, r);
1025
1028
  }
1026
1029
  }
1027
1030
  /**
@@ -1037,7 +1040,7 @@ class T {
1037
1040
  type: "update",
1038
1041
  data: e
1039
1042
  };
1040
- n.emit(C, s), n.emit(c, s);
1043
+ n.emit(C, s), n.emit(f, s);
1041
1044
  }
1042
1045
  }
1043
1046
  /**
@@ -1046,14 +1049,14 @@ class T {
1046
1049
  * @param silent
1047
1050
  */
1048
1051
  removeBlock(e, t = !1) {
1049
- const i = this.blocks, s = i.findIndex((r) => r.id === e);
1050
- if (s > -1 ? (i.splice(s, 1), this.currentFile?.id === e && this.deactivate(t)) : console.warn(`not found PageFile for id: ${e} `), !t) {
1051
- const r = {
1052
+ const i = this.getBlock(e), s = this.blocks, r = s.findIndex((a) => a.id === e);
1053
+ if (r > -1 ? (s.splice(r, 1), this.currentFile?.id === e && this.deactivate(t)) : console.warn(`not found PageFile for id: ${e} `), !t) {
1054
+ const a = {
1052
1055
  model: this,
1053
1056
  type: "delete",
1054
- data: e
1057
+ data: i
1055
1058
  };
1056
- n.emit(C, r), n.emit(c, r);
1059
+ n.emit(C, a), n.emit(f, a);
1057
1060
  }
1058
1061
  }
1059
1062
  /**
@@ -1084,13 +1087,13 @@ class T {
1084
1087
  (r) => r.name === e.name || r.id === e.id
1085
1088
  );
1086
1089
  let s;
1087
- if (i ? (s = "update", Object.assign(i, e)) : (s = "create", e.id = f(), this.apis.push(e)), !t) {
1090
+ if (i ? (s = "update", Object.assign(i, e)) : (s = "create", e.id = m(), this.apis.push(e)), !t) {
1088
1091
  const r = {
1089
1092
  model: this,
1090
1093
  type: s,
1091
1094
  data: e
1092
1095
  };
1093
- n.emit(S, r), n.emit(c, r);
1096
+ n.emit(k, r), n.emit(f, r);
1094
1097
  }
1095
1098
  }
1096
1099
  /**
@@ -1106,7 +1109,7 @@ class T {
1106
1109
  type: "delete",
1107
1110
  data: e
1108
1111
  };
1109
- n.emit(S, s), n.emit(c, s);
1112
+ n.emit(k, s), n.emit(f, s);
1110
1113
  }
1111
1114
  }
1112
1115
  existApiName(e, t = []) {
@@ -1119,7 +1122,7 @@ class T {
1119
1122
  type: "update",
1120
1123
  data: e
1121
1124
  };
1122
- n.emit(c, i);
1125
+ n.emit(f, i);
1123
1126
  }
1124
1127
  }
1125
1128
  publish(e) {
@@ -1128,10 +1131,10 @@ class T {
1128
1131
  type: "publish",
1129
1132
  data: e || this
1130
1133
  };
1131
- e ? n.emit(B, t) : n.emit(J, t);
1134
+ e ? n.emit(J, t) : n.emit(B, t);
1132
1135
  }
1133
1136
  }
1134
- const g = "EVENT_HISTORY_CHANGE", O = "EVENT_HISTORY_LOAD";
1137
+ const A = "EVENT_HISTORY_CHANGE", O = "EVENT_HISTORY_LOAD";
1135
1138
  class $ {
1136
1139
  options = { max: 50 };
1137
1140
  index = -1;
@@ -1164,19 +1167,19 @@ class $ {
1164
1167
  */
1165
1168
  add(e, t = !1) {
1166
1169
  const { max: i } = this.options, s = {
1167
- id: f(),
1170
+ id: m(),
1168
1171
  label: (/* @__PURE__ */ new Date()).toLocaleString(),
1169
1172
  dsl: H(e)
1170
1173
  };
1171
1174
  if (this.items.unshift(s), this.items.length > i) {
1172
1175
  const r = this.items.splice(i);
1173
- t || n.emit(g, {
1176
+ t || n.emit(A, {
1174
1177
  model: this,
1175
1178
  type: "delete",
1176
1179
  data: r.map((a) => a.id)
1177
1180
  });
1178
1181
  }
1179
- this.index = -1, t || n.emit(g, {
1182
+ this.index = -1, t || n.emit(A, {
1180
1183
  model: this,
1181
1184
  type: "create",
1182
1185
  data: s
@@ -1189,7 +1192,7 @@ class $ {
1189
1192
  */
1190
1193
  remove(e, t = !1) {
1191
1194
  const i = this.items.findIndex((s) => s.id === e);
1192
- i > -1 ? (this.items.splice(i, 1), i === this.index ? this.index = -1 : this.index >= this.items.length && (this.index = this.items.length - 1)) : console.warn(`not found HistoryItem for id: ${e} `), t || n.emit(g, {
1195
+ i > -1 ? (this.items.splice(i, 1), i === this.index ? this.index = -1 : this.index >= this.items.length && (this.index = this.items.length - 1)) : console.warn(`not found HistoryItem for id: ${e} `), t || n.emit(A, {
1193
1196
  model: this,
1194
1197
  type: "delete",
1195
1198
  data: [e]
@@ -1230,7 +1233,7 @@ class $ {
1230
1233
  clear(e = !1) {
1231
1234
  this.index = -1;
1232
1235
  const t = this.items.map((i) => i.id);
1233
- this.items = [], e || n.emit(g, {
1236
+ this.items = [], e || n.emit(A, {
1234
1237
  model: this,
1235
1238
  type: "clear",
1236
1239
  data: t
@@ -1255,24 +1258,24 @@ export {
1255
1258
  u as BlockModel,
1256
1259
  _ as DirectiveModel,
1257
1260
  d as EVENT_BLOCK_CHANGE,
1258
- g as EVENT_HISTORY_CHANGE,
1261
+ A as EVENT_HISTORY_CHANGE,
1259
1262
  O as EVENT_HISTORY_LOAD,
1260
1263
  l as EVENT_NODE_CHANGE,
1261
1264
  V as EVENT_PROJECT_ACTIVED,
1262
- S as EVENT_PROJECT_APIS_CHANGE,
1265
+ k as EVENT_PROJECT_APIS_CHANGE,
1263
1266
  C as EVENT_PROJECT_BLOCKS_CHANGE,
1264
- c as EVENT_PROJECT_CHANGE,
1267
+ f as EVENT_PROJECT_CHANGE,
1265
1268
  I as EVENT_PROJECT_DEPS_CHANGE,
1266
- B as EVENT_PROJECT_FILE_PUBLISH,
1267
- A as EVENT_PROJECT_PAGES_CHANGE,
1268
- J as EVENT_PROJECT_PUBLISH,
1269
+ J as EVENT_PROJECT_FILE_PUBLISH,
1270
+ g as EVENT_PROJECT_PAGES_CHANGE,
1271
+ B as EVENT_PROJECT_PUBLISH,
1269
1272
  y as EventModel,
1270
1273
  $ as HistoryModel,
1271
1274
  p as NodeModel,
1272
1275
  T as ProjectModel,
1273
1276
  x as PropModel,
1274
1277
  j as Service,
1275
- F as VTJ_CORE_VERSION,
1278
+ G as VTJ_CORE_VERSION,
1276
1279
  D as cloneDsl,
1277
1280
  n as emitter,
1278
1281
  K as isBlock,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vtj/core",
3
3
  "private": false,
4
- "version": "0.7.2",
4
+ "version": "0.7.3",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build": "vue-tsc && vite build",
@@ -14,7 +14,7 @@
14
14
  "mitt": "~3.0.1"
15
15
  },
16
16
  "devDependencies": {
17
- "@vtj/cli": "^0.7.2"
17
+ "@vtj/cli": "^0.7.3"
18
18
  },
19
19
  "exports": {
20
20
  ".": {
@@ -30,7 +30,7 @@
30
30
  "dist",
31
31
  "types"
32
32
  ],
33
- "gitHead": "9cdd39f009940b6e9c6546a4504cd99458c44ec0",
33
+ "gitHead": "86a13552ff03a6e623efe6ea0515f5f780a9fde1",
34
34
  "publishConfig": {
35
35
  "access": "public"
36
36
  }
@@ -33,7 +33,7 @@ export declare class BlockModel {
33
33
  * 获取DSL
34
34
  * @returns
35
35
  */
36
- toDsl(): BlockSchema;
36
+ toDsl(version?: string): BlockSchema;
37
37
  /**
38
38
  * 销毁
39
39
  */
@@ -110,13 +110,13 @@ export declare class BlockModel {
110
110
  removeEmit(emit: string, silent?: boolean): void;
111
111
  /**
112
112
  * 设置插槽
113
- * @param emit
113
+ * @param slot
114
114
  * @param silent
115
115
  */
116
116
  setSlot(slot: string, silent?: boolean): void;
117
117
  /**
118
118
  * 删除插槽
119
- * @param emit
119
+ * @param slot
120
120
  * @param silent
121
121
  */
122
122
  removeSlot(slot: string, silent?: boolean): void;
@@ -51,7 +51,7 @@ export declare class ProjectModel {
51
51
  constructor(schema: ProjectSchema);
52
52
  update(schema: Partial<ProjectSchema>, silent?: boolean): void;
53
53
  isPageFile(file: PageFile | BlockFile): file is PageFile;
54
- toDsl(): ProjectSchema;
54
+ toDsl(version?: string): ProjectSchema;
55
55
  /**
56
56
  * 打开文件
57
57
  * @param file
@@ -16,4 +16,6 @@ export declare abstract class Service {
16
16
  abstract publish(project: ProjectSchema): Promise<boolean>;
17
17
  abstract publishFile(project: ProjectSchema, file: PageFile | BlockFile): Promise<boolean>;
18
18
  abstract getRaw(project: ProjectSchema, dsl: BlockSchema): Promise<string>;
19
+ abstract createRawPage(file: PageFile): Promise<boolean>;
20
+ abstract removeRawPage(id: string): Promise<boolean>;
19
21
  }
@@ -2,7 +2,7 @@
2
2
  * Copyright (c) 2024, VTJ.PRO All rights reserved.
3
3
  * @name @vtj/core
4
4
  * @author CHC chenhuachun1549@dingtalk.com
5
- * @version 0.7.0
5
+ * @version 0.7.2
6
6
  * @license <a href="https://vtj.pro/license.html">MIT License</a>
7
7
  */
8
- export declare const version = "0.7.0";
8
+ export declare const version = "0.7.2";