mce 0.15.3 → 0.15.4

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.
@@ -38,7 +38,6 @@ export declare class Doc extends Model {
38
38
  reset(): this;
39
39
  protected _addNode(data: Element, options?: AddNodeOptions): Node;
40
40
  addNode(data: Element, options?: AddNodeOptions): Node;
41
- addNodes(dataItems: Element[], options?: AddNodeOptions): Node[];
42
41
  set(source: Document): this;
43
42
  protected _proxyProps(obj: CoreObject, yMap: Y.Map<any>, isMeta?: boolean): void;
44
43
  protected _proxyChildren(node: Node, childrenIds: Y.Array<string>): void;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Node as Node$1, Element2D, Timeline, Engine, Camera2D, DrawboardEffect, Aabb2D, IN_BROWSER, clamp, assets, TimelineNode, Transform2D, Obb2D, render, Vector2 as Vector2$1, customNodes, Animation, IN_MAC_OS } from "modern-canvas";
2
- import { reactive, computed, watch, markRaw, ref, warn, shallowRef, onBeforeUnmount, defineComponent, createElementBlock, createCommentVNode, unref, openBlock, normalizeStyle, toDisplayString, createVNode, useAttrs, createBlock, resolveDynamicComponent, normalizeClass, mergeProps, createElementVNode, inject, toValue, getCurrentInstance, provide, useId, readonly, toRef, onMounted, onDeactivated, onActivated, onScopeDispose, useModel, useTemplateRef, withDirectives, vShow, vModelText, nextTick, Fragment, renderList, renderSlot, mergeModels, resolveComponent, withModifiers, withCtx, Teleport, createTextVNode, createSlots, normalizeProps, guardReactiveProps, onBeforeMount, h, isRef, useSlots } from "vue";
2
+ import { reactive, computed, watch, markRaw, isReactive, ref, warn, shallowRef, onBeforeUnmount, defineComponent, createElementBlock, createCommentVNode, unref, openBlock, normalizeStyle, toDisplayString, createVNode, useAttrs, createBlock, resolveDynamicComponent, normalizeClass, mergeProps, createElementVNode, inject, toValue, getCurrentInstance, provide, useId, readonly, toRef, onMounted, onDeactivated, onActivated, onScopeDispose, useModel, useTemplateRef, withDirectives, vShow, vModelText, nextTick, Fragment, renderList, renderSlot, mergeModels, resolveComponent, withModifiers, withCtx, Teleport, createTextVNode, createSlots, normalizeProps, guardReactiveProps, onBeforeMount, h, isRef, useSlots } from "vue";
3
3
  import { useFileDialog, useEventListener, isClient, onClickOutside, useDebounceFn, useResizeObserver as useResizeObserver$1, useLocalStorage, useImage } from "@vueuse/core";
4
4
  import { getObjectValueByPath, setObjectValueByPath, Observable, Reactivable, idGenerator, property, normalizeTextContent, normalizeCRLF, isEqualObject } from "modern-idoc";
5
5
  import { saveAs } from "file-saver";
@@ -536,11 +536,6 @@ class Doc extends Model {
536
536
  async load(initFn) {
537
537
  return super.load(async () => {
538
538
  await initFn?.();
539
- this._proxyNode(
540
- this.root,
541
- this._yProps,
542
- this._yChildrenIds
543
- );
544
539
  this._yChildren.observe(this._yChildrenChange.bind(this));
545
540
  });
546
541
  }
@@ -611,9 +606,6 @@ class Doc extends Model {
611
606
  addNode(data, options) {
612
607
  return this.transact(() => this._addNode(data, options));
613
608
  }
614
- addNodes(dataItems, options) {
615
- return this.transact(() => dataItems.map((data) => this._addNode(data, options)));
616
- }
617
609
  set(source) {
618
610
  const { children = [], meta = {}, ..._props } = source;
619
611
  const props = {
@@ -628,7 +620,12 @@ class Doc extends Model {
628
620
  }
629
621
  this._yProps.set("meta", new Y.Map(Object.entries(meta)));
630
622
  this._transacting = true;
631
- this.addNodes(children);
623
+ this._proxyNode(
624
+ this.root,
625
+ this._yProps,
626
+ this._yChildrenIds
627
+ );
628
+ this.root.append(children);
632
629
  return this;
633
630
  }
634
631
  _proxyProps(obj, yMap, isMeta = false) {
@@ -695,8 +692,13 @@ class Doc extends Model {
695
692
  return;
696
693
  }
697
694
  this.transact(() => {
698
- childrenIds.insert(newIndex, [child.id]);
699
695
  this._debug(`addChild ${child.id}`, child.name, newIndex);
696
+ if (!isReactive(child)) {
697
+ child = reactive(child);
698
+ node.children[newIndex] = child;
699
+ }
700
+ this._proxyNode(child);
701
+ childrenIds.insert(newIndex, [child.id]);
700
702
  });
701
703
  });
702
704
  node.on("removeChild", (child, oldIndex) => {
@@ -704,11 +706,11 @@ class Doc extends Model {
704
706
  return;
705
707
  }
706
708
  this.transact(() => {
709
+ this._debug(`removeChild ${child.id}`, child.name, oldIndex);
707
710
  const index = childrenIds.toJSON().indexOf(child.id);
708
711
  if (index > -1) {
709
712
  childrenIds.delete(index, 1);
710
713
  }
711
- this._debug(`removeChild ${child.id}`, child.name, oldIndex);
712
714
  });
713
715
  });
714
716
  node.children.forEach((child) => {
@@ -793,6 +795,7 @@ class Doc extends Model {
793
795
  });
794
796
  node.on("destroy", () => {
795
797
  this._nodeMap.delete(node.id);
798
+ this._yChildren.delete(node.id);
796
799
  this._debug("destroy", node.id);
797
800
  });
798
801
  this._proxyProps(node, yNode);
@@ -2080,6 +2083,7 @@ const _2_load = defineMixin((editor) => {
2080
2083
  const load = async (source) => {
2081
2084
  state.value = "loading";
2082
2085
  const items = [];
2086
+ let hasLoader = false;
2083
2087
  try {
2084
2088
  for (const loader of loaders.values()) {
2085
2089
  if (await loader.test(source)) {
@@ -2089,13 +2093,14 @@ const _2_load = defineMixin((editor) => {
2089
2093
  } else {
2090
2094
  items.push(res);
2091
2095
  }
2096
+ hasLoader = true;
2092
2097
  break;
2093
2098
  }
2094
2099
  }
2095
2100
  } finally {
2096
2101
  state.value = void 0;
2097
2102
  }
2098
- if (!items.length) {
2103
+ if (!hasLoader) {
2099
2104
  throw new Error(`Failed to load source "${source}"`);
2100
2105
  }
2101
2106
  return items;
@@ -3295,36 +3300,74 @@ const _clipboard = definePlugin((editor, options) => {
3295
3300
  } = editor;
3296
3301
  const copiedData = ref();
3297
3302
  const useClipboard = options.clipboard !== false && SUPPORTS_CLIPBOARD;
3298
- function toClipboardItem(source) {
3303
+ const copy = async (source) => {
3299
3304
  if (typeof source === "string") {
3300
- const type = "text/plain";
3301
- return new ClipboardItem({ [type]: new Blob([source], { type }) });
3305
+ if (useClipboard) {
3306
+ await navigator.clipboard.write([
3307
+ new ClipboardItem({
3308
+ "text/plain": new Blob([source], { type: "text/plain" })
3309
+ })
3310
+ ]);
3311
+ }
3302
3312
  } else if (source instanceof Blob) {
3303
- return new ClipboardItem({ [source.type]: source });
3304
- } else {
3305
- const type = "text/html";
3306
- const content = `<mce-clipboard>${JSON.stringify(source)}</mce-clipboard>`;
3307
- return new ClipboardItem({
3308
- [type]: new Blob([content], { type })
3309
- });
3310
- }
3311
- }
3312
- const copy = async (source) => {
3313
- if (source === void 0) {
3314
- source = selection.value.map((v) => {
3315
- const json = v.toJSON();
3316
- delete json.style.left;
3317
- delete json.style.top;
3318
- return json;
3319
- });
3320
- }
3321
- if (useClipboard) {
3322
- await navigator.clipboard.write([
3323
- toClipboardItem(source)
3324
- ]);
3313
+ if (useClipboard) {
3314
+ await navigator.clipboard.write([
3315
+ new ClipboardItem({
3316
+ [source.type]: source
3317
+ })
3318
+ ]);
3319
+ }
3325
3320
  } else {
3326
- if (Array.isArray(source)) {
3327
- copiedData.value = source;
3321
+ if (useClipboard) {
3322
+ const textItems = [];
3323
+ let html = "";
3324
+ if (!source) {
3325
+ const selected = selection.value;
3326
+ html += `<mce-clipboard>${JSON.stringify(selected.map((v) => {
3327
+ const json = v.toJSON();
3328
+ if (json.style) {
3329
+ delete json.style.left;
3330
+ delete json.style.top;
3331
+ }
3332
+ return json;
3333
+ }))}</mce-clipboard>`;
3334
+ const cb = (child) => {
3335
+ if (child instanceof Element2D) {
3336
+ const _text2 = child.text.base.toString();
3337
+ textItems.push(_text2);
3338
+ html += `<span>${_text2}</span>`;
3339
+ }
3340
+ return false;
3341
+ };
3342
+ selected.forEach((node) => {
3343
+ cb(node);
3344
+ node.findOne(cb);
3345
+ });
3346
+ } else {
3347
+ html += `<mce-clipboard>${JSON.stringify(source)}</mce-clipboard>`;
3348
+ }
3349
+ const items = {};
3350
+ if (textItems.length) {
3351
+ items["text/plain"] = new Blob([textItems.join("\n")], { type: "text/plain" });
3352
+ }
3353
+ if (html) {
3354
+ items["text/html"] = new Blob([html], { type: "text/html" });
3355
+ }
3356
+ await navigator.clipboard.write([
3357
+ new ClipboardItem(items)
3358
+ ]);
3359
+ } else {
3360
+ if (source === void 0) {
3361
+ source = selection.value.map((v) => {
3362
+ const json = v.toJSON();
3363
+ delete json.style.left;
3364
+ delete json.style.top;
3365
+ return json;
3366
+ });
3367
+ }
3368
+ if (Array.isArray(source)) {
3369
+ copiedData.value = source;
3370
+ }
3328
3371
  }
3329
3372
  }
3330
3373
  };
@@ -3343,10 +3386,17 @@ const _clipboard = definePlugin((editor, options) => {
3343
3386
  async function _paste(items) {
3344
3387
  const elements = [];
3345
3388
  for (const item of items) {
3346
- for (const type of item.types) {
3389
+ const types = [...item.types];
3390
+ const index = types.indexOf("text/html");
3391
+ if (index > -1) {
3392
+ types.splice(index, 1);
3393
+ types.unshift("text/html");
3394
+ }
3395
+ for (const type of types) {
3347
3396
  const blob = await item.getType(type);
3348
3397
  if (await canLoad(blob)) {
3349
3398
  elements.push(...await load(blob));
3399
+ break;
3350
3400
  } else {
3351
3401
  console.warn(`Unhandled clipboard ${blob.type}`, await blob.text());
3352
3402
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mce",
3
3
  "type": "module",
4
- "version": "0.15.3",
4
+ "version": "0.15.4",
5
5
  "description": "The headless canvas editor framework. only the ESM.",
6
6
  "author": "wxm",
7
7
  "license": "MIT",