@tmagic/stage 1.4.16 → 1.5.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,13 +32,15 @@ import type {
32
32
  } from 'moveable';
33
33
  import MoveableHelper from 'moveable-helper';
34
34
 
35
- import { calcValueByFontsize } from '@tmagic/utils';
35
+ import { calcValueByFontsize, getIdFromEl, setIdToEl } from '@tmagic/utils';
36
36
 
37
37
  import { DRAG_EL_ID_PREFIX, GHOST_EL_ID_PREFIX, Mode, ZIndex } from './const';
38
38
  import TargetShadow from './TargetShadow';
39
39
  import type { DragResizeHelperConfig, Rect, TargetElement } from './types';
40
40
  import { getAbsolutePosition, getBorderWidth, getMarginValue, getOffset } from './util';
41
41
 
42
+ const getId = getIdFromEl();
43
+
42
44
  /**
43
45
  * 拖拽/改变大小等操作发生时,moveable会抛出各种状态事件,DragResizeHelper负责响应这些事件,对目标节点target和拖拽节点targetShadow进行修改;
44
46
  * 其中目标节点是DragResizeHelper直接改的,targetShadow作为直接被操作的拖拽框,是调用moveableHelper改的;
@@ -239,15 +241,15 @@ export default class DragResizeHelper {
239
241
  events.forEach((ev) => {
240
242
  const { width, height, beforeTranslate } = ev.drag;
241
243
  const frameSnapShot = this.framesSnapShot.find(
242
- (frameItem) => frameItem.id === ev.target.id.replace(DRAG_EL_ID_PREFIX, ''),
244
+ (frameItem) => frameItem.id === getId(ev.target)?.replace(DRAG_EL_ID_PREFIX, ''),
243
245
  );
244
246
  if (!frameSnapShot) return;
245
247
  const targeEl = this.targetList.find(
246
- (targetItem) => targetItem.id === ev.target.id.replace(DRAG_EL_ID_PREFIX, ''),
248
+ (targetItem) => targetItem.id === getId(ev.target)?.replace(DRAG_EL_ID_PREFIX, ''),
247
249
  );
248
250
  if (!targeEl) return;
249
251
  // 元素与其所属组同时加入多选列表时,只更新父元素
250
- const isParentIncluded = this.targetList.find((targetItem) => targetItem.id === targeEl.parentElement?.id);
252
+ const isParentIncluded = this.targetList.find((targetItem) => getId(targetItem) === getId(targeEl.parentElement));
251
253
 
252
254
  if (!isParentIncluded) {
253
255
  // 更新页面元素位置
@@ -277,15 +279,18 @@ export default class DragResizeHelper {
277
279
  // 拖动过程更新
278
280
  events.forEach((ev) => {
279
281
  const frameSnapShot = this.framesSnapShot.find(
280
- (frameItem) => ev.target.id.startsWith(DRAG_EL_ID_PREFIX) && ev.target.id.endsWith(frameItem.id),
282
+ (frameItem) => getId(ev.target)?.startsWith(DRAG_EL_ID_PREFIX) && getId(ev.target)?.endsWith(frameItem.id),
281
283
  );
282
284
  if (!frameSnapShot) return;
283
285
  const targeEl = this.targetList.find(
284
- (targetItem) => ev.target.id.startsWith(DRAG_EL_ID_PREFIX) && ev.target.id.endsWith(targetItem.id),
286
+ (targetItem) =>
287
+ getId(ev.target)?.startsWith(DRAG_EL_ID_PREFIX) &&
288
+ getId(targetItem) &&
289
+ getId(ev.target)?.endsWith(getId(targetItem)!),
285
290
  );
286
291
  if (!targeEl) return;
287
292
  // 元素与其所属组同时加入多选列表时,只更新父元素
288
- const isParentIncluded = this.targetList.find((targetItem) => targetItem.id === targeEl.parentElement?.id);
293
+ const isParentIncluded = this.targetList.find((targetItem) => getId(targetItem) === getId(targeEl.parentElement));
289
294
  if (!isParentIncluded) {
290
295
  // 更新页面元素位置
291
296
  const { marginLeft, marginTop } = getMarginValue(targeEl);
@@ -312,7 +317,7 @@ export default class DragResizeHelper {
312
317
  const shadowEls = this.getShadowEls();
313
318
 
314
319
  if (shadowEls.length) {
315
- shadowEl = shadowEls.find((item) => item.id.endsWith(el.id));
320
+ shadowEl = shadowEls.find((item) => getId(item)?.endsWith(getId(el) || ''));
316
321
  }
317
322
 
318
323
  if (parentEl && this.mode === Mode.ABSOLUTE && shadowEl) {
@@ -348,14 +353,18 @@ export default class DragResizeHelper {
348
353
  events.forEach((ev) => {
349
354
  // 实际目标元素
350
355
  const matchEventTarget = this.targetList.find(
351
- (targetItem) => ev.target.id.startsWith(DRAG_EL_ID_PREFIX) && ev.target.id.endsWith(targetItem.id),
356
+ (targetItem) =>
357
+ getId(ev.target)?.startsWith(DRAG_EL_ID_PREFIX) && getId(ev.target)?.endsWith(getId(targetItem) || ''),
352
358
  );
353
359
  if (!matchEventTarget) return;
354
- this.framesSnapShot.push({
355
- left: matchEventTarget.offsetLeft,
356
- top: matchEventTarget.offsetTop,
357
- id: matchEventTarget.id,
358
- });
360
+
361
+ const id = getId(matchEventTarget);
362
+ id &&
363
+ this.framesSnapShot.push({
364
+ left: matchEventTarget.offsetLeft,
365
+ top: matchEventTarget.offsetTop,
366
+ id,
367
+ });
359
368
  });
360
369
  }
361
370
 
@@ -370,7 +379,7 @@ export default class DragResizeHelper {
370
379
  const ghostEl = el.cloneNode(true) as HTMLElement;
371
380
  this.setGhostElChildrenId(ghostEl);
372
381
  const { top, left } = getAbsolutePosition(el, getOffset(el));
373
- ghostEl.id = `${GHOST_EL_ID_PREFIX}${el.id}`;
382
+ setIdToEl()(ghostEl, `${GHOST_EL_ID_PREFIX}${getId(el)}`);
374
383
  ghostEl.style.zIndex = ZIndex.GHOST_EL;
375
384
  ghostEl.style.opacity = '.5';
376
385
  ghostEl.style.position = 'absolute';
@@ -384,8 +393,10 @@ export default class DragResizeHelper {
384
393
 
385
394
  private setGhostElChildrenId(el: Element): void {
386
395
  for (const child of Array.from(el.children)) {
387
- if (child.id) {
388
- child.id = `${GHOST_EL_ID_PREFIX}${child.id}`;
396
+ const el = child as HTMLElement;
397
+ const id = getId(el);
398
+ if (id) {
399
+ setIdToEl()(el, `${GHOST_EL_ID_PREFIX}${id}`);
389
400
  }
390
401
 
391
402
  if (child.children.length) {
package/src/StageCore.ts CHANGED
@@ -21,6 +21,7 @@ import { EventEmitter } from 'events';
21
21
  import type { MoveableOptions, OnDragStart } from 'moveable';
22
22
 
23
23
  import type { Id } from '@tmagic/schema';
24
+ import { getIdFromEl } from '@tmagic/utils';
24
25
 
25
26
  import ActionManager from './ActionManager';
26
27
  import { DEFAULT_ZOOM } from './const';
@@ -64,7 +65,7 @@ export default class StageCore extends EventEmitter {
64
65
  runtimeUrl: config.runtimeUrl,
65
66
  zoom: config.zoom,
66
67
  renderType: config.renderType,
67
- customizedRender: async (): Promise<HTMLElement | null> => {
68
+ customizedRender: async (): Promise<HTMLElement | null | void> => {
68
69
  if (this?.customizedRender) {
69
70
  return await this.customizedRender(this);
70
71
  }
@@ -336,13 +337,14 @@ export default class StageCore extends EventEmitter {
336
337
  private initActionManagerEvent(): void {
337
338
  this.actionManager
338
339
  .on('before-select', (el: HTMLElement, event?: MouseEvent) => {
339
- this.select(el.id, event);
340
+ const id = getIdFromEl()(el);
341
+ id && this.select(id, event);
340
342
  })
341
343
  .on('select', (selectedEl: HTMLElement, event: MouseEvent) => {
342
344
  this.emit('select', selectedEl, event);
343
345
  })
344
346
  .on('before-multi-select', (els: HTMLElement[]) => {
345
- this.multiSelect(els.map((el) => el.id));
347
+ this.multiSelect(els.map((el) => getIdFromEl()(el)).filter((id) => Boolean(id)) as string[]);
346
348
  })
347
349
  .on('multi-select', (selectedElList: HTMLElement[], event: MouseEvent) => {
348
350
  this.emit('multi-select', selectedElList, event);
@@ -19,6 +19,8 @@
19
19
  /* eslint-disable no-param-reassign */
20
20
  import Moveable, { MoveableOptions } from 'moveable';
21
21
 
22
+ import { getIdFromEl } from '@tmagic/utils';
23
+
22
24
  import { Mode, StageDragStatus } from './const';
23
25
  import DragResizeHelper from './DragResizeHelper';
24
26
  import MoveableOptionsManager from './MoveableOptionsManager';
@@ -328,10 +330,12 @@ export default class StageDragResize extends MoveableOptionsManager {
328
330
  this.emit('sort', up(deltaTop, this.target));
329
331
  }
330
332
  } else {
331
- this.emit('sort', {
332
- src: this.target.id,
333
- dist: this.target.id,
334
- });
333
+ const id = getIdFromEl()(this.target);
334
+ id &&
335
+ this.emit('sort', {
336
+ src: id,
337
+ dist: id,
338
+ });
335
339
  }
336
340
  }
337
341
 
@@ -18,6 +18,8 @@
18
18
 
19
19
  import Moveable from 'moveable';
20
20
 
21
+ import { getIdFromEl } from '@tmagic/utils';
22
+
21
23
  import { DRAG_EL_ID_PREFIX, Mode, StageDragStatus } from './const';
22
24
  import DragResizeHelper from './DragResizeHelper';
23
25
  import MoveableOptionsManager from './MoveableOptionsManager';
@@ -132,7 +134,8 @@ export default class StageMultiDragResize extends MoveableOptionsManager {
132
134
  const { inputTarget, targets } = e;
133
135
  // 如果有多个元素被选中,同时点击的元素在选中元素中的其中一项,可能是多选态切换为该元素的单选态,抛事件给上一层继续判断是否切换
134
136
  if (targets.length > 1 && targets.includes(inputTarget)) {
135
- this.emit('change-to-select', inputTarget.id.replace(DRAG_EL_ID_PREFIX, ''), e.inputEvent);
137
+ const id = getIdFromEl()(inputTarget as HTMLElement)?.replace(DRAG_EL_ID_PREFIX, '');
138
+ id && this.emit('change-to-select', id, e.inputEvent);
136
139
  }
137
140
  });
138
141
  }
@@ -19,7 +19,7 @@
19
19
  import { EventEmitter } from 'events';
20
20
 
21
21
  import { Id } from '@tmagic/schema';
22
- import { getHost, injectStyle, isSameDomain } from '@tmagic/utils';
22
+ import { getElById, getHost, injectStyle, isSameDomain } from '@tmagic/utils';
23
23
 
24
24
  import { DEFAULT_ZOOM, RenderType } from './const';
25
25
  import style from './style.css?raw';
@@ -36,7 +36,7 @@ export default class StageRender extends EventEmitter {
36
36
  private runtimeUrl?: string;
37
37
  private zoom = DEFAULT_ZOOM;
38
38
  private renderType: RenderType;
39
- private customizedRender?: () => Promise<HTMLElement | null>;
39
+ private customizedRender?: () => Promise<HTMLElement | null | void>;
40
40
 
41
41
  constructor({ runtimeUrl, zoom, customizedRender, renderType = RenderType.IFRAME }: StageRenderConfig) {
42
42
  super();
@@ -152,7 +152,7 @@ export default class StageRender extends EventEmitter {
152
152
  }
153
153
 
154
154
  public getTargetElement(id: Id): HTMLElement | null {
155
- return this.getDocument()?.getElementById(`${id}`) || null;
155
+ return getElById()(this.getDocument(), id);
156
156
  }
157
157
 
158
158
  /**
@@ -15,7 +15,7 @@
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
17
  */
18
- import { guid } from '@tmagic/utils';
18
+ import { getElById, getIdFromEl, guid, setIdToEl } from '@tmagic/utils';
19
19
 
20
20
  import { Mode, ZIndex } from './const';
21
21
  import type { TargetElement as ShadowElement, TargetShadowConfig, UpdateDragEl } from './types';
@@ -93,7 +93,7 @@ export default class TargetShadow {
93
93
  private updateEl(target: ShadowElement, src?: ShadowElement): ShadowElement {
94
94
  const el = src || globalThis.document.createElement('div');
95
95
 
96
- el.id = `${this.idPrefix}_${target.id}`;
96
+ setIdToEl()(el, `${this.idPrefix}_${getIdFromEl()(target)}`);
97
97
 
98
98
  el.style.cssText = getTargetElStyle(target, this.zIndex);
99
99
 
@@ -108,7 +108,7 @@ export default class TargetShadow {
108
108
  el.style.transform = `translate3d(${-this.scrollLeft}px, ${-this.scrollTop}px, 0)`;
109
109
  }
110
110
 
111
- if (!globalThis.document.getElementById(el.id)) {
111
+ if (!getElById()(globalThis.document, getIdFromEl()(el))) {
112
112
  this.container.append(el);
113
113
  }
114
114
 
package/src/types.ts CHANGED
@@ -30,7 +30,7 @@ export type TargetElement = HTMLElement | SVGElement;
30
30
 
31
31
  export type CanSelect = (el: HTMLElement, event: MouseEvent, stop: () => boolean) => boolean | Promise<boolean>;
32
32
  export type IsContainer = (el: HTMLElement) => boolean | Promise<boolean>;
33
- export type CustomizeRender = (renderer: StageCore) => Promise<HTMLElement> | HTMLElement;
33
+ export type CustomizeRender = (renderer: StageCore) => Promise<HTMLElement | void> | HTMLElement | void;
34
34
  /** 业务方自定义的moveableOptions,可以是配置,也可以是回调函数 */
35
35
  export type CustomizeMoveableOptions =
36
36
  | ((config?: CustomizeMoveableOptionsCallbackConfig) => MoveableOptions)
@@ -60,7 +60,7 @@ export interface StageCoreConfig {
60
60
  moveableOptions?: CustomizeMoveableOptions;
61
61
  /** runtime 的HTML地址,可以是一个HTTP地址,如果和编辑器不同域,需要设置跨域,也可以是一个相对或绝对路径 */
62
62
  runtimeUrl?: string;
63
- render?: (renderer: StageCore) => Promise<HTMLElement> | HTMLElement;
63
+ render?: CustomizeRender;
64
64
  autoScrollIntoView?: boolean;
65
65
  updateDragEl?: UpdateDragEl;
66
66
  disabledDragStart?: boolean;
@@ -105,7 +105,7 @@ export interface StageRenderConfig {
105
105
  runtimeUrl?: string;
106
106
  zoom: number | undefined;
107
107
  renderType?: RenderType;
108
- customizedRender?: () => Promise<HTMLElement | null>;
108
+ customizedRender?: () => Promise<HTMLElement | null | void>;
109
109
  }
110
110
 
111
111
  export interface StageMaskConfig {