@blocklet/pages-kit-inner-components 0.6.76 → 0.6.78
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/lib/cjs/add-component.js +1 -1
- package/lib/cjs/chunks/{draft-data-1hI-ev3X.js → draft-data-CkVvOh60.js} +1 -1
- package/lib/cjs/chunks/{home-BEBAcHLJ.js → home-CjhOAEa7.js} +2 -2
- package/lib/cjs/chunks/html-xfTPTsl5.js +1 -0
- package/lib/cjs/chunks/{index-DC4XWhpO.js → index-CiYwdeK9.js} +1 -1
- package/lib/cjs/chunks/{publish-button-Brxy74Ql.js → publish-button-DV23sfgB.js} +1 -1
- package/lib/cjs/chunks/site-state-CtiXHtSX.js +1 -0
- package/lib/cjs/chunks/state-BdtFHk36.js +1 -0
- package/lib/cjs/home.js +1 -1
- package/lib/cjs/locales.js +2 -2
- package/lib/cjs/project-html.js +1 -1
- package/lib/cjs/resources.js +1 -1
- package/lib/cjs/setting.js +2 -2
- package/lib/cjs/site-state.js +1 -1
- package/lib/es/add-component.js +2 -2
- package/lib/es/chunks/{draft-data--WtLtXxy.js → draft-data-BOoVhbon.js} +1 -1
- package/lib/es/chunks/{home-iBLOByRu.js → home-BZB9zOlO.js} +3 -3
- package/lib/es/chunks/{html-DfzaaIYa.js → html-DOgzvdOd.js} +11 -10
- package/lib/es/chunks/{index-BlZcgfTW.js → index-Bggolyu_.js} +2 -2
- package/lib/es/chunks/{publish-button-CHmiGlT7.js → publish-button-Dm2Nce-b.js} +3 -3
- package/lib/es/chunks/{site-state-BXM-5tZw.js → site-state-D4wvb7PN.js} +300 -300
- package/lib/es/chunks/{state-DS2djQbR.js → state-DNdwHwty.js} +173 -165
- package/lib/es/home.js +3 -3
- package/lib/es/locales.js +2 -0
- package/lib/es/project-html.js +1 -1
- package/lib/es/resources.js +1 -1
- package/lib/es/setting.js +680 -670
- package/lib/es/site-state.js +1 -1
- package/package.json +3 -3
- package/lib/cjs/chunks/html-JOhPskS4.js +0 -1
- package/lib/cjs/chunks/site-state-CcYU6tKv.js +0 -1
- package/lib/cjs/chunks/state-BAjDjwwp.js +0 -1
package/lib/es/site-state.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "./chunks/components-DLwSTd_N.js";
|
|
2
|
-
import { c as O, a as b, b as h, S as y, e as L, f as U, h as q, d as v, g as z, i as C, m as F, t as G, j as J, u as K } from "./chunks/site-state-
|
|
2
|
+
import { c as O, a as b, b as h, S as y, e as L, f as U, h as q, d as v, g as z, i as C, m as F, t as G, j as J, u as K } from "./chunks/site-state-D4wvb7PN.js";
|
|
3
3
|
import { nextId as Q } from "@blocklet/pages-kit/utils/common";
|
|
4
4
|
import "@blocklet/pages-kit/utils/page-model";
|
|
5
5
|
import "@blocklet/pages-kit/utils/property";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blocklet/pages-kit-inner-components",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.78",
|
|
4
4
|
"description": "Pages Kit inner components library",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -201,8 +201,8 @@
|
|
|
201
201
|
"yaml": "^2.5.0",
|
|
202
202
|
"yjs": "^13.6.18",
|
|
203
203
|
"zustand": "^4.5.5",
|
|
204
|
-
"@blocklet/pages-kit": "^0.6.
|
|
205
|
-
"@blocklet/pages-kit-block-studio": "^0.6.
|
|
204
|
+
"@blocklet/pages-kit": "^0.6.78",
|
|
205
|
+
"@blocklet/pages-kit-block-studio": "^0.6.78"
|
|
206
206
|
},
|
|
207
207
|
"devDependencies": {
|
|
208
208
|
"@trivago/prettier-plugin-sort-imports": "^5.2.1",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var s=Object.create;var a=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var i=Object.getPrototypeOf,h=Object.prototype.hasOwnProperty;var y=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let c of l(t))!h.call(e,c)&&c!==n&&a(e,c,{get:()=>t[c],enumerable:!(r=u(t,c))||r.enumerable});return e};var f=(e,t,n)=>(n=e!=null?s(i(e)):{},y(t||!e||!e.__esModule?a(n,"default",{value:e,enumerable:!0}):n,e));const d=require("crypto"),g=async()=>await import("cheerio"),o={};function p(e,t){const n=JSON.stringify([e,t]);return d.createHash("md5").update(n).digest("hex")}async function C(e,t){{let n=await g();try{const r=n.load(e);return t.forEach(c=>{r(c.selector.cheerio).replaceWith(c.value)}),r.html()}catch{}finally{n=null}}return T(e,t)}function T(e,t){let n=e;return t.forEach(r=>{const c=typeof r.selector.pattern=="string"?new RegExp(r.selector.pattern,"gis"):r.selector.pattern;n=n.replace(c,r.value)}),n}async function j(e,t){if(!t||t.length===0)return e;const n=p(e,t);return o[n]??=C(e,t).finally(()=>{setTimeout(()=>{delete o[n]},10*1e3)}),o[n]}function m(e){return e.match(/<body[^>]*>(.*?)<\/body>/s)?.[1]||""}function H(e,t){return!t||Object.keys(t).length===0?!0:Object.entries(t).every(([n,r])=>e.includes(`${n}=${r}`))}exports.extractBodyContent=m;exports.injectToHtml=j;exports.matchCacheKey=H;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";const f=require("./components-DLLpFRy5.js"),F=require("@syncedstore/core"),Je=require("yjs"),We=require("@blocklet/pages-kit/utils/data-source"),Q=require("@blocklet/pages-kit/utils/route"),Qe=require("lodash/cloneDeep"),Xe=require("@blocklet/sdk/lib/config"),I=require("fs"),g=require("path"),Te=require("@blocklet/pages-kit/utils/common"),Ze=require("@blocklet/pages-kit/utils/page-model"),et=require("@blocklet/pages-kit/utils/property"),Ie=require("@blocklet/sdk/lib/component"),tt=require("@reactivedata/reactive"),X=require("glob"),st=require("lib0/decoding"),at=require("lib0/encoding"),nt=require("lodash/debounce"),ot=require("lodash/get"),rt=require("lodash/isEmpty"),it=require("lodash/set"),ct=require("lodash/union"),be=require("lru-cache"),lt=require("p-limit"),C=require("sequelize"),pt=require("stream/promises"),ut=require("tar"),z=require("ufo"),dt=require("wait-on"),G=require("y-protocols/awareness"),le=require("y-protocols/sync"),ft=require("yaml"),De=require("./html-JOhPskS4.js");require("sqlite3");require("@blocklet/pages-kit/types/state");const $=t=>t&&t.__esModule?t:{default:t};function ae(t){if(t&&t.__esModule)return t;const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const s in t)if(s!=="default"){const a=Object.getOwnPropertyDescriptor(t,s);Object.defineProperty(e,s,a.get?a:{enumerable:!0,get:()=>t[s]})}}return e.default=t,Object.freeze(e)}const U=ae(Je),Le=$(Qe),w=$(Xe),pe=ae(st),k=ae(at),_e=$(nt),Ee=$(ot),we=$(rt),ve=$(it),gt=$(ct),mt=$(lt),ht=$(dt),x=ae(ft),yt=C.DataTypes.sqlite.DATE.parse;C.DataTypes.sqlite.DATE.parse=(t,e)=>typeof t=="number"?new Date(t):yt(t,e);const M=new C.Sequelize({dialect:"sqlite",storage:f.databaseUrl,benchmark:process.env.ENABLE_SEQUELIZE_BENCHMARK==="true",retry:{match:[/SQLITE_BUSY/],name:"query",max:10},logging:process.env.ENABLE_SEQUELIZE_LOGGING==="true"?console.log:!1});M.query("pragma journal_mode = WAL;");M.query("pragma synchronous = normal;");M.query("pragma journal_size_limit = 33554432;");M.query("pragma cache_size = -2000;");M.query("pragma mmap_size = 33554432;");process.on("SIGINT",async()=>{await M.close(),process.exit(0)});process.on("SIGTERM",async()=>{await M.close(),process.exit(0)});async function St(t,e){try{if(t.getDialect()!=="sqlite")return;const[s]=await t.query("SELECT 1");if(!s||s.length===0)return;await t.query("PRAGMA shrink_memory;")}catch(s){if(s.name==="SequelizeConnectionError"||s?.message&&/closed!/.test(s.message))return;console.error("Failed to cleanup SQLite memory",e,s)}}let fe=null;fe&&clearInterval(fe);fe=setInterval(async()=>{f.logger.info("Start cleanupSqliteMemory"),await St(M,f.databaseUrl),f.logger.info("End cleanupSqliteMemory")},60*1e3*10);class Ae extends C.Model{}Ae.init({id:{type:C.DataTypes.UUID,allowNull:!1,primaryKey:!0,defaultValue:C.DataTypes.UUIDV4},projectId:{type:C.DataTypes.UUID,allowNull:!1},componentId:{type:C.DataTypes.STRING,allowNull:!1}},{sequelize:M,tableName:"ProjectComponents",timestamps:!1});class L extends C.Model{static async getProjectByIdOrSlug(e){return L.findOne({where:{[C.Op.or]:[{id:e},{slug:e}]}})}}L.init({id:{type:C.DataTypes.UUID,defaultValue:C.DataTypes.UUIDV4,primaryKey:!0},name:{type:C.DataTypes.STRING,allowNull:!1},description:C.DataTypes.TEXT,createdAt:C.DataTypes.DATE,updatedAt:C.DataTypes.DATE,createdBy:{type:C.DataTypes.STRING,allowNull:!1},updatedBy:{type:C.DataTypes.STRING,allowNull:!1},slug:C.DataTypes.STRING,icon:C.DataTypes.STRING,pinnedAt:C.DataTypes.DATE,useAllResources:C.DataTypes.BOOLEAN,npmSecret:C.DataTypes.STRING,relatedBlocklets:{type:C.DataTypes.JSON,allowNull:!1,defaultValue:{},get(){const t=this.getDataValue("relatedBlocklets");if(typeof t=="object")return t??{};try{return t?JSON.parse(t):{}}catch(e){return f.logger.error("Failed to parse relatedBlocklets",{error:e,rawValue:t}),{}}},set(t){try{this.setDataValue("relatedBlocklets",t?JSON.stringify(t):"{}")}catch(e){f.logger.error("Failed to set relatedBlocklets",{error:e,value:t}),this.setDataValue("relatedBlocklets","{}")}}},productionState:{type:C.DataTypes.JSON,allowNull:!1,defaultValue:{},get(){const t=this.getDataValue("productionState");if(typeof t=="object")return t??{};try{return t?JSON.parse(t):{}}catch(e){return f.logger.error("Failed to parse productionState",{error:e,rawValue:t}),{}}},set(t){try{this.setDataValue("productionState",t?JSON.stringify(t):"{}")}catch(e){f.logger.error("Failed to set productionState",{error:e,value:t}),this.setDataValue("productionState","{}")}}}},{sequelize:M,paranoid:!0});L.hasMany(Ae,{foreignKey:"projectId",as:"components"});const It="z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o",ge="page",me="trigger-reload-project-resource",Pe=It,bt="z2qa7BQdkEb3TwYyEYC1psK6uvmGnHSUHt5RM";function Z(t){t.observeDeep(e=>{e.some(s=>s.changes.keys.has("updatedAt")||s.changes.keys.has("publishedAt"))||t.set("updatedAt",new Date().toISOString())})}function Ue(){return I.mkdtempSync(g.join(w.default.env.dataDir,"tmp-"))}function ee(t,e,s=[]){return Array.isArray(t)?t.flatMap((a,n)=>ee(a,e,[...s,n])):typeof t=="object"?t===null?[]:Object.entries(t).flatMap(([a,n])=>ee(n,e,[...s,a])):e(t)?[s]:[]}function R(t){return t.filter(e=>e!=null)}function Et(t){t.pages&&Object.keys(t.pages).forEach(s=>{const a=F.getYjsValue(t.pages[s]);a&&a instanceof U.Map&&Z(a)});const e=F.getYjsValue(t.pages);e&&e instanceof U.Map&&e.observe(s=>{s.changes.keys.forEach((a,n)=>{if(a.action==="add"){const o=F.getYjsValue(t.pages[n]);o&&o instanceof U.Map&&Z(o)}})})}function wt(t){t.routes&&Object.keys(t.routes).forEach(s=>{const a=F.getYjsValue(t.routes?.[s]);a&&a instanceof U.Map&&Z(a)});const e=F.getYjsValue(t.routes);e&&e instanceof U.Map&&e.observe(s=>{s.changes.keys.forEach((a,n)=>{if(a.action==="add"){const o=F.getYjsValue(t.routes?.[n]);o&&o instanceof U.Map&&Z(o)}})})}function At(t,e){for(const s of e||Object.keys(t.routes||{})){let a=s,n=[];if(s.includes("-")){const[o,...c]=s.split("-");a=o,n=c||[]}if(t.routes?.[a]!==void 0){t.routes[a].publishedAt=new Date().toISOString();const o=t.routes[a];if(!o||!o.params||o.params.length===0)continue;if(s.includes("-")&&n.length>0){const c=Q.getRouteMetaDataByOptionIds(n,o);c&&(c.publishedAt=new Date().toISOString())}if(!e){const c=Q.generateParamCombinations({basePath:o.path,params:o.params,routeId:o.id,paramsOptions:o.paramsOptions,currentIndex:0,currentParams:[],currentOptionIds:[],result:[]});for(const p of c)p.routeMetaData??={},p.routeMetaData.publishedAt=new Date().toISOString()}}}}function ue({page:t,route:e,state:s,routeId:a,routePathInfo:n}){f.logger.info(`Executing datasource data assembly, routeId: ${a}, routePathInfo: ${JSON.stringify(n)}`);const o={...Le.default(t),id:a,slug:n?.path??e.path,createdAt:e.createdAt,updatedAt:n?.routeMetaData?.updatedAt??e.updatedAt,publishedAt:n?.routeMetaData?.publishedAt??e.publishedAt,isPublic:(n?.routeMetaData?.isPublic??e.isPublic)&&e.isPublic};for(const c of s.supportedLocales){if(e.dataSource){let p=e.id;n&&(p=n.paramOptionIds.join("-"));const l=e.dataSource.pathDataMappings?.[p]?.dataCache?.[c.locale]??e.dataSource.pathDataMappings?.[p]?.dataCache?.[s.config.defaultLocale||"en"];if(!l)continue;We.setPageDataSource(o,s,c.locale,l)}n&&n.routeMetaData&&(n.routeMetaData.publishedAt=new Date().toISOString())}return o}["true","1","yes","y"].includes(process.env.USE_FS_CACHE_HTML??"");const Pt=60*60*1e3,Y=new be.LRUCache({max:100,ttl:Pt,ttlResolution:10*1e3,allowStale:!0});function jt(t,e=[]){let s=0;const a=Array.from(Y.keys());for(const n of a)for(const o of t){if(De.matchCacheKey(n,{currentPath:o})){Y.delete(n),s++,f.logger.info(`[Cache CLEAR] key: ${n}`);break}for(const c of e)if(De.matchCacheKey(n,{currentPath:`/${c}${o}`})){Y.delete(n),s++,f.logger.info(`[Cache CLEAR] key: ${n}`);break}}return f.logger.info(`[Cache CLEAR] cleared ${s} entries for paths:`,t),s}function Ot(){const t=Y.size;return Y.clear(),f.logger.info(`[Cache CLEAR ALL] cleared ${t} entries`),t}w.default.events.on(w.default.Events.envUpdate,Ot);const{uploadToMediaKit:Ct}=require("@blocklet/uploader-server"),je=/^\w+(\w|-|\.)+\w+\.(jpe?g|png|gif|svg|bmp|webp|mp4|m4v|webm)$/,H=/mediakit:\/\/([a-f0-9]{32}\.(jpe?g|png|gif|svg|bmp|webp|mp4|m4v|webm))/i,ke=/mediakit:\/\/([a-f0-9]{32}\.(jpe?g|png|gif|svg|bmp|webp|mp4|m4v|webm))/gi,Tt=1e4,Dt=3e4,J=0,de=1,vt=0,kt=1,he=w.default,q=g.join(process.env.BLOCKLET_DATA_DIR,"site-state"),Rt=["production","draft"],Lt=["production"];function te(t){return t?.replace(/\//g,"|")||""}function Me(){const t=he.env.languages?.map(s=>({locale:s.code,name:s.name}))||[],e=t[0]?.locale||"en";return{pageIds:[],pages:{},routeIds:[],routes:{},dataSourceIds:[],dataSources:{},components:{},supportedLocales:t,config:{defaultLocale:e},resources:{}}}class O extends U.Doc{constructor(e){super(),this.options=e,I.existsSync(this.draftYjsFilePath)&&U.applyUpdate(this,I.readFileSync(this.draftYjsFilePath)),this.syncedStore=tt.reactive(F.syncedStore({pages:{},pageIds:[],components:{},supportedLocales:[],config:{},resources:{},routeIds:[],routes:{},dataSourceIds:[],dataSources:{}},this)),this.initObserver(),this.on("update",this.updateHandler),this.awareness=new G.Awareness(this),this.awareness.on("update",this.awarenessChangeHandler),this.ensureDataStructure()}static RELEASE_DELAY=5*60*1e3;static PERIODIC_CHECK_INTERVAL=2*60*60*1e3;static sharedInstances={};static pageUrlMapCache=new be.LRUCache({max:100,ttl:1e3*60*60*24});static periodicCheckTimer;static safeDeleteProjectStateDir(e){if(!e)throw new Error("Should provide project context");try{const s=g.join(q,e),a=g.join(q,`@del-${e}`);I.renameSync(s,a)}catch(s){f.logger.error("Failed to safe delete project state dir:",s)}}static async getProjectIds(){return(await L.findAll({attributes:["id"],raw:!0}))?.map(e=>e.id)}static get projectIds(){return X.globSync("*/",{cwd:q,ignore:["@del-*","@tmp-*",".*","staging","production","@backup-*","undefined"]})}static get allShared(){return this.projectIds.map(e=>O.shared(e))}static shared(e){if(!e)throw new Error("Should provide project context");let s=O.sharedInstances[e];return s||(s=new O({path:g.join(q,e)}),O.sharedInstances[e]=s,s)}static async getProductionState(e){const s=await L.findByPk(e,{attributes:["productionState"]});if(we.default(s?.productionState)){const a=g.join(q,e,"production"),n=await Oe(a,{includeResources:!0})??Me();if(!n?.config?.defaultLocale){n.config??={};const o=he.env.languages?.map(c=>({locale:c.code,name:c.name}))||[];n.config.defaultLocale=o[0]?.locale}return n}return s?.productionState}destroy(){this.cancelRelease(),this.save({flush:!0}),this.conns.forEach((s,a)=>this.closeConn(a)),this.awareness.destroy();const e=g.basename(this.options.path);delete O.sharedInstances[e],super.destroy()}initObserver(){Et(this.syncedStore),wt(this.syncedStore)}get draftYjsFilePath(){return g.join(this.options.path,"draft.yjs")}static async getStateByProjectId(e,s){if(s==="draft"){const a=O.shared(e);return JSON.parse(JSON.stringify(a.syncedStore))}return O.getProductionState(e)}async getState(e){if(e==="draft")return JSON.parse(JSON.stringify(this.syncedStore));const s=g.basename(this.options.path);return O.getProductionState(s)}async setState(e,s){const a=await qe(s,{exportAssets:!1,includeResources:!0}),n=this.getPublishDir(e);if(I.mkdirSync(g.dirname(n),{recursive:!0}),I.rmSync(n,{force:!0,recursive:!0}),I.renameSync(a,n),e==="production"){const o=g.basename(this.options.path);O.pageUrlMapCache.delete(o),await L.update({productionState:{...s}},{where:{id:o}})}}getPublishDir(e){return g.join(this.options.path,e)}syncedStore;conns=new Map;awareness;releaseTimer;awarenessChangeHandler=({added:e,updated:s,removed:a},n)=>{const o=e.concat(s,a);if(n!==null){const l=this.conns.get(n);l&&(e.forEach(S=>{l.add(S)}),a.forEach(S=>{l.delete(S)}))}const c=k.createEncoder();k.writeVarUint(c,de),k.writeVarUint8Array(c,G.encodeAwarenessUpdate(this.awareness,o));const p=k.toUint8Array(c);this.conns.forEach((l,S)=>this.send(S,p))};updateHandler=e=>{const s=k.createEncoder();k.writeVarUint(s,J),le.writeUpdate(s,e);const a=k.toUint8Array(s);this.conns.forEach((n,o)=>this.send(o,a))};ensureDataStructure=()=>{const{supportedLocales:e,pages:s,pageIds:a,config:n,routes:o,routeIds:c}=this.syncedStore;{const p=new Set(Object.keys(s));let l=0;for(;l<a.length;){const S=a[l];p.has(S)?(p.delete(S),l++):a.splice(l,1)}}{const p=new Set(Object.keys(o));let l=0;for(;l<c.length;){const S=c[l];p.has(S)?(p.delete(S),l++):c.splice(l,1)}}e.splice(0,e.length),e.push(...he.env.languages.map(p=>({locale:p.code,name:p.name}))),n.defaultLocale=e[0]?.locale;{let p=0;const l=new Set;for(;p<e.length;){const{locale:S}=e[p];l.has(S)?e.splice(p,1):(p++,l.add(S))}}};send=(e,s)=>{e.readyState!==vt&&e.readyState!==kt&&this.closeConn(e);try{e.send(s,a=>{a&&this.closeConn(e)})}catch{this.closeConn(e)}};closeConn=e=>{if(e.removeAllListeners(),this.conns.has(e)){const s=this.conns.get(e);this.conns.delete(e),s&&G.removeAwarenessStates(this.awareness,Array.from(s),null)}e.close(),this.checkAndScheduleRelease()};checkAndScheduleRelease(){this.conns.size===0&&this.scheduleRelease()}scheduleRelease(){this.cancelRelease();const e=g.basename(this.options.path);this.releaseTimer=setTimeout(()=>{f.logger.info(`[SiteState] releasing instance due to no active connections: ${e}`),this.conns.size===0&&(this.releaseTimer=void 0,this.destroy())},O.RELEASE_DELAY),f.logger.info(`[SiteState] scheduled release for project ${e} in ${O.RELEASE_DELAY/1e3}s`)}cancelRelease(){if(this.releaseTimer){clearTimeout(this.releaseTimer),this.releaseTimer=void 0;const e=g.basename(this.options.path);f.logger.info(`[SiteState] cancelled scheduled release for project ${e}`)}}autoSave=_e.default(()=>{I.mkdirSync(g.dirname(this.draftYjsFilePath),{recursive:!0}),I.writeFileSync(this.draftYjsFilePath,U.encodeStateAsUpdate(this))},Tt);save=({flush:e=!1}={})=>{this.autoSave(),e&&this.autoSave.flush()};publish=async({mode:e,routes:s})=>{const a=await this.getState("draft"),n=await this.getState("production");await Se(a,n,{routes:s,mergeMode:"replace",deleteRoutes:!0,publishMode:e}),n.config.publishedAt=new Date().getTime(),At(this.syncedStore,s),await this.setState(e,n),await this.clearPageCacheForRoutes(s,n)};mergeState=async(e,s)=>{const a=JSON.parse(JSON.stringify(s));e.config.fontFamily??={};const n=a.config?.fontFamily,o=e.config?.fontFamily;e.config.fontFamily.title=n?.title||o?.title,e.config.fontFamily.description=n?.description||o?.description,await new Promise((c,p)=>{this.transact(async()=>{try{const l=await Se(e,s);c(l)}catch(l){p(l)}})})};clearPageCacheForRoutes=async(e,s)=>{const a=g.basename(this.options.path),o=(await L.findByPk(a))?.slug||a;let c=e;(!c||c.length===0)&&(c=s.pageIds??[]),f.logger.info(`[SiteState] clearing page cache for project ${a}, routes:`,c||[]);const p=s.supportedLocales.map(A=>A.locale),l=[],S=c.filter(A=>s.pageIds?.includes(A));for(const A of S){const E=s.pages[A].slug;o&&o!==a&&(o==="/"?l.push(E):l.push(`/${o}${E}`)),l.push(`/${a}${E}`)}if(f.logger.info(`[SiteState] clearing page cache for project ${a}, pathPatterns:`,l),l.length>0)try{const A=jt(l,p);f.logger.info(`[SiteState] cleared ${A} page cache entries for project ${a}, routes:`,c)}catch{}};addConnection=e=>{if(this.conns.has(e))return;this.cancelRelease(),e.binaryType="arraybuffer",this.conns.set(e,new Set),e.on("message",n=>this.messageListener(e,new Uint8Array(n)));let s=!0;const a=setInterval(()=>{if(!s)this.conns.has(e)&&this.closeConn(e),clearInterval(a);else if(this.conns.has(e)){s=!1;try{e.ping()}catch{this.closeConn(e),clearInterval(a)}}},Dt);e.on("close",()=>{this.closeConn(e),clearInterval(a)}),e.on("pong",()=>{s=!0});{const n=k.createEncoder();k.writeVarUint(n,J),le.writeSyncStep1(n,this),this.send(e,k.toUint8Array(n));const o=this.awareness.getStates();if(o.size>0){const c=k.createEncoder();k.writeVarUint(c,de),k.writeVarUint8Array(c,G.encodeAwarenessUpdate(this.awareness,Array.from(o.keys()))),this.send(e,k.toUint8Array(c))}}};messageListener=(e,s)=>{try{const a=k.createEncoder(),n=pe.createDecoder(s),o=pe.readVarUint(n);switch(o){case J:k.writeVarUint(a,J),le.readSyncMessage(n,a,this,null),k.length(a)>1&&(this.ensureDataStructure(),this.send(e,k.toUint8Array(a)));break;case de:{G.applyAwarenessUpdate(this.awareness,pe.readVarUint8Array(n),e);break}default:f.logger.warn(`Unsupported messageType ${o}`)}}catch(a){f.logger.error(a)}this.save()};static async pageUrlMap(e,s){let a=[];s?a=[s]:a=await this.getProjectIds();let n={};if(e==="production"&&a?.length){const o=new Map(a?.map(c=>[c,!0])||[]);for(const c of a){const p=O.pageUrlMapCache.get(c);p&&(n={...n,...p},o.delete(c))}a=Array.from(o.keys())}if(a?.length){const o=await L.findAll({where:{id:{[C.Op.in]:a}}});await Promise.all(o?.map(async c=>{const p=c.id,l=c.slug||p,S={},A=e==="production"&&c?.productionState?c.productionState:await O.getStateByProjectId(c.id,e),v=gt.default(w.default.env.languages?.map(u=>u.code)||[],A.supportedLocales?.map(u=>u.locale)||[]),E=(u,i)=>{l&&(S[z.joinURL("/",l,u)]={...i,shouldRedirect:!0,mainPage:!0}),S[z.joinURL("/",p,u)]={...i,shouldRedirect:!0,mainPage:!0};for(const h of v){const T={...i,locale:h};S[z.joinURL("/",h,p,u)]=T,l&&(S[z.joinURL("/",h,l,u)]=T)}};if(e==="draft")for(const u of A.routeIds||[]){const i=A?.routes?.[u];if(!i)continue;if(i.params&&i.params.length>0){const m=Q.generateParamCombinations({basePath:i.path,params:i.params,routeId:i.id,paramsOptions:i.paramsOptions,currentIndex:0,currentParams:[],currentOptionIds:[],result:[]});for(const d of m){const y=d.path,P={projectId:p,projectSlug:l,pageSlug:y,pageId:i.displayTemplateId||"",routeId:u,defaultLocale:v?.[0],locales:v,publishedAt:A.config.publishedAt,isPublic:i.isPublic&&d?.routeMetaData?.isPublic};E(y,P)}}const h=i.path,T={projectId:p,projectSlug:l,pageSlug:h,pageId:i.displayTemplateId||"",routeId:u,defaultLocale:v?.[0],locales:v,publishedAt:A.config.publishedAt,isPublic:i.isPublic};E(h,T)}for(const u of A.pageIds||[]){const i=A.pages[u];if(!i||e==="production"&&!i.isPublic)continue;const h=i.slug,T=c.slug||p,m={projectId:p,projectSlug:T,pageSlug:h,pageId:u,defaultLocale:v?.[0],locales:v,publishedAt:A.config.publishedAt,isPublic:i.isPublic,templateConfig:i.templateConfig};E(h,m)}e==="production"&&O.pageUrlMapCache.set(p,S),n={...n,...S}}))}return n}getDocumentSize(){return U.encodeStateAsUpdate(this).byteLength}static getInstancesSizeInfo(){const e=[];for(const[s,a]of Object.entries(O.sharedInstances)){const n=a.getDocumentSize();e.push({projectId:s,sizeInBytes:n,sizeInMB:`${(n/(1024*1024)).toFixed(2)} MB`,activeConnections:a.conns.size})}return e}static startPeriodicCheck(){this.periodicCheckTimer||(this.periodicCheckTimer=setInterval(()=>{this.performPeriodicCheck()},this.PERIODIC_CHECK_INTERVAL),f.logger.info(`[SiteState] periodic check started, interval: ${this.PERIODIC_CHECK_INTERVAL/(60*60*1e3)} hours`))}static stopPeriodicCheck(){this.periodicCheckTimer&&(clearInterval(this.periodicCheckTimer),this.periodicCheckTimer=void 0,f.logger.info("[SiteState] periodic check stopped"))}static performPeriodicCheck(){const e=Object.keys(O.sharedInstances).length,s=[],a=[];for(const[n,o]of Object.entries(O.sharedInstances))o.conns.size===0?s.push({projectId:n,instance:o}):a.push({projectId:n,connections:o.conns.size});if(f.logger.info(`[SiteState] periodic check summary: total instances: ${e}, with connections: ${a.length}, without connections: ${s.length}`),s.length>0){f.logger.info(`[SiteState] releasing ${s.length} instances without connections:`,s.map(o=>o.projectId));let n=0;for(const{projectId:o,instance:c}of s)try{f.logger.info(`[SiteState] releasing instance due to periodic check: ${o}`),c.destroy(),n++}catch(p){f.logger.error(`[SiteState] failed to release instance ${o} during periodic check:`,p)}f.logger.info(`[SiteState] periodic check completed: ${n}/${s.length} instances released successfully`)}else e>0?f.logger.debug("[SiteState] periodic check: all instances have active connections"):f.logger.debug("[SiteState] periodic check: no instances exist")}}async function _t(t,e,s){if(!t||!I.existsSync(t)||!I.lstatSync(t).isFile())return null;let a=s[t];return a||(a=(async()=>{try{return(await Ct({filePath:t,fileName:e}))?.data?.filename}catch(n){return f.logger.error(`Failed to upload asset ${t}:`,n),null}})(),s[t]=a),a}const $e=async(t,e)=>{const s=g.basename(t),a=await Ie.call({name:Pe,path:z.joinURL("/uploads",s),responseType:"stream",method:"GET"});if(a.status>=200&&a.status<400){const n=I.createWriteStream(e);await pt.pipeline(a.data,n)}else throw new Error(`download asset failed ${a.status}`)},Ne=async(t,e)=>{await Promise.all(t.map(async s=>{try{await $e(s,g.join(e,g.basename(s)))}catch(a){f.logger.error(`Failed to export assets: ${s}, ${a}`)}}))};function Fe(t){return je.test(t)?[t]:H.test(t)?(ke.lastIndex=0,Array.from(t.matchAll(ke)).map(s=>s[1]).filter(s=>!!s)):[]}async function W(t,e,s){const{getFilename:a,exportAssets:n}=s,o=g.join(e,a(t));if(I.mkdirSync(g.dirname(o),{recursive:!0}),I.writeFileSync(o,x.stringify(t)),n){const p=ee(t,l=>typeof l=="string"&&(je.test(l)||H.test(l))).map(l=>{const S=Ee.default(t,l);return Fe(S)}).flat().filter(Boolean);await Ne(p,g.dirname(o))}}const ye=new be.LRUCache({max:100,ttl:1*60*1e3});async function Re(t,e,s){const a=ee(t,p=>typeof p=="string"&&(je.test(p)||H.test(p))),n=mt.default(2),o=a.map(p=>n(async()=>{try{const l=Ee.default(t,p),S=Fe(l);for(const A of S){const v=g.basename(A),E=s.getFilePath(A,p),u=E?`${E}:${v}`:v,i=ye.get(u);if(i){H.test(l)||ve.default(t,p,i);return}const h=await _t(E,v,e);h&&(H.test(l)||ve.default(t,p,h),ye.set(u,h))}}catch(l){f.logger.error(`Failed to process upload for path ${p.join(".")}:`,l.message||l.reason)}})),c=await Promise.allSettled(o);s.onFinish?.(c)}async function qe(t,{exportAssets:e,pageIds:s="all",componentIds:a="all",rawConfig:n,includeResources:o=!1,routeIds:c="all"}={}){const p=s==="all"?t.pageIds:s,l=et.getComponentDependencies({state:t,pageIds:p,componentIds:a==="all"?Object.keys(t.components):a});Object.entries(t.components).forEach(([r,b])=>{b.data?.renderer?.type==="component-template"&&l.push(r)});const S=c==="all"?t.routeIds:c,A=r=>({id:r.id,name:r.name,isTemplateSection:r.isTemplateSection??!1,templateDescription:r.templateDescription,component:r.component,config:r.config,visibility:r.visibility,sections:r?.sectionIds?R(r?.sectionIds?.map(b=>{const D=r.sections?.[b];return D&&A(D)})):void 0}),v=(r,b)=>({id:r.id,createdAt:r.createdAt,updatedAt:r.updatedAt,publishedAt:r.publishedAt,isPublic:r.isPublic??!0,templateConfig:r.templateConfig,meta:r.locales?.[b]??{},sections:R(r.sectionIds.map(D=>{const N=r.sections[D];return N&&A(N)})),dataSource:Object.fromEntries(Object.entries(r.dataSource||{}).map(([D,N])=>[D,N?.[b]??{}]))}),E=r=>({id:r.id,createdAt:r.createdAt,updatedAt:r.updatedAt,publishedAt:r.publishedAt,path:r.path,handler:r.handler,isPublic:r.isPublic??!0,params:r.params??[],enabledGenerate:r.enabledGenerate??!1,displayTemplateId:r.displayTemplateId,dataSource:r.dataSource}),u=R(S.map(r=>{const b=t.routes[r];return b&&E(b)})),i=R(t.supportedLocales.map(r=>r.locale).flatMap(r=>p.map(b=>{const D=t.pages[b];return D&&{locale:r,slug:D.slug,page:v(D,r)}}))),h=Ue(),T=g.join(h,"pages");I.mkdirSync(T,{recursive:!0});const m=g.join(h,"components");I.mkdirSync(m,{recursive:!0});const d=g.join(h,"routes");I.mkdirSync(d,{recursive:!0});for(const{locale:r,slug:b,page:D}of i)await W(D,T,{getFilename:()=>`${te(b)||"index"}.${r}.yml`,exportAssets:e});for(const r of u)await W(r,d,{getFilename:()=>`${te(r.path)||"index"}.yml`,exportAssets:e});for(const r of l){const b=t.components[r]?.data;b&&await W(b,m,{getFilename:D=>`${D.name||"unnamed"}.${D.id}.yml`,exportAssets:e})}const y=g.join(h,".blocklet/pages/pages.config.yml");I.mkdirSync(g.dirname(y),{recursive:!0});const P={pages:R(p.map(r=>{const b=t.pages[r];return b&&{id:r,slug:b.slug}})),routes:R(S.map(r=>{const b=t.routes[r];return b&&{id:r,path:b.path}})),components:R(l.map(r=>{const b=t.components[r]?.data;return b&&{id:r,name:b.name}})),...o?{resources:{components:R(Object.keys(t.resources?.components||{}).filter(r=>l.includes(r)).map(r=>({id:r,name:t.resources?.components?.[r]?.component?.name})))}}:{},supportedLocales:t.supportedLocales,config:t.config};I.writeFileSync(y,x.stringify(P));const j=g.join(h,"config.source.json");if(n&&I.writeFileSync(j,JSON.stringify(n)),o){const r=g.join(h,"resources"),b=g.join(r,"components");I.mkdirSync(b,{recursive:!0});for(const B of Object.keys(t?.resources?.components??{}).filter(_=>l.includes(_))){const _=t.resources?.components?.[B]?.component;_&&await W(_,b,{getFilename:V=>`${V.name||"unnamed"}.${V.id}.yml`,exportAssets:e})}const D=g.join(h,"chunks");I.mkdirSync(D,{recursive:!0});const N=Ut();for(const B of Object.keys(t?.resources?.components??{}).filter(_=>l.includes(_))){const _=t.resources?.components?.[B]?.component;if(_&&_.renderer?.type==="react-component"){const V=_.renderer?.chunks??[];if(V?.length>0)for(const ie of V){const Ce=g.join(D,ie),ce=N?.[ie];try{ce&&I.existsSync(ce)&&!I.existsSync(Ce)&&I.copyFileSync(ce,Ce)}catch(Ke){f.logger.error(`copy chunk ${ie} error`,Ke.message)}}}}}return h}async function Oe(t,{importAssets:e,includeResources:s}={}){if(!I.existsSync(t))return null;let a,n=!1;try{I.lstatSync(t).isDirectory()?a=t:/\.(tgz|gz|tar)$/.test(t)&&(n=!0,a=Ue(),await ut.x({file:t,C:a}));const o=X.globSync("**/.blocklet/pages/pages.config.yml",{cwd:a,absolute:!0}).at(0),c=o&&g.join(g.dirname(o),"../../pages"),p=o&&g.join(g.dirname(o),"../../components"),l=o&&g.join(g.dirname(o),"../../routes");if(!o)return null;const S=x.parse(I.readFileSync(o).toString()),A=(m,d,y)=>{let P=g.join(m,`${d}${y?`.${y}`:""}.yml`);return(!I.existsSync(P)||!I.lstatSync(P).isFile())&&(P=g.join(m,d,`index${y?`.${y}`:""}.yml`),!I.existsSync(P)||!I.lstatSync(P))?null:x.parse(I.readFileSync(P).toString())},v=(m,d)=>{try{const y=X.globSync(`*.${d}.yml`,{cwd:m,absolute:!0})[0];return y?x.parse(I.readFileSync(y).toString()):null}catch(y){f.logger.error("parse component error",y)}return null},E=(m,d)=>{let y=g.join(m,`${d}.yml`);return(!I.existsSync(y)||!I.lstatSync(y).isFile())&&(y=g.join(m,d,"index.yml"),!I.existsSync(y)||!I.lstatSync(y))?null:x.parse(I.readFileSync(y).toString())},u=R(S.pages.map(({slug:m})=>{const d=R(S.supportedLocales.map(({locale:j})=>{const r=c?A(c,te(m),j):void 0;if(r)return{locale:j,page:r};const b=c?A(c,m,j):void 0;return b&&{locale:j,page:b}})),y=d[0]?.page;if(!y)return null;const P=y.sections.map(Ze.unzipSection);return{id:y.id||Te.nextId(),createdAt:y.createdAt,updatedAt:y.updatedAt,publishedAt:y.publishedAt,isPublic:y.isPublic??!0,templateConfig:y.templateConfig,slug:m,sections:Object.fromEntries(P.map(j=>[j.id,j])),sectionIds:P.map(j=>j.id),locales:Object.fromEntries(d.map(({locale:j,page:r})=>[j,r.meta])),dataSource:y.dataSource?Object.fromEntries([...new Set(d.flatMap(({page:j})=>Object.keys(j.dataSource??{})))].map(j=>[j,Object.fromEntries(d.map(({locale:r,page:b})=>{const D=b.dataSource?.[j];return[r,D||{}]}))])):Object.fromEntries([...new Set(d.flatMap(({page:j})=>j.sections.map(r=>r.id)))].map(j=>[j,Object.fromEntries(d.map(({locale:r,page:b})=>{const D=b.dataSource?.[j];if(D)return[r,D];const N=b.sections.find(B=>B.id===j);return[r,N?.properties||{}]}))]))}})),i=R(S?.routes?.map(({path:m})=>{const d=l?E(l,te(m)):void 0;return{...d,id:d?.id||Te.nextId(),createdAt:d?.createdAt??new Date().toISOString(),updatedAt:d?.updatedAt??new Date().toISOString(),publishedAt:new Date(0).toISOString(),path:d?.path??`/${d?.id}`,params:d?.params,handler:d?.handler??"Pages Kit",isPublic:d?.isPublic??!0,enabledGenerate:d?.enabledGenerate??!1,displayTemplateId:d?.displayTemplateId??void 0,dataSource:d?.dataSource??{}}})??[]),h=p?R(S.components?.map(({id:m})=>v(p,m))??[]):[];if(e){const m=(...d)=>{f.logger.info(`[${n?g.basename(t):g.basename(g.join(t,"../../../../"))}] importAssets:`,...d)};try{m("wait image-bin api ready"),await ht.default({resources:[`${Ie.getComponentWebEndpoint(f.IMAGE_BIN_NAME)}/api/sdk/uploads`],validateStatus:P=>P>=200&&P<=500}),m("image-bin api is ready");const d={},y={};m("start to upload assets"),await Promise.allSettled([Re(h,d,{getFilePath:P=>p&&g.join(p,P),onFinish:P=>{m(`upload ${P.length} component assets`)}}),Re(u,y,{getFilePath:(P,j)=>{const r=Ee.default(u,j.slice(0,1));return c&&g.join(c,g.dirname(r.slug),P)},onFinish:P=>{m(`upload ${P.length} page assets`)}})]),m("upload assets done"),ye.clear(),global.gc&&global.gc()}catch(d){m("Error during asset import:",d)}}const T={};if(s){const m=o&&g.join(g.dirname(o),"../../resources/components"),d=R(S.resources?.components?.map(({id:y})=>v(m,y))??[]);d.length>0&&(T.components=Object.fromEntries(d.map((y,P)=>[y.id,{index:P,component:y}])))}return{supportedLocales:S.supportedLocales,pageIds:u.map(m=>m.id),components:Object.fromEntries(h.map((m,d)=>[m.id,{index:d,data:m}])),pages:Object.fromEntries(u.map(m=>[m.id,m])),config:S.config||{},resources:T,routeIds:i.map(m=>m.id),routes:Object.fromEntries(i.map(m=>[m.id,m])),dataSourceIds:[],dataSources:{}}}finally{n&&a&&I.rmSync(a,{force:!0,recursive:!0})}}async function Se(t,e,{routes:s,mergeMode:a="byUpdateTime",deleteRoutes:n=!1,publishMode:o=void 0}={}){try{o&&f.clearPreloadComponentsCacheByMode(o)}catch(E){f.logger.error("clear preload page cache error",{error:E})}const{pages:c,pageIds:p,routeIds:l,routes:S,supportedLocales:A}=t;if(o==="production"){let E=s??[],u=null;for(const i of l??[]){const h=S?.[i];if(h?.params&&h?.params.length>0&&h?.paramsOptions&&h?.paramsOptions.length>0){const T=Q.generateParamCombinations({basePath:h.path,params:h.params,routeId:h.id,paramsOptions:h.paramsOptions,currentIndex:0,currentParams:[],currentOptionIds:[],result:[]}),m=Object.fromEntries(T.map(d=>[`${i}-${d.paramOptionIds.join("-")}`,d]));u={...u||{},...m},s||(E=[...E,...T.map(d=>`${i}-${d.paramOptionIds.join("-")}`)])}else s||E.push(i)}f.logger.info("routeIds to be published: ",E);for(const i of E){let h=i;if(h.includes("-")){const[d]=h.split("-");h=d}const T=S?.[h];if(!T){const d=e.pageIds.indexOf(h);d!==-1&&n&&(e.pageIds.splice(d,1),delete e.pages[h]);for(const y of e.pageIds)y.includes(`${h}-`)&&(e.pageIds.splice(e.pageIds.indexOf(y),1),delete e.pages[y]);f.logger.info("delete main route page",h);continue}if(i.includes("-")&&!u?.[i]){const d=e.pageIds.indexOf(i);d!==-1&&n&&(e.pageIds.splice(d,1),delete e.pages[i]),f.logger.info("delete page",i);continue}if(!T.displayTemplateId){f.logger.info("no display template",i);continue}const m=c[T.displayTemplateId];if(!m){f.logger.info("no template page",i);continue}if(e.pageIds.includes(i)){if(f.logger.info("has need update page",i),a==="replace")e.pages[i]=ue({page:m,route:T,state:t,routeId:i,routePathInfo:u?.[i]}),f.logger.info("replace page",i);else if(a==="byUpdateTime"){const d=e.pages[T.id];(!d||T.updatedAt&&T.updatedAt>d.updatedAt)&&(e.pages[i]=ue({page:m,route:T,state:t,routeId:i,routePathInfo:u?.[i]}),f.logger.info("replace page by update time",i))}}else e.pageIds.push(i),e.pages[i]=ue({page:m,route:T,state:t,routeId:i,routePathInfo:u?.[i]}),f.logger.info("add page",i)}if(n&&!s)for(const i of e.pageIds)E?.includes(i)||(delete e.pages[i],f.logger.info("delete page",i)),e.pageIds=[...e.pageIds].filter(h=>E?.includes(h))}else{for(const E of p){const u=c[E];if(u)if(e.pageIds.includes(u.id)){if(a==="replace")e.pages[u.id]=u;else if(a==="byUpdateTime"){const i=e.pages[u.id];(!i||u.updatedAt&&u.updatedAt>i.updatedAt)&&(e.pages[u.id]=u)}}else e.pageIds.push(u.id),e.pages[u.id]=u}for(const E of l){const u=S[E];if(u)if(e.routeIds.includes(u.id)){if(a==="replace")e.routes[u.id]=u;else if(a==="byUpdateTime"){const i=e.routes[u.id];(!i||u.updatedAt&&u.updatedAt>i.updatedAt)&&(e.routes[u.id]=u)}}else e.routeIds.push(u.id),e.routes[u.id]=u}}if(e.supportedLocales.splice(0,e.supportedLocales.length),e.supportedLocales.push(...Le.default(A)),n)for(const E of Object.keys(e.components))delete e.components[E];let v=JSON.parse(JSON.stringify(t.components));v=Object.fromEntries(await Promise.all(Object.entries(v).map(async([E,u])=>{const i=await xe(u?.data);return[E,{...u,data:i}]}))),Object.assign(e.components,v),Object.assign(e.config,JSON.parse(JSON.stringify(t.config))),we.default(t.resources.components)||(e.resources.components=JSON.parse(JSON.stringify(t.resources.components||{})))}const xe=f.memoizeWithFs(async t=>{if(!we.default(t?.properties))return t;if(t?.renderer?.type==="react-component"){const{script:e,PROPERTIES_SCHEMA:s}=t?.renderer||{};if(s||e&&e.includes("PROPERTIES_SCHEMA"))try{const a=await f.getExportSchemaValueFromCode(e??"","PROPERTIES_SCHEMA",t.id,s);a&&a.length>0&&t&&(t.properties={},a.forEach((n,o)=>{t?.properties&&(t.properties[n.id]={index:o,data:n})}))}catch(a){f.logger.error("getPropertiesFromCode error",{componentId:t.id,name:t.name},{error:a})}}return t},{subdir:"getPropertiesFromCode"});let se,K,ne,oe;const Be=()=>Ie.getResources({types:[{did:Pe,type:ge},{did:bt,type:ge}]}),Ut=()=>{const t=Be(),e={};return t.forEach(s=>{const a=X.globSync("**/.blocklet/pages/pages.config.yml",{cwd:s.path,absolute:!0}).at(0),n=a&&g.join(g.dirname(a),"../../chunks");if(n&&I.existsSync(n)){const o=I.readdirSync(n);for(const c of o)e[c]=g.join(n,c)}}),e};function Ve(){return se=(async()=>{const t=Be();K=(await Promise.all(t.map(async s=>{const a=s.path?await Oe(s.path,{importAssets:!1}):void 0;return a?{blockletId:s.did,state:a,blockletTitle:s.title}:void 0}))).filter(s=>!!s),ne=K.reduce((s,a)=>Object.assign(s,Object.fromEntries(Object.values(a.state.pages).map(n=>n?[n?.id,{page:n,blockletId:a.blockletId}]:[]))),{});const e=K.reduce((s,a)=>Object.assign(s,Object.fromEntries(Object.values(a.state.components).map(n=>[n.data.id,{blockletId:a.blockletId,component:n.data}]))),{});oe=Object.fromEntries(await Promise.all(Object.entries(e).map(async([s,a])=>{const n=await xe(a.component);return[s,{...a,component:n}]})))})(),se}function Ge(t){const e=_e.default(async()=>{await Ve().catch(s=>{f.logger.error("load resource states error",{error:s})}),await t?.({states:K,pages:ne,components:oe})},3e3,{leading:!1,trailing:!0});return e(),w.default.events.on(w.default.Events.componentAdded,e),w.default.events.on(w.default.Events.componentRemoved,e),w.default.events.on(w.default.Events.componentStarted,e),w.default.events.on(w.default.Events.componentStopped,e),w.default.events.on(w.default.Events.componentUpdated,e),w.default.events.on(me,e),()=>{w.default.events.off(w.default.Events.componentAdded,e),w.default.events.off(w.default.Events.componentRemoved,e),w.default.events.off(w.default.Events.componentStarted,e),w.default.events.off(w.default.Events.componentStopped,e),w.default.events.off(w.default.Events.componentUpdated,e),w.default.events.off(me,e)}}const ze=Symbol.for("GLOBAL_RESOURCE_STATES_LISTENER_KEY"),Ye=Symbol.for("GLOBAL_ENV_UPDATE_LISTENER_KEY"),re=globalThis;re[ze]?.();re[ze]=Ge(async({pages:t,components:e})=>{const s=await O.getProjectIds();f.logger.info(`start update resource states projects(${s.length})`,s),await Promise.race([new Promise(a=>{setTimeout(()=>{a({})},30*1e3)}),Promise.all(s.map(async a=>{He({projectId:a,pages:t,components:e})}))]).catch(a=>{f.logger.error("update resource states failed:",a)})});re[Ye]?.();re[Ye]=()=>{const t=()=>{O.pageUrlMapCache.clear(),f.logger.info("[Cache CLEAR ALL] clear all page url map cache by env update")};return w.default.events.on(w.default.Events.envUpdate,t),()=>{w.default.events.off(w.default.Events.envUpdate,t)}};O.startPeriodicCheck();process.on("beforeExit",()=>{O.stopPeriodicCheck()});process.on("SIGINT",()=>{O.stopPeriodicCheck(),process.exit(0)});process.on("SIGTERM",()=>{O.stopPeriodicCheck(),process.exit(0)});async function He({projectId:t,pages:e,components:s}){const a=O.sharedInstances[t];if(!a){f.logger.info(`projectId: ${t} not found in sharedInstances`);return}if(a.syncedStore.resources.pages=e,(await L.findByPk(t))?.useAllResources)a.syncedStore.resources.components=s;else{const c=(await Ae.findAll({where:{projectId:t}})).map(l=>l.componentId),p=Object.fromEntries(Object.entries(s||{}).filter(([l])=>c.includes(l)));a.syncedStore.resources.components=p}f.logger.info(`update [${t}] resource states:`,{pages:Object.keys(a.syncedStore.resources.pages||{}).length,components:Object.keys(a.syncedStore.resources.components||{}).length})}async function Mt(t){return He({projectId:t,pages:ne,components:oe})}async function $t(){f.logger.info("trigger reload all project resource"),w.default.events.emit(me)}async function Nt({ensureLoaded:t=!0}={}){return t&&(se??=Ve(),await se),{states:K,pages:ne,components:oe}}exports.COMPONENT_DID=Pe;exports.PUBLISH_MODES=Lt;exports.Project=L;exports.RESOURCE_TYPE=ge;exports.SITE_STATE_PATH=q;exports.STATE_MODES=Rt;exports.SiteState=O;exports.downloadAsset=$e;exports.downloadAssets=Ne;exports.fromPackage=Oe;exports.getDefaultState=Me;exports.getResourceStates=Nt;exports.initPackResourceStates=Ge;exports.mergeState=Se;exports.toPackage=qe;exports.triggerReloadAllProjectResource=$t;exports.updateResourceStatesByProjectId=Mt;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";const me=require("react/jsx-runtime"),R=require("ufo"),ge=require("@blocklet/js-sdk"),Se=require("lodash/isEmpty"),Ie=require("yaml"),Pe=require("@arcblock/ux/lib/Locale/context"),q=require("@blocklet/pages-kit/utils/common"),ye=require("@blocklet/pages-kit/utils/page-model");require("@mui/material");const ee=require("@syncedstore/core"),Ce=require("@syncedstore/react"),we=require("ahooks"),J=require("immer"),he=require("lodash/cloneDeep"),be=require("lodash/debounce"),g=require("react"),te=require("react-router-dom"),ve=require("y-indexeddb"),Oe=require("y-websocket"),xe=require("yjs");require("@blocklet/pages-kit/types");const G=o=>o&&o.__esModule?o:{default:o};function ke(o){if(o&&o.__esModule)return o;const n=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(o){for(const S in o)if(S!=="default"){const f=Object.getOwnPropertyDescriptor(o,S);Object.defineProperty(n,S,f.get?f:{enumerable:!0,get:()=>o[S]})}}return n.default=o,Object.freeze(n)}const Ee=G(Se),Te=ke(Ie),je=G(ee),A=G(he),oe=G(be);function ne(){const{pathname:o}=window.location,S=o.match(/\/admin\/(maker|previewer)\/([^/]+)/)?.[2],f=window.__PROJECT_ID__;if(!S&&!f)throw new Error("Unable to get projectId from pathname");return S||f}function Me(o){return`${o}:pages:version`}function Ne(o,n){return n==="yaml"?Ee.default(o)?"":Te.stringify(o,{indent:2}):n==="json"?JSON.parse(JSON.stringify(o||{})):n==="array"?JSON.parse(JSON.stringify(o||[])):o}const K=window.blocklet?.prefix||"/",z=ge.createAxios({timeout:200*1e3}),Le=window.location.protocol==="https:"?"wss":"ws",_e=o=>R.joinURL(`${Le}://${window.location.hostname}`,K,`api/${o}/ws/pages`),Ae="z8ia1mAXo8ZE7ytGF36L5uBf9kD2kenhqFGp9";blocklet?.componentMountPoints.find(o=>o.did===Ae)?.mountPoint||R.joinURL(K,"/image-bin");function Re(o){return o&&!/^(https?:\/\/|\/)/.test(o)?window.location.origin+R.joinURL(K,"uploads",o):o}const De=window.innerWidth<=750;function $e(o,n,S){if(!o||/\.gif/.test(o))return o;const f=(De?n/1.5:n)*(S==="quality"?1.5:1);return Je(o,{imageFilter:"resize",w:f,f:"webp"})}function Je(o,n){return!o||!n?o:R.withQuery(o,n)}async function qe(o){return z.get(`/api/projects/${o}`).then(n=>n.data)}async function Ue(o){return z.post(`/api/projects/${o.id}/update`,o).then(n=>n.data)}const X="@COMPONENT-TEMPLATE-",W=o=>`${X}${o}`,F=o=>o.startsWith(X),Z=o=>F(o)?o.slice(X.length):null;function ce(o,n={}){return n[o.id]=o,o.sectionIds&&o.sections&&o.sectionIds.forEach(S=>{const f=o.sections?.[S];f&&ce(f,n)}),n}function re(o,n){const S=W(o.id),f=n.section,O=ce(f);n.dataSource??={};const w=n.dataSource;return{id:S,createdAt:o.createdAt,updatedAt:o.updatedAt,publishedAt:o.updatedAt,slug:o.id,sections:O,sectionIds:[f.id],isPublic:!0,templateConfig:{isTemplate:!0},locales:{},dataSource:w}}const Be=500,se=g.createContext(null);function Fe({url:o,name:n,children:S,waitingSynced:f}){const[O,w]=g.useState(!1),{locale:L}=Pe.useLocaleContext(),b=Q(),j=`${b}:MakerLocalState`,x=`${b}:${n}`,[v,P]=we.useLocalStorageState(j,{defaultValue:{},listenStorageChange:!0}),D=g.useRef(v);D.current=v;const $=g.useMemo(()=>je.default({dataSourceIds:[],dataSources:{},pages:{},pageIds:[],routes:{},routeIds:[],components:{},supportedLocales:[],config:{},resources:{}}),[]),C=g.useMemo(()=>ee.getYjsDoc($),[$]);g.useMemo(()=>new ve.IndexeddbPersistence(x,C),[x,C]);const h=g.useMemo(()=>new Oe.WebsocketProvider(o,x,C,{params:{},resyncInterval:5*60*1e3}),[o,x,C]),B=g.useCallback(()=>{h&&(h.wsconnected?(h.disconnect(),setTimeout(()=>h.connect(),100)):h.connect())},[h]),e=g.useMemo(()=>new xe.UndoManager([C.getArray("pageIds"),C.getMap("pages"),C.getArray("supportedLocales"),C.getMap("config"),C.getMap("dataSources"),C.getArray("dataSourceIds"),C.getArray("routeIds"),C.getMap("routes")],{doc:C}),[C]),[t,c]=g.useState(null),[r,d]=g.useState(!1),[i,l]=g.useState(void 0),m=g.useCallback(async()=>{if(b){d(!0);try{const s=await qe(b);c(s)}catch(s){console.error("Failed to fetch project:",s)}finally{d(!1)}}},[b]),u=g.useCallback(async(s,a)=>{if(l(void 0),!!b){a?.optimisticUpdate&&t&&c(I=>({...I,...s}));try{const I=await Ue({...s});c(I)}catch(I){console.error("Failed to update project:",I),a?.optimisticUpdate&&c(t),l(I?.response?.data)}}},[b,t]);g.useEffect(()=>{b&&m()},[b,m]);const p=g.useMemo(()=>({doc:C,state:$,localState:{...v,currentLocale:L},setLocalState:s=>P(typeof s=="function"?a=>s(a??{}):s),undoManager:e,projectState:{project:t,isLoadingProject:r,error:i},updateProject:u,forceSync:B}),[C,$,JSON.stringify(v),P,e,t,r,u,i,L,B]);return g.useEffect(()=>{e.on("stack-item-added",({stackItem:s})=>{const{currentPageId:a,currentLocale:I,pages:y}=D.current;s.meta.set("localState",{currentPageId:a,currentLocale:I,pages:y})}),e.on("stack-item-popped",({stackItem:s})=>{const a=s.meta.get("localState");a&&P(I=>({...I,...a}))})},[e,P]),g.useEffect(()=>{h.awareness.setLocalState(v)},[h,v]),g.useEffect(()=>{h.once("synced",()=>{w(!0)});let s=0,a=null;const I=3,y=5e3,M=15e3,k=()=>{s=0,P(T=>({...T,networkStatus:void 0}))},E=()=>{P(T=>({...T,networkStatus:"offline"}))},N=oe.default(({status:T})=>{if(T==="connected")k(),a&&(clearTimeout(a),a=null);else if(T==="disconnected"){if(s++,!a){const le=Date.now();let Y=0;const pe=Math.ceil(M/y),H=()=>{Y++;const fe=Date.now()-le;if(h.wsconnected){k();return}if(fe>=M||Y>=pe){E();return}a=setTimeout(H,y)};a=setTimeout(H,y)}s>=I&&E()}},Be),_=()=>{h.wsconnected||h.connect(),k()},U=()=>{E()};return h.on("status",N),window.addEventListener("online",_),window.addEventListener("offline",U),h.wsconnected&&k(),()=>{a&&clearTimeout(a),h.off("status",N),window.removeEventListener("online",_),window.removeEventListener("offline",U)}},[h,P]),f&&!O?null:me.jsx(se.Provider,{value:p,children:S})}const ie=()=>{const o=g.useContext(se);if(!o)throw new Error("Context is null");const n=Ce.useSyncedStore(o.state),{setLocalState:S,localState:f}=o,O=te.useNavigate(),w=Q(),L=g.useRef({}),b=g.useCallback(e=>{const t=n.components[e]?.data;return!t||t.renderer?.type!=="component-template"?void 0:(L.current[e]||(L.current[e]={page:re(t,t.renderer)}),L.current[e]?.page)},[n.components]),j=g.useCallback(e=>{const t=n.components[e]?.data;if(!t||t.renderer?.type!=="component-template")return;const c=b(e);if(c)return{componentId:e,component:t,renderer:t.renderer,page:c}},[b,n.components]),x=g.useCallback(e=>{const t=n.pages[e];if(t)return{kind:"page",pageId:e,page:t};if(!F(e))return;const c=Z(e);if(!c)return;const r=j(c);if(r)return{kind:"component-template",pageId:e,page:r.page,componentId:r.componentId,component:r.component,renderer:r.renderer}},[j,n.pages]),v=g.useCallback(e=>{const t=x(e);if(!t)throw new Error(`Page ${e} is not exists`);return t},[x]),P=g.useMemo(()=>({get currentPage(){const{localState:{currentPageId:e,currentComponentId:t}}=o;if(e){const c=x(e);if(c)return c.page}if(t){const c=j(t);if(c)return c.page}},get currentSection(){const{localState:{currentPageId:e,...t}}=o,{currentPage:c}=this;if(!c||!e)return;const r=t.pages?.[e]?.currentSectionId;if(r)return c.sections?.[r]??this.currentPageModel?.getComponentById(r)?.model},get currentSectionProperties(){const{localState:{currentPageId:e,...t}}=o,{currentPage:c}=this;if(!c||!e)return;const r=t.pages?.[e]?.currentSectionId;if(!r)return;const d=c.dataSource?.[r];if(!d)return;const{currentLocale:i}=this.currentLocaleMap;return i?d[i]:void 0},get currentSectionPropertiesWithFallback(){const{localState:{currentPageId:e,...t}}=o,{currentPage:c}=this;if(!c||!e)return;const r=t.pages?.[e]?.currentSectionId;if(!r)return;const d=c.dataSource?.[r];if(!d)return;const{currentLocale:i,defaultLocale:l,fallbackLocale:m}=this.currentLocaleMap;if(i&&d[i])return d[i];if(l&&d[l])return d[l];if(m&&d[m])return d[m]},get currentLocaleMap(){const{localState:{currentLocale:e},state:{supportedLocales:t,config:{defaultLocale:c}}}=o;return{currentLocale:e,defaultLocale:c,fallbackLocale:t?.[0]?.locale??"en"}},get currentRoute(){const{localState:{currentRouteId:e}}=o;return e?n.routes?.[e]:void 0},get currentPageModel(){const e=this.currentPage??null;if(e)return ye.getPageTemplateModel(e,!1)}}),[JSON.stringify(f),n]),D=g.useMemo(()=>{if(f.currentPageId){const e=x(f.currentPageId);if(e?.kind==="component-template")return e}if(f.currentComponentId){const e=j(f.currentComponentId);if(e)return{kind:"component-template",pageId:W(e.componentId),page:e.page,componentId:e.componentId,component:e.component,renderer:e.renderer}}},[f.currentPageId,f.currentComponentId,x,j]),$=g.useCallback(()=>D?.kind==="component-template"?D:void 0,[D]),C=g.useMemo(()=>({setCurrentSectionId:(e,t)=>{const c=F(e),r=c?Z(e):null;e===f.currentPageId&&t===f.pages?.[e]?.currentSectionId&&(!c||r===f.currentComponentId)||(O(c&&r?R.joinURL("/admin/maker",w??"","components",r):R.joinURL("/admin/maker",w??"","pages",e)),S(d=>J.produce(d,i=>{i.currentPageId=e,c?r&&(i.currentComponentId=r):i.currentComponentId=void 0,i.pages??={},i.pages[e]??={},i.pages[e].currentSectionId=t??void 0,t?window.sessionStorage.setItem("iframe_show_id",`"${t}"`):window.sessionStorage.removeItem("iframe_show_id")})))},setCurrentComponentId:e=>{const c=n.components[e]?.data?.renderer?.type==="component-template",r=c?W(e):void 0;e===f.currentComponentId&&(!c||f.currentPageId===r)||(O(R.joinURL("/admin/maker",w??"","components",e)),S(d=>J.produce(d,i=>{i.currentComponentId=e,c&&r?(i.currentPageId=r,i.pages??={},i.pages[r]??={}):i.currentPageId&&F(i.currentPageId)&&(i.currentPageId=void 0)})))},setCurrentRouteId:({routeId:e,dynamicParams:t})=>{S(c=>J.produce(c,r=>{r.currentRouteId=e,r.currentDynamicParams=t}))},setCustomComponentPreviewerProperties:(()=>{const e={};let t="";const c=oe.default(()=>{S(r=>J.produce(r,d=>{d.customComponentPreviewerProperties??={},d.customComponentPreviewerProperties[t]={...d.customComponentPreviewerProperties[t],...A.default(e[t])},delete e[t]}))},5);return(r,d)=>{const i=e[r]??{};Object.assign(i,d),e[r]=i,t=r,c()}})(),setRouteGenerateProcess:e=>{S(t=>J.produce(t,c=>{e?c.routeGenerateProcess={...c.routeGenerateProcess,...e}:c.routeGenerateProcess={}}))},resetRouteGenerateProcess:()=>{S(e=>J.produce(e,t=>{t.routeGenerateProcess={progress:"complete"}}))}}),[S,O,w,P]),h=g.useCallback((e,t,c,r,d)=>{const i=v(e),{page:l}=i,m=A.default(t),u=c?A.default(c):void 0,p=V(m);return o.doc.transact(()=>{if(d&&d.component==="layout-block"){const s=P.currentPageModel?.getComponentById(d.id);s&&(s.model.sections??={},s.model.sections[p.id]=p,s.model.sectionIds??=[],s.model.sectionIds.push(p.id))}else if(i.kind==="component-template"){const s=i.renderer.section;s.sections??={},s.sections[p.id]=p,s.sectionIds??=[],s.sectionIds.push(p.id)}else l.sections[p.id]=p,l.sectionIds.splice(r??l.sectionIds.length,0,p.id);if(u){l.dataSource??={};const s=(a,I)=>{const y=u[a.id];y&&(l.dataSource[I.id]=A.default(y)),a.component==="layout-block"&&a.sectionIds?.length&&a.sectionIds.forEach((M,k)=>{const E=a.sections?.[M],N=I.sectionIds?.[k],_=N?I.sections?.[N]:void 0;E&&_&&s(E,_)})};s(m,p)}}),C.setCurrentSectionId(e,p.id),p},[v,P,o.doc,C]),B=g.useMemo(()=>({addPage:(e,t)=>{const c=e?.id??q.nextId(),r={...e,id:c,createdAt:e?.createdAt??new Date().toISOString(),updatedAt:e?.updatedAt??new Date().toISOString(),publishedAt:new Date(0).toISOString(),slug:e?.slug??`/${c}`,sections:e?.sections??{},sectionIds:e?.sectionIds??[],isPublic:e?.isPublic??!0,templateConfig:e?.templateConfig??{isTemplate:e?.templateConfig?.isTemplate??!0,displayTemplateId:e?.templateConfig?.displayTemplateId??void 0,dataSourceIds:e?.templateConfig?.dataSourceIds??void 0,dataSourceParameters:e?.templateConfig?.dataSourceParameters??{},enabledGenerate:e?.templateConfig?.enabledGenerate??!1,agentId:e?.templateConfig?.agentId??void 0}};return o.doc.transact(()=>{n.pages[r.id]=r,n.pageIds.splice(t??n.pageIds.length,0,r.id)}),r},addRoute:(e,t)=>{const c=e?.id??q.nextId(),r={...e,id:c,createdAt:e?.createdAt??new Date().toISOString(),updatedAt:e?.updatedAt??new Date().toISOString(),publishedAt:new Date(0).toISOString(),path:e?.path??`/${c}`,handler:e?.handler??"Pages Kit",isPublic:e?.isPublic??!0,params:e?.params??[],enabledGenerate:e?.enabledGenerate??!1,displayTemplateId:e?.displayTemplateId??void 0,dataSource:e?.dataSource??{}};return o.doc.transact(()=>{n.routes??={},n.routeIds??=[],n.routes[r.id]=r,n.routeIds.splice(t??n.routeIds.length,0,r.id)}),r},deleteRoute:e=>{if(!e)return;const t=n.routeIds?.indexOf(e);t!==void 0&&t!==-1&&o.doc.transact(()=>{n.routeIds?.splice(t,1),delete n.routes?.[e]})},deletePage:e=>{const t=n.pageIds.indexOf(e);t!==-1&&o.doc.transact(()=>{n.pageIds.splice(t,1),delete n.pages[e]})},movePage:(e,t)=>{const{pageIds:c}=n,r=c.indexOf(e);r>=0&&c.splice(t,0,...c.splice(r,1))},addSection:(e,t,c,r)=>{const d=v(e),{page:i}=d,l=t?.config,m=typeof l?.componentId=="string"?l.componentId:void 0;if(t.component==="custom-component"&&m){const a=(n.components[m]?.data??n.resources?.components?.[m]?.component)?.renderer;if(a?.type==="component-template")return h(e,a.section,a.dataSource,c,r)}const u={...t,id:t.id??q.nextId(),isTemplateSection:t.isTemplateSection??!0},p=A.default(u.locales);return delete u.locales,o.doc.transact(()=>{if(r&&r.component==="layout-block"){const s=P.currentPageModel?.getComponentById(r.id);s&&(s.model.sections??={},s.model.sections[u.id]=u,s.model.sectionIds??=[],s.model.sectionIds.push(u.id))}else if(d.kind==="component-template"){const s=d.renderer.section;s.sections??={},s.sections[u.id]=u,s.sectionIds??=[],s.sectionIds.push(u.id)}else i.sections[u.id]=u,i.sectionIds.splice(c??i.sectionIds.length,0,u.id);p&&(i.dataSource??={},i.dataSource[u.id]=p)}),u},deleteSection:(e,t)=>{const c=v(e),{page:r}=c;o.doc.transact(()=>{const d=[],i=P.currentPageModel?.getComponentById(t),l=i?.parent;if(i&&(d.push(i.model.id,...i.all().map(m=>m.model.id)),l?.model.component==="layout-block")){const m=l.model.sectionIds?.indexOf(t);m!==void 0&&m!==-1&&(l.model.sectionIds?.splice(m,1),delete l.model.sections?.[t]),l.model.config?.gridSettings&&Object.keys(l.model.config.gridSettings).forEach(u=>{try{delete l.model.config.gridSettings[u]?.[t]}catch{}}),l.model.config?.backgroundSectionId===t&&(l.model.config.backgroundSectionId=void 0)}d.forEach(m=>{const u=r.sectionIds.indexOf(m);u!==-1&&r.sectionIds.splice(u,1),r.sections[m]&&delete r.sections[m],r.dataSource?.[m]&&delete r.dataSource?.[m]})})},lockSection:(e,t)=>{v(e);const c=P.currentPageModel?.getComponentById(t)?.model;c&&(c.locked=!c.locked)},toggleSectionVisibility:(e,t,c)=>{v(e);const r=P.currentPageModel?.getComponentById(t)?.model;r&&(r.visibility=c)},moveSection:(e,t,c,r)=>{const d=v(e),{page:i}=d,l=P.currentPageModel;if(!l)throw new Error(`Page model for ${e} is not available`);const m=p=>{const s=l.getComponentById(p);if(!s)return null;const a=s.model,I=s.parent;if(!I)return{section:a,container:i,containerType:"root",index:i.sectionIds.indexOf(p)};const y=I.model;return{section:a,container:y,containerType:"layout-block",index:y.sectionIds.indexOf(p)}},u=m(t);if(!u)throw new Error(`Section ${t} not found`);o.doc.transact(()=>{if(c.startsWith("layout-")){const a=c.replace("layout-",""),y=l.getComponentById(a)?.model;if(!y||y.component!=="layout-block")throw new Error(`Layout block ${a} not found`);if(a===t||((E,N)=>{const _=l.getComponentById(E),U=l.getComponentById(N);if(!_||!U||_.model.component!=="layout-block")return!1;let T=U.parent;for(;T;){if(T.model.id===E)return!0;T=T.parent}return!1})(t,a))return;const k=JSON.parse(JSON.stringify(u.section));y.sections||(y.sections={}),y.sectionIds||(y.sectionIds=[]),y.sections[t]=k,y.sectionIds.push(t),u.container.sectionIds.splice(u.index,1),u.containerType==="layout-block"&&delete u.container.sections[t];return}const p=m(c);if(!p)throw new Error(`Target section ${c} not found`);let s=p.index;if(r==="bottom"&&(s=p.index+1),u.container===p.container){const{sectionIds:a}=u.container;a.splice(u.index,1),u.index<s&&(s-=1),a.splice(s,0,t)}else{const a=JSON.parse(JSON.stringify(u.section));p.containerType==="root"||(p.container.sections||(p.container.sections={}),p.container.sections[t]=a),p.container.sectionIds.splice(s,0,t),u.container.sectionIds.splice(u.index,1),u.containerType==="layout-block"&&delete u.container.sections[t]}})},copySection:(e,t)=>{v(e);const c=P.currentPageModel?.getComponentById(t);if(!c?.model)throw new Error(`Section ${t} is not exists`);const r=JSON.parse(JSON.stringify(c.model)),d=V(r),i=d.id,l=c?.parent;if(l){if(l.model.sections[i]=d,l.model.sectionIds.push(i),P.currentPage?.dataSource?.[t]&&(P.currentPage.dataSource[i]=JSON.parse(JSON.stringify(P.currentPage.dataSource[t])),d.component==="layout-block")){const m=(u,p)=>{const s=u.sectionIds,a=p.sectionIds;s?.forEach((I,y)=>{const M=a?.[y];if(M){if(M&&P.currentPage?.dataSource){const N=P.currentPage.dataSource[M];N&&(P.currentPage.dataSource[I]=JSON.parse(JSON.stringify(N)))}const k=u.sections?.[I],E=p?.sections?.[M];k?.component==="layout-block"&&k.sectionIds&&E?.sectionIds&&m(k,E)}})};d.sectionIds&&r.sectionIds&&m(d,r)}C.setCurrentSectionId(e,i)}return d},extractSectionToComponentTemplate:(e,t)=>{const c=v(e),{page:r}=c,i=P.currentPageModel?.getComponentById(t)?.model;if(!i){console.warn(`Section ${t} is not exists`);return}const l=JSON.parse(JSON.stringify(i));if(l.component!=="layout-block"||!(l.sectionIds?.length??0)){console.warn(`Section ${t} is not eligible for component template extraction.`);return}const m=(s,a=[])=>(s&&(a.push(s.id),s.sectionIds?.forEach(I=>{const y=s.sections?.[I];m(y,a)})),a),p=m(i).reduce((s,a)=>{const I=r.dataSource?.[a];return I&&(s[a]=JSON.parse(JSON.stringify(I))),s},{});return{section:l,dataSource:Object.keys(p).length?p:void 0}},addComponentTemplate:({name:e,description:t,section:c,dataSource:r})=>{const d=q.nextId(),i=new Date().toISOString(),l=A.default(c),m=r?A.default(r):void 0;return o.doc.transact(()=>{const u=Object.values(n.components).length;n.components[d]={index:u,data:{id:d,name:e.trim()||void 0,description:t?.trim()||void 0,createdAt:i,updatedAt:i,renderer:{type:"component-template",section:l,dataSource:m}}}}),n.components[d]},addDataSource:e=>{const t=e.id??q.nextId(),c={...e??{},id:t,createdAt:new Date().toISOString()};return o.doc.transact(()=>{n.dataSourceIds??=[],n.dataSources??={},n.dataSourceIds.push(t),n.dataSources[t]=c}),n.dataSources[t]},deleteDataSource:e=>{const t=n.dataSourceIds.indexOf(e);t!==-1&&o.doc.transact(()=>{n.dataSourceIds.splice(t,1),delete n.dataSources[e]})}}),[n,o.doc,P]);return{...o,state:n,actions:B,localActions:C,...P,getComponentTemplateContext:$}},Q=()=>{const{projectId:o}=te.useParams();return o??ne()??""},Ge=()=>window.__PROJECT_SLUG__??"";function ae(o){const{resources:n}=o;if(!n.components)return{};const S={};return window?.blocklet?.componentMountPoints?.forEach(f=>{S[f.did]=f}),Object.fromEntries(Object.entries(n.components).map(([f,{blockletId:O,component:w}])=>[f,{data:w,blockletId:O,blockletTitle:S[O]?.title||O}]))}function de(o){const{components:n}=o;return n||{}}function ue(o){return{...ae(o),...de(o)}}const We=()=>ue(ie().state);function V(o){const n=JSON.parse(JSON.stringify(o)),S=q.nextId();if(n.component==="layout-block"){const f={...n.config??{}},O=n.sectionIds?.map(w=>{const L=n.sections?.[w],b=V(n.sections?.[w]),j=b.id;return L?.id===n.config?.backgroundSectionId&&(f.backgroundSectionId=b.id),n.config?.gridSettings&&Object.keys(n.config.gridSettings).forEach(x=>{f.gridSettings[x]={...f.gridSettings[x]??{},[j]:{...n.config?.gridSettings?.[x]?.[w],id:j}},delete n.config?.gridSettings?.[x]?.[w]}),b});n.sections=Object.fromEntries(O?.map(w=>[w.id,w])??[]),n.sectionIds=O?.map(w=>w.id)??[],n.config=f}return{...n,id:S}}exports.StoreProvider=Fe;exports.api=z;exports.autoResizeImage=$e;exports.createComponentTemplatePage=re;exports.getAllComponents=ue;exports.getCustomComponents=de;exports.getImageAbsoluteUrl=Re;exports.getProjectIdFromPathnameOrWindow=ne;exports.getProjectPageVersionKey=Me;exports.getResourceComponents=ae;exports.getWssURL=_e;exports.transformValue=Ne;exports.useAllComponents=We;exports.useProjectId=Q;exports.useProjectSlug=Ge;exports.useStore=ie;
|