@tmagic/stage 1.3.5 → 1.3.6

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.
@@ -279,7 +279,7 @@
279
279
  el.id = `${this.idPrefix}${target.id}`;
280
280
  el.style.cssText = getTargetElStyle(target, this.zIndex);
281
281
  if (typeof this.updateDragEl === "function") {
282
- this.updateDragEl(el, target);
282
+ this.updateDragEl(el, target, this.container);
283
283
  }
284
284
  const isFixed = isFixedParent(target);
285
285
  const mode = this.container.dataset.mode || Mode.ABSOLUTE;
@@ -584,6 +584,11 @@
584
584
  ContainerHighlightType2["ALT"] = "alt";
585
585
  return ContainerHighlightType2;
586
586
  })(ContainerHighlightType || {});
587
+ var RenderType = /* @__PURE__ */ ((RenderType2) => {
588
+ RenderType2["IFRAME"] = "iframe";
589
+ RenderType2["NATIVE"] = "native";
590
+ return RenderType2;
591
+ })(RenderType || {});
587
592
  var SelectStatus = /* @__PURE__ */ ((SelectStatus2) => {
588
593
  SelectStatus2["SELECT"] = "select";
589
594
  SelectStatus2["MULTI_SELECT"] = "multiSelect";
@@ -1788,13 +1793,13 @@
1788
1793
  /**
1789
1794
  * 在up事件中负责对外通知选中事件,通知画布之外的编辑器更新
1790
1795
  */
1791
- mouseUpHandler = () => {
1796
+ mouseUpHandler = (event) => {
1792
1797
  utils.getDocument().removeEventListener("mouseup", this.mouseUpHandler);
1793
1798
  this.container.addEventListener("mousemove", this.mouseMoveHandler);
1794
1799
  if (this.isMultiSelectStatus) {
1795
- this.emit("multi-select", this.selectedElList);
1800
+ this.emit("multi-select", this.selectedElList, event);
1796
1801
  } else {
1797
- this.emit("select", this.selectedEl);
1802
+ this.emit("select", this.selectedEl, event);
1798
1803
  }
1799
1804
  };
1800
1805
  mouseLeaveHandler = (event) => {
@@ -2203,22 +2208,22 @@
2203
2208
  contentWindow = null;
2204
2209
  runtime = null;
2205
2210
  iframe;
2211
+ nativeContainer;
2206
2212
  runtimeUrl;
2207
2213
  zoom = DEFAULT_ZOOM;
2214
+ renderType;
2208
2215
  customizedRender;
2209
- constructor({ runtimeUrl, zoom, customizedRender }) {
2216
+ constructor({ runtimeUrl, zoom, customizedRender, renderType = RenderType.IFRAME }) {
2210
2217
  super();
2218
+ this.renderType = renderType;
2211
2219
  this.runtimeUrl = runtimeUrl || "";
2212
2220
  this.customizedRender = customizedRender;
2213
2221
  this.setZoom(zoom);
2214
- this.iframe = globalThis.document.createElement("iframe");
2215
- this.iframe.src = utils.isSameDomain(this.runtimeUrl) ? this.runtimeUrl : "";
2216
- this.iframe.style.cssText = `
2217
- border: 0;
2218
- width: 100%;
2219
- height: 100%;
2220
- `;
2221
- this.iframe.addEventListener("load", this.loadHandler);
2222
+ if (this.renderType === RenderType.IFRAME) {
2223
+ this.createIframe();
2224
+ } else if (this.renderType === RenderType.NATIVE) {
2225
+ this.createNativeContainer();
2226
+ }
2222
2227
  }
2223
2228
  getMagicApi = () => ({
2224
2229
  onPageElUpdate: (el) => this.emit("page-el-update", el),
@@ -2258,18 +2263,19 @@
2258
2263
  * @param el 将页面挂载到该Dom节点上
2259
2264
  */
2260
2265
  async mount(el) {
2261
- if (!this.iframe) {
2262
- throw Error("mount 失败");
2263
- }
2264
- if (!utils.isSameDomain(this.runtimeUrl) && this.runtimeUrl) {
2265
- let html = await fetch(this.runtimeUrl).then((res) => res.text());
2266
- const base = `${location.protocol}//${utils.getHost(this.runtimeUrl)}`;
2267
- html = html.replace("<head>", `<head>
2266
+ if (this.iframe) {
2267
+ if (!utils.isSameDomain(this.runtimeUrl) && this.runtimeUrl) {
2268
+ let html = await fetch(this.runtimeUrl).then((res) => res.text());
2269
+ const base = `${location.protocol}//${utils.getHost(this.runtimeUrl)}`;
2270
+ html = html.replace("<head>", `<head>
2268
2271
  <base href="${base}">`);
2269
- this.iframe.srcdoc = html;
2272
+ this.iframe.srcdoc = html;
2273
+ }
2274
+ el.appendChild(this.iframe);
2275
+ this.postTmagicRuntimeReady();
2276
+ } else if (this.nativeContainer) {
2277
+ el.appendChild(this.nativeContainer);
2270
2278
  }
2271
- el.appendChild(this.iframe);
2272
- this.postTmagicRuntimeReady();
2273
2279
  }
2274
2280
  getRuntime = () => {
2275
2281
  if (this.runtime)
@@ -2299,6 +2305,12 @@
2299
2305
  x = x - rect.left;
2300
2306
  y = y - rect.top;
2301
2307
  }
2308
+ } else if (this.nativeContainer) {
2309
+ const rect = this.nativeContainer.getClientRects()[0];
2310
+ if (rect) {
2311
+ x = x - rect.left;
2312
+ y = y - rect.top;
2313
+ }
2302
2314
  }
2303
2315
  return this.getDocument()?.elementsFromPoint(x / this.zoom, y / this.zoom);
2304
2316
  }
@@ -2315,12 +2327,34 @@
2315
2327
  * 销毁实例
2316
2328
  */
2317
2329
  destroy() {
2318
- this.iframe?.removeEventListener("load", this.loadHandler);
2330
+ this.iframe?.removeEventListener("load", this.iframeLoadHandler);
2319
2331
  this.contentWindow = null;
2320
2332
  this.iframe?.remove();
2321
2333
  this.iframe = void 0;
2322
2334
  this.removeAllListeners();
2323
2335
  }
2336
+ createIframe() {
2337
+ this.iframe = globalThis.document.createElement("iframe");
2338
+ this.iframe.src = this.runtimeUrl && utils.isSameDomain(this.runtimeUrl) ? this.runtimeUrl : "";
2339
+ this.iframe.style.cssText = `
2340
+ border: 0;
2341
+ width: 100%;
2342
+ height: 100%;
2343
+ `;
2344
+ this.iframe.addEventListener("load", this.iframeLoadHandler);
2345
+ return this.iframe;
2346
+ }
2347
+ async createNativeContainer() {
2348
+ this.contentWindow = globalThis;
2349
+ this.nativeContainer = globalThis.document.createElement("div");
2350
+ this.contentWindow.magic = this.getMagicApi();
2351
+ if (this.customizedRender) {
2352
+ const el = await this.customizedRender();
2353
+ if (el) {
2354
+ this.nativeContainer.appendChild(el);
2355
+ }
2356
+ }
2357
+ }
2324
2358
  /**
2325
2359
  * 在runtime中对被选中的元素进行标记,部分组件有对选中态进行特殊显示的需求
2326
2360
  * @param el 被选中的元素
@@ -2332,7 +2366,7 @@
2332
2366
  addSelectedClassName(el, doc);
2333
2367
  }
2334
2368
  }
2335
- loadHandler = async () => {
2369
+ iframeLoadHandler = async () => {
2336
2370
  if (!this.contentWindow?.magic) {
2337
2371
  this.postTmagicRuntimeReady();
2338
2372
  }
@@ -2374,6 +2408,7 @@
2374
2408
  this.renderer = new StageRender({
2375
2409
  runtimeUrl: config.runtimeUrl,
2376
2410
  zoom: config.zoom,
2411
+ renderType: config.renderType,
2377
2412
  customizedRender: async () => {
2378
2413
  if (this?.customizedRender) {
2379
2414
  return await this.customizedRender(this);
@@ -2575,12 +2610,12 @@
2575
2610
  initActionManagerEvent() {
2576
2611
  this.actionManager.on("before-select", (idOrEl, event) => {
2577
2612
  this.select(idOrEl, event);
2578
- }).on("select", (selectedEl) => {
2579
- this.emit("select", selectedEl);
2613
+ }).on("select", (selectedEl, event) => {
2614
+ this.emit("select", selectedEl, event);
2580
2615
  }).on("before-multi-select", (idOrElList) => {
2581
2616
  this.multiSelect(idOrElList);
2582
- }).on("multi-select", (selectedElList) => {
2583
- this.emit("multi-select", selectedElList);
2617
+ }).on("multi-select", (selectedElList, event) => {
2618
+ this.emit("multi-select", selectedElList, event);
2584
2619
  });
2585
2620
  }
2586
2621
  /**
@@ -2641,6 +2676,7 @@
2641
2676
  exports.Mode = Mode;
2642
2677
  exports.MouseButton = MouseButton;
2643
2678
  exports.PAGE_CLASS = PAGE_CLASS;
2679
+ exports.RenderType = RenderType;
2644
2680
  exports.SELECTED_CLASS = SELECTED_CLASS;
2645
2681
  exports.SelectStatus = SelectStatus;
2646
2682
  exports.StageDragResize = StageDragResize;