@tmagic/editor 1.8.0-beta.1 → 1.8.0-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es/Editor.vue_vue_type_script_setup_true_lang.js +31 -5
- package/dist/es/components/CodeBlockEditor.vue_vue_type_script_setup_true_lang.js +21 -113
- package/dist/es/components/CodeParams.vue_vue_type_script_setup_true_lang.js +7 -1
- package/dist/es/components/CompareForm.js +5 -0
- package/dist/es/components/CompareForm.vue_vue_type_script_setup_true_lang.js +196 -0
- package/dist/es/components/ContentMenu.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/components/FloatingBox.vue_vue_type_script_setup_true_lang.js +2 -2
- package/dist/es/components/Icon.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/components/Resizer.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/components/ScrollBar.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/components/ScrollViewer.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/components/SearchInput.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/components/SplitView.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/components/ToolButton.vue_vue_type_script_setup_true_lang.js +7 -7
- package/dist/es/components/Tree.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/components/TreeNode.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/editorProps.js +2 -0
- package/dist/es/fields/Code.vue_vue_type_script_setup_true_lang.js +20 -4
- package/dist/es/fields/CodeLink.vue_vue_type_script_setup_true_lang.js +3 -6
- package/dist/es/fields/CodeSelect.vue_vue_type_script_setup_true_lang.js +19 -3
- package/dist/es/fields/CodeSelectCol.vue_vue_type_script_setup_true_lang.js +42 -8
- package/dist/es/fields/CondOpSelect.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/DataSourceFieldSelect/FieldSelect.vue_vue_type_script_setup_true_lang.js +14 -6
- package/dist/es/fields/DataSourceFieldSelect/Index.vue_vue_type_script_setup_true_lang.js +3 -3
- package/dist/es/fields/DataSourceFields.vue_vue_type_script_setup_true_lang.js +41 -16
- package/dist/es/fields/DataSourceInput.vue_vue_type_script_setup_true_lang.js +2 -2
- package/dist/es/fields/DataSourceMethodSelect.vue_vue_type_script_setup_true_name_true_lang.js +9 -5
- package/dist/es/fields/DataSourceMethods.vue_vue_type_script_setup_true_lang.js +40 -19
- package/dist/es/fields/DataSourceMocks.vue_vue_type_script_setup_true_lang.js +17 -9
- package/dist/es/fields/DataSourceSelect.vue_vue_type_script_setup_true_lang.js +4 -2
- package/dist/es/fields/DisplayConds.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/EventSelect.vue_vue_type_script_setup_true_lang.js +59 -12
- package/dist/es/fields/KeyValue.vue_vue_type_script_setup_true_lang.js +12 -7
- package/dist/es/fields/PageFragmentSelect.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/StyleSetter/Index.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/StyleSetter/components/BackgroundPosition.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/StyleSetter/components/Border.vue_vue_type_script_setup_true_lang.js +28 -6
- package/dist/es/fields/StyleSetter/components/Box.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/StyleSetter/icons/background-repeat/NoRepeat.js +1 -1
- package/dist/es/fields/StyleSetter/icons/background-repeat/Repeat.js +1 -1
- package/dist/es/fields/StyleSetter/icons/background-repeat/RepeatX.js +1 -1
- package/dist/es/fields/StyleSetter/icons/background-repeat/RepeatY.js +1 -1
- package/dist/es/fields/StyleSetter/icons/display/Block.js +1 -1
- package/dist/es/fields/StyleSetter/icons/display/Flex.js +1 -1
- package/dist/es/fields/StyleSetter/icons/display/Inline.js +1 -1
- package/dist/es/fields/StyleSetter/icons/display/InlineBlock.js +1 -1
- package/dist/es/fields/StyleSetter/icons/display/None.js +1 -1
- package/dist/es/fields/StyleSetter/icons/flex-direction/Column.js +1 -1
- package/dist/es/fields/StyleSetter/icons/flex-direction/ColumnReverse.js +1 -1
- package/dist/es/fields/StyleSetter/icons/flex-direction/Row.js +1 -1
- package/dist/es/fields/StyleSetter/icons/flex-direction/RowReverse.js +1 -1
- package/dist/es/fields/StyleSetter/icons/justify-content/Center.js +1 -1
- package/dist/es/fields/StyleSetter/icons/justify-content/FlexEnd.js +1 -1
- package/dist/es/fields/StyleSetter/icons/justify-content/FlexStart.js +1 -1
- package/dist/es/fields/StyleSetter/icons/justify-content/SpaceAround.js +1 -1
- package/dist/es/fields/StyleSetter/icons/justify-content/SpaceBetween.js +1 -1
- package/dist/es/fields/StyleSetter/icons/text-align/Center.js +1 -1
- package/dist/es/fields/StyleSetter/icons/text-align/Left.js +1 -1
- package/dist/es/fields/StyleSetter/icons/text-align/Right.js +1 -1
- package/dist/es/fields/StyleSetter/pro/Background.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/StyleSetter/pro/Border.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/StyleSetter/pro/Font.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/StyleSetter/pro/Layout.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/StyleSetter/pro/Position.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/StyleSetter/pro/Transform.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/UISelect.vue_vue_type_script_setup_true_lang.js +9 -6
- package/dist/es/hooks/use-code-block-edit.js +7 -4
- package/dist/es/hooks/use-data-source-edit.js +5 -2
- package/dist/es/hooks/use-stage.js +22 -10
- package/dist/es/icons/CenterIcon.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/icons/CodeIcon.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/icons/FolderMinusIcon.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/index.js +14 -3
- package/dist/es/initService.js +7 -7
- package/dist/es/layouts/AddPageBox.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/CodeEditor.vue_vue_type_script_setup_true_lang.js +7 -6
- package/dist/es/layouts/Framework.vue_vue_type_script_setup_true_lang.js +14 -9
- package/dist/es/layouts/NavMenu.vue_vue_type_script_setup_true_lang.js +12 -4
- package/dist/es/layouts/NavMenuColumn.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/history-list/Bucket.js +5 -0
- package/dist/es/layouts/history-list/Bucket.vue_vue_type_script_setup_true_lang.js +71 -0
- package/dist/es/layouts/history-list/BucketTab.js +5 -0
- package/dist/es/layouts/history-list/BucketTab.vue_vue_type_script_setup_true_lang.js +62 -0
- package/dist/es/layouts/history-list/GroupRow.js +5 -0
- package/dist/es/layouts/history-list/GroupRow.vue_vue_type_script_setup_true_lang.js +269 -0
- package/dist/es/layouts/history-list/HistoryDiffDialog.js +5 -0
- package/dist/es/layouts/history-list/HistoryDiffDialog.vue_vue_type_script_setup_true_lang.js +294 -0
- package/dist/es/layouts/history-list/HistoryListPanel.js +5 -0
- package/dist/es/layouts/history-list/HistoryListPanel.vue_vue_type_script_setup_true_lang.js +327 -0
- package/dist/es/layouts/history-list/InitialRow.js +5 -0
- package/dist/es/layouts/history-list/InitialRow.vue_vue_type_script_setup_true_lang.js +83 -0
- package/dist/es/layouts/history-list/PageTab.js +5 -0
- package/dist/es/layouts/history-list/PageTab.vue_vue_type_script_setup_true_lang.js +95 -0
- package/dist/es/layouts/history-list/composables.js +184 -0
- package/dist/es/layouts/history-list/useHistoryList.js +56 -0
- package/dist/es/layouts/history-list/useHistoryRevert.js +313 -0
- package/dist/es/layouts/page-bar/AddButton.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/page-bar/PageBar.vue_vue_type_script_setup_true_lang.js +5 -3
- package/dist/es/layouts/page-bar/PageBarScrollContainer.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/page-bar/PageList.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/page-bar/Search.vue_vue_type_script_setup_true_lang.js +2 -2
- package/dist/es/layouts/props-panel/FormPanel.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/props-panel/PropsPanel.vue_vue_type_script_setup_true_lang.js +6 -2
- package/dist/es/layouts/sidebar/ComponentListPanel.vue_vue_type_script_setup_true_lang.js +2 -2
- package/dist/es/layouts/sidebar/Sidebar.vue_vue_type_script_setup_true_lang.js +8 -4
- package/dist/es/layouts/sidebar/code-block/CodeBlockList.vue_vue_type_script_setup_true_lang.js +4 -4
- package/dist/es/layouts/sidebar/code-block/CodeBlockListPanel.vue_vue_type_script_setup_true_lang.js +2 -2
- package/dist/es/layouts/sidebar/code-block/useContentMenu.js +1 -1
- package/dist/es/layouts/sidebar/data-source/DataSourceAddButton.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/sidebar/data-source/DataSourceConfigPanel.vue_vue_type_script_setup_true_lang.js +19 -6
- package/dist/es/layouts/sidebar/data-source/DataSourceList.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/sidebar/data-source/DataSourceListPanel.vue_vue_type_script_setup_true_lang.js +25 -3
- package/dist/es/layouts/sidebar/data-source/useContentMenu.js +1 -1
- package/dist/es/layouts/sidebar/layer/LayerMenu.vue_vue_type_script_setup_true_lang.js +6 -6
- package/dist/es/layouts/sidebar/layer/LayerNodeTool.vue_vue_type_script_setup_true_lang.js +2 -2
- package/dist/es/layouts/sidebar/layer/LayerPanel.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/sidebar/layer/use-node-status.js +1 -1
- package/dist/es/layouts/workspace/Breadcrumb.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/workspace/Workspace.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/workspace/viewer/NodeListMenu.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/workspace/viewer/Stage.vue_vue_type_script_setup_true_lang.js +2 -2
- package/dist/es/layouts/workspace/viewer/StageOverlay.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/layouts/workspace/viewer/ViewerMenu.vue_vue_type_script_setup_true_lang.js +9 -9
- package/dist/es/services/BaseService.js +31 -68
- package/dist/es/services/codeBlock.js +286 -8
- package/dist/es/services/componentList.js +2 -2
- package/dist/es/services/dataSource.js +287 -8
- package/dist/es/services/dep.js +21 -9
- package/dist/es/services/editor.js +655 -194
- package/dist/es/services/events.js +2 -2
- package/dist/es/services/history.js +390 -47
- package/dist/es/services/keybinding.js +5 -5
- package/dist/es/services/props.js +8 -8
- package/dist/es/services/stageOverlay.js +2 -2
- package/dist/es/services/storage.js +2 -2
- package/dist/es/services/ui.js +5 -2
- package/dist/es/style.css +456 -8
- package/dist/es/utils/code-block.js +122 -0
- package/dist/es/utils/content-menu.js +19 -12
- package/dist/es/utils/data-source/index.js +2 -0
- package/dist/es/utils/dep/worker.js +1 -1
- package/dist/es/utils/editor.js +105 -51
- package/dist/es/utils/history.js +220 -0
- package/dist/es/utils/indexed-db.js +86 -0
- package/dist/es/utils/undo-redo.js +80 -1
- package/dist/style.css +456 -8
- package/dist/tmagic-editor.umd.cjs +11149 -6646
- package/package.json +8 -8
- package/src/Editor.vue +25 -1
- package/src/components/CodeBlockEditor.vue +20 -109
- package/src/components/CodeParams.vue +6 -0
- package/src/components/CompareForm.vue +277 -0
- package/src/components/ToolButton.vue +2 -2
- package/src/editorProps.ts +9 -0
- package/src/fields/Code.vue +19 -3
- package/src/fields/CodeLink.vue +2 -5
- package/src/fields/CodeSelect.vue +17 -1
- package/src/fields/CodeSelectCol.vue +37 -3
- package/src/fields/DataSourceFieldSelect/FieldSelect.vue +23 -3
- package/src/fields/DataSourceFieldSelect/Index.vue +2 -2
- package/src/fields/DataSourceFields.vue +48 -10
- package/src/fields/DataSourceInput.vue +3 -3
- package/src/fields/DataSourceMethodSelect.vue +12 -4
- package/src/fields/DataSourceMethods.vue +55 -24
- package/src/fields/DataSourceMocks.vue +13 -3
- package/src/fields/DataSourceSelect.vue +4 -1
- package/src/fields/EventSelect.vue +57 -8
- package/src/fields/KeyValue.vue +17 -4
- package/src/fields/StyleSetter/components/Border.vue +15 -6
- package/src/fields/UISelect.vue +5 -2
- package/src/hooks/use-code-block-edit.ts +9 -5
- package/src/hooks/use-data-source-edit.ts +2 -2
- package/src/hooks/use-stage.ts +26 -9
- package/src/index.ts +8 -0
- package/src/initService.ts +1 -1
- package/src/layouts/CodeEditor.vue +17 -5
- package/src/layouts/Framework.vue +11 -6
- package/src/layouts/NavMenu.vue +12 -3
- package/src/layouts/history-list/Bucket.vue +91 -0
- package/src/layouts/history-list/BucketTab.vue +72 -0
- package/src/layouts/history-list/GroupRow.vue +304 -0
- package/src/layouts/history-list/HistoryDiffDialog.vue +281 -0
- package/src/layouts/history-list/HistoryListPanel.vue +367 -0
- package/src/layouts/history-list/InitialRow.vue +71 -0
- package/src/layouts/history-list/PageTab.vue +118 -0
- package/src/layouts/history-list/composables.ts +309 -0
- package/src/layouts/history-list/useHistoryList.ts +60 -0
- package/src/layouts/history-list/useHistoryRevert.ts +421 -0
- package/src/layouts/page-bar/PageBar.vue +4 -2
- package/src/layouts/props-panel/PropsPanel.vue +5 -1
- package/src/layouts/sidebar/ComponentListPanel.vue +9 -5
- package/src/layouts/sidebar/Sidebar.vue +8 -3
- package/src/layouts/sidebar/code-block/CodeBlockList.vue +5 -5
- package/src/layouts/sidebar/code-block/CodeBlockListPanel.vue +1 -1
- package/src/layouts/sidebar/code-block/useContentMenu.ts +1 -1
- package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +31 -2
- package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +27 -2
- package/src/layouts/sidebar/data-source/useContentMenu.ts +1 -1
- package/src/layouts/sidebar/layer/LayerMenu.vue +19 -11
- package/src/layouts/sidebar/layer/LayerNodeTool.vue +7 -4
- package/src/layouts/sidebar/layer/use-node-status.ts +9 -6
- package/src/layouts/workspace/viewer/Stage.vue +1 -1
- package/src/layouts/workspace/viewer/ViewerMenu.vue +8 -8
- package/src/services/BaseService.ts +35 -86
- package/src/services/codeBlock.ts +361 -5
- package/src/services/dataSource.ts +336 -6
- package/src/services/dep.ts +32 -9
- package/src/services/editor.ts +834 -194
- package/src/services/history.ts +509 -49
- package/src/services/keybinding.ts +3 -3
- package/src/services/props.ts +2 -2
- package/src/services/ui.ts +7 -0
- package/src/theme/history-list-panel.scss +557 -0
- package/src/theme/page-bar.scss +0 -4
- package/src/theme/props-panel.scss +3 -3
- package/src/theme/style-setter/border.scss +4 -1
- package/src/theme/theme.scss +1 -0
- package/src/type.ts +613 -31
- package/src/utils/code-block.ts +150 -0
- package/src/utils/content-menu.ts +25 -13
- package/src/utils/data-source/index.ts +2 -0
- package/src/utils/dep/worker.ts +5 -3
- package/src/utils/editor.ts +166 -60
- package/src/utils/history.ts +286 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/indexed-db.ts +122 -0
- package/src/utils/undo-redo.ts +111 -0
- package/types/index.d.ts +3197 -896
- package/dist/es/utils/compose.js +0 -44
- package/dist/es/utils/editor-history.js +0 -100
- package/src/utils/compose.ts +0 -52
- package/src/utils/editor-history.ts +0 -138
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
//#region packages/editor/src/utils/dep/worker.ts?worker&inline
|
|
2
|
-
var jsContent = "(function() {\n //#region packages/schema/src/index.ts\n const NODE_CONDS_KEY = \"displayConds\";\n const NODE_DISABLE_DATA_SOURCE_KEY = \"_tmagic_node_disabled_data_source\";\n const NODE_DISABLE_CODE_BLOCK_KEY = \"_tmagic_node_disabled_code_block\";\n let HookType = /* @__PURE__ */ function(HookType) {\n /** 代码块钩子标识 */\n HookType[\"CODE\"] = \"code\";\n return HookType;\n }({});\n //#endregion\n //#region packages/utils/src/const.ts\n const DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX = \"ds-field::\";\n //#endregion\n //#region packages/utils/src/index.ts\n const isObject = (obj) => Object.prototype.toString.call(obj) === \"[object Object]\";\n const getKeysArray = (keys) => `${keys}`.replace(/\\[(\\d+)\\]/g, \".$1\").split(\".\");\n const dataSourceTemplateRegExp = /\\$\\{([\\s\\S]+?)\\}/g;\n //#endregion\n //#region packages/dep/src/types.ts\n /** 依赖收集的目标类型 */\n let 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/src/Target.ts\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 constructor(options) {\n this.isTarget = options.isTarget;\n this.id = options.id;\n this.name = options.name;\n this.isCollectByDefault = options.isCollectByDefault ?? true;\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/src/utils.ts\n const INTEGER_REGEXP = /^\\d+$/;\n const createCodeBlockTarget = (id, codeBlock, initialDeps = {}) => new Target({\n type: DepTargetType.CODE_BLOCK,\n id,\n initialDeps,\n name: codeBlock.name,\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 const 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 const 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 const 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 const 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 const 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 const 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 const createDataSourceTarget = (ds, initialDeps = {}) => new Target({\n type: DepTargetType.DATA_SOURCE,\n id: ds.id,\n initialDeps,\n isTarget: (key, value) => isDataSourceTarget(ds, key, value)\n });\n const createDataSourceCondTarget = (ds, initialDeps = {}) => new Target({\n type: DepTargetType.DATA_SOURCE_COND,\n id: ds.id,\n initialDeps,\n isTarget: (key, value) => isDataSourceCondTarget(ds, key, value)\n });\n const createDataSourceMethodTarget = (ds, initialDeps = {}) => new Target({\n type: DepTargetType.DATA_SOURCE_METHOD,\n id: ds.id,\n initialDeps,\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 const 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/src/Watcher.ts\n const DATA_SOURCE_TARGET_TYPES = 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 this.collectByCallback(nodes, type, ({ node, target }) => {\n this.removeTargetDep(target, node);\n this.collectItem(node, target, depExtendedData, deep);\n });\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 collectItem(node, target, depExtendedData = {}, deep = false) {\n if (node[\"_tmagic_node_disabled_data_source\"] && DATA_SOURCE_TARGET_TYPES.has(target.type)) return;\n if (node[\"_tmagic_node_disabled_code_block\"] && target.type === DepTargetType.CODE_BLOCK) return;\n const collectTarget = (config, prop = \"\") => {\n const doCollect = (key, value) => {\n const keyIsItems = key === this.childrenProp;\n const fullKey = prop ? `${prop}.${key}` : key;\n if (target.isTarget(fullKey, value)) target.updateDep({\n id: node[this.idProp],\n name: `${node[this.nameProp] || node[this.idProp]}`,\n data: depExtendedData,\n key: fullKey\n });\n else if (!keyIsItems && Array.isArray(value)) for (let i = 0, l = value.length; i < l; i++) {\n const item = value[i];\n if (isObject(item)) collectTarget(item, `${fullKey}[${i}]`);\n }\n else if (isObject(value)) collectTarget(value, fullKey);\n if (keyIsItems && deep && Array.isArray(value)) for (const child of value) this.collectItem(child, target, depExtendedData, deep);\n };\n for (const [key, value] of Object.entries(config)) {\n if (typeof value === \"undefined\" || value === \"\") continue;\n doCollect(key, value);\n }\n };\n collectTarget(node);\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 //#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 onmessage = (e) => {\n const watcher = new Watcher({ initialTargets: {} });\n const { dsl } = e.data;\n try {\n const mApp = eval(`(${dsl})`);\n if (!mApp) postMessage({});\n watcher.clearTargets();\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 watcher.collectByCallback(mApp.items, void 0, ({ node, target }) => {\n watcher.collectItem(node, target, { pageId: node.id }, true);\n });\n const data = {\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 data[target.type][target.id] = target.deps;\n });\n postMessage(data);\n } catch (e) {\n error(e);\n postMessage({});\n }\n };\n //#endregion\n})();\n";
|
|
2
|
+
var jsContent = "(function() {\n //#region packages/schema/src/index.ts\n const NODE_CONDS_KEY = \"displayConds\";\n const NODE_DISABLE_DATA_SOURCE_KEY = \"_tmagic_node_disabled_data_source\";\n const NODE_DISABLE_CODE_BLOCK_KEY = \"_tmagic_node_disabled_code_block\";\n let HookType = /* @__PURE__ */ function(HookType) {\n /** 代码块钩子标识 */\n HookType[\"CODE\"] = \"code\";\n return HookType;\n }({});\n //#endregion\n //#region packages/utils/src/const.ts\n const DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX = \"ds-field::\";\n //#endregion\n //#region packages/utils/src/index.ts\n const isObject = (obj) => Object.prototype.toString.call(obj) === \"[object Object]\";\n const getKeysArray = (keys) => `${keys}`.replace(/\\[(\\d+)\\]/g, \".$1\").split(\".\");\n const dataSourceTemplateRegExp = /\\$\\{([\\s\\S]+?)\\}/g;\n //#endregion\n //#region packages/dep/src/types.ts\n /** 依赖收集的目标类型 */\n let 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/src/Target.ts\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 constructor(options) {\n this.isTarget = options.isTarget;\n this.id = options.id;\n this.name = options.name;\n this.isCollectByDefault = options.isCollectByDefault ?? true;\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/src/utils.ts\n const INTEGER_REGEXP = /^\\d+$/;\n const createCodeBlockTarget = (id, codeBlock, initialDeps = {}) => new Target({\n type: DepTargetType.CODE_BLOCK,\n id,\n initialDeps,\n name: codeBlock.name,\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 const 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 const 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 const 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 const 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 const 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 const 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 const createDataSourceTarget = (ds, initialDeps = {}) => new Target({\n type: DepTargetType.DATA_SOURCE,\n id: ds.id,\n initialDeps,\n isTarget: (key, value) => isDataSourceTarget(ds, key, value)\n });\n const createDataSourceCondTarget = (ds, initialDeps = {}) => new Target({\n type: DepTargetType.DATA_SOURCE_COND,\n id: ds.id,\n initialDeps,\n isTarget: (key, value) => isDataSourceCondTarget(ds, key, value)\n });\n const createDataSourceMethodTarget = (ds, initialDeps = {}) => new Target({\n type: DepTargetType.DATA_SOURCE_METHOD,\n id: ds.id,\n initialDeps,\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 const 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/src/Watcher.ts\n const 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 onmessage = (e) => {\n const watcher = new Watcher({ initialTargets: {} });\n const { dsl } = e.data;\n try {\n const mApp = eval(`(${dsl})`);\n if (!mApp) postMessage({});\n watcher.clearTargets();\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 data = {\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 data[target.type][target.id] = target.deps;\n });\n postMessage(data);\n } catch (e) {\n error(e);\n postMessage({});\n }\n };\n //#endregion\n})();\n";
|
|
3
3
|
var blob = typeof self !== "undefined" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", jsContent], { type: "text/javascript;charset=utf-8" });
|
|
4
4
|
function WorkerWrapper(options) {
|
|
5
5
|
let objURL;
|
package/dist/es/utils/editor.js
CHANGED
|
@@ -2,7 +2,7 @@ import { LayerOffset, Layout } from "../type.js";
|
|
|
2
2
|
import { isFixed } from "@tmagic/stage";
|
|
3
3
|
import { calcValueByFontsize, getElById, getNodePath, isNumber, isPage, isPageFragment, isPop, isValueIncludeDataSource } from "@tmagic/utils";
|
|
4
4
|
import { NODE_CONDS_KEY, Target, Watcher } from "@tmagic/core";
|
|
5
|
-
import { cloneDeep, get, isObject } from "lodash-es";
|
|
5
|
+
import { cloneDeep as cloneDeep$1, get, isObject } from "lodash-es";
|
|
6
6
|
import serialize from "serialize-javascript";
|
|
7
7
|
import { detailedDiff } from "deep-object-diff";
|
|
8
8
|
//#region packages/editor/src/utils/editor.ts
|
|
@@ -74,12 +74,26 @@ var getMiddleTop = (node, parentNode, stage) => {
|
|
|
74
74
|
}
|
|
75
75
|
return (Math.min(parentHeight, wrapperHeightDeal) - height) / 2;
|
|
76
76
|
};
|
|
77
|
+
var removeConflictPosition = (style, primary, secondary) => {
|
|
78
|
+
const isInvalid = (value) => value === "" || value === 0 || value === void 0 || value === null;
|
|
79
|
+
if (!(primary in style) || !(secondary in style)) return;
|
|
80
|
+
const primaryValue = style[primary];
|
|
81
|
+
const secondaryValue = style[secondary];
|
|
82
|
+
const primaryInvalid = isInvalid(primaryValue);
|
|
83
|
+
const secondaryInvalid = isInvalid(secondaryValue);
|
|
84
|
+
if (primaryInvalid && !secondaryInvalid) delete style[primary];
|
|
85
|
+
else if (secondaryInvalid && !primaryInvalid) delete style[secondary];
|
|
86
|
+
else if (primaryInvalid && secondaryInvalid) if (secondaryValue === 0 && primaryValue !== 0) delete style[primary];
|
|
87
|
+
else delete style[secondary];
|
|
88
|
+
};
|
|
77
89
|
var getInitPositionStyle = (style = {}, layout) => {
|
|
78
90
|
if (layout === Layout.ABSOLUTE) {
|
|
79
91
|
const newStyle = {
|
|
80
92
|
...style,
|
|
81
93
|
position: "absolute"
|
|
82
94
|
};
|
|
95
|
+
removeConflictPosition(newStyle, "left", "right");
|
|
96
|
+
removeConflictPosition(newStyle, "top", "bottom");
|
|
83
97
|
if (typeof newStyle.left === "undefined" && typeof newStyle.right === "undefined") newStyle.left = 0;
|
|
84
98
|
return newStyle;
|
|
85
99
|
}
|
|
@@ -104,58 +118,63 @@ var setLayout = (node, layout) => {
|
|
|
104
118
|
}
|
|
105
119
|
return node;
|
|
106
120
|
};
|
|
107
|
-
var
|
|
121
|
+
var hasPositionValue = (style, key) => typeof style?.[key] !== "undefined" && style?.[key] !== "";
|
|
122
|
+
/**
|
|
123
|
+
* 沿节点路径累加(或抵消)某一方向上的定位偏移量
|
|
124
|
+
* 当路径上的祖先使用了反方向定位(例如计算 left 时祖先用了 right)或非数值定位时,
|
|
125
|
+
* 无法简单地通过求和换算坐标,此时返回 0 表示放弃补偿、保持原值
|
|
126
|
+
* @param path 从根到目标节点的路径(含节点自身)
|
|
127
|
+
* @param dir 需要累加的定位方向
|
|
128
|
+
* @param opposite 反方向定位属性
|
|
129
|
+
* @param sign change2Fixed 取 1(累加祖先偏移),Fixed2Other 取 -1(抵消祖先偏移)
|
|
130
|
+
* @param init 偏移量初始值
|
|
131
|
+
*/
|
|
132
|
+
var accumulatePositionOffset = (path, dir, opposite, sign, init = 0) => {
|
|
133
|
+
let offset = init;
|
|
134
|
+
for (const value of path) {
|
|
135
|
+
if (hasPositionValue(value.style, opposite) || !isNumber(value.style?.[dir] || 0)) return 0;
|
|
136
|
+
offset = offset + sign * Number(value.style?.[dir] || 0);
|
|
137
|
+
}
|
|
138
|
+
return offset;
|
|
139
|
+
};
|
|
140
|
+
var change2Fixed = (node, path) => {
|
|
108
141
|
const style = { ...node.style || {} };
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
left
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (value.style?.right || !isNumber(value.style?.left || 0)) {
|
|
116
|
-
offset.left = 0;
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
offset.left = offset.left + Number(value.style?.left || 0);
|
|
142
|
+
if (!hasPositionValue(node.style, "right") && isNumber(node.style?.left || 0)) {
|
|
143
|
+
const left = accumulatePositionOffset(path, "left", "right", 1);
|
|
144
|
+
if (left) style.left = left;
|
|
145
|
+
} else if (hasPositionValue(node.style, "right") && isNumber(node.style?.right || 0)) {
|
|
146
|
+
const right = accumulatePositionOffset(path, "right", "left", 1);
|
|
147
|
+
if (right) style.right = right;
|
|
120
148
|
}
|
|
121
|
-
if (!node.style
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
149
|
+
if (!hasPositionValue(node.style, "bottom") && isNumber(node.style?.top || 0)) {
|
|
150
|
+
const top = accumulatePositionOffset(path, "top", "bottom", 1);
|
|
151
|
+
if (top) style.top = top;
|
|
152
|
+
} else if (hasPositionValue(node.style, "bottom") && isNumber(node.style?.bottom || 0)) {
|
|
153
|
+
const bottom = accumulatePositionOffset(path, "bottom", "top", 1);
|
|
154
|
+
if (bottom) style.bottom = bottom;
|
|
127
155
|
}
|
|
128
|
-
if (offset.left) style.left = offset.left;
|
|
129
|
-
if (offset.top) style.top = offset.top;
|
|
130
156
|
return style;
|
|
131
157
|
};
|
|
132
|
-
var Fixed2Other = async (node,
|
|
133
|
-
const path = getNodePath(node.id, root.items);
|
|
158
|
+
var Fixed2Other = async (node, path, getLayout) => {
|
|
134
159
|
const cur = path.pop();
|
|
135
160
|
const offset = {
|
|
136
|
-
left:
|
|
137
|
-
top:
|
|
161
|
+
left: 0,
|
|
162
|
+
top: 0,
|
|
163
|
+
right: 0,
|
|
164
|
+
bottom: 0
|
|
138
165
|
};
|
|
139
|
-
if (!node.style
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
offset.left = offset.left - Number(value.style?.left || 0);
|
|
145
|
-
}
|
|
146
|
-
if (!node.style?.bottom && isNumber(node.style?.top || 0)) for (const value of path) {
|
|
147
|
-
if (value.style?.bottom || !isNumber(value.style?.top || 0)) {
|
|
148
|
-
offset.top = 0;
|
|
149
|
-
break;
|
|
150
|
-
}
|
|
151
|
-
offset.top = offset.top - Number(value.style?.top || 0);
|
|
152
|
-
}
|
|
166
|
+
if (!hasPositionValue(node.style, "right") && isNumber(node.style?.left || 0)) offset.left = accumulatePositionOffset(path, "left", "right", -1, Number(cur?.style?.left || 0));
|
|
167
|
+
else if (hasPositionValue(node.style, "right") && isNumber(node.style?.right || 0)) offset.right = accumulatePositionOffset(path, "right", "left", -1, Number(cur?.style?.right || 0));
|
|
168
|
+
if (!hasPositionValue(node.style, "bottom") && isNumber(node.style?.top || 0)) offset.top = accumulatePositionOffset(path, "top", "bottom", -1, Number(cur?.style?.top || 0));
|
|
169
|
+
else if (hasPositionValue(node.style, "bottom") && isNumber(node.style?.bottom || 0)) offset.bottom = accumulatePositionOffset(path, "bottom", "top", -1, Number(cur?.style?.bottom || 0));
|
|
153
170
|
const style = node.style || {};
|
|
154
171
|
const parent = path.pop();
|
|
155
172
|
if (!parent) return getRelativeStyle(style);
|
|
156
173
|
if (await getLayout(parent) !== Layout.RELATIVE) {
|
|
157
174
|
if (offset.left) style.left = offset.left;
|
|
158
175
|
if (offset.top) style.top = offset.top;
|
|
176
|
+
if (offset.right) style.right = offset.right;
|
|
177
|
+
if (offset.bottom) style.bottom = offset.bottom;
|
|
159
178
|
return {
|
|
160
179
|
...style,
|
|
161
180
|
position: "absolute"
|
|
@@ -187,11 +206,11 @@ var fixNodeLeft = (config, parent, doc) => {
|
|
|
187
206
|
};
|
|
188
207
|
var fixNodePosition = (config, parent, stage) => {
|
|
189
208
|
if (config.style?.position !== "absolute") return config.style;
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
209
|
+
const style = { ...config.style || {} };
|
|
210
|
+
const baseStyle = config.style || {};
|
|
211
|
+
if (!("right" in baseStyle)) style.left = fixNodeLeft(config, parent, stage?.renderer?.contentWindow?.document);
|
|
212
|
+
if (!("top" in baseStyle) && !("bottom" in baseStyle)) style.top = getMiddleTop(config, parent, stage);
|
|
213
|
+
return style;
|
|
195
214
|
};
|
|
196
215
|
var serializeConfig = (config) => serialize(config, {
|
|
197
216
|
space: 2,
|
|
@@ -268,13 +287,14 @@ var buildChangeRecords = (value, basePath) => {
|
|
|
268
287
|
var resolveSelectedNode = (config, getNodeInfoFn, rootId) => {
|
|
269
288
|
const id = typeof config === "string" || typeof config === "number" ? config : config.id;
|
|
270
289
|
if (!id) throw new Error("没有ID,无法选中");
|
|
271
|
-
const { node, parent, page } = getNodeInfoFn(id);
|
|
290
|
+
const { node, parent, page, path } = getNodeInfoFn(id);
|
|
272
291
|
if (!node) throw new Error("获取不到组件信息");
|
|
273
292
|
if (node.id === rootId) throw new Error("不能选根节点");
|
|
274
293
|
return {
|
|
275
294
|
node,
|
|
276
295
|
parent,
|
|
277
|
-
page
|
|
296
|
+
page,
|
|
297
|
+
path
|
|
278
298
|
};
|
|
279
299
|
};
|
|
280
300
|
/**
|
|
@@ -286,11 +306,11 @@ var resolveSelectedNode = (config, getNodeInfoFn, rootId) => {
|
|
|
286
306
|
* @param getLayoutFn 获取父节点布局方式的回调函数
|
|
287
307
|
* @returns 处理后的节点配置(深拷贝)
|
|
288
308
|
*/
|
|
289
|
-
var toggleFixedPosition = async (dist, src,
|
|
290
|
-
const newConfig = cloneDeep(dist);
|
|
309
|
+
var toggleFixedPosition = async (dist, src, path, getLayoutFn) => {
|
|
310
|
+
const newConfig = cloneDeep$1(dist);
|
|
291
311
|
if (!isPop(src) && newConfig.style?.position) {
|
|
292
|
-
if (isFixed(newConfig.style) && !isFixed(src.style || {})) newConfig.style = change2Fixed(newConfig,
|
|
293
|
-
else if (!isFixed(newConfig.style) && isFixed(src.style || {})) newConfig.style = await Fixed2Other(newConfig,
|
|
312
|
+
if (isFixed(newConfig.style) && !isFixed(src.style || {})) newConfig.style = change2Fixed(newConfig, path);
|
|
313
|
+
else if (!isFixed(newConfig.style) && isFixed(src.style || {})) newConfig.style = await Fixed2Other(newConfig, path, getLayoutFn);
|
|
294
314
|
}
|
|
295
315
|
return newConfig;
|
|
296
316
|
};
|
|
@@ -431,5 +451,39 @@ var classifyDragSources = (configs, targetParent, getNodeInfo) => {
|
|
|
431
451
|
aborted: false
|
|
432
452
|
};
|
|
433
453
|
};
|
|
454
|
+
/**
|
|
455
|
+
* 给「回滚」生成的新 step 用的简短描述生成器。
|
|
456
|
+
* 与 UI 层 `describePageStep` 同义,但避免 service 反向依赖 layouts/,故放在此工具函数中。
|
|
457
|
+
*/
|
|
458
|
+
var describeStepForRevert = (step) => {
|
|
459
|
+
const items = step.diff ?? [];
|
|
460
|
+
const withId = (node, label) => {
|
|
461
|
+
const id = node?.id;
|
|
462
|
+
if (id === void 0 || id === null || `${id}` === "") return label;
|
|
463
|
+
return label ? `${label}(id: ${id})` : `id: ${id}`;
|
|
464
|
+
};
|
|
465
|
+
switch (step.opType) {
|
|
466
|
+
case "add": {
|
|
467
|
+
const count = items.length;
|
|
468
|
+
const node = items[0]?.newSchema;
|
|
469
|
+
const label = node?.name || node?.type || "";
|
|
470
|
+
return `撤回新增 ${count} 个节点${count === 1 ? `(${withId(node, label)})` : ""}`;
|
|
471
|
+
}
|
|
472
|
+
case "remove": {
|
|
473
|
+
const count = items.length;
|
|
474
|
+
const node = items[0]?.oldSchema;
|
|
475
|
+
const label = node?.name || node?.type || "";
|
|
476
|
+
return `还原已删除的 ${count} 个节点${count === 1 ? `(${withId(node, label)})` : ""}`;
|
|
477
|
+
}
|
|
478
|
+
default:
|
|
479
|
+
if (items.length === 1) {
|
|
480
|
+
const { newSchema, oldSchema, changeRecords } = items[0];
|
|
481
|
+
const target = withId(newSchema || oldSchema, newSchema?.name || newSchema?.type || oldSchema?.name || oldSchema?.type || "");
|
|
482
|
+
const propPath = changeRecords?.[0]?.propPath;
|
|
483
|
+
return propPath ? `还原 ${target} · ${propPath}` : `还原 ${target}`;
|
|
484
|
+
}
|
|
485
|
+
return `还原 ${items.length} 个节点的修改`;
|
|
486
|
+
}
|
|
487
|
+
};
|
|
434
488
|
//#endregion
|
|
435
|
-
export { COPY_CODE_STORAGE_KEY, COPY_DS_STORAGE_KEY, COPY_STORAGE_KEY, Fixed2Other, buildChangeRecords, calcAlignCenterStyle, calcLayerTargetIndex, calcMoveStyle, change2Fixed, classifyDragSources, collectRelatedNodes, editorNodeMergeCustomizer, fixNodeLeft, fixNodePosition, generatePageName, generatePageNameByApp, getGuideLineFromCache, getInitPositionStyle, getNodeIndex, getPageFragmentList, getPageList, getPageNameList, getRelativeStyle, isIncludeDataSource, moveItemsInContainer, resolveSelectedNode, serializeConfig, setChildrenLayout, setLayout, toggleFixedPosition };
|
|
489
|
+
export { COPY_CODE_STORAGE_KEY, COPY_DS_STORAGE_KEY, COPY_STORAGE_KEY, Fixed2Other, buildChangeRecords, calcAlignCenterStyle, calcLayerTargetIndex, calcMoveStyle, change2Fixed, classifyDragSources, collectRelatedNodes, describeStepForRevert, editorNodeMergeCustomizer, fixNodeLeft, fixNodePosition, generatePageName, generatePageNameByApp, getGuideLineFromCache, getInitPositionStyle, getNodeIndex, getPageFragmentList, getPageList, getPageNameList, getRelativeStyle, isIncludeDataSource, moveItemsInContainer, resolveSelectedNode, serializeConfig, setChildrenLayout, setLayout, toggleFixedPosition };
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { UndoRedo } from "./undo-redo.js";
|
|
2
|
+
import { guid } from "@tmagic/utils";
|
|
3
|
+
import { cloneDeep } from "lodash-es";
|
|
4
|
+
import serialize from "serialize-javascript";
|
|
5
|
+
//#region packages/editor/src/utils/history.ts
|
|
6
|
+
/**
|
|
7
|
+
* 「回滚」生成的新 step 简短描述。代码块 / 数据源共用。
|
|
8
|
+
* 二者逻辑一致,仅展示名取值字段不同(代码块取 `name`,数据源取 `title`),
|
|
9
|
+
* 因此通过 `getLabel` 注入取值方式。
|
|
10
|
+
*
|
|
11
|
+
* @param id 关联的代码块 / 数据源 id
|
|
12
|
+
* @param diff 单条变更 diff(缺省视为空)
|
|
13
|
+
* @param getLabel 从快照取展示名
|
|
14
|
+
*/
|
|
15
|
+
var describeRevertStep = (id, { oldSchema, newSchema, changeRecords } = {}, getLabel) => {
|
|
16
|
+
const labelOf = (schema) => getLabel(schema) || schema.id;
|
|
17
|
+
if (!oldSchema && newSchema) return `撤回新增 ${labelOf(newSchema) || id}`;
|
|
18
|
+
if (oldSchema && !newSchema) return `还原已删除的 ${labelOf(oldSchema) || id}`;
|
|
19
|
+
const label = newSchema && getLabel(newSchema) || oldSchema && getLabel(oldSchema) || `${id}`;
|
|
20
|
+
const propPath = changeRecords?.[0]?.propPath;
|
|
21
|
+
return propPath ? `还原 ${label} · ${propPath}` : `还原 ${label}`;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* 根据 old/new 是否为 null 推断 opType(与 push 时的约定一致)。
|
|
25
|
+
*/
|
|
26
|
+
var detectStackOpType = (oldVal, newVal) => {
|
|
27
|
+
if (oldVal === null && newVal !== null) return "add";
|
|
28
|
+
if (oldVal !== null && newVal === null) return "remove";
|
|
29
|
+
return "update";
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* 构造一条代码块 / 数据源「按 id 分栈」的历史记录:两者除 payload 字段命名外完全一致。
|
|
33
|
+
*
|
|
34
|
+
* - `add`:oldValue = null;`remove`:newValue = null;`update`:两者都有,可带 changeRecords 做局部更新。
|
|
35
|
+
* - 内容会做 cloneDeep 防止后续被外部引用篡改;opType 依据 old/new 是否为 null 推断。
|
|
36
|
+
* - 仅负责构造 step 并返回,入栈与事件 emit 由统一的 history.push(stepType, step, id) 处理。
|
|
37
|
+
* - 不直接驱动业务 service,调用方负责实际写回。
|
|
38
|
+
*/
|
|
39
|
+
var createStackStep = (id, payload) => {
|
|
40
|
+
if (!id) return null;
|
|
41
|
+
const oldSchema = payload.oldValue ? cloneDeep(payload.oldValue) : null;
|
|
42
|
+
const newSchema = payload.newValue ? cloneDeep(payload.newValue) : null;
|
|
43
|
+
const changeRecords = payload.changeRecords?.length ? cloneDeep(payload.changeRecords) : void 0;
|
|
44
|
+
const opType = detectStackOpType(payload.oldValue, payload.newValue);
|
|
45
|
+
const schema = payload.newValue ?? payload.oldValue;
|
|
46
|
+
const name = payload.name ?? schema?.name ?? schema?.title ?? "";
|
|
47
|
+
return {
|
|
48
|
+
uuid: guid(),
|
|
49
|
+
data: {
|
|
50
|
+
name,
|
|
51
|
+
id
|
|
52
|
+
},
|
|
53
|
+
opType,
|
|
54
|
+
diff: [{
|
|
55
|
+
...newSchema !== null ? { newSchema } : {},
|
|
56
|
+
...oldSchema !== null ? { oldSchema } : {},
|
|
57
|
+
...opType === "update" && changeRecords ? { changeRecords } : {}
|
|
58
|
+
}],
|
|
59
|
+
historyDescription: payload.historyDescription,
|
|
60
|
+
source: payload.source,
|
|
61
|
+
operator: payload.operator,
|
|
62
|
+
rootStep: payload.rootStep,
|
|
63
|
+
timestamp: Date.now(),
|
|
64
|
+
extra: payload.extra
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
var markStackSaved = (undoRedo) => {
|
|
68
|
+
if (!undoRedo) return;
|
|
69
|
+
undoRedo.updateElements((element) => {
|
|
70
|
+
element.saved = false;
|
|
71
|
+
});
|
|
72
|
+
undoRedo.updateCurrentElement((element) => {
|
|
73
|
+
element.saved = true;
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* 把单个历史栈(页面 / 代码块 / 数据源 / 扩展类型)的步骤列表按"目标"做相邻合并:
|
|
78
|
+
* - 单实体的 'update' 按 targetId 与相邻同 targetId 的 update 合并到一个 group,组内可展开查看每步;
|
|
79
|
+
* - 'add' / 'remove' 始终独立成组(语义上是结构变更,不应被收纳进单实体修改组);
|
|
80
|
+
* - 多实体 'update'(如页面批量改属性)也独立成组(无明确单一目标,避免误合并)。
|
|
81
|
+
*
|
|
82
|
+
* 各类型行为完全一致,仅 `kind` 与 step 快照类型不同,统一由本方法处理。
|
|
83
|
+
*/
|
|
84
|
+
var mergeSteps = (kind, id, list, cursor) => {
|
|
85
|
+
const groups = [];
|
|
86
|
+
let current = null;
|
|
87
|
+
const currentIndex = cursor - 1;
|
|
88
|
+
list.forEach((step, index) => {
|
|
89
|
+
const applied = index < cursor;
|
|
90
|
+
const isCurrent = index === currentIndex;
|
|
91
|
+
const targetId = detectTargetId(step);
|
|
92
|
+
const targetName = detectTargetName(step);
|
|
93
|
+
const entry = {
|
|
94
|
+
step,
|
|
95
|
+
index,
|
|
96
|
+
applied,
|
|
97
|
+
isCurrent
|
|
98
|
+
};
|
|
99
|
+
const mergeable = step.opType === "update" && targetId !== void 0;
|
|
100
|
+
if (mergeable && current?.opType === "update" && current.targetId === targetId) {
|
|
101
|
+
current.steps.push(entry);
|
|
102
|
+
current.applied = applied;
|
|
103
|
+
if (isCurrent) current.isCurrent = true;
|
|
104
|
+
if (targetName) current.targetName = targetName;
|
|
105
|
+
} else {
|
|
106
|
+
current = {
|
|
107
|
+
kind,
|
|
108
|
+
id,
|
|
109
|
+
opType: step.opType,
|
|
110
|
+
targetId: mergeable ? targetId : void 0,
|
|
111
|
+
targetName,
|
|
112
|
+
steps: [entry],
|
|
113
|
+
applied,
|
|
114
|
+
isCurrent
|
|
115
|
+
};
|
|
116
|
+
groups.push(current);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
return groups;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* 解析 step 中的"目标 id"用于合并:
|
|
123
|
+
* - 单实体 update:取唯一一项 diff 的快照 id;快照无 id 时(如 CodeBlockContent)回退到 `step.data.id`
|
|
124
|
+
* (即资源 id),使代码块 / 数据源同样能按资源合并相邻 update;
|
|
125
|
+
* - 其它情形(多实体 update / add / remove):返回 undefined,表示不参与合并。
|
|
126
|
+
*/
|
|
127
|
+
var detectTargetId = (step) => {
|
|
128
|
+
if (step.opType !== "update") return void 0;
|
|
129
|
+
const items = step.diff;
|
|
130
|
+
if (items?.length !== 1) return void 0;
|
|
131
|
+
const newSchema = items[0].newSchema;
|
|
132
|
+
const oldSchema = items[0].oldSchema;
|
|
133
|
+
return newSchema?.id ?? oldSchema?.id ?? step.data?.id;
|
|
134
|
+
};
|
|
135
|
+
/** 解析 step 中的目标可读名(用于 UI 展示)。 */
|
|
136
|
+
var detectTargetName = (step) => {
|
|
137
|
+
const items = step.diff;
|
|
138
|
+
if (step.opType === "update") {
|
|
139
|
+
if (items?.length === 1) {
|
|
140
|
+
const node = items[0].newSchema || items[0].oldSchema;
|
|
141
|
+
return node?.name || node?.type || (node?.id !== void 0 ? `${node.id}` : void 0);
|
|
142
|
+
}
|
|
143
|
+
return items?.length ? `${items.length} 个节点` : void 0;
|
|
144
|
+
}
|
|
145
|
+
if (step.opType === "add") {
|
|
146
|
+
if (items?.length === 1) {
|
|
147
|
+
const n = items[0].newSchema;
|
|
148
|
+
return n?.name || n?.type || `${n?.id}`;
|
|
149
|
+
}
|
|
150
|
+
return items?.length ? `${items.length} 个节点` : void 0;
|
|
151
|
+
}
|
|
152
|
+
if (step.opType === "remove") {
|
|
153
|
+
if (items?.length === 1) {
|
|
154
|
+
const n = items[0].oldSchema;
|
|
155
|
+
return n?.name || n?.type || `${n?.id}`;
|
|
156
|
+
}
|
|
157
|
+
return items?.length ? `${items.length} 个节点` : void 0;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* 把 `Record<Id, UndoRedo>` 整体序列化为 `Record<Id, SerializedUndoRedo>`。
|
|
162
|
+
*
|
|
163
|
+
* 序列化(深克隆)的同一趟里,只把每条 step 中可能含函数的 `diff` 用 serialize-javascript 序列化成字符串,
|
|
164
|
+
* 其余字段(uuid / opType / timestamp / `modifiedNodeIds` Map 等)原样保留,交给 IndexedDB 结构化克隆。
|
|
165
|
+
* 这样既能写入函数,又避免序列化整份快照的开销;读取时再由 {@link parseStacksStepDiff} 还原 diff。
|
|
166
|
+
* 不含 `diff` 的元素(如通用栈)原样透传。
|
|
167
|
+
*/
|
|
168
|
+
var serializeStacks = (stacks) => {
|
|
169
|
+
const result = {};
|
|
170
|
+
Object.entries(stacks).forEach(([id, undoRedo]) => {
|
|
171
|
+
if (!undoRedo) return;
|
|
172
|
+
const serialized = undoRedo.serialize();
|
|
173
|
+
result[id] = {
|
|
174
|
+
...serialized,
|
|
175
|
+
elementList: serialized.elementList.map((step) => step.diff === void 0 ? step : Object.assign({}, step, { diff: serialize(step.diff) }))
|
|
176
|
+
};
|
|
177
|
+
});
|
|
178
|
+
return result;
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* 把 `Record<Id, SerializedUndoRedo>` 整体还原为 `Record<Id, UndoRedo>`。
|
|
182
|
+
* 还原时把每个栈的游标定位到最近一条已保存(`saved === true`)记录之后。
|
|
183
|
+
*
|
|
184
|
+
* 与 {@link serializeStacks} 相反:当传入 `parse`(parseDSL)时,把每条 step 中以字符串形式存储的 `diff`
|
|
185
|
+
* 解析回真实对象(含函数);不含 `diff` 的元素(如通用栈)原样透传。
|
|
186
|
+
*/
|
|
187
|
+
var deserializeStacks = (stacks = {}, parse) => {
|
|
188
|
+
const result = {};
|
|
189
|
+
Object.entries(stacks).forEach(([id, serialized]) => {
|
|
190
|
+
if (!serialized) return;
|
|
191
|
+
const elementList = parse ? serialized.elementList.map((step) => {
|
|
192
|
+
const { diff } = step;
|
|
193
|
+
return typeof diff === "string" ? Object.assign({}, step, { diff: parse(`(${diff})`) }) : step;
|
|
194
|
+
}) : serialized.elementList;
|
|
195
|
+
result[id] = UndoRedo.fromSerialized({
|
|
196
|
+
...serialized,
|
|
197
|
+
elementList
|
|
198
|
+
}, { isSavedStep: (element) => element.saved === true });
|
|
199
|
+
});
|
|
200
|
+
return result;
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* 按 id 从「按 id 分栈」的记录表(代码块 / 数据源)中获取(或创建)对应的 UndoRedo 栈。
|
|
204
|
+
*/
|
|
205
|
+
var getOrCreateStack = (stacks, id) => {
|
|
206
|
+
if (!stacks[id]) stacks[id] = new UndoRedo();
|
|
207
|
+
return stacks[id];
|
|
208
|
+
};
|
|
209
|
+
/**
|
|
210
|
+
* 撤销下限:当栈 index 0 是 `opType: 'initial'` 的基线 step 时为 1(基线不可被撤销),否则为 0。
|
|
211
|
+
* 适用于所有历史类型(page / codeBlock / dataSource / 扩展),把 cursor 钉在基线之上,
|
|
212
|
+
* 保证 undo / canUndo / goto 都不会越过初始基线。
|
|
213
|
+
*/
|
|
214
|
+
var undoFloor = (undoRedo) => {
|
|
215
|
+
return undoRedo.getElementList()[0]?.opType === "initial" ? 1 : 0;
|
|
216
|
+
};
|
|
217
|
+
/** 将单次 push 产生的 history uuid(或 null)转为 *AndGetHistoryId 返回用的 uuid 列表。 */
|
|
218
|
+
var getLastPushedHistoryIds = (historyId) => historyId ? [historyId] : [];
|
|
219
|
+
//#endregion
|
|
220
|
+
export { createStackStep, describeRevertStep, deserializeStacks, detectStackOpType, detectTargetId, detectTargetName, getLastPushedHistoryIds, getOrCreateStack, markStackSaved, mergeSteps, serializeStacks, undoFloor };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
//#region packages/editor/src/utils/indexed-db.ts
|
|
2
|
+
/**
|
|
3
|
+
* 一组极简的、基于原生 IndexedDB 的 Promise KV 工具,避免引入额外依赖。
|
|
4
|
+
* 仅用于浏览器环境;在不支持 IndexedDB 的环境(如 SSR / 部分测试环境)下会 reject。
|
|
5
|
+
*/
|
|
6
|
+
/** 是否处于支持 IndexedDB 的环境。 */
|
|
7
|
+
var isIndexedDBSupported = () => typeof indexedDB !== "undefined" && indexedDB !== null;
|
|
8
|
+
/**
|
|
9
|
+
* 打开(必要时升级)数据库,确保目标 objectStore 存在后返回连接。
|
|
10
|
+
*
|
|
11
|
+
* 由于 objectStore 只能在 `onupgradeneeded` 内创建,这里先以当前版本打开,
|
|
12
|
+
* 若发现 store 不存在则关闭连接、以更高版本重开来按需创建,兼容动态 storeName。
|
|
13
|
+
*/
|
|
14
|
+
var openIndexedDB = (dbName, storeName) => new Promise((resolve, reject) => {
|
|
15
|
+
if (!isIndexedDBSupported()) {
|
|
16
|
+
reject(/* @__PURE__ */ new Error("当前环境不支持 IndexedDB"));
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const request = indexedDB.open(dbName);
|
|
20
|
+
request.onupgradeneeded = () => {
|
|
21
|
+
const db = request.result;
|
|
22
|
+
if (!db.objectStoreNames.contains(storeName)) db.createObjectStore(storeName);
|
|
23
|
+
};
|
|
24
|
+
request.onerror = () => reject(request.error);
|
|
25
|
+
request.onsuccess = () => {
|
|
26
|
+
const db = request.result;
|
|
27
|
+
if (db.objectStoreNames.contains(storeName)) {
|
|
28
|
+
resolve(db);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const nextVersion = db.version + 1;
|
|
32
|
+
db.close();
|
|
33
|
+
const upgradeRequest = indexedDB.open(dbName, nextVersion);
|
|
34
|
+
upgradeRequest.onupgradeneeded = () => {
|
|
35
|
+
const upgradeDb = upgradeRequest.result;
|
|
36
|
+
if (!upgradeDb.objectStoreNames.contains(storeName)) upgradeDb.createObjectStore(storeName);
|
|
37
|
+
};
|
|
38
|
+
upgradeRequest.onerror = () => reject(upgradeRequest.error);
|
|
39
|
+
upgradeRequest.onsuccess = () => resolve(upgradeRequest.result);
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
/** 写入(覆盖)一条记录。value 通过结构化克隆存储,支持 Map / Set 等结构。 */
|
|
43
|
+
var idbSet = async (dbName, storeName, key, value) => {
|
|
44
|
+
const db = await openIndexedDB(dbName, storeName);
|
|
45
|
+
try {
|
|
46
|
+
await new Promise((resolve, reject) => {
|
|
47
|
+
const tx = db.transaction(storeName, "readwrite");
|
|
48
|
+
tx.objectStore(storeName).put(value, key);
|
|
49
|
+
tx.oncomplete = () => resolve();
|
|
50
|
+
tx.onabort = () => reject(tx.error);
|
|
51
|
+
tx.onerror = () => reject(tx.error);
|
|
52
|
+
});
|
|
53
|
+
} finally {
|
|
54
|
+
db.close();
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
/** 读取一条记录,不存在时返回 undefined。 */
|
|
58
|
+
var idbGet = async (dbName, storeName, key) => {
|
|
59
|
+
const db = await openIndexedDB(dbName, storeName);
|
|
60
|
+
try {
|
|
61
|
+
return await new Promise((resolve, reject) => {
|
|
62
|
+
const request = db.transaction(storeName, "readonly").objectStore(storeName).get(key);
|
|
63
|
+
request.onsuccess = () => resolve(request.result);
|
|
64
|
+
request.onerror = () => reject(request.error);
|
|
65
|
+
});
|
|
66
|
+
} finally {
|
|
67
|
+
db.close();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
/** 删除一条记录。 */
|
|
71
|
+
var idbDelete = async (dbName, storeName, key) => {
|
|
72
|
+
const db = await openIndexedDB(dbName, storeName);
|
|
73
|
+
try {
|
|
74
|
+
await new Promise((resolve, reject) => {
|
|
75
|
+
const tx = db.transaction(storeName, "readwrite");
|
|
76
|
+
tx.objectStore(storeName).delete(key);
|
|
77
|
+
tx.oncomplete = () => resolve();
|
|
78
|
+
tx.onabort = () => reject(tx.error);
|
|
79
|
+
tx.onerror = () => reject(tx.error);
|
|
80
|
+
});
|
|
81
|
+
} finally {
|
|
82
|
+
db.close();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
//#endregion
|
|
86
|
+
export { idbDelete, idbGet, idbSet, isIndexedDBSupported, openIndexedDB };
|