@tmagic/editor 1.8.3 → 1.8.4-beta.1

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.
@@ -7835,10 +7835,10 @@
7835
7835
  collect(nodes, depExtendedData = {}, deep = false, type) {
7836
7836
  this.set("collecting", true);
7837
7837
  const targets = this.watcher.getCollectableTargets(type);
7838
- if (targets.length) for (const node of nodes) {
7839
- if ((0, _tmagic_utils.isPage)(node)) for (const target of targets) this.removePageDep(target, depExtendedData);
7838
+ if (targets.length) {
7839
+ for (const node of nodes) if ((0, _tmagic_utils.isPage)(node)) for (const target of targets) this.removePageDep(target, depExtendedData);
7840
7840
  else this.watcher.removeTargetsDep(targets, node);
7841
- this.watcher.collectItems(node, targets, depExtendedData, deep);
7841
+ for (const node of nodes) this.watcher.collectItems(node, targets, depExtendedData, deep);
7842
7842
  }
7843
7843
  this.set("collecting", false);
7844
7844
  this.emit("collected", nodes, deep);
@@ -11391,7 +11391,7 @@
11391
11391
  };
11392
11392
  //#endregion
11393
11393
  //#region packages/editor/src/utils/dep/worker.ts?worker&inline
11394
- var jsContent = "(function() {\n //#region packages/schema/dist/es/index.js\n var NODE_CONDS_KEY = \"displayConds\";\n var NODE_DISABLE_DATA_SOURCE_KEY = \"_tmagic_node_disabled_data_source\";\n var NODE_DISABLE_CODE_BLOCK_KEY = \"_tmagic_node_disabled_code_block\";\n var HookType = /* @__PURE__ */ function(HookType) {\n /** 代码块钩子标识 */\n HookType[\"CODE\"] = \"code\";\n return HookType;\n }({});\n //#endregion\n //#region packages/utils/dist/es/const.js\n var DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX = \"ds-field::\";\n //#endregion\n //#region packages/utils/dist/es/index.js\n var isObject = (obj) => Object.prototype.toString.call(obj) === \"[object Object]\";\n var getKeysArray = (keys) => `${keys}`.replace(/\\[(\\d+)\\]/g, \".$1\").split(\".\");\n var dataSourceTemplateRegExp = /\\$\\{([\\s\\S]+?)\\}/g;\n //#endregion\n //#region packages/dep/dist/es/types.js\n /** 依赖收集的目标类型 */\n var DepTargetType = /* @__PURE__ */ function(DepTargetType) {\n DepTargetType[\"DEFAULT\"] = \"default\";\n /** 代码块 */\n DepTargetType[\"CODE_BLOCK\"] = \"code-block\";\n /** 数据源 */\n DepTargetType[\"DATA_SOURCE\"] = \"data-source\";\n /** 数据源方法 */\n DepTargetType[\"DATA_SOURCE_METHOD\"] = \"data-source-method\";\n /** 数据源条件 */\n DepTargetType[\"DATA_SOURCE_COND\"] = \"data-source-cond\";\n return DepTargetType;\n }({});\n //#endregion\n //#region packages/dep/dist/es/Target.js\n /**\n * 需要收集依赖的目标\n * 例如:一个代码块可以为一个目标\n */\n var Target = class {\n /**\n * 如何识别目标\n */\n isTarget;\n /**\n * 目标id,不可重复\n * 例如目标是代码块,则为代码块id\n */\n id;\n /**\n * 目标名称,用于显示在依赖列表中\n */\n name;\n /**\n * 不同的目标可以进行分类,例如代码块,数据源可以为两个不同的type\n */\n type = DepTargetType.DEFAULT;\n /**\n * 依赖详情\n * 实例:{ 'node_id': { name: 'node_name', keys: [ created, mounted ] } }\n */\n deps = {};\n /**\n * 是否默认收集,默认为true,当值为false时需要传入type参数给collect方法才会被收集\n */\n isCollectByDefault;\n /**\n * 可序列化描述,有该描述的 target 可以在 worker 中重建,从而把依赖收集放到子线程执行\n */\n descriptor;\n constructor(options) {\n this.isTarget = options.isTarget;\n this.id = options.id;\n this.name = options.name;\n this.isCollectByDefault = options.isCollectByDefault ?? true;\n this.descriptor = options.descriptor;\n if (options.type) this.type = options.type;\n if (options.initialDeps) this.deps = options.initialDeps;\n }\n /**\n * 更新依赖\n * @param option 节点配置\n * @param key 哪个key配置了这个目标的id\n */\n updateDep({ id, name, key, data }) {\n const dep = this.deps[id] || {\n name,\n keys: []\n };\n dep.name = name;\n dep.data = data;\n this.deps[id] = dep;\n if (!dep.keys.includes(key)) dep.keys.push(key);\n }\n /**\n * 删除依赖\n * @param node 哪个节点的依赖需要移除,如果为空,则移除所有依赖\n * @param key 节点下哪个key需要移除,如果为空,则移除改节点下的所有依赖key\n * @returns void\n */\n removeDep(id, key) {\n if (typeof id === \"undefined\") {\n Object.keys(this.deps).forEach((depKey) => {\n delete this.deps[depKey];\n });\n return;\n }\n const dep = this.deps[id];\n if (!dep) return;\n if (key) {\n const index = dep.keys.indexOf(key);\n dep.keys.splice(index, 1);\n if (dep.keys.length === 0) delete this.deps[id];\n } else delete this.deps[id];\n }\n /**\n * 判断指定节点下的指定key是否存在在依赖列表中\n * @param node 哪个节点\n * @param key 哪个key\n * @returns boolean\n */\n hasDep(id, key) {\n return this.deps[id]?.keys.includes(key) ?? false;\n }\n destroy() {\n this.deps = {};\n }\n };\n //#endregion\n //#region packages/dep/dist/es/utils.js\n var INTEGER_REGEXP = /^\\d+$/;\n var createCodeBlockTarget = (id, codeBlock, initialDeps = {}) => new Target({\n type: DepTargetType.CODE_BLOCK,\n id,\n initialDeps,\n name: codeBlock.name,\n descriptor: {\n type: DepTargetType.CODE_BLOCK,\n id,\n codeBlock: { name: codeBlock.name }\n },\n isTarget: (_key, value) => {\n if (id === value) return true;\n if (value?.hookType === HookType.CODE && Array.isArray(value.hookData)) return value.hookData.some((item) => item.codeId === id);\n return false;\n }\n });\n /**\n * ['array'] ['array', '0'] ['array', '0', 'a'] 这种返回false\n * ['array', 'a'] 这种返回true\n * @param keys\n * @param fields\n * @returns boolean\n */\n var isIncludeArrayField = (keys, fields) => {\n let f = fields;\n return keys.some((key, index) => {\n const field = f.find(({ name }) => name === key);\n f = field?.fields || [];\n return field?.type === \"array\" && index < keys.length - 1 && !INTEGER_REGEXP.test(keys[index + 1]);\n });\n };\n /**\n * 判断模板(value)是不是使用数据源Id(dsId),如:`xxx${dsId.field}xxx${dsId.field}`\n * @param value any\n * @param dsId string | number\n * @param hasArray boolean true: 一定要包含有需要迭代的模板; false: 一定要包含普通模板;\n * @returns boolean\n */\n var isDataSourceTemplate = (value, ds, hasArray = false) => {\n const templates = value.match(dataSourceTemplateRegExp) || [];\n if (templates.length <= 0) return false;\n for (const tpl of templates) {\n const keys = getKeysArray(tpl.substring(2, tpl.length - 1));\n const dsId = keys.shift();\n if (!dsId || dsId !== ds.id) continue;\n if (hasArray === isIncludeArrayField(keys, ds.fields)) return true;\n }\n return false;\n };\n /**\n * 指定数据源的字符串模板,如:{ isBindDataSourceField: true, dataSourceId: 'id', template: `xxx${field}xxx`}\n * @param value any\n * @param dsId string | number\n * @returns boolean\n */\n var isSpecificDataSourceTemplate = (value, dsId) => value?.isBindDataSourceField && value.dataSourceId && value.dataSourceId === dsId && typeof value.template === \"string\";\n /**\n * 关联数据源字段,格式为 [前缀+数据源ID, 字段名]\n * 使用data-source-field-select value: 'value' 可以配置出来\n * @param value any[]\n * @param id string | number\n * @returns boolean\n */\n var isUseDataSourceField = (value, id) => {\n if (!Array.isArray(value) || typeof value[0] !== \"string\") return false;\n const [prefixId] = value;\n const prefixIndex = prefixId.indexOf(DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX);\n if (prefixIndex === -1) return false;\n return prefixId.substring(prefixIndex + DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX.length) === id;\n };\n var isDataSourceTarget = (ds, key, value, hasArray = false) => {\n if (!value) return false;\n const valueType = typeof value;\n if (valueType !== \"string\" && valueType !== \"object\") return false;\n if (`${key}`.startsWith(\"displayConds\")) return false;\n if (valueType === \"string\") return isDataSourceTemplate(value, ds, hasArray);\n if (isObject(value) && value.isBindDataSource && value.dataSourceId === ds.id) return true;\n if (isSpecificDataSourceTemplate(value, ds.id)) return true;\n if (isUseDataSourceField(value, ds.id)) {\n const [, ...keys] = value;\n const includeArray = isIncludeArrayField(keys, ds.fields);\n return hasArray ? includeArray : !includeArray;\n }\n return false;\n };\n var isDataSourceCondTarget = (ds, key, value, hasArray = false) => {\n if (!Array.isArray(value) || !ds) return false;\n const [dsId, ...keys] = value;\n if (dsId !== ds.id || !`${key}`.startsWith(\"displayConds\")) return false;\n if (ds.fields?.some((field) => field.name === keys[0])) {\n const includeArray = isIncludeArrayField(keys, ds.fields);\n return hasArray ? includeArray : !includeArray;\n }\n return false;\n };\n var createDataSourceTarget = (ds, initialDeps = {}) => new Target({\n type: DepTargetType.DATA_SOURCE,\n id: ds.id,\n initialDeps,\n descriptor: {\n type: DepTargetType.DATA_SOURCE,\n ds: {\n id: ds.id,\n fields: ds.fields || []\n }\n },\n isTarget: (key, value) => isDataSourceTarget(ds, key, value)\n });\n var createDataSourceCondTarget = (ds, initialDeps = {}) => new Target({\n type: DepTargetType.DATA_SOURCE_COND,\n id: ds.id,\n initialDeps,\n descriptor: {\n type: DepTargetType.DATA_SOURCE_COND,\n ds: {\n id: ds.id,\n fields: ds.fields || []\n }\n },\n isTarget: (key, value) => isDataSourceCondTarget(ds, key, value)\n });\n var createDataSourceMethodTarget = (ds, initialDeps = {}) => new Target({\n type: DepTargetType.DATA_SOURCE_METHOD,\n id: ds.id,\n initialDeps,\n descriptor: {\n type: DepTargetType.DATA_SOURCE_METHOD,\n ds: {\n id: ds.id,\n methods: (ds.methods || []).map((method) => ({ name: method.name })),\n fields: (ds.fields || []).map((field) => ({ name: field.name }))\n }\n },\n isTarget: (_key, value) => {\n if (!Array.isArray(value)) return false;\n const [dsId, methodName] = value;\n if (!methodName || dsId !== ds.id) return false;\n if (ds.methods?.some((method) => method.name === methodName)) return true;\n if (ds.fields?.some((field) => field.name === methodName)) return false;\n return true;\n }\n });\n /**\n * 用可序列化描述重建 target,使依赖收集可以在 worker 等无法传递函数的环境中进行\n * @param descriptor 由工厂函数写入 target 的描述\n * @param initialDeps 初始依赖\n * @returns Target\n */\n var createTargetByDescriptor = (descriptor, initialDeps = {}) => {\n switch (descriptor.type) {\n case DepTargetType.CODE_BLOCK: return createCodeBlockTarget(descriptor.id, descriptor.codeBlock, initialDeps);\n case DepTargetType.DATA_SOURCE: return createDataSourceTarget(descriptor.ds, initialDeps);\n case DepTargetType.DATA_SOURCE_COND: return createDataSourceCondTarget(descriptor.ds, initialDeps);\n case DepTargetType.DATA_SOURCE_METHOD: return createDataSourceMethodTarget(descriptor.ds, initialDeps);\n default: throw new Error(`unknown target descriptor: ${JSON.stringify(descriptor)}`);\n }\n };\n var traverseTarget = (targetsList, cb, type) => {\n if (type) {\n const targets = targetsList[type];\n if (targets) for (const target of Object.values(targets)) cb(target);\n return;\n }\n for (const targets of Object.values(targetsList)) for (const target of Object.values(targets)) cb(target);\n };\n //#endregion\n //#region packages/dep/dist/es/Watcher.js\n var DATA_SOURCE_TARGET_TYPES = /* @__PURE__ */ new Set([\n DepTargetType.DATA_SOURCE,\n DepTargetType.DATA_SOURCE_COND,\n DepTargetType.DATA_SOURCE_METHOD\n ]);\n var Watcher = class {\n targetsList = {};\n childrenProp = \"items\";\n idProp = \"id\";\n nameProp = \"name\";\n constructor(options) {\n if (options?.initialTargets) this.targetsList = options.initialTargets;\n if (options?.childrenProp) this.childrenProp = options.childrenProp;\n }\n getTargetsList() {\n return this.targetsList;\n }\n /**\n * 获取指定类型中的所有target\n * @param type 分类\n * @returns Target[]\n */\n getTargets(type = DepTargetType.DEFAULT) {\n return this.targetsList[type] || {};\n }\n /**\n * 添加新的目标\n * @param target Target\n */\n addTarget(target) {\n const targets = this.getTargets(target.type) || {};\n this.targetsList[target.type] = targets;\n targets[target.id] = target;\n }\n /**\n * 获取指定id的target\n * @param id target id\n * @returns Target\n */\n getTarget(id, type = DepTargetType.DEFAULT) {\n return this.getTargets(type)[id];\n }\n /**\n * 判断是否存在指定id的target\n * @param id target id\n * @returns boolean\n */\n hasTarget(id, type = DepTargetType.DEFAULT) {\n return Boolean(this.getTarget(id, type));\n }\n /**\n * 判断是否存在指定类型的target\n * @param type target type\n * @returns boolean\n */\n hasSpecifiedTypeTarget(type = DepTargetType.DEFAULT) {\n return Object.keys(this.getTargets(type)).length > 0;\n }\n /**\n * 删除指定id的target\n * @param id target id\n */\n removeTarget(id, type = DepTargetType.DEFAULT) {\n const targets = this.getTargets(type);\n if (targets[id]) {\n targets[id].destroy();\n delete targets[id];\n }\n }\n /**\n * 删除指定分类的所有target\n * @param type 分类\n * @returns void\n */\n removeTargets(type = DepTargetType.DEFAULT) {\n const targets = this.targetsList[type];\n if (!targets) return;\n for (const target of Object.values(targets)) target.destroy();\n delete this.targetsList[type];\n }\n /**\n * 删除所有target\n */\n clearTargets() {\n for (const key of Object.keys(this.targetsList)) delete this.targetsList[key];\n }\n /**\n * 收集依赖\n * @param nodes 需要收集的节点\n * @param deep 是否需要收集子节点\n * @param type 强制收集指定类型的依赖\n */\n collect(nodes, depExtendedData = {}, deep = false, type) {\n const targets = this.getCollectableTargets(type);\n if (!targets.length) return;\n for (const node of nodes) {\n this.removeTargetsDep(targets, node);\n this.collectItems(node, targets, depExtendedData, deep);\n }\n }\n /**\n * 获取本次需要参与收集的 target(过滤规则与 collectByCallback 一致)\n *\n * 注:供 editor 的 dep service / worker 跨包批量收集时复用,因此为 public。\n * @param type 强制收集指定类型的依赖\n */\n getCollectableTargets(type) {\n const targets = [];\n traverseTarget(this.targetsList, (target) => {\n if (!type && !target.isCollectByDefault) return;\n targets.push(target);\n }, type);\n return targets;\n }\n collectByCallback(nodes, type, cb) {\n traverseTarget(this.targetsList, (target) => {\n if (!type && !target.isCollectByDefault) return;\n for (const node of nodes) cb({\n node,\n target\n });\n }, type);\n }\n /**\n * 清除所有目标的依赖\n * @param nodes 需要清除依赖的节点\n */\n clear(nodes, type) {\n let { targetsList } = this;\n if (type) targetsList = { [type]: this.getTargets(type) };\n const clearedItemsNodeIds = /* @__PURE__ */ new Set();\n traverseTarget(targetsList, (target) => {\n if (nodes) for (const node of nodes) {\n target.removeDep(node[this.idProp]);\n if (Array.isArray(node[this.childrenProp]) && node[this.childrenProp].length && !clearedItemsNodeIds.has(node[this.idProp])) {\n clearedItemsNodeIds.add(node[this.idProp]);\n this.clear(node[this.childrenProp]);\n }\n }\n else target.removeDep();\n });\n }\n /**\n * 清除指定类型的依赖\n * @param type 类型\n * @param nodes 需要清除依赖的节点\n */\n clearByType(type, nodes) {\n this.clear(nodes, type);\n }\n /**\n * 收集单个 target 的依赖,等价于 collectItems(node, [target], ...)\n */\n collectItem(node, target, depExtendedData = {}, deep = false) {\n this.collectItems(node, [target], depExtendedData, deep);\n }\n removeTargetDep(target, node, key) {\n target.removeDep(node[this.idProp], key);\n if (typeof key === \"undefined\" && Array.isArray(node[this.childrenProp]) && node[this.childrenProp].length) for (const item of node[this.childrenProp]) this.removeTargetDep(target, item, key);\n }\n /**\n * 与 removeTargetDep 等价,但一次子树递归同时处理多个 target,\n * 把删除阶段的结构遍历从 ×targets 降到 ×1。\n *\n * 注:供 editor 的 dep service 跨包批量删除时复用,因此为 public。\n */\n removeTargetsDep(targets, node, key) {\n const id = node[this.idProp];\n for (const target of targets) target.removeDep(id, key);\n if (typeof key === \"undefined\" && Array.isArray(node[this.childrenProp]) && node[this.childrenProp].length) for (const item of node[this.childrenProp]) this.removeTargetsDep(targets, item, key);\n }\n /**\n * 与 collectItem 等价,但一次遍历同时处理多个 target(不含删除阶段)。\n *\n * 关键优化:原实现对每个 target 都完整遍历一遍节点树(O(targets × 树规模)),大页面 + 大量数据源时,\n * 结构遍历(Object.entries / 递归 / fullKey 字符串拼接)会被重复 targets 次。这里改为「整棵树只遍历一次,\n * 在每个属性上检查所有 target」,把结构遍历开销从 ×targets 降到 ×1,isTarget 调用次数不变,收集结果完全一致。\n *\n * 注:供 editor 的 dep service / worker 跨包批量收集时复用,因此为 public。\n */\n collectItems(node, targets, depExtendedData = {}, deep = false) {\n const activeTargets = this.filterTargetsByNode(node, targets);\n if (!activeTargets.length) return;\n this.collectTargetForTargets(node, node, \"\", activeTargets, depExtendedData, deep);\n }\n filterTargetsByNode(node, targets) {\n const disableDataSource = Boolean(node[NODE_DISABLE_DATA_SOURCE_KEY]);\n const disableCodeBlock = Boolean(node[NODE_DISABLE_CODE_BLOCK_KEY]);\n if (!disableDataSource && !disableCodeBlock) return targets;\n return targets.filter((target) => {\n if (disableDataSource && DATA_SOURCE_TARGET_TYPES.has(target.type)) return false;\n if (disableCodeBlock && target.type === DepTargetType.CODE_BLOCK) return false;\n return true;\n });\n }\n collectTargetForTargets(node, config, prop, targets, depExtendedData, deep) {\n const id = node[this.idProp];\n const name = `${node[this.nameProp] || node[this.idProp]}`;\n for (const [key, value] of Object.entries(config)) {\n if (typeof value === \"undefined\" || value === \"\") continue;\n const keyIsItems = key === this.childrenProp;\n const fullKey = prop ? `${prop}.${key}` : key;\n let notMatched = null;\n for (let i = 0, l = targets.length; i < l; i++) {\n const target = targets[i];\n if (target.isTarget(fullKey, value, config)) target.updateDep({\n id,\n name,\n data: depExtendedData,\n key: fullKey\n });\n else (notMatched || (notMatched = [])).push(target);\n }\n if (notMatched) {\n if (!keyIsItems && Array.isArray(value)) for (let i = 0, l = value.length; i < l; i++) {\n const item = value[i];\n if (isObject(item)) this.collectTargetForTargets(node, item, `${fullKey}[${i}]`, notMatched, depExtendedData, deep);\n }\n else if (isObject(value)) this.collectTargetForTargets(node, value, fullKey, notMatched, depExtendedData, deep);\n }\n if (keyIsItems && deep && Array.isArray(value)) for (const child of value) this.collectItems(child, targets, depExtendedData, deep);\n }\n }\n };\n //#endregion\n //#region packages/editor/src/utils/logger.ts\n const error = (...args) => {\n if (process.env.NODE_ENV === \"development\") console.error(\"magic editor: \", ...args);\n };\n //#endregion\n //#region packages/editor/src/utils/dep/worker.ts\n /**\n * 收集本次会覆盖到的节点 id\n * 必须与 deep 一致:deep=false 时不能带上子孙 id,否则写回阶段 removeDep 会清掉未重收的子节点依赖\n */\n const getNodeIds = (nodes, deep, ids = []) => {\n for (const node of nodes) {\n ids.push(node.id);\n if (deep && Array.isArray(node.items) && node.items.length) getNodeIds(node.items, deep, ids);\n }\n return ids;\n };\n const collectDsl = ({ id, dsl }) => {\n try {\n const mApp = eval(`(${dsl})`);\n if (!mApp) {\n postMessage({\n id,\n deps: {}\n });\n return;\n }\n const watcher = new Watcher({ initialTargets: {} });\n if (mApp.codeBlocks) for (const [id, code] of Object.entries(mApp.codeBlocks)) watcher.addTarget(createCodeBlockTarget(id, code));\n if (mApp.dataSources) for (const ds of mApp.dataSources) {\n watcher.addTarget(createDataSourceTarget(ds, {}));\n watcher.addTarget(createDataSourceMethodTarget(ds, {}));\n watcher.addTarget(createDataSourceCondTarget(ds, {}));\n }\n const targets = watcher.getCollectableTargets();\n for (const page of mApp.items) watcher.collectItems(page, targets, { pageId: page.id }, true);\n const deps = {\n [DepTargetType.DATA_SOURCE]: {},\n [DepTargetType.DATA_SOURCE_METHOD]: {},\n [DepTargetType.DATA_SOURCE_COND]: {},\n [DepTargetType.CODE_BLOCK]: {}\n };\n traverseTarget(watcher.getTargetsList(), (target) => {\n deps[target.type][target.id] = target.deps;\n });\n const response = {\n id,\n deps\n };\n postMessage(response);\n } catch (e) {\n error(e);\n postMessage({\n id,\n deps: {},\n failed: true\n });\n }\n };\n const collectNodes = ({ id, payload }) => {\n try {\n const { nodes, targets: descriptors, depExtendedData, deep } = eval(`(${payload})`);\n const watcher = new Watcher({ initialTargets: {} });\n const targets = descriptors.map((descriptor) => createTargetByDescriptor(descriptor));\n for (const node of nodes) watcher.collectItems(node, targets, depExtendedData, deep);\n const deps = {};\n for (const target of targets) {\n if (!deps[target.type]) deps[target.type] = {};\n deps[target.type][target.id] = target.deps;\n }\n const response = {\n id,\n deps,\n nodeIds: getNodeIds(nodes, deep)\n };\n postMessage(response);\n } catch (e) {\n error(e);\n postMessage({\n id,\n deps: {},\n nodeIds: [],\n failed: true\n });\n }\n };\n /**\n * 依赖收集 worker\n *\n * 依赖收集需要深度遍历整棵节点树并对每个属性做 target 匹配,节点/数据源多时会长时间占用主线程导致页面卡死,\n * 因此把遍历匹配放到 worker 中执行。\n * 全量与增量收集共用一个常驻 worker:收集逻辑只打进一份 inline worker 产物,\n * 也不会因为频繁收集而反复创建线程,每个请求用 id 与调用方对应。\n */\n onmessage = (e) => {\n if (\"dsl\" in e.data) {\n collectDsl(e.data);\n return;\n }\n collectNodes(e.data);\n };\n //#endregion\n})();\n";
11394
+ var jsContent = "(function() {\n //#region packages/schema/dist/es/index.js\n var NODE_CONDS_KEY = \"displayConds\";\n var NODE_DISABLE_DATA_SOURCE_KEY = \"_tmagic_node_disabled_data_source\";\n var NODE_DISABLE_CODE_BLOCK_KEY = \"_tmagic_node_disabled_code_block\";\n var HookType = /* @__PURE__ */ function(HookType) {\n /** 代码块钩子标识 */\n HookType[\"CODE\"] = \"code\";\n return HookType;\n }({});\n //#endregion\n //#region packages/utils/dist/es/const.js\n var DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX = \"ds-field::\";\n //#endregion\n //#region packages/utils/dist/es/index.js\n var isObject = (obj) => Object.prototype.toString.call(obj) === \"[object Object]\";\n var getKeysArray = (keys) => `${keys}`.replace(/\\[(\\d+)\\]/g, \".$1\").split(\".\");\n var dataSourceTemplateRegExp = /\\$\\{([\\s\\S]+?)\\}/g;\n //#endregion\n //#region packages/dep/dist/es/types.js\n /** 依赖收集的目标类型 */\n var DepTargetType = /* @__PURE__ */ function(DepTargetType) {\n DepTargetType[\"DEFAULT\"] = \"default\";\n /** 代码块 */\n DepTargetType[\"CODE_BLOCK\"] = \"code-block\";\n /** 数据源 */\n DepTargetType[\"DATA_SOURCE\"] = \"data-source\";\n /** 数据源方法 */\n DepTargetType[\"DATA_SOURCE_METHOD\"] = \"data-source-method\";\n /** 数据源条件 */\n DepTargetType[\"DATA_SOURCE_COND\"] = \"data-source-cond\";\n return DepTargetType;\n }({});\n //#endregion\n //#region packages/dep/dist/es/Target.js\n /**\n * 需要收集依赖的目标\n * 例如:一个代码块可以为一个目标\n */\n var Target = class {\n /**\n * 如何识别目标\n */\n isTarget;\n /**\n * 目标id,不可重复\n * 例如目标是代码块,则为代码块id\n */\n id;\n /**\n * 目标名称,用于显示在依赖列表中\n */\n name;\n /**\n * 不同的目标可以进行分类,例如代码块,数据源可以为两个不同的type\n */\n type = DepTargetType.DEFAULT;\n /**\n * 依赖详情\n * 实例:{ 'node_id': { name: 'node_name', keys: [ created, mounted ] } }\n */\n deps = {};\n /**\n * 是否默认收集,默认为true,当值为false时需要传入type参数给collect方法才会被收集\n */\n isCollectByDefault;\n /**\n * 可序列化描述,有该描述的 target 可以在 worker 中重建,从而把依赖收集放到子线程执行\n */\n descriptor;\n constructor(options) {\n this.isTarget = options.isTarget;\n this.id = options.id;\n this.name = options.name;\n this.isCollectByDefault = options.isCollectByDefault ?? true;\n this.descriptor = options.descriptor;\n if (options.type) this.type = options.type;\n if (options.initialDeps) this.deps = options.initialDeps;\n }\n /**\n * 更新依赖\n * @param option 节点配置\n * @param key 哪个key配置了这个目标的id\n */\n updateDep({ id, name, key, data }) {\n const dep = this.deps[id] || {\n name,\n keys: []\n };\n dep.name = name;\n dep.data = data;\n this.deps[id] = dep;\n if (!dep.keys.includes(key)) dep.keys.push(key);\n }\n /**\n * 删除依赖\n * @param node 哪个节点的依赖需要移除,如果为空,则移除所有依赖\n * @param key 节点下哪个key需要移除,如果为空,则移除改节点下的所有依赖key\n * @returns void\n */\n removeDep(id, key) {\n if (typeof id === \"undefined\") {\n Object.keys(this.deps).forEach((depKey) => {\n delete this.deps[depKey];\n });\n return;\n }\n const dep = this.deps[id];\n if (!dep) return;\n if (key) {\n const index = dep.keys.indexOf(key);\n dep.keys.splice(index, 1);\n if (dep.keys.length === 0) delete this.deps[id];\n } else delete this.deps[id];\n }\n /**\n * 判断指定节点下的指定key是否存在在依赖列表中\n * @param node 哪个节点\n * @param key 哪个key\n * @returns boolean\n */\n hasDep(id, key) {\n return this.deps[id]?.keys.includes(key) ?? false;\n }\n destroy() {\n this.deps = {};\n }\n };\n //#endregion\n //#region packages/dep/dist/es/utils.js\n var INTEGER_REGEXP = /^\\d+$/;\n var createCodeBlockTarget = (id, codeBlock, initialDeps = {}) => new Target({\n type: DepTargetType.CODE_BLOCK,\n id,\n initialDeps,\n name: codeBlock.name,\n descriptor: {\n type: DepTargetType.CODE_BLOCK,\n id,\n codeBlock: { name: codeBlock.name }\n },\n isTarget: (_key, value) => {\n if (id === value) return true;\n if (value?.hookType === HookType.CODE && Array.isArray(value.hookData)) return value.hookData.some((item) => item.codeId === id);\n return false;\n }\n });\n /**\n * ['array'] ['array', '0'] ['array', '0', 'a'] 这种返回false\n * ['array', 'a'] 这种返回true\n * @param keys\n * @param fields\n * @returns boolean\n */\n var isIncludeArrayField = (keys, fields) => {\n let f = fields;\n return keys.some((key, index) => {\n const field = f.find(({ name }) => name === key);\n f = field?.fields || [];\n return field?.type === \"array\" && index < keys.length - 1 && !INTEGER_REGEXP.test(keys[index + 1]);\n });\n };\n /**\n * 判断模板(value)是不是使用数据源Id(dsId),如:`xxx${dsId.field}xxx${dsId.field}`\n * @param value any\n * @param dsId string | number\n * @param hasArray boolean true: 一定要包含有需要迭代的模板; false: 一定要包含普通模板;\n * @returns boolean\n */\n var isDataSourceTemplate = (value, ds, hasArray = false) => {\n const templates = value.match(dataSourceTemplateRegExp) || [];\n if (templates.length <= 0) return false;\n for (const tpl of templates) {\n const keys = getKeysArray(tpl.substring(2, tpl.length - 1));\n const dsId = keys.shift();\n if (!dsId || dsId !== ds.id) continue;\n if (hasArray === isIncludeArrayField(keys, ds.fields)) return true;\n }\n return false;\n };\n /**\n * 指定数据源的字符串模板,如:{ isBindDataSourceField: true, dataSourceId: 'id', template: `xxx${field}xxx`}\n * @param value any\n * @param dsId string | number\n * @returns boolean\n */\n var isSpecificDataSourceTemplate = (value, dsId) => value?.isBindDataSourceField && value.dataSourceId && value.dataSourceId === dsId && typeof value.template === \"string\";\n /**\n * 关联数据源字段,格式为 [前缀+数据源ID, 字段名]\n * 使用data-source-field-select value: 'value' 可以配置出来\n * @param value any[]\n * @param id string | number\n * @returns boolean\n */\n var isUseDataSourceField = (value, id) => {\n if (!Array.isArray(value) || typeof value[0] !== \"string\") return false;\n const [prefixId] = value;\n const prefixIndex = prefixId.indexOf(DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX);\n if (prefixIndex === -1) return false;\n return prefixId.substring(prefixIndex + DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX.length) === id;\n };\n var isDataSourceTarget = (ds, key, value, hasArray = false) => {\n if (!value) return false;\n const valueType = typeof value;\n if (valueType !== \"string\" && valueType !== \"object\") return false;\n if (`${key}`.startsWith(\"displayConds\")) return false;\n if (valueType === \"string\") return isDataSourceTemplate(value, ds, hasArray);\n if (isObject(value) && value.isBindDataSource && value.dataSourceId === ds.id) return true;\n if (isSpecificDataSourceTemplate(value, ds.id)) return true;\n if (isUseDataSourceField(value, ds.id)) {\n const [, ...keys] = value;\n const includeArray = isIncludeArrayField(keys, ds.fields);\n return hasArray ? includeArray : !includeArray;\n }\n return false;\n };\n var isDataSourceCondTarget = (ds, key, value, hasArray = false) => {\n if (!Array.isArray(value) || !ds) return false;\n const [dsId, ...keys] = value;\n if (dsId !== ds.id || !`${key}`.startsWith(\"displayConds\")) return false;\n if (ds.fields?.some((field) => field.name === keys[0])) {\n const includeArray = isIncludeArrayField(keys, ds.fields);\n return hasArray ? includeArray : !includeArray;\n }\n return false;\n };\n var createDataSourceTarget = (ds, initialDeps = {}) => new Target({\n type: DepTargetType.DATA_SOURCE,\n id: ds.id,\n initialDeps,\n descriptor: {\n type: DepTargetType.DATA_SOURCE,\n ds: {\n id: ds.id,\n fields: ds.fields || []\n }\n },\n isTarget: (key, value) => isDataSourceTarget(ds, key, value)\n });\n var createDataSourceCondTarget = (ds, initialDeps = {}) => new Target({\n type: DepTargetType.DATA_SOURCE_COND,\n id: ds.id,\n initialDeps,\n descriptor: {\n type: DepTargetType.DATA_SOURCE_COND,\n ds: {\n id: ds.id,\n fields: ds.fields || []\n }\n },\n isTarget: (key, value) => isDataSourceCondTarget(ds, key, value)\n });\n var createDataSourceMethodTarget = (ds, initialDeps = {}) => new Target({\n type: DepTargetType.DATA_SOURCE_METHOD,\n id: ds.id,\n initialDeps,\n descriptor: {\n type: DepTargetType.DATA_SOURCE_METHOD,\n ds: {\n id: ds.id,\n methods: (ds.methods || []).map((method) => ({ name: method.name })),\n fields: (ds.fields || []).map((field) => ({ name: field.name }))\n }\n },\n isTarget: (_key, value) => {\n if (!Array.isArray(value)) return false;\n const [dsId, methodName] = value;\n if (!methodName || dsId !== ds.id) return false;\n if (ds.methods?.some((method) => method.name === methodName)) return true;\n if (ds.fields?.some((field) => field.name === methodName)) return false;\n return true;\n }\n });\n /**\n * 用可序列化描述重建 target,使依赖收集可以在 worker 等无法传递函数的环境中进行\n * @param descriptor 由工厂函数写入 target 的描述\n * @param initialDeps 初始依赖\n * @returns Target\n */\n var createTargetByDescriptor = (descriptor, initialDeps = {}) => {\n switch (descriptor.type) {\n case DepTargetType.CODE_BLOCK: return createCodeBlockTarget(descriptor.id, descriptor.codeBlock, initialDeps);\n case DepTargetType.DATA_SOURCE: return createDataSourceTarget(descriptor.ds, initialDeps);\n case DepTargetType.DATA_SOURCE_COND: return createDataSourceCondTarget(descriptor.ds, initialDeps);\n case DepTargetType.DATA_SOURCE_METHOD: return createDataSourceMethodTarget(descriptor.ds, initialDeps);\n default: throw new Error(`unknown target descriptor: ${JSON.stringify(descriptor)}`);\n }\n };\n var traverseTarget = (targetsList, cb, type) => {\n if (type) {\n const targets = targetsList[type];\n if (targets) for (const target of Object.values(targets)) cb(target);\n return;\n }\n for (const targets of Object.values(targetsList)) for (const target of Object.values(targets)) cb(target);\n };\n //#endregion\n //#region packages/dep/dist/es/Watcher.js\n var DATA_SOURCE_TARGET_TYPES = /* @__PURE__ */ new Set([\n DepTargetType.DATA_SOURCE,\n DepTargetType.DATA_SOURCE_COND,\n DepTargetType.DATA_SOURCE_METHOD\n ]);\n var Watcher = class {\n targetsList = {};\n childrenProp = \"items\";\n idProp = \"id\";\n nameProp = \"name\";\n constructor(options) {\n if (options?.initialTargets) this.targetsList = options.initialTargets;\n if (options?.childrenProp) this.childrenProp = options.childrenProp;\n }\n getTargetsList() {\n return this.targetsList;\n }\n /**\n * 获取指定类型中的所有target\n * @param type 分类\n * @returns Target[]\n */\n getTargets(type = DepTargetType.DEFAULT) {\n return this.targetsList[type] || {};\n }\n /**\n * 添加新的目标\n * @param target Target\n */\n addTarget(target) {\n const targets = this.getTargets(target.type) || {};\n this.targetsList[target.type] = targets;\n targets[target.id] = target;\n }\n /**\n * 获取指定id的target\n * @param id target id\n * @returns Target\n */\n getTarget(id, type = DepTargetType.DEFAULT) {\n return this.getTargets(type)[id];\n }\n /**\n * 判断是否存在指定id的target\n * @param id target id\n * @returns boolean\n */\n hasTarget(id, type = DepTargetType.DEFAULT) {\n return Boolean(this.getTarget(id, type));\n }\n /**\n * 判断是否存在指定类型的target\n * @param type target type\n * @returns boolean\n */\n hasSpecifiedTypeTarget(type = DepTargetType.DEFAULT) {\n return Object.keys(this.getTargets(type)).length > 0;\n }\n /**\n * 删除指定id的target\n * @param id target id\n */\n removeTarget(id, type = DepTargetType.DEFAULT) {\n const targets = this.getTargets(type);\n if (targets[id]) {\n targets[id].destroy();\n delete targets[id];\n }\n }\n /**\n * 删除指定分类的所有target\n * @param type 分类\n * @returns void\n */\n removeTargets(type = DepTargetType.DEFAULT) {\n const targets = this.targetsList[type];\n if (!targets) return;\n for (const target of Object.values(targets)) target.destroy();\n delete this.targetsList[type];\n }\n /**\n * 删除所有target\n */\n clearTargets() {\n for (const key of Object.keys(this.targetsList)) delete this.targetsList[key];\n }\n /**\n * 收集依赖\n * @param nodes 需要收集的节点\n * @param deep 是否需要收集子节点\n * @param type 强制收集指定类型的依赖\n */\n collect(nodes, depExtendedData = {}, deep = false, type) {\n const targets = this.getCollectableTargets(type);\n if (!targets.length) return;\n for (const node of nodes) this.removeTargetsDep(targets, node);\n for (const node of nodes) this.collectItems(node, targets, depExtendedData, deep);\n }\n /**\n * 获取本次需要参与收集的 target(过滤规则与 collectByCallback 一致)\n *\n * 注:供 editor 的 dep service / worker 跨包批量收集时复用,因此为 public。\n * @param type 强制收集指定类型的依赖\n */\n getCollectableTargets(type) {\n const targets = [];\n traverseTarget(this.targetsList, (target) => {\n if (!type && !target.isCollectByDefault) return;\n targets.push(target);\n }, type);\n return targets;\n }\n collectByCallback(nodes, type, cb) {\n traverseTarget(this.targetsList, (target) => {\n if (!type && !target.isCollectByDefault) return;\n for (const node of nodes) cb({\n node,\n target\n });\n }, type);\n }\n /**\n * 清除所有目标的依赖\n * @param nodes 需要清除依赖的节点\n */\n clear(nodes, type) {\n let { targetsList } = this;\n if (type) targetsList = { [type]: this.getTargets(type) };\n const clearedItemsNodeIds = /* @__PURE__ */ new Set();\n traverseTarget(targetsList, (target) => {\n if (nodes) for (const node of nodes) {\n target.removeDep(node[this.idProp]);\n if (Array.isArray(node[this.childrenProp]) && node[this.childrenProp].length && !clearedItemsNodeIds.has(node[this.idProp])) {\n clearedItemsNodeIds.add(node[this.idProp]);\n this.clear(node[this.childrenProp]);\n }\n }\n else target.removeDep();\n });\n }\n /**\n * 清除指定类型的依赖\n * @param type 类型\n * @param nodes 需要清除依赖的节点\n */\n clearByType(type, nodes) {\n this.clear(nodes, type);\n }\n /**\n * 收集单个 target 的依赖,等价于 collectItems(node, [target], ...)\n */\n collectItem(node, target, depExtendedData = {}, deep = false) {\n this.collectItems(node, [target], depExtendedData, deep);\n }\n removeTargetDep(target, node, key) {\n target.removeDep(node[this.idProp], key);\n if (typeof key === \"undefined\" && Array.isArray(node[this.childrenProp]) && node[this.childrenProp].length) for (const item of node[this.childrenProp]) this.removeTargetDep(target, item, key);\n }\n /**\n * 与 removeTargetDep 等价,但一次子树递归同时处理多个 target,\n * 把删除阶段的结构遍历从 ×targets 降到 ×1。\n *\n * 注:供 editor 的 dep service 跨包批量删除时复用,因此为 public。\n */\n removeTargetsDep(targets, node, key) {\n const id = node[this.idProp];\n for (const target of targets) target.removeDep(id, key);\n if (typeof key === \"undefined\" && Array.isArray(node[this.childrenProp]) && node[this.childrenProp].length) for (const item of node[this.childrenProp]) this.removeTargetsDep(targets, item, key);\n }\n /**\n * 与 collectItem 等价,但一次遍历同时处理多个 target(不含删除阶段)。\n *\n * 关键优化:原实现对每个 target 都完整遍历一遍节点树(O(targets × 树规模)),大页面 + 大量数据源时,\n * 结构遍历(Object.entries / 递归 / fullKey 字符串拼接)会被重复 targets 次。这里改为「整棵树只遍历一次,\n * 在每个属性上检查所有 target」,把结构遍历开销从 ×targets 降到 ×1,isTarget 调用次数不变,收集结果完全一致。\n *\n * 注:供 editor 的 dep service / worker 跨包批量收集时复用,因此为 public。\n */\n collectItems(node, targets, depExtendedData = {}, deep = false) {\n const activeTargets = this.filterTargetsByNode(node, targets);\n if (!activeTargets.length) return;\n this.collectTargetForTargets(node, node, \"\", activeTargets, depExtendedData, deep);\n }\n filterTargetsByNode(node, targets) {\n const disableDataSource = Boolean(node[NODE_DISABLE_DATA_SOURCE_KEY]);\n const disableCodeBlock = Boolean(node[NODE_DISABLE_CODE_BLOCK_KEY]);\n if (!disableDataSource && !disableCodeBlock) return targets;\n return targets.filter((target) => {\n if (disableDataSource && DATA_SOURCE_TARGET_TYPES.has(target.type)) return false;\n if (disableCodeBlock && target.type === DepTargetType.CODE_BLOCK) return false;\n return true;\n });\n }\n collectTargetForTargets(node, config, prop, targets, depExtendedData, deep) {\n const id = node[this.idProp];\n const name = `${node[this.nameProp] || node[this.idProp]}`;\n for (const [key, value] of Object.entries(config)) {\n if (typeof value === \"undefined\" || value === \"\") continue;\n const keyIsItems = key === this.childrenProp;\n const fullKey = prop ? `${prop}.${key}` : key;\n let notMatched = null;\n for (let i = 0, l = targets.length; i < l; i++) {\n const target = targets[i];\n if (target.isTarget(fullKey, value, config)) target.updateDep({\n id,\n name,\n data: depExtendedData,\n key: fullKey\n });\n else (notMatched || (notMatched = [])).push(target);\n }\n if (notMatched) {\n if (!keyIsItems && Array.isArray(value)) for (let i = 0, l = value.length; i < l; i++) {\n const item = value[i];\n if (isObject(item)) this.collectTargetForTargets(node, item, `${fullKey}[${i}]`, notMatched, depExtendedData, deep);\n }\n else if (isObject(value)) this.collectTargetForTargets(node, value, fullKey, notMatched, depExtendedData, deep);\n }\n if (keyIsItems && deep && Array.isArray(value)) for (const child of value) this.collectItems(child, targets, depExtendedData, deep);\n }\n }\n };\n //#endregion\n //#region packages/editor/src/utils/logger.ts\n const error = (...args) => {\n if (process.env.NODE_ENV === \"development\") console.error(\"magic editor: \", ...args);\n };\n //#endregion\n //#region packages/editor/src/utils/dep/worker.ts\n /**\n * 收集本次会覆盖到的节点 id\n * 必须与 deep 一致:deep=false 时不能带上子孙 id,否则写回阶段 removeDep 会清掉未重收的子节点依赖\n */\n const getNodeIds = (nodes, deep, ids = []) => {\n for (const node of nodes) {\n ids.push(node.id);\n if (deep && Array.isArray(node.items) && node.items.length) getNodeIds(node.items, deep, ids);\n }\n return ids;\n };\n const collectDsl = ({ id, dsl }) => {\n try {\n const mApp = eval(`(${dsl})`);\n if (!mApp) {\n postMessage({\n id,\n deps: {}\n });\n return;\n }\n const watcher = new Watcher({ initialTargets: {} });\n if (mApp.codeBlocks) for (const [id, code] of Object.entries(mApp.codeBlocks)) watcher.addTarget(createCodeBlockTarget(id, code));\n if (mApp.dataSources) for (const ds of mApp.dataSources) {\n watcher.addTarget(createDataSourceTarget(ds, {}));\n watcher.addTarget(createDataSourceMethodTarget(ds, {}));\n watcher.addTarget(createDataSourceCondTarget(ds, {}));\n }\n const targets = watcher.getCollectableTargets();\n for (const page of mApp.items) watcher.collectItems(page, targets, { pageId: page.id }, true);\n const deps = {\n [DepTargetType.DATA_SOURCE]: {},\n [DepTargetType.DATA_SOURCE_METHOD]: {},\n [DepTargetType.DATA_SOURCE_COND]: {},\n [DepTargetType.CODE_BLOCK]: {}\n };\n traverseTarget(watcher.getTargetsList(), (target) => {\n deps[target.type][target.id] = target.deps;\n });\n const response = {\n id,\n deps\n };\n postMessage(response);\n } catch (e) {\n error(e);\n postMessage({\n id,\n deps: {},\n failed: true\n });\n }\n };\n const collectNodes = ({ id, payload }) => {\n try {\n const { nodes, targets: descriptors, depExtendedData, deep } = eval(`(${payload})`);\n const watcher = new Watcher({ initialTargets: {} });\n const targets = descriptors.map((descriptor) => createTargetByDescriptor(descriptor));\n for (const node of nodes) watcher.collectItems(node, targets, depExtendedData, deep);\n const deps = {};\n for (const target of targets) {\n if (!deps[target.type]) deps[target.type] = {};\n deps[target.type][target.id] = target.deps;\n }\n const response = {\n id,\n deps,\n nodeIds: getNodeIds(nodes, deep)\n };\n postMessage(response);\n } catch (e) {\n error(e);\n postMessage({\n id,\n deps: {},\n nodeIds: [],\n failed: true\n });\n }\n };\n /**\n * 依赖收集 worker\n *\n * 依赖收集需要深度遍历整棵节点树并对每个属性做 target 匹配,节点/数据源多时会长时间占用主线程导致页面卡死,\n * 因此把遍历匹配放到 worker 中执行。\n * 全量与增量收集共用一个常驻 worker:收集逻辑只打进一份 inline worker 产物,\n * 也不会因为频繁收集而反复创建线程,每个请求用 id 与调用方对应。\n */\n onmessage = (e) => {\n if (\"dsl\" in e.data) {\n collectDsl(e.data);\n return;\n }\n collectNodes(e.data);\n };\n //#endregion\n})();\n";
11395
11395
  var blob = typeof self !== "undefined" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", jsContent], { type: "text/javascript;charset=utf-8" });
11396
11396
  function WorkerWrapper(options) {
11397
11397
  let objURL;
@@ -11836,6 +11836,7 @@
11836
11836
  containerHighlightAddOnly: stageOptions.containerHighlightAddOnly,
11837
11837
  disabledDragStart: stageOptions.disabledDragStart,
11838
11838
  renderType: stageOptions.renderType,
11839
+ iframeSandbox: stageOptions.iframeSandbox,
11839
11840
  canSelect: (el, event, stop) => {
11840
11841
  if (!stageOptions.canSelect) return true;
11841
11842
  const elCanSelect = stageOptions.canSelect?.(el);
@@ -13182,21 +13183,19 @@
13182
13183
  };
13183
13184
  editorService.add(pageConfig);
13184
13185
  };
13186
+ const addNormalPage = () => addPage(_tmagic_core.NodeType.PAGE);
13187
+ const addPageFragment = () => addPage(_tmagic_core.NodeType.PAGE_FRAGMENT);
13185
13188
  return (_ctx, _cache) => {
13186
13189
  return showAddPageButton.value ? ((0, vue.openBlock)(), (0, vue.createElementBlock)("div", _hoisted_1$75, [(0, vue.createVNode)((0, vue.unref)(_tmagic_design.TMagicPopover), { "popper-class": "data-source-list-panel-add-menu" }, {
13187
13190
  reference: (0, vue.withCtx)(() => [(0, vue.createVNode)(Icon_default, { icon: (0, vue.unref)(_element_plus_icons_vue.Plus) }, null, 8, ["icon"])]),
13188
13191
  default: (0, vue.withCtx)(() => [(0, vue.createVNode)(ToolButton_default, { data: {
13189
13192
  type: "button",
13190
13193
  text: "页面",
13191
- handler: () => {
13192
- addPage((0, vue.unref)(_tmagic_core.NodeType).PAGE);
13193
- }
13194
+ handler: addNormalPage
13194
13195
  } }, null, 8, ["data"]), (0, vue.createVNode)(ToolButton_default, { data: {
13195
13196
  type: "button",
13196
13197
  text: "页面片",
13197
- handler: () => {
13198
- addPage((0, vue.unref)(_tmagic_core.NodeType).PAGE_FRAGMENT);
13199
- }
13198
+ handler: addPageFragment
13200
13199
  } }, null, 8, ["data"])]),
13201
13200
  _: 1
13202
13201
  })])) : ((0, vue.openBlock)(), (0, vue.createElementBlock)("div", _hoisted_2$30));
@@ -15860,7 +15859,7 @@
15860
15859
  items: { default: () => [] },
15861
15860
  width: {},
15862
15861
  gap: { default: 3 },
15863
- popoverWidth: { default: 180 }
15862
+ popoverWidth: {}
15864
15863
  },
15865
15864
  setup(__props) {
15866
15865
  const props = __props;
@@ -15891,7 +15890,17 @@
15891
15890
  const containerWidth = (0, vue.ref)(0);
15892
15891
  const hiddenIndexSet = (0, vue.ref)(/* @__PURE__ */ new Set());
15893
15892
  const hasOverflow = (0, vue.computed)(() => hiddenIndexSet.value.size > 0);
15894
- const overflowItems = (0, vue.computed)(() => props.items.filter((_, index) => hiddenIndexSet.value.has(index)));
15893
+ const overflowItems = (0, vue.computed)(() => {
15894
+ const items = props.items.filter((_, index) => hiddenIndexSet.value.has(index)).map((item) => item.type === "divider" ? {
15895
+ ...item,
15896
+ direction: "horizontal"
15897
+ } : item);
15898
+ let start = 0;
15899
+ let end = items.length;
15900
+ while (start < end && items[start].type === "divider") start += 1;
15901
+ while (end > start && items[end - 1].type === "divider") end -= 1;
15902
+ return items.slice(start, end);
15903
+ });
15895
15904
  const measureAndCompute = () => {
15896
15905
  if (!columnEl.value) return;
15897
15906
  containerWidth.value = columnEl.value.clientWidth;
@@ -19294,7 +19303,11 @@
19294
19303
  let timeout;
19295
19304
  let clientX;
19296
19305
  let clientY;
19297
- const appendComponent = ({ text, type, data = {} }) => {
19306
+ const appendComponent = async (item) => {
19307
+ if (stageOptions?.beforeAppendComponent) {
19308
+ if (await stageOptions.beforeAppendComponent(item) === false) return;
19309
+ }
19310
+ const { text, type, data = {} } = item;
19298
19311
  editorService.add({
19299
19312
  name: text,
19300
19313
  type,
@@ -20462,6 +20475,9 @@
20462
20475
  left: (0, _tmagic_utils.calcValueByFontsize)(doc, left / zoom.value)
20463
20476
  };
20464
20477
  dragData.inputEvent = e;
20478
+ if (props.stageOptions.beforeDropComponent) {
20479
+ if (await props.stageOptions.beforeDropComponent(dragData, parent) === false) return;
20480
+ }
20465
20481
  editorService.add(dragData, parent, { historySource: "component-panel" });
20466
20482
  }
20467
20483
  };
@@ -20814,8 +20830,8 @@
20814
20830
  this.register(map);
20815
20831
  }
20816
20832
  reset() {
20817
- this.controllers.forEach((keycon$2) => {
20818
- keycon$2.destroy();
20833
+ this.controllers.forEach((keycon) => {
20834
+ keycon.destroy();
20819
20835
  });
20820
20836
  this.controllers.clear();
20821
20837
  this.bindingList = [];
@@ -20828,15 +20844,15 @@
20828
20844
  const { type, eventType, command, keybinding, bound } = item;
20829
20845
  if (name && name !== type) continue;
20830
20846
  if (bound) continue;
20831
- const keycon$3 = this.controllers.get(type);
20832
- if (!keycon$3) continue;
20847
+ const keycon = this.controllers.get(type);
20848
+ if (!keycon) continue;
20833
20849
  const handler = (e) => {
20834
20850
  e.inputEvent.preventDefault();
20835
20851
  this.commands[command]?.(e.inputEvent);
20836
20852
  };
20837
20853
  this.getKeyconKeys(keybinding).forEach((keys) => {
20838
- if (keys[0]) keycon$3[eventType](keys, handler);
20839
- else keycon$3[eventType](handler);
20854
+ if (keys[0]) keycon[eventType](keys, handler);
20855
+ else keycon[eventType](handler);
20840
20856
  });
20841
20857
  item.bound = true;
20842
20858
  }
@@ -21374,6 +21390,7 @@
21374
21390
  stageContentMenu: {},
21375
21391
  runtimeUrl: {},
21376
21392
  renderType: {},
21393
+ iframeSandbox: { type: [String, Boolean] },
21377
21394
  autoScrollIntoView: { type: Boolean },
21378
21395
  propsConfigs: {},
21379
21396
  propsValues: {},
@@ -21410,6 +21427,8 @@
21410
21427
  layerNodeIsExpandable: { type: Function },
21411
21428
  canDropIn: { type: Function },
21412
21429
  beforeDblclick: { type: Function },
21430
+ beforeAppendComponent: { type: Function },
21431
+ beforeDropComponent: { type: Function },
21413
21432
  beforeLayerNodeDblclick: { type: Function },
21414
21433
  historyListExtraTabs: {},
21415
21434
  pageBarSortOptions: {},
@@ -21459,11 +21478,14 @@
21459
21478
  containerHighlightAddOnly: props.containerHighlightAddOnly,
21460
21479
  disabledDragStart: props.disabledDragStart,
21461
21480
  renderType: props.renderType,
21481
+ iframeSandbox: props.iframeSandbox,
21462
21482
  guidesOptions: props.guidesOptions,
21463
21483
  disabledMultiSelect: props.disabledMultiSelect,
21464
21484
  alwaysMultiSelect: props.alwaysMultiSelect,
21465
21485
  disabledFlashTip: props.disabledFlashTip,
21466
- beforeDblclick: props.beforeDblclick
21486
+ beforeDblclick: props.beforeDblclick,
21487
+ beforeAppendComponent: props.beforeAppendComponent,
21488
+ beforeDropComponent: props.beforeDropComponent
21467
21489
  };
21468
21490
  stageOverlay_default.set("stageOptions", stageOptions);
21469
21491
  (0, vue.provide)("services", services);
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.8.3",
2
+ "version": "1.8.4-beta.1",
3
3
  "name": "@tmagic/editor",
4
4
  "type": "module",
5
5
  "sideEffects": [
@@ -68,11 +68,11 @@
68
68
  "moveable": "^0.53.0",
69
69
  "serialize-javascript": "^7.1.1",
70
70
  "sortablejs": "^1.15.7",
71
- "@tmagic/design": "1.8.3",
72
- "@tmagic/form": "1.8.3",
73
- "@tmagic/utils": "1.8.3",
74
- "@tmagic/stage": "1.8.3",
75
- "@tmagic/table": "1.8.3"
71
+ "@tmagic/design": "1.8.4-beta.1",
72
+ "@tmagic/form": "1.8.4-beta.1",
73
+ "@tmagic/stage": "1.8.4-beta.1",
74
+ "@tmagic/utils": "1.8.4-beta.1",
75
+ "@tmagic/table": "1.8.4-beta.1"
76
76
  },
77
77
  "devDependencies": {
78
78
  "@types/events": "^3.0.3",
@@ -85,8 +85,8 @@
85
85
  "monaco-editor": "^0.55.1 ",
86
86
  "type-fest": "^5.2.0",
87
87
  "typescript": "^6.0.3",
88
- "vue": "^3.5.43",
89
- "@tmagic/core": "1.8.3"
88
+ "vue": "^3.5.44",
89
+ "@tmagic/core": "1.8.4-beta.1"
90
90
  },
91
91
  "peerDependenciesMeta": {
92
92
  "typescript": {
package/src/Editor.vue CHANGED
@@ -224,11 +224,14 @@ const stageOptions: StageOptions = {
224
224
  containerHighlightAddOnly: props.containerHighlightAddOnly,
225
225
  disabledDragStart: props.disabledDragStart,
226
226
  renderType: props.renderType,
227
+ iframeSandbox: props.iframeSandbox,
227
228
  guidesOptions: props.guidesOptions,
228
229
  disabledMultiSelect: props.disabledMultiSelect,
229
230
  alwaysMultiSelect: props.alwaysMultiSelect,
230
231
  disabledFlashTip: props.disabledFlashTip,
231
232
  beforeDblclick: props.beforeDblclick,
233
+ beforeAppendComponent: props.beforeAppendComponent,
234
+ beforeDropComponent: props.beforeDropComponent,
232
235
  };
233
236
 
234
237
  stageOverlayService.set('stageOptions', stageOptions);
@@ -13,6 +13,8 @@ import StageCore, {
13
13
  import { getIdFromEl } from '@tmagic/utils';
14
14
 
15
15
  import type {
16
+ BeforeAppendComponent,
17
+ BeforeDropComponent,
16
18
  CanDropInFunction,
17
19
  ComponentGroup,
18
20
  CustomContentMenuFunction,
@@ -62,6 +64,16 @@ export interface EditorProps {
62
64
  runtimeUrl?: string;
63
65
  /** 是用iframe渲染还是直接渲染 */
64
66
  renderType?: RenderType;
67
+ /**
68
+ * 画布 iframe 的 sandbox 属性值(仅 iframe 渲染时生效)
69
+ *
70
+ * - 不传 / `false`:不设置 sandbox 属性(默认)
71
+ * - 字符串:原样写入,token 空格分隔,如 `'allow-scripts allow-same-origin allow-downloads'`
72
+ *
73
+ * 需要注意编辑器自身依赖 `allow-scripts` + `allow-same-origin`(读写 iframe 内 DOM),
74
+ * 其余 token(`allow-downloads`、`allow-popups`、`allow-forms` 等)按业务需要放开。
75
+ */
76
+ iframeSandbox?: string | false;
65
77
  /** 选中时是否自动滚动到可视区域 */
66
78
  autoScrollIntoView?: boolean;
67
79
  /** 组件的属性配置表单的dsl */
@@ -150,6 +162,17 @@ export interface EditorProps {
150
162
  canDropIn?: CanDropInFunction;
151
163
  /** 画布双击前的钩子函数,返回 false 则阻止默认的双击行为 */
152
164
  beforeDblclick?: (event: MouseEvent) => Promise<boolean | void> | boolean | void;
165
+ /**
166
+ * 点击组件列表新增组件前的钩子,入参为当前组件列表项,允许异步函数。
167
+ * 仅点击新增时调用,拖入画布不走该钩子;返回 false 取消本次新增。
168
+ * 可直接修改入参(例如补全 data),修改会反映到最终新增的节点上。
169
+ */
170
+ beforeAppendComponent?: BeforeAppendComponent;
171
+ /**
172
+ * 组件列表拖入画布、写入节点前的钩子,入参为即将新增的节点和父节点,允许异步函数。
173
+ * 仅当拖拽类型为组件列表时,在位置计算完成之后调用;返回 false 取消本次拖入。
174
+ */
175
+ beforeDropComponent?: BeforeDropComponent;
153
176
  /** 组件树节点双击前的钩子函数,返回 false 则阻止默认的双击行为 */
154
177
  beforeLayerNodeDblclick?: (event: MouseEvent, data: TreeNodeData) => Promise<boolean | void> | boolean | void;
155
178
  /** 历史记录面板的自定义扩展 tab,追加在内置的页面/数据源/代码块 tab 之后 */
@@ -31,6 +31,7 @@ export const useStage = (stageOptions: StageOptions) => {
31
31
  containerHighlightAddOnly: stageOptions.containerHighlightAddOnly,
32
32
  disabledDragStart: stageOptions.disabledDragStart,
33
33
  renderType: stageOptions.renderType,
34
+ iframeSandbox: stageOptions.iframeSandbox,
34
35
  canSelect: (el, event, stop) => {
35
36
  if (!stageOptions.canSelect) return true;
36
37
 
@@ -62,13 +62,14 @@ const props = withDefaults(
62
62
  width?: number;
63
63
  /** 子元素之间的间距,需与 SCSS gap 保持一致 */
64
64
  gap?: number;
65
- /** Popover 内容宽度 */
66
- popoverWidth?: number;
65
+ /**
66
+ * Popover 内容宽度。不传时按内容撑开,避免下拉框超出菜单。
67
+ */
68
+ popoverWidth?: number | string;
67
69
  }>(),
68
70
  {
69
71
  items: () => [],
70
72
  gap: 3,
71
- popoverWidth: 180,
72
73
  },
73
74
  );
74
75
 
@@ -110,7 +111,18 @@ const containerWidth = ref(0);
110
111
  const hiddenIndexSet = ref<Set<number>>(new Set());
111
112
 
112
113
  const hasOverflow = computed(() => hiddenIndexSet.value.size > 0);
113
- const overflowItems = computed(() => props.items.filter((_, index) => hiddenIndexSet.value.has(index)));
114
+ // 更多菜单是纵向列表:分割线改成横线;收进去的首尾分割线没有分组意义,直接去掉。
115
+ const overflowItems = computed(() => {
116
+ const items = props.items
117
+ .filter((_, index) => hiddenIndexSet.value.has(index))
118
+ .map((item) => (item.type === 'divider' ? { ...item, direction: 'horizontal' as const } : item));
119
+
120
+ let start = 0;
121
+ let end = items.length;
122
+ while (start < end && items[start].type === 'divider') start += 1;
123
+ while (end > start && items[end - 1].type === 'divider') end -= 1;
124
+ return items.slice(start, end);
125
+ });
114
126
 
115
127
  const measureAndCompute = () => {
116
128
  if (!columnEl.value) return;
@@ -13,18 +13,14 @@
13
13
  :data="{
14
14
  type: 'button',
15
15
  text: '页面',
16
- handler: () => {
17
- addPage(NodeType.PAGE);
18
- },
16
+ handler: addNormalPage,
19
17
  }"
20
18
  ></ToolButton>
21
19
  <ToolButton
22
20
  :data="{
23
21
  type: 'button',
24
22
  text: '页面片',
25
- handler: () => {
26
- addPage(NodeType.PAGE_FRAGMENT);
27
- },
23
+ handler: addPageFragment,
28
24
  }"
29
25
  ></ToolButton>
30
26
  </TMagicPopover>
@@ -62,4 +58,7 @@ const addPage = (type: NodeType.PAGE | NodeType.PAGE_FRAGMENT) => {
62
58
  };
63
59
  editorService.add(pageConfig);
64
60
  };
61
+
62
+ const addNormalPage = () => addPage(NodeType.PAGE);
63
+ const addPageFragment = () => addPage(NodeType.PAGE_FRAGMENT);
65
64
  </script>
@@ -97,7 +97,13 @@ let timeout: ReturnType<typeof setTimeout> | undefined;
97
97
  let clientX: number;
98
98
  let clientY: number;
99
99
 
100
- const appendComponent = ({ text, type, data = {} }: ComponentItem): void => {
100
+ const appendComponent = async (item: ComponentItem): Promise<void> => {
101
+ if (stageOptions?.beforeAppendComponent) {
102
+ const result = await stageOptions.beforeAppendComponent(item);
103
+ if (result === false) return;
104
+ }
105
+
106
+ const { text, type, data = {} } = item;
101
107
  editorService.add(
102
108
  {
103
109
  name: text,
@@ -450,6 +450,11 @@ const dropHandler = async (e: DragEvent) => {
450
450
 
451
451
  dragData.inputEvent = e;
452
452
 
453
+ if (props.stageOptions.beforeDropComponent) {
454
+ const result = await props.stageOptions.beforeDropComponent(dragData, parent);
455
+ if (result === false) return;
456
+ }
457
+
453
458
  editorService.add(dragData, parent, { historySource: 'component-panel' });
454
459
  }
455
460
  };
@@ -150,8 +150,10 @@ class Dep extends BaseService {
150
150
  const targets = this.watcher.getCollectableTargets(type);
151
151
 
152
152
  if (targets.length) {
153
+ // 先统一删除所有节点的旧依赖,再统一收集。
154
+ // 如果逐节点“删除 + 收集”,共用 id 的多棵树(如复制出的页面片),
155
+ // 后一棵树的删除会清掉前一棵树刚写入的依赖。
153
156
  for (const node of nodes) {
154
- // 先删除原有依赖,再批量收集
155
157
  if (isPage(node)) {
156
158
  for (const target of targets) {
157
159
  this.removePageDep(target, depExtendedData);
@@ -159,6 +161,8 @@ class Dep extends BaseService {
159
161
  } else {
160
162
  this.watcher.removeTargetsDep(targets, node);
161
163
  }
164
+ }
165
+ for (const node of nodes) {
162
166
  this.watcher.collectItems(node, targets, depExtendedData, deep);
163
167
  }
164
168
  }
@@ -120,7 +120,12 @@
120
120
  }
121
121
  }
122
122
 
123
- .m-editor-nav-menu-popover {
123
+ .tmagic-design-popper.m-editor-nav-menu-popover {
124
+ // 不固定宽度,按内容撑开,避免下拉框超出菜单;content-box 让 padding 不挤占内容宽度。
125
+ width: max-content;
126
+ min-width: 180px;
127
+ box-sizing: content-box;
128
+
124
129
  .m-editor-nav-menu-overflow-list {
125
130
  display: flex;
126
131
  flex-direction: column;
@@ -139,8 +144,30 @@
139
144
  }
140
145
 
141
146
  &.divider {
142
- padding: 0;
147
+ display: flex;
148
+ align-items: center;
149
+ align-self: stretch;
150
+ width: auto;
151
+ height: auto;
152
+ padding: 4px 12px;
143
153
  cursor: default;
154
+ box-sizing: border-box;
155
+
156
+ // 更多菜单是纵向列表,分割线必须盖过组件默认的竖线样式
157
+ .tmagic-design-divider,
158
+ .el-divider,
159
+ .t-divider {
160
+ display: block;
161
+ flex: 1 1 auto;
162
+ width: 100%;
163
+ min-width: 0;
164
+ height: 0;
165
+ margin: 0;
166
+ border: 0;
167
+ border-top: 1px solid var(--el-border-color, #dcdfe6);
168
+ border-left: 0;
169
+ position: static;
170
+ }
144
171
 
145
172
  &:hover {
146
173
  background-color: transparent;
package/src/type.ts CHANGED
@@ -221,8 +221,20 @@ export interface StageOptions {
221
221
  */
222
222
  disabledFlashTip?: boolean;
223
223
  zoom?: number;
224
+ /** 画布 iframe 的 sandbox 属性值,不传 / false 表示不设置,见 @tmagic/stage 的 StageRenderConfig.iframeSandbox */
225
+ iframeSandbox?: string | false;
224
226
  /** 画布双击前的钩子函数,返回 false 则阻止默认的双击行为 */
225
227
  beforeDblclick?: (event: MouseEvent) => Promise<boolean | void> | boolean | void;
228
+ /**
229
+ * 点击组件列表新增组件前的钩子,详见 EditorProps.beforeAppendComponent。
230
+ * 仅点击新增时调用;返回 false 取消新增,支持异步。
231
+ */
232
+ beforeAppendComponent?: BeforeAppendComponent;
233
+ /**
234
+ * 组件列表拖入画布、写入节点前的钩子,详见 EditorProps.beforeDropComponent。
235
+ * 仅 dragType 为组件列表时调用;返回 false 取消本次拖入,支持异步。
236
+ */
237
+ beforeDropComponent?: BeforeDropComponent;
226
238
  }
227
239
 
228
240
  // #region NodeInvalidInfo
@@ -676,6 +688,18 @@ export interface ComponentItem {
676
688
  }
677
689
  // #endregion ComponentItem
678
690
 
691
+ /**
692
+ * 点击组件列表新增组件前的钩子。
693
+ * 入参为当前组件列表项,允许异步;返回 false 时取消本次新增。
694
+ */
695
+ export type BeforeAppendComponent = (item: ComponentItem) => boolean | void | Promise<boolean | void>;
696
+
697
+ /**
698
+ * 组件列表拖入画布、写入节点前的钩子。
699
+ * 入参为即将新增的节点和父节点,允许异步;返回 false 时取消本次拖入。
700
+ */
701
+ export type BeforeDropComponent = (dragData: AddMNode, parent: MContainer) => boolean | void | Promise<boolean | void>;
702
+
679
703
  // #region ComponentGroup
680
704
  export interface ComponentGroup {
681
705
  /** 显示文案 */