@tmagic/editor 1.8.2 → 1.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es/Editor.vue_vue_type_script_setup_true_lang.js +16 -24
- package/dist/es/fields/CondOpSelect.vue_vue_type_script_setup_true_lang.js +2 -2
- package/dist/es/fields/DataSourceFieldSelect/FieldSelect.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/DataSourceFieldSelect/Index.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/DataSourceFields.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/DataSourceInput.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/es/fields/DataSourceMethodSelect.vue_vue_type_script_setup_true_name_true_lang.js +1 -1
- package/dist/es/fields/StyleSetter/configs.js +1 -1
- package/dist/es/fields/configs/codeSelect.js +5 -7
- package/dist/es/fields/configs/displayConds.js +8 -9
- package/dist/es/fields/configs/eventSelect.js +30 -27
- package/dist/es/hooks/use-stage.js +18 -18
- package/dist/es/index.js +9 -16
- package/dist/es/initService.js +33 -106
- package/dist/es/layouts/Framework.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 +2 -2
- package/dist/es/layouts/props-panel/use-style-panel.js +1 -1
- package/dist/es/sdk/index.js +151 -0
- package/dist/es/{services → sdk/services}/BaseService.js +1 -1
- package/dist/es/{services/codeBlock.js → sdk/services/CodeBlock.js} +37 -40
- package/dist/es/{services/componentList.js → sdk/services/ComponentList.js} +5 -6
- package/dist/es/{services/dataSource.js → sdk/services/DataSource.js} +38 -37
- package/dist/es/{services/dep.js → sdk/services/Dep.js} +24 -14
- package/dist/es/{services/editor.js → sdk/services/Editor.js} +124 -87
- package/dist/es/{services/events.js → sdk/services/Events.js} +13 -13
- package/dist/es/{services/history.js → sdk/services/History.js} +86 -33
- package/dist/es/{services/props.js → sdk/services/Props.js} +25 -26
- package/dist/es/{services/storage.js → sdk/services/WebStorage.js} +31 -8
- package/dist/es/sdk/utils/dep.js +100 -0
- package/dist/es/sdk/utils/operator.js +70 -0
- package/dist/es/sdk/utils/position.js +8 -0
- package/dist/es/sdk/utils/props.js +281 -0
- package/dist/es/sdk/validate/index.js +286 -0
- package/dist/es/sdk/wiring.js +163 -0
- package/dist/es/sdk.js +15 -0
- package/dist/es/services/keybinding.js +21 -21
- package/dist/es/services/sdk-instance.js +37 -0
- package/dist/es/services/stageOverlay.js +14 -14
- package/dist/es/services/ui.js +6 -6
- package/dist/es/utils/data-source/formConfigs/base.js +1 -1
- package/dist/es/utils/data-source/formConfigs/http.js +1 -1
- package/dist/es/utils/editor.js +2 -2
- package/dist/es/utils/event.js +25 -21
- package/dist/es/utils/operator.js +11 -57
- package/dist/es/utils/props.js +12 -282
- package/dist/es/utils/type-match-rules.js +10 -12
- package/dist/tmagic-editor-headless.umd.cjs +4690 -2891
- package/dist/tmagic-editor-sdk.umd.cjs +9911 -0
- package/dist/tmagic-editor.umd.cjs +14126 -13312
- package/package.json +12 -7
- package/src/Editor.vue +11 -9
- package/src/fields/configs/codeSelect.ts +6 -9
- package/src/fields/configs/displayConds.ts +10 -9
- package/src/fields/configs/eventSelect.ts +40 -25
- package/src/hooks/use-stage.ts +1 -1
- package/src/index.ts +11 -9
- package/src/initService.ts +59 -188
- package/src/layouts/Framework.vue +1 -1
- package/src/layouts/props-panel/PropsPanel.vue +1 -1
- package/src/layouts/props-panel/use-style-panel.ts +1 -1
- package/src/sdk/index.ts +264 -0
- package/src/{services/codeBlock.ts → sdk/services/CodeBlock.ts} +43 -38
- package/src/{services/componentList.ts → sdk/services/ComponentList.ts} +8 -7
- package/src/{services/dataSource.ts → sdk/services/DataSource.ts} +38 -32
- package/src/{services/dep.ts → sdk/services/Dep.ts} +24 -11
- package/src/{services/editor.ts → sdk/services/Editor.ts} +175 -86
- package/src/{services/events.ts → sdk/services/Events.ts} +15 -12
- package/src/{services/history.ts → sdk/services/History.ts} +100 -28
- package/src/{services/props.ts → sdk/services/Props.ts} +25 -25
- package/src/{services/storage.ts → sdk/services/WebStorage.ts} +41 -7
- package/src/sdk/type.ts +191 -0
- package/src/sdk/utils/dep.ts +196 -0
- package/src/sdk/utils/operator.ts +114 -0
- package/src/sdk/utils/position.ts +23 -0
- package/src/sdk/utils/props.ts +368 -0
- package/src/sdk/validate/index.ts +501 -0
- package/src/sdk/wiring.ts +211 -0
- package/src/sdk.ts +19 -0
- package/src/services/keybinding.ts +2 -2
- package/src/services/sdk-instance.ts +66 -0
- package/src/services/stageOverlay.ts +2 -2
- package/src/services/ui.ts +3 -3
- package/src/type.ts +9 -9
- package/src/utils/data-source/formConfigs/base.ts +1 -1
- package/src/utils/data-source/formConfigs/http.ts +1 -1
- package/src/utils/dep/collect-worker-client.ts +9 -6
- package/src/utils/dep/worker.ts +5 -0
- package/src/utils/editor.ts +2 -1
- package/src/utils/event.ts +36 -18
- package/src/utils/operator.ts +21 -87
- package/src/utils/props.ts +17 -332
- package/src/utils/type-match-rules.ts +1 -3
- package/types/index.d.ts +742 -528
- package/types/sdk.d.ts +2572 -0
- /package/src/{services → sdk/services}/BaseService.ts +0 -0
package/src/sdk/type.ts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Tencent is pleased to support the open source community by making TMagicEditor available.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2025 Tencent. All rights reserved.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @fileoverview DSL SDK 的公共类型:响应式注入、适配器、上下文、校验结果。
|
|
21
|
+
*
|
|
22
|
+
* 这里只放类型,禁止引入运行时依赖(vue / stage / design 等)。
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type { DataSourceSchema, EventOption, Id, MApp, MNode } from '@tmagic/core';
|
|
26
|
+
import type { FormConfig } from '@tmagic/form';
|
|
27
|
+
|
|
28
|
+
import type {
|
|
29
|
+
CanDropInFunction,
|
|
30
|
+
ComponentGroup,
|
|
31
|
+
DatasourceTypeOption,
|
|
32
|
+
PropsFormConfigFunction,
|
|
33
|
+
PropsFormValueFunction,
|
|
34
|
+
} from '@editor/type';
|
|
35
|
+
import type { CollectWorkerPayload, CollectWorkerResult, DepsData } from '@editor/utils/dep/worker';
|
|
36
|
+
|
|
37
|
+
import type CodeBlock from './services/CodeBlock';
|
|
38
|
+
import type ComponentList from './services/ComponentList';
|
|
39
|
+
import type DataSource from './services/DataSource';
|
|
40
|
+
import type Dep from './services/Dep';
|
|
41
|
+
import type Editor from './services/Editor';
|
|
42
|
+
import type Events from './services/Events';
|
|
43
|
+
import type History from './services/History';
|
|
44
|
+
import type Props from './services/Props';
|
|
45
|
+
import type WebStorage from './services/WebStorage';
|
|
46
|
+
import type { Validator } from './validate';
|
|
47
|
+
|
|
48
|
+
// #region Reactivity
|
|
49
|
+
/**
|
|
50
|
+
* 响应式实现,默认直接使用 Vue 的 `reactive` / `shallowReactive` / `toRaw` / `nextTick`;
|
|
51
|
+
* 可通过 `SdkOptions.reactivity` 覆盖。
|
|
52
|
+
*/
|
|
53
|
+
export interface Reactivity {
|
|
54
|
+
reactive: <T extends object>(target: T) => T;
|
|
55
|
+
shallowReactive: <T extends object>(target: T) => T;
|
|
56
|
+
toRaw: <T>(value: T) => T;
|
|
57
|
+
nextTick: () => Promise<void>;
|
|
58
|
+
}
|
|
59
|
+
// #endregion Reactivity
|
|
60
|
+
|
|
61
|
+
// #region StorageLike
|
|
62
|
+
/** 存储适配器接口,`localStorage` 天然满足 */
|
|
63
|
+
export interface StorageLike {
|
|
64
|
+
readonly length: number;
|
|
65
|
+
clear(): void;
|
|
66
|
+
getItem(key: string): string | null;
|
|
67
|
+
key(index: number): string | null;
|
|
68
|
+
removeItem(key: string): void;
|
|
69
|
+
setItem(key: string, value: string): void;
|
|
70
|
+
}
|
|
71
|
+
// #endregion StorageLike
|
|
72
|
+
|
|
73
|
+
// #region HistoryPersistence
|
|
74
|
+
/** 历史快照的持久化适配器(浏览器端对应 IndexedDB)。不提供则 `saveToIndexedDB` / `restoreFromIndexedDB` 会 reject */
|
|
75
|
+
export interface HistoryPersistence {
|
|
76
|
+
get<T = unknown>(dbName: string, storeName: string, key: IDBValidKey): Promise<T | undefined>;
|
|
77
|
+
set(dbName: string, storeName: string, key: IDBValidKey, value: unknown): Promise<void>;
|
|
78
|
+
}
|
|
79
|
+
// #endregion HistoryPersistence
|
|
80
|
+
|
|
81
|
+
// #region CollectWorkerLike
|
|
82
|
+
/**
|
|
83
|
+
* 依赖收集 worker 适配器。浏览器端注入基于 Web Worker 的实现,Node 下不提供,
|
|
84
|
+
* 依赖收集会退回主线程的空闲队列(`IdleTask`,Node 下由 setTimeout 驱动)。
|
|
85
|
+
*/
|
|
86
|
+
export interface CollectWorkerLike {
|
|
87
|
+
readonly isSupported: boolean;
|
|
88
|
+
collectDsl(dsl: MApp): Promise<DepsData | null>;
|
|
89
|
+
collect(payload: CollectWorkerPayload): Promise<CollectWorkerResult | null>;
|
|
90
|
+
terminate(): void;
|
|
91
|
+
abort(): void;
|
|
92
|
+
}
|
|
93
|
+
// #endregion CollectWorkerLike
|
|
94
|
+
|
|
95
|
+
// #region ValidateMode
|
|
96
|
+
/**
|
|
97
|
+
* 校验模式
|
|
98
|
+
* - `strict`:修改前校验,不通过抛出 {@link DslValidationError},DSL 不被修改
|
|
99
|
+
* - `record`:只把校验结果写进 `invalidNodeIds`
|
|
100
|
+
* - `off`:不做额外校验
|
|
101
|
+
*/
|
|
102
|
+
export type ValidateMode = 'strict' | 'record' | 'off';
|
|
103
|
+
// #endregion ValidateMode
|
|
104
|
+
|
|
105
|
+
// #region ValidationError
|
|
106
|
+
export interface DslValidationIssue {
|
|
107
|
+
/** 出错对象类型 */
|
|
108
|
+
target: 'node' | 'data-source' | 'code-block' | 'dsl';
|
|
109
|
+
/** 出错对象 id */
|
|
110
|
+
id?: Id;
|
|
111
|
+
/** 出错属性路径,结构性错误时为空 */
|
|
112
|
+
path?: string;
|
|
113
|
+
/** 错误分类:structure 为结构合法性,props 为属性表单校验 */
|
|
114
|
+
kind: 'structure' | 'props';
|
|
115
|
+
message: string;
|
|
116
|
+
/** 可选的修正建议(如可选值列表) */
|
|
117
|
+
suggestion?: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface DslValidationResult {
|
|
121
|
+
valid: boolean;
|
|
122
|
+
errors: DslValidationIssue[];
|
|
123
|
+
}
|
|
124
|
+
// #endregion ValidationError
|
|
125
|
+
|
|
126
|
+
// #region SdkOptions
|
|
127
|
+
export interface SdkOptions {
|
|
128
|
+
/** 初始 DSL,会被深拷贝,不会修改传入的对象 */
|
|
129
|
+
dsl?: MApp | null;
|
|
130
|
+
/** 此前通过 `history.serialize()` 导出的历史快照 */
|
|
131
|
+
history?: import('@editor/type').PersistedHistoryState | null;
|
|
132
|
+
|
|
133
|
+
reactivity?: Partial<Reactivity>;
|
|
134
|
+
/** 存储适配器,默认内存实现 */
|
|
135
|
+
storage?: StorageLike;
|
|
136
|
+
/** 把序列化后的字符串还原成值(含函数),默认 `JSON.parse`;浏览器端传入 `eval` 实现 */
|
|
137
|
+
parseDSL?: (code: string) => any;
|
|
138
|
+
historyPersistence?: HistoryPersistence;
|
|
139
|
+
collectWorker?: CollectWorkerLike;
|
|
140
|
+
/** 提示信息,默认不做任何事 */
|
|
141
|
+
notify?: (type: 'success' | 'error' | 'warning' | 'info', message: string) => void;
|
|
142
|
+
/** 复制文本到剪贴板,默认不做任何事 */
|
|
143
|
+
copyText?: (text: string) => Promise<void> | void;
|
|
144
|
+
|
|
145
|
+
/** 校验模式,默认 `strict` */
|
|
146
|
+
validateMode?: ValidateMode;
|
|
147
|
+
/** 是否自动绑定依赖收集连线,默认 true;浏览器默认实例传 false,由 initService 挂载时绑定 */
|
|
148
|
+
autoWiring?: boolean;
|
|
149
|
+
|
|
150
|
+
// 登记表,与 EditorProps 同名项一一对应
|
|
151
|
+
componentGroupList?: ComponentGroup[];
|
|
152
|
+
propsConfigs?: Record<string, FormConfig | PropsFormConfigFunction>;
|
|
153
|
+
propsValues?: Record<string, Partial<MNode> | PropsFormValueFunction>;
|
|
154
|
+
eventMethodList?: Record<string, { events: EventOption[]; methods: EventOption[] }>;
|
|
155
|
+
datasourceList?: DatasourceTypeOption[];
|
|
156
|
+
datasourceConfigs?: Record<string, FormConfig>;
|
|
157
|
+
datasourceValues?: Record<string, Partial<DataSourceSchema>>;
|
|
158
|
+
datasourceEventMethodList?: Record<string, { events: EventOption[]; methods: EventOption[] }>;
|
|
159
|
+
disabledCodeBlock?: boolean;
|
|
160
|
+
disabledDataSource?: boolean;
|
|
161
|
+
disabledPageFragment?: boolean;
|
|
162
|
+
disabledMultiSelect?: boolean;
|
|
163
|
+
alwaysMultiSelect?: boolean;
|
|
164
|
+
canDropIn?: CanDropInFunction;
|
|
165
|
+
}
|
|
166
|
+
// #endregion SdkOptions
|
|
167
|
+
|
|
168
|
+
// #region SdkContext
|
|
169
|
+
/** service 之间互相访问的上下文,用懒 getter 解开循环依赖 */
|
|
170
|
+
export interface SdkContext {
|
|
171
|
+
readonly reactivity: Reactivity;
|
|
172
|
+
readonly storage: WebStorage;
|
|
173
|
+
readonly editor: Editor;
|
|
174
|
+
readonly props: Props;
|
|
175
|
+
readonly history: History;
|
|
176
|
+
readonly dataSource: DataSource;
|
|
177
|
+
readonly codeBlock: CodeBlock;
|
|
178
|
+
readonly dep: Dep;
|
|
179
|
+
readonly events: Events;
|
|
180
|
+
readonly componentList: ComponentList;
|
|
181
|
+
|
|
182
|
+
readonly options: Readonly<SdkOptions>;
|
|
183
|
+
/** 校验模式,可在运行时通过 `sdk.ctx.validateMode = ...` 切换 */
|
|
184
|
+
validateMode: ValidateMode;
|
|
185
|
+
/** 校验器,`createDslSdk` 之后可用 */
|
|
186
|
+
validator: Validator;
|
|
187
|
+
/** 登记表全部就绪后 resolve */
|
|
188
|
+
ready: Promise<void>;
|
|
189
|
+
parseDSL: (code: string) => any;
|
|
190
|
+
}
|
|
191
|
+
// #endregion SdkContext
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Tencent is pleased to support the open source community by making TMagicEditor available.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2025 Tencent. All rights reserved.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @fileoverview 依赖收集的纯规则:哪些变化需要重新收集、依赖目标怎么建、依赖信息怎么写回 root。
|
|
21
|
+
*
|
|
22
|
+
* SDK 的 `wiring`(同步收集)与浏览器端 `initService`(空闲队列收集 + 画布同步)共用,
|
|
23
|
+
* 两边只在「怎么收集」上不同,规则必须保持一致。
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import type { CodeBlockDSL, DataSourceSchema, Id, MApp, MComponent, Target } from '@tmagic/core';
|
|
27
|
+
import {
|
|
28
|
+
createCodeBlockTarget,
|
|
29
|
+
createDataSourceCondTarget,
|
|
30
|
+
createDataSourceMethodTarget,
|
|
31
|
+
createDataSourceTarget,
|
|
32
|
+
DepTargetType,
|
|
33
|
+
NODE_CONDS_KEY,
|
|
34
|
+
NodeType,
|
|
35
|
+
} from '@tmagic/core';
|
|
36
|
+
import type { ChangeRecord } from '@tmagic/form';
|
|
37
|
+
import { isValueIncludeDataSource } from '@tmagic/utils';
|
|
38
|
+
|
|
39
|
+
import { isIncludeDataSource } from '@editor/utils/editor';
|
|
40
|
+
|
|
41
|
+
import type Dep from '../services/Dep';
|
|
42
|
+
import type { Reactivity } from '../type';
|
|
43
|
+
|
|
44
|
+
type DataSourceDepType = DepTargetType.DATA_SOURCE | DepTargetType.DATA_SOURCE_COND | DepTargetType.DATA_SOURCE_METHOD;
|
|
45
|
+
|
|
46
|
+
/** 数据源依赖类型 -> 写回 root 的字段 */
|
|
47
|
+
const DEPS_KEY = {
|
|
48
|
+
[DepTargetType.DATA_SOURCE]: 'dataSourceDeps',
|
|
49
|
+
[DepTargetType.DATA_SOURCE_COND]: 'dataSourceCondDeps',
|
|
50
|
+
[DepTargetType.DATA_SOURCE_METHOD]: 'dataSourceMethodDeps',
|
|
51
|
+
} as const;
|
|
52
|
+
|
|
53
|
+
const DATA_SOURCE_DEP_TYPES: DataSourceDepType[] = [
|
|
54
|
+
DepTargetType.DATA_SOURCE,
|
|
55
|
+
DepTargetType.DATA_SOURCE_COND,
|
|
56
|
+
DepTargetType.DATA_SOURCE_METHOD,
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
const DATA_SOURCE_TARGET_CREATORS = {
|
|
60
|
+
[DepTargetType.DATA_SOURCE]: createDataSourceTarget,
|
|
61
|
+
[DepTargetType.DATA_SOURCE_COND]: createDataSourceCondTarget,
|
|
62
|
+
[DepTargetType.DATA_SOURCE_METHOD]: createDataSourceMethodTarget,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const isDataSourceDepType = (type: string): type is DataSourceDepType => type in DEPS_KEY;
|
|
66
|
+
|
|
67
|
+
type UpdatedItem = { newNode: MComponent; oldNode: MComponent; changeRecords?: ChangeRecord[] };
|
|
68
|
+
|
|
69
|
+
/** 显示条件(`NODE_CONDS_KEY`)相关的字段 */
|
|
70
|
+
const isCondsPath = (propPath: string) =>
|
|
71
|
+
new RegExp(`${NODE_CONDS_KEY}.(\\d)+.cond`).test(propPath) ||
|
|
72
|
+
new RegExp(`${NODE_CONDS_KEY}.(\\d)+.cond.(\\d)+.value`).test(propPath) ||
|
|
73
|
+
propPath === NODE_CONDS_KEY;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 判断一批已更新的节点哪些需要重新收集数据源依赖。
|
|
77
|
+
*
|
|
78
|
+
* - 根节点、数据源相关的字段变化、修改的 key 已经在收集到的依赖中:需要重新收集
|
|
79
|
+
* - 其余只需要收集代码块、数据源方法依赖
|
|
80
|
+
*/
|
|
81
|
+
export const classifyUpdatedNodes = (
|
|
82
|
+
ctx: { dep: Pick<Dep, 'getTargets'> },
|
|
83
|
+
data: UpdatedItem[],
|
|
84
|
+
): { needRecollectNodes: MComponent[]; normalNodes: MComponent[] } => {
|
|
85
|
+
const needRecollectNodes: MComponent[] = [];
|
|
86
|
+
const normalNodes: MComponent[] = [];
|
|
87
|
+
|
|
88
|
+
const needRecollect = ({ newNode, oldNode, changeRecords }: UpdatedItem) => {
|
|
89
|
+
if (!changeRecords?.length) return isIncludeDataSource(newNode, oldNode);
|
|
90
|
+
|
|
91
|
+
const dataSourceTargets = Object.values(ctx.dep.getTargets(DepTargetType.DATA_SOURCE) || {});
|
|
92
|
+
|
|
93
|
+
return changeRecords.some(({ propPath, value }) => {
|
|
94
|
+
if (!propPath || isCondsPath(propPath) || isValueIncludeDataSource(value)) return true;
|
|
95
|
+
|
|
96
|
+
// 修改的key在收集的依赖中,则需要触发重新收集
|
|
97
|
+
return dataSourceTargets.some((target) => target.deps[newNode.id]?.keys.includes(propPath));
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
for (const item of data) {
|
|
102
|
+
// 同一节点只归入其中一类
|
|
103
|
+
const isRecollect = item.newNode.type !== NodeType.ROOT && needRecollect(item);
|
|
104
|
+
(isRecollect ? needRecollectNodes : normalNodes).push(item.newNode);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return { needRecollectNodes, normalNodes };
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/** 数据源变化规则:命中的 propPath 需要重建并重新收集哪些依赖 */
|
|
111
|
+
const DATA_SOURCE_RECOLLECT_RULES: { types: DataSourceDepType[]; test: (propPath: string) => boolean }[] = [
|
|
112
|
+
{
|
|
113
|
+
types: [DepTargetType.DATA_SOURCE, DepTargetType.DATA_SOURCE_COND],
|
|
114
|
+
test: (path) =>
|
|
115
|
+
path === 'fields' ||
|
|
116
|
+
/fields.(\d)+.name/.test(path) ||
|
|
117
|
+
/fields.(\d)+.defaultValue/.test(path) ||
|
|
118
|
+
/fields.(\d)+$/.test(path),
|
|
119
|
+
},
|
|
120
|
+
{ types: [DepTargetType.DATA_SOURCE], test: (path) => path === 'mocks' },
|
|
121
|
+
{
|
|
122
|
+
types: [DepTargetType.DATA_SOURCE_METHOD],
|
|
123
|
+
test: (path) => path === 'methods' || /methods.(\d)+.name/.test(path) || /methods.(\d)+$/.test(path),
|
|
124
|
+
},
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 数据源更新后需要重建 / 重新收集的依赖类型;没有影响依赖的变化时返回 `null`。
|
|
129
|
+
* 按变更记录顺序,取第一条命中规则的记录。
|
|
130
|
+
*/
|
|
131
|
+
export const getDataSourceRecollectTypes = (changeRecords: ChangeRecord[]): DataSourceDepType[] | null => {
|
|
132
|
+
for (const { propPath } of changeRecords) {
|
|
133
|
+
if (!propPath) continue;
|
|
134
|
+
|
|
135
|
+
const rule = DATA_SOURCE_RECOLLECT_RULES.find(({ test }) => test(propPath));
|
|
136
|
+
if (rule) return rule.types;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return null;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/** 为数据源创建依赖目标(默认三类都建),`reactive` 用于使依赖结果在浏览器端可被订阅 */
|
|
143
|
+
export const addDataSourceTargets = (
|
|
144
|
+
dep: Pick<Dep, 'addTarget'>,
|
|
145
|
+
ds: DataSourceSchema,
|
|
146
|
+
reactive: Reactivity['reactive'],
|
|
147
|
+
types: DataSourceDepType[] = DATA_SOURCE_DEP_TYPES,
|
|
148
|
+
) => {
|
|
149
|
+
for (const type of types) {
|
|
150
|
+
dep.addTarget(DATA_SOURCE_TARGET_CREATORS[type](ds, reactive({})));
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
export const removeDataSourceTargets = (
|
|
155
|
+
dep: Pick<Dep, 'removeTarget'>,
|
|
156
|
+
id: string,
|
|
157
|
+
types: DataSourceDepType[] = DATA_SOURCE_DEP_TYPES,
|
|
158
|
+
) => {
|
|
159
|
+
for (const type of types) {
|
|
160
|
+
dep.removeTarget(id, type);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/** root 变化后重建所有依赖目标:代码块 + 数据源 */
|
|
165
|
+
export const resetDepTargets = (
|
|
166
|
+
dep: Pick<Dep, 'clearTargets' | 'addTarget'>,
|
|
167
|
+
codeBlocks: CodeBlockDSL,
|
|
168
|
+
dataSources: DataSourceSchema[],
|
|
169
|
+
reactive: Reactivity['reactive'],
|
|
170
|
+
) => {
|
|
171
|
+
dep.clearTargets();
|
|
172
|
+
|
|
173
|
+
for (const [id, code] of Object.entries(codeBlocks)) {
|
|
174
|
+
dep.addTarget(createCodeBlockTarget(id, code));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
for (const ds of dataSources) {
|
|
178
|
+
addDataSourceTargets(dep, ds, reactive);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/** 依赖目标新增后,把依赖信息写回 root */
|
|
183
|
+
export const writeTargetDeps = (root: MApp | null | undefined, target: Target) => {
|
|
184
|
+
if (!root || !isDataSourceDepType(target.type)) return;
|
|
185
|
+
|
|
186
|
+
const key = DEPS_KEY[target.type];
|
|
187
|
+
root[key] = root[key] || {};
|
|
188
|
+
root[key]![target.id] = target.deps;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/** 依赖目标移除后,从 root 上清掉对应的依赖信息 */
|
|
192
|
+
export const removeTargetDeps = (root: MApp | null | undefined, id: Id, type: string) => {
|
|
193
|
+
if (!root || !isDataSourceDepType(type)) return;
|
|
194
|
+
|
|
195
|
+
delete root[DEPS_KEY[type]]?.[id];
|
|
196
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { isEmpty } from 'lodash-es';
|
|
2
|
+
|
|
3
|
+
import type { Id, MContainer, MNode } from '@tmagic/core';
|
|
4
|
+
import { NodeType } from '@tmagic/core';
|
|
5
|
+
import { calcValueByFontsize, getElById, isPage, isPageOrFragment } from '@tmagic/utils';
|
|
6
|
+
|
|
7
|
+
import type { AddMNode, PastePosition } from '@editor/type';
|
|
8
|
+
import { generatePageNameByApp, getInitPositionStyle } from '@editor/utils/editor';
|
|
9
|
+
|
|
10
|
+
import type { SdkContext } from '../type';
|
|
11
|
+
|
|
12
|
+
type OperatorContext = Pick<SdkContext, 'editor' | 'props' | 'reactivity'>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 粘贴前置操作:返回分配了新id以及校准了坐标的配置
|
|
16
|
+
* @param position 粘贴的坐标
|
|
17
|
+
* @param config 待粘贴的元素配置(复制时保存的那份配置)
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
export const beforePaste = (
|
|
21
|
+
ctx: OperatorContext,
|
|
22
|
+
position: PastePosition,
|
|
23
|
+
config: MNode[],
|
|
24
|
+
doc?: Document,
|
|
25
|
+
): MNode[] => {
|
|
26
|
+
if (!config[0]?.style) return config;
|
|
27
|
+
const curNode = ctx.editor.get('node');
|
|
28
|
+
// 将数组中第一个元素的坐标作为参照点
|
|
29
|
+
const { left: referenceLeft, top: referenceTop } = config[0].style;
|
|
30
|
+
// 坐标校准后的粘贴数据
|
|
31
|
+
const pasteConfigs: MNode[] = config.map((configItem: MNode): MNode => {
|
|
32
|
+
// 解构 position 对象,从 position 删除 offsetX、offsetY字段
|
|
33
|
+
const { offsetX = 0, offsetY = 0, ...positionClone } = position;
|
|
34
|
+
let pastePosition = positionClone;
|
|
35
|
+
|
|
36
|
+
if (!isEmpty(pastePosition) && curNode?.items) {
|
|
37
|
+
// 如果没有传入粘贴坐标则可能为键盘操作,不再转换
|
|
38
|
+
// 如果粘贴时选中了容器,则将元素粘贴到容器内,坐标需要转换为相对于容器的坐标
|
|
39
|
+
pastePosition = getPositionInContainer(ctx, pastePosition, curNode.id, doc);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 将所有待粘贴元素坐标相对于多选第一个元素坐标重新计算,以保证多选粘贴后元素间距不变
|
|
43
|
+
if (pastePosition.left && configItem.style?.left) {
|
|
44
|
+
pastePosition.left = configItem.style.left - referenceLeft + pastePosition.left;
|
|
45
|
+
}
|
|
46
|
+
if (pastePosition.top && configItem.style?.top) {
|
|
47
|
+
pastePosition.top = configItem.style?.top - referenceTop + pastePosition.top;
|
|
48
|
+
}
|
|
49
|
+
const pasteConfig = ctx.props.setNewItemId(configItem, false);
|
|
50
|
+
|
|
51
|
+
if (pasteConfig.style) {
|
|
52
|
+
const { left, top } = pasteConfig.style;
|
|
53
|
+
// 判断能转换为数字时,做粘贴偏移量计算
|
|
54
|
+
if (typeof left === 'number' || (!!left && !isNaN(Number(left)))) {
|
|
55
|
+
pasteConfig.style.left = Number(left) + offsetX;
|
|
56
|
+
}
|
|
57
|
+
if (typeof top === 'number' || (!!top && !isNaN(Number(top)))) {
|
|
58
|
+
pasteConfig.style.top = Number(top) + offsetY;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
pasteConfig.style = {
|
|
62
|
+
...pasteConfig.style,
|
|
63
|
+
...pastePosition,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const root = ctx.editor.get('root');
|
|
67
|
+
if (isPageOrFragment(pasteConfig) && root) {
|
|
68
|
+
pasteConfig.name = generatePageNameByApp(root, isPage(pasteConfig) ? NodeType.PAGE : NodeType.PAGE_FRAGMENT);
|
|
69
|
+
}
|
|
70
|
+
return pasteConfig as MNode;
|
|
71
|
+
});
|
|
72
|
+
return pasteConfigs;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 将元素粘贴到容器内时,将相对于画布坐标转换为相对于容器的坐标
|
|
77
|
+
* @param position PastePosition 粘贴时相对于画布的坐标
|
|
78
|
+
* @param id 元素id
|
|
79
|
+
* @returns PastePosition 转换后的坐标
|
|
80
|
+
*/
|
|
81
|
+
export const getPositionInContainer = (ctx: OperatorContext, position: PastePosition = {}, id: Id, doc?: Document) => {
|
|
82
|
+
let { left = 0, top = 0 } = position;
|
|
83
|
+
const stageDoc = ctx.editor.get('stage')?.renderer?.contentWindow?.document;
|
|
84
|
+
const parentEl = stageDoc && getElById()(stageDoc, `${id}`);
|
|
85
|
+
const parentElRect = parentEl?.getBoundingClientRect();
|
|
86
|
+
left = left - calcValueByFontsize(doc, parentElRect?.left || 0);
|
|
87
|
+
top = top - calcValueByFontsize(doc, parentElRect?.top || 0);
|
|
88
|
+
return {
|
|
89
|
+
left,
|
|
90
|
+
top,
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export const getAddParent = (ctx: OperatorContext, node: MNode) => {
|
|
95
|
+
const curNode = ctx.editor.get('node');
|
|
96
|
+
|
|
97
|
+
let parentNode;
|
|
98
|
+
if (isPage(node)) {
|
|
99
|
+
parentNode = ctx.editor.get('root');
|
|
100
|
+
} else if (curNode?.items) {
|
|
101
|
+
parentNode = curNode as MContainer;
|
|
102
|
+
} else if (curNode?.id) {
|
|
103
|
+
parentNode = ctx.editor.getParentById(curNode.id, false);
|
|
104
|
+
}
|
|
105
|
+
return parentNode;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export const getDefaultConfig = async (ctx: OperatorContext, addNode: AddMNode, parentNode: MContainer) => {
|
|
109
|
+
const { type, inputEvent, ...config } = addNode;
|
|
110
|
+
const layout = await ctx.editor.getLayout(ctx.reactivity.toRaw(parentNode), addNode as MNode);
|
|
111
|
+
const newNode = { ...ctx.reactivity.toRaw(await ctx.props.getPropsValue(type, config)) };
|
|
112
|
+
newNode.style = getInitPositionStyle(newNode.style, layout);
|
|
113
|
+
return newNode;
|
|
114
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Tencent is pleased to support the open source community by making TMagicEditor available.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2025 Tencent. All rights reserved.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 是否是 fixed 定位。与 `@tmagic/stage` 的同名函数等价,在这里单独实现,
|
|
21
|
+
* 避免 SDK 在 Node 下为了这一行逻辑引入整个画布依赖。
|
|
22
|
+
*/
|
|
23
|
+
export const isFixed = (style: { position?: string }): boolean => style.position === 'fixed';
|