mce 0.16.0 → 0.16.2

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.
@@ -1,5 +1,4 @@
1
1
  import type { CoreObject } from 'modern-canvas';
2
- import type { Document, Element } from 'modern-idoc';
3
2
  import type { YModelEvents } from './YModel';
4
3
  import { Node } from 'modern-canvas';
5
4
  import * as Y from 'yjs';
@@ -21,20 +20,17 @@ export interface YDoc {
21
20
  emit: <K extends keyof YDocEvents & string>(event: K, ...args: YDocEvents[K]) => this;
22
21
  }
23
22
  export declare class YDoc extends YModel {
24
- protected readonly root: Node;
25
23
  protected _yChildren: Y.Map<YNode>;
26
24
  protected _yChildrenIds: Y.Array<string>;
27
- protected _nodeMap: Map<string, Node>;
28
- constructor(root: Node, id?: string);
25
+ _nodeMap: Map<string, Node>;
26
+ constructor(id?: string);
29
27
  load(initFn?: () => void | Promise<void>): Promise<this>;
30
28
  protected _isSelfTransaction(transaction: Y.Transaction): boolean;
31
29
  protected _debug(..._args: any[]): void;
32
30
  protected _yChildrenChange(event: Y.YMapEvent<Y.Map<unknown>>, transaction: Y.Transaction): void;
33
31
  reset(): this;
34
32
  destroy(): void;
35
- protected _addNode(data: Element, options?: AddNodeOptions): Node;
36
- addNode(data: Element, options?: AddNodeOptions): Node;
37
- set(source: Document): this;
33
+ proxyRoot(root: Node, props: Record<string, any>): this;
38
34
  protected _proxyProps(obj: CoreObject, yMap: Y.Map<any>, isMeta?: boolean): void;
39
35
  protected _proxyChildren(node: Node, childrenIds: Y.Array<string>): void;
40
36
  protected _proxyNode(node: Node, yNode?: YNode, yChildrenIds?: Y.Array<string>): void;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Node as Node$1, Element2D, Camera2D, Timeline, DrawboardEffect, Engine, Aabb2D, IN_BROWSER, clamp, assets, TimelineNode, Video2D, Transform2D, Obb2D, render, Vector2 as Vector2$1, Lottie2D, customNodes, Animation, IN_MAC_OS } from "modern-canvas";
1
+ import { Element2D, Node as Node$1, Camera2D, Timeline, DrawboardEffect, Engine, Aabb2D, IN_BROWSER, clamp, assets, TimelineNode, Video2D, Transform2D, Obb2D, render, Vector2 as Vector2$1, Lottie2D, customNodes, Animation, IN_MAC_OS } from "modern-canvas";
2
2
  import { reactive, computed, watch, markRaw, isReactive, ref, onBeforeMount, onScopeDispose, warn, shallowRef, defineComponent, unref, openBlock, createElementBlock, normalizeStyle as normalizeStyle$1, toDisplayString, createCommentVNode, onMounted, createVNode, useAttrs, createBlock, resolveDynamicComponent, normalizeClass, mergeProps, createElementVNode, inject, toValue, getCurrentInstance, provide, useId, onBeforeUnmount, readonly, toRef, onDeactivated, onActivated, useModel, useTemplateRef, withDirectives, withModifiers, vModelText, vShow, nextTick, Fragment, renderList, renderSlot, resolveComponent, withCtx, mergeModels, Teleport, createTextVNode, createSlots, normalizeProps, guardReactiveProps, h, isRef, effectScope, useSlots } from "vue";
3
3
  import { useFileDialog, useEventListener, isClient, onClickOutside, useDebounceFn, useImage, useResizeObserver as useResizeObserver$1, useLocalStorage } from "@vueuse/core";
4
4
  import { getObjectValueByPath, setObjectValueByPath, Observable, idGenerator, normalizeTextContent, property, isCRLF, textContentToString, normalizeCRLF, isEqualObject } from "modern-idoc";
@@ -468,21 +468,18 @@ class YModel extends Observable {
468
468
  }
469
469
  }
470
470
  class YDoc extends YModel {
471
- constructor(root, id) {
471
+ _yChildren;
472
+ _yChildrenIds;
473
+ _nodeMap = /* @__PURE__ */ new Map();
474
+ constructor(id) {
472
475
  super(id);
473
- this.root = root;
474
476
  this._yChildren = markRaw(this._yDoc.getMap("children"));
475
477
  this._yChildrenIds = markRaw(this._yDoc.getArray("childrenIds"));
476
478
  this._setupUndoManager([
477
479
  this._yChildren,
478
480
  this._yChildrenIds
479
481
  ]);
480
- this._yProps.set("id", this.root.id);
481
- this._yProps.set("name", this.root.name);
482
482
  }
483
- _yChildren;
484
- _yChildrenIds;
485
- _nodeMap = /* @__PURE__ */ new Map();
486
483
  async load(initFn) {
487
484
  return super.load(async () => {
488
485
  await initFn?.();
@@ -518,7 +515,6 @@ class YDoc extends YModel {
518
515
  }
519
516
  reset() {
520
517
  super.reset();
521
- this.root.resetProperties();
522
518
  this._yChildren.clear();
523
519
  this._yChildrenIds.delete(0, this._yChildrenIds.length);
524
520
  this._nodeMap.clear();
@@ -528,58 +524,21 @@ class YDoc extends YModel {
528
524
  }
529
525
  destroy() {
530
526
  this.reset();
531
- this.root.remove();
532
527
  super.destroy();
533
528
  }
534
- _addNode(data, options = {}) {
535
- const { parentId, index, regenId } = options;
536
- let parent;
537
- if (parentId && parentId !== this.root.id) {
538
- parent = this._nodeMap.get(parentId) ?? this.root;
539
- } else {
540
- parent = this.root;
541
- }
542
- const value = {
543
- ...data,
544
- meta: {
545
- inCanvasIs: "Element2D",
546
- ...data?.meta ?? {}
547
- }
548
- };
549
- if (regenId) {
550
- delete value.id;
551
- }
552
- const node = reactive(Node$1.parse(value));
553
- if (index === void 0) {
554
- parent.appendChild(node);
555
- } else {
556
- parent.moveChild(node, index);
557
- }
558
- this._proxyNode(node);
559
- return node;
560
- }
561
- addNode(data, options) {
562
- return this.transact(() => this._addNode(data, options));
563
- }
564
- set(source) {
565
- const { children = [], meta = {}, ..._props } = source;
566
- const props = {
567
- id: this.root.id,
568
- name: this.root.name,
569
- ..._props
570
- };
529
+ proxyRoot(root, props) {
530
+ const { meta = {}, ..._props } = props;
571
531
  this.reset();
572
- for (const key in props) {
573
- this._yProps.set(key, props[key]);
532
+ for (const key in _props) {
533
+ this._yProps.set(key, _props[key]);
574
534
  }
575
535
  this._yProps.set("meta", new Y.Map(Object.entries(meta)));
576
536
  this._transacting = true;
577
537
  this._proxyNode(
578
- this.root,
538
+ root,
579
539
  this._yProps,
580
540
  this._yChildrenIds
581
541
  );
582
- this.root.append(children);
583
542
  return this;
584
543
  }
585
544
  _proxyProps(obj, yMap, isMeta = false) {
@@ -837,7 +796,7 @@ class Doc extends Node$1 {
837
796
  }
838
797
  }
839
798
  }
840
- const _doc2 = new YDoc(this, id);
799
+ const _doc2 = new YDoc(id);
841
800
  _doc2.on(
842
801
  "update",
843
802
  throttle((update, origin) => this.emit("update", update, origin), 200)
@@ -851,15 +810,24 @@ class Doc extends Node$1 {
851
810
  transact = (fn, should = true) => {
852
811
  return this._yDoc.transact(fn, should);
853
812
  };
854
- addNode = (data, options) => {
855
- return this._yDoc.addNode(data, options);
856
- };
857
813
  undo = () => {
858
814
  this._yDoc.undoManager.undo();
859
815
  };
860
816
  redo = () => {
861
817
  this._yDoc.undoManager.redo();
862
818
  };
819
+ set = (source) => {
820
+ const { children = [], ..._props } = source;
821
+ const props = {
822
+ id: this.id,
823
+ name: this.name,
824
+ ..._props
825
+ };
826
+ this.resetProperties();
827
+ this._yDoc.proxyRoot(this, props);
828
+ this.append(children);
829
+ return this;
830
+ };
863
831
  async load() {
864
832
  const source = this._source;
865
833
  await this._yDoc.load(async () => {
@@ -872,9 +840,9 @@ class Doc extends Node$1 {
872
840
  }
873
841
  if (source && typeof source !== "string") {
874
842
  if (Array.isArray(source)) {
875
- this._yDoc.set({ children: source });
843
+ this.set({ children: source });
876
844
  } else {
877
- this._yDoc.set(source);
845
+ this.set(source);
878
846
  }
879
847
  }
880
848
  });
@@ -3530,13 +3498,32 @@ const _4_3_element = defineMixin((editor) => {
3530
3498
  const isArray = Array.isArray(value);
3531
3499
  let offsetIndex = index;
3532
3500
  const elements = root.value.transact(() => {
3501
+ const parentId = parent?.id;
3502
+ const index2 = offsetIndex;
3533
3503
  const values = isArray ? value : [value];
3534
- const elements2 = values.map((element) => {
3535
- const el = root.value.addNode(element, {
3536
- parentId: parent?.id,
3537
- index: offsetIndex,
3538
- regenId
3539
- });
3504
+ const elements2 = values.map((data) => {
3505
+ let parent2;
3506
+ if (parentId && parentId !== root.value.id) {
3507
+ parent2 = root.value._yDoc._nodeMap.get(parentId) ?? root.value;
3508
+ } else {
3509
+ parent2 = root.value;
3510
+ }
3511
+ const value2 = {
3512
+ ...data,
3513
+ meta: {
3514
+ inCanvasIs: "Element2D",
3515
+ ...data?.meta ?? {}
3516
+ }
3517
+ };
3518
+ if (regenId) {
3519
+ delete value2.id;
3520
+ }
3521
+ const el = reactive(Node$1.parse(value2));
3522
+ if (index2 === void 0) {
3523
+ parent2.appendChild(el);
3524
+ } else {
3525
+ parent2.moveChild(el, index2);
3526
+ }
3540
3527
  if (offsetIndex !== void 0) {
3541
3528
  offsetIndex++;
3542
3529
  }
@@ -4257,7 +4244,8 @@ const _doc = definePlugin((editor, options) => {
4257
4244
  docLoading,
4258
4245
  renderEngine,
4259
4246
  config,
4260
- to
4247
+ to,
4248
+ waitUntilFontLoad
4261
4249
  } = editor;
4262
4250
  const getDoc = () => {
4263
4251
  return to("json");
@@ -4269,7 +4257,6 @@ const _doc = definePlugin((editor, options) => {
4269
4257
  oldRoot.remove();
4270
4258
  renderEngine.value.root.append(_root);
4271
4259
  root.value = _root;
4272
- _root._yDoc.root = root.value;
4273
4260
  emit("setDoc", _root, oldRoot);
4274
4261
  return _root;
4275
4262
  };
@@ -4277,6 +4264,7 @@ const _doc = definePlugin((editor, options) => {
4277
4264
  docLoading.value = true;
4278
4265
  emit("docLoading", source);
4279
4266
  try {
4267
+ await waitUntilFontLoad();
4280
4268
  const _doc2 = await setDoc(await load(source));
4281
4269
  emit("docLoaded", source, _doc2);
4282
4270
  docLoading.value = false;
@@ -1,5 +1,6 @@
1
- import type { Element2D, Node, Vector2Like } from 'modern-canvas';
1
+ import type { Element2D, Vector2Like } from 'modern-canvas';
2
2
  import type { Element } from 'modern-idoc';
3
+ import { Node } from 'modern-canvas';
3
4
  declare global {
4
5
  namespace Mce {
5
6
  type AddElementPosition = Vector2Like | Anchor | 'screenCenter' | 'pointer';
@@ -1,7 +1,6 @@
1
1
  import type { NodeEvents } from 'modern-canvas';
2
- import type { Element } from 'modern-idoc';
2
+ import type { Document } from 'modern-idoc';
3
3
  import type * as Y from 'yjs';
4
- import type { AddNodeOptions } from '../crdt';
5
4
  import { Node } from 'modern-canvas';
6
5
  import { YDoc } from '../crdt';
7
6
  export interface DocEvents extends NodeEvents {
@@ -16,12 +15,12 @@ export interface Doc {
16
15
  }
17
16
  export declare class Doc extends Node {
18
17
  protected readonly _localDb: boolean;
19
- protected _yDoc: YDoc;
18
+ _yDoc: YDoc;
20
19
  protected _source: any;
21
20
  constructor(source?: Mce.DocumentSource, _localDb?: boolean);
22
21
  transact: <T>(fn: () => T, should?: boolean) => T;
23
- addNode: (data: Element, options?: AddNodeOptions) => Node;
24
22
  undo: () => void;
25
23
  redo: () => void;
24
+ set: (source: Document) => this;
26
25
  load(): Promise<void>;
27
26
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mce",
3
3
  "type": "module",
4
- "version": "0.16.0",
4
+ "version": "0.16.2",
5
5
  "description": "A headless infinite canvas editor framework built on WebGL rendering, supports exporting to image, video, and PPT. Only the ESM.",
6
6
  "author": "wxm",
7
7
  "license": "MIT",