@ones-editor/editor 5.0.5 → 5.0.6-beta.p8216.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.
package/dist/index.js CHANGED
@@ -25270,6 +25270,63 @@ var __publicField = (obj, key, value) => {
25270
25270
  focus: pos
25271
25271
  };
25272
25272
  }
25273
+ const WIKI_MACRO_TYPE = "ones-open-wiki-macro";
25274
+ function getWikiMacroExportSpec(data2) {
25275
+ if (!data2 || data2.protocolVersion !== 2 || !Array.isArray(data2.exportSpec))
25276
+ return [];
25277
+ return data2.exportSpec.filter((spec) => {
25278
+ if (!spec || typeof spec !== "object")
25279
+ return false;
25280
+ if (spec.type === "text")
25281
+ return typeof spec.data === "string";
25282
+ if (spec.type === "image")
25283
+ return typeof spec.data === "string" && !!spec.data;
25284
+ return spec.type === "link" && spec.data && typeof spec.data.title === "string" && typeof spec.data.url === "string" && /^(https?:\/\/|mailto:)/i.test(spec.data.url.trim());
25285
+ });
25286
+ }
25287
+ function getWikiMacroResources(data2) {
25288
+ if (!data2 || data2.protocolVersion !== 2)
25289
+ return [];
25290
+ const resources = /* @__PURE__ */ new Set();
25291
+ if (Array.isArray(data2.resources)) {
25292
+ data2.resources.forEach((resource) => {
25293
+ if (typeof resource === "string" && resource)
25294
+ resources.add(resource);
25295
+ });
25296
+ }
25297
+ getWikiMacroExportSpec(data2).forEach((spec) => {
25298
+ if (spec.type === "image" && typeof spec.data === "string" && spec.data)
25299
+ resources.add(spec.data);
25300
+ });
25301
+ return Array.from(resources);
25302
+ }
25303
+ function replaceWikiMacroResources(data2, resources) {
25304
+ if (!data2 || data2.protocolVersion !== 2)
25305
+ return;
25306
+ if (Array.isArray(data2.resources)) {
25307
+ data2.resources = data2.resources.map((resource) => {
25308
+ var _a;
25309
+ return (_a = resources.get(resource)) != null ? _a : resource;
25310
+ });
25311
+ }
25312
+ getWikiMacroExportSpec(data2).forEach((spec) => {
25313
+ var _a;
25314
+ if (spec.type === "image" && typeof spec.data === "string")
25315
+ spec.data = (_a = resources.get(spec.data)) != null ? _a : spec.data;
25316
+ });
25317
+ }
25318
+ function wikiMacroToText(data2) {
25319
+ return getWikiMacroExportSpec(data2).map((spec) => {
25320
+ if (spec.type === "text" && typeof spec.data === "string")
25321
+ return spec.data;
25322
+ if (spec.type === "link" && spec.data && typeof spec.data === "object") {
25323
+ const link2 = spec.data;
25324
+ if (typeof link2.title === "string" && typeof link2.url === "string")
25325
+ return `${link2.title} ${link2.url}`;
25326
+ }
25327
+ return "";
25328
+ }).filter(Boolean).join("\n");
25329
+ }
25273
25330
  const logger$44 = getLogger("repair-duplicated-block-ids");
25274
25331
  const ROOT_CONTAINER_ID = "root";
25275
25332
  function buildChildContainersDelta(doc2, block) {
@@ -27120,6 +27177,9 @@ var __publicField = (obj, key, value) => {
27120
27177
  return text2;
27121
27178
  }
27122
27179
  function blockToText(editor, blockData, doc2, path) {
27180
+ if (blockData.type === "embed" && blockData.embedType === WIKI_MACRO_TYPE) {
27181
+ return wikiMacroToText(blockData.embedData);
27182
+ }
27123
27183
  const blockClass = getBlockClassByType(editor, blockData.type);
27124
27184
  if (blockClass.convertTo) {
27125
27185
  return blockClass.convertTo(editor, blockData, doc2, "text", path);
@@ -29902,6 +29962,10 @@ var __publicField = (obj, key, value) => {
29902
29962
  function defaultGetResourceFromBlock(editor, doc2, block, resources) {
29903
29963
  if (block.type === "embed") {
29904
29964
  const embedData = block.embedData;
29965
+ if (block.embedType === WIKI_MACRO_TYPE) {
29966
+ getWikiMacroResources(embedData).forEach((resource) => resources.add(resource));
29967
+ return;
29968
+ }
29905
29969
  const src = embedData.src;
29906
29970
  if (src && typeof src === "string") {
29907
29971
  for (const item of src.split("|")) {
@@ -30288,6 +30352,10 @@ ${codeText}
30288
30352
  function defaultReplaceBlockResources(doc2, block, resources) {
30289
30353
  if (block.type === "embed") {
30290
30354
  const embedData = block.embedData;
30355
+ if (block.embedType === WIKI_MACRO_TYPE) {
30356
+ replaceWikiMacroResources(embedData, resources);
30357
+ return;
30358
+ }
30291
30359
  const src = embedData.src;
30292
30360
  if (src && typeof src === "string") {
30293
30361
  const newSrc = resources.get(src);
@@ -31664,6 +31732,12 @@ ${codeText}
31664
31732
  return this.deleteChildContainer(editor);
31665
31733
  }
31666
31734
  }
31735
+ class AsyncAction extends Action {
31736
+ constructor(editor, operation) {
31737
+ super(editor, "", editor.selection.range.toDocRange());
31738
+ this.operation = operation;
31739
+ }
31740
+ }
31667
31741
  const BATCH_ACTION_TS = 1e3 * 3;
31668
31742
  const logger$3f = getLogger("undo");
31669
31743
  class UndoManager {
@@ -31674,6 +31748,9 @@ ${codeText}
31674
31748
  __publicField(this, "currentGroup", null);
31675
31749
  __publicField(this, "groupCounter", 0);
31676
31750
  __publicField(this, "undoRedoing", false);
31751
+ __publicField(this, "undoLocks", /* @__PURE__ */ new Set());
31752
+ __publicField(this, "pendingAction");
31753
+ __publicField(this, "resetVersion", 0);
31677
31754
  __publicField(this, "handleSelectionChange", (editor) => {
31678
31755
  if (this.currentGroup) {
31679
31756
  this.currentGroup.setAfterRange(editor.selection.range.toDocRange());
@@ -31689,18 +31766,25 @@ ${codeText}
31689
31766
  this.editor = editor;
31690
31767
  this.undoCursor = -1;
31691
31768
  this.editor.addListener("selectionChanged", this.handleSelectionChange);
31769
+ this.editor.addListener("destroy", () => this.reset());
31692
31770
  }
31693
31771
  reset() {
31772
+ this.resetVersion += 1;
31773
+ this.actions.forEach((action) => this.discard(action));
31694
31774
  this.actions = [];
31695
31775
  this.undoCursor = -1;
31696
31776
  }
31697
31777
  prepareSaveUndoState() {
31778
+ if (this.pendingAction)
31779
+ throw new Error("An asynchronous undo action is pending");
31698
31780
  if (this.undoCursor !== -1) {
31699
- this.actions.splice(this.undoCursor);
31781
+ this.actions.splice(this.undoCursor).forEach((action) => this.discard(action));
31700
31782
  this.undoCursor = -1;
31701
31783
  }
31702
31784
  }
31703
31785
  addAction(action) {
31786
+ if (this.pendingAction)
31787
+ throw new Error("An asynchronous undo action is pending");
31704
31788
  if (this.currentGroup) {
31705
31789
  this.currentGroup.addAction(action);
31706
31790
  } else {
@@ -31710,6 +31794,79 @@ ${codeText}
31710
31794
  });
31711
31795
  }
31712
31796
  }
31797
+ addAsyncAction(operation) {
31798
+ if (this.currentGroup)
31799
+ throw new Error("An asynchronous undo action cannot join a synchronous group");
31800
+ this.prepareSaveUndoState();
31801
+ const action = new AsyncAction(this.editor, operation);
31802
+ this.addAction(action);
31803
+ return () => {
31804
+ const index2 = this.actions.indexOf(action);
31805
+ if (index2 === -1)
31806
+ return;
31807
+ this.actions.splice(index2, 1);
31808
+ if (this.undoCursor !== -1 && index2 < this.undoCursor)
31809
+ this.undoCursor -= 1;
31810
+ this.discard(action);
31811
+ };
31812
+ }
31813
+ discard(action) {
31814
+ var _a, _b;
31815
+ if (action instanceof AsyncAction)
31816
+ (_b = (_a = action.operation).onDiscard) == null ? void 0 : _b.call(_a);
31817
+ }
31818
+ acquireUndoLock() {
31819
+ const lock = Symbol("macro-edit-session");
31820
+ this.undoLocks.add(lock);
31821
+ return () => {
31822
+ this.undoLocks.delete(lock);
31823
+ };
31824
+ }
31825
+ async undoAsync() {
31826
+ if (this.pendingAction)
31827
+ return false;
31828
+ const accepted = this.undo();
31829
+ return this.pendingAction || accepted;
31830
+ }
31831
+ async redoAsync() {
31832
+ if (this.pendingAction)
31833
+ return false;
31834
+ const accepted = this.redo();
31835
+ return this.pendingAction || accepted;
31836
+ }
31837
+ runAsyncAction(action, index2, direction) {
31838
+ const release = this.editor.acquireReadonly();
31839
+ const resetVersion = this.resetVersion;
31840
+ this.pendingAction = Promise.resolve().then(() => action.operation[direction]()).then((success) => {
31841
+ if (!success)
31842
+ return false;
31843
+ if (this.resetVersion !== resetVersion || this.actions[index2] !== action)
31844
+ return false;
31845
+ this.undoCursor = direction === "undo" ? index2 : index2 + 1;
31846
+ const range = direction === "undo" ? action.beforeRange : action.afterRange;
31847
+ if (range) {
31848
+ try {
31849
+ this.editor.selection.setRange(fromDocRange(this.editor, range));
31850
+ } catch (error2) {
31851
+ logger$3f.warn(`failed to restore selection after asynchronous ${direction}`);
31852
+ }
31853
+ }
31854
+ this.editor.input.handleChanged(action);
31855
+ return true;
31856
+ }).catch((error2) => {
31857
+ try {
31858
+ action.operation.onError(error2);
31859
+ } catch (notificationError) {
31860
+ logger$3f.warn("failed to report asynchronous undo error");
31861
+ }
31862
+ return false;
31863
+ }).finally(() => {
31864
+ this.pendingAction = void 0;
31865
+ release();
31866
+ this.checkRange();
31867
+ });
31868
+ return true;
31869
+ }
31713
31870
  beginGroup() {
31714
31871
  if (this.groupCounter === 0) {
31715
31872
  assert(logger$3f, !this.currentGroup, "current group already exists");
@@ -31778,12 +31935,17 @@ ${codeText}
31778
31935
  }
31779
31936
  }
31780
31937
  undo() {
31938
+ if (this.pendingAction || this.undoLocks.size)
31939
+ return false;
31781
31940
  if (this.undoCursor === -1) {
31782
31941
  this.undoCursor = this.actions.length;
31783
31942
  }
31784
31943
  try {
31785
31944
  this.undoRedoing = true;
31786
31945
  while (this.undoCursor > 0) {
31946
+ const candidate = this.actions[this.undoCursor - 1];
31947
+ if (candidate instanceof AsyncAction)
31948
+ return this.runAsyncAction(candidate, this.undoCursor - 1, "undo");
31787
31949
  this.undoCursor -= 1;
31788
31950
  const action = this.actions[this.undoCursor];
31789
31951
  if (action.undo(this.editor)) {
@@ -31811,6 +31973,8 @@ ${codeText}
31811
31973
  }
31812
31974
  }
31813
31975
  redo() {
31976
+ if (this.pendingAction || this.undoLocks.size)
31977
+ return false;
31814
31978
  if (this.undoCursor === -1) {
31815
31979
  this.undoCursor = this.actions.length;
31816
31980
  }
@@ -31818,6 +31982,8 @@ ${codeText}
31818
31982
  this.undoRedoing = true;
31819
31983
  while (this.undoCursor < this.actions.length) {
31820
31984
  const action = this.actions[this.undoCursor];
31985
+ if (action instanceof AsyncAction)
31986
+ return this.runAsyncAction(action, this.undoCursor, "redo");
31821
31987
  this.undoCursor += 1;
31822
31988
  if (action.redo(this.editor)) {
31823
31989
  try {
@@ -31846,6 +32012,8 @@ ${codeText}
31846
32012
  }
31847
32013
  }
31848
32014
  canUndo() {
32015
+ if (this.pendingAction || this.undoLocks.size)
32016
+ return false;
31849
32017
  if (this.actions.length === 0) {
31850
32018
  return false;
31851
32019
  }
@@ -31855,6 +32023,8 @@ ${codeText}
31855
32023
  return true;
31856
32024
  }
31857
32025
  canRedo() {
32026
+ if (this.pendingAction || this.undoLocks.size)
32027
+ return false;
31858
32028
  if (this.actions.length === 0) {
31859
32029
  return false;
31860
32030
  }
@@ -31880,6 +32050,8 @@ ${codeText}
31880
32050
  if (lastAction === action) {
31881
32051
  return true;
31882
32052
  }
32053
+ if (lastAction instanceof AsyncAction)
32054
+ return false;
31883
32055
  if (!this.undo()) {
31884
32056
  return false;
31885
32057
  }
@@ -32453,6 +32625,7 @@ ${codeText}
32453
32625
  __publicField(this, "_readonly", false);
32454
32626
  __publicField(this, "_scrollable", true);
32455
32627
  __publicField(this, "_destroyed", false);
32628
+ __publicField(this, "readonlyLocks", /* @__PURE__ */ new Set());
32456
32629
  __publicField(this, "getColor", (index2) => {
32457
32630
  const colors = this.options.colors;
32458
32631
  if (colors.length === 0) {
@@ -32508,11 +32681,21 @@ ${codeText}
32508
32681
  this.emit("load", this, false);
32509
32682
  }
32510
32683
  get readonly() {
32511
- return this._readonly;
32684
+ return this._readonly || this.readonlyLocks.size > 0;
32685
+ }
32686
+ acquireReadonly() {
32687
+ const lock = Symbol("pending-document-action");
32688
+ this.readonlyLocks.add(lock);
32689
+ this.readonly = this._readonly;
32690
+ return () => {
32691
+ if (!this.readonlyLocks.delete(lock))
32692
+ return;
32693
+ this.readonly = this._readonly;
32694
+ };
32512
32695
  }
32513
32696
  set readonly(val) {
32514
32697
  this._readonly = val;
32515
- if (val) {
32698
+ if (this.readonly) {
32516
32699
  addClass(this.rootElement, "readonly");
32517
32700
  } else {
32518
32701
  removeClass(this.rootElement, "readonly");
@@ -40345,6 +40528,10 @@ ${codeText}
40345
40528
  });
40346
40529
  });
40347
40530
  }
40531
+ getDocumentVersion() {
40532
+ var _a;
40533
+ return (_a = this.doc.version) != null ? _a : void 0;
40534
+ }
40348
40535
  toJSON() {
40349
40536
  return editorDoc2DocObject(this.data());
40350
40537
  }
@@ -97862,7 +98049,7 @@ ${JSON.stringify(error2, null, 2)}`);
97862
98049
  }
97863
98050
  }
97864
98051
  });
97865
- editor.version = "5.0.5";
98052
+ editor.version = "5.0.6-beta.p8216.1";
97866
98053
  return editor;
97867
98054
  }
97868
98055
  function isDoc(doc2) {
@@ -97998,7 +98185,7 @@ ${JSON.stringify(error2, null, 2)}`);
97998
98185
  OnesEditorDropTarget.register(editor);
97999
98186
  OnesEditorTocProvider.register(editor);
98000
98187
  OnesEditorExclusiveBlock.register(editor);
98001
- editor.version = "5.0.5";
98188
+ editor.version = "5.0.6-beta.p8216.1";
98002
98189
  return editor;
98003
98190
  } catch (error2) {
98004
98191
  try {
@@ -98141,7 +98328,7 @@ ${JSON.stringify(error2, null, 2)}`);
98141
98328
  }
98142
98329
  }
98143
98330
  });
98144
- editor.version = "5.0.5";
98331
+ editor.version = "5.0.6-beta.p8216.1";
98145
98332
  return editor;
98146
98333
  }
98147
98334
  const emojis$1 = {
@@ -144159,6 +144346,7 @@ ${JSON.stringify(error2, null, 2)}`);
144159
144346
  exports2.UnorderedListIcon = UnorderedListIcon;
144160
144347
  exports2.UnorderedListItem = UnorderedListItem;
144161
144348
  exports2.VideoIcon = VideoIcon;
144349
+ exports2.WIKI_MACRO_TYPE = WIKI_MACRO_TYPE;
144162
144350
  exports2.WarningFilledIcon = WarningFilledIcon;
144163
144351
  exports2.WarningIcon = WarningIcon;
144164
144352
  exports2.WebPageIcon = WebPageIcon;
@@ -144514,6 +144702,8 @@ ${JSON.stringify(error2, null, 2)}`);
144514
144702
  exports2.getTextWidth = getTextWidth;
144515
144703
  exports2.getToolbar = getToolbar;
144516
144704
  exports2.getToolbarDefaultItems = getToolbarDefaultItems;
144705
+ exports2.getWikiMacroExportSpec = getWikiMacroExportSpec;
144706
+ exports2.getWikiMacroResources = getWikiMacroResources;
144517
144707
  exports2.groupEachCommand = groupEachCommand;
144518
144708
  exports2.handleBlockElementUpdated = handleBlockElementUpdated$1;
144519
144709
  exports2.hasAltOnly = hasAltOnly;
@@ -144652,6 +144842,7 @@ ${JSON.stringify(error2, null, 2)}`);
144652
144842
  exports2.replaceAllCaseInsensitive = replaceAllCaseInsensitive;
144653
144843
  exports2.replaceAllText = replaceAllText;
144654
144844
  exports2.replaceText = replaceText;
144845
+ exports2.replaceWikiMacroResources = replaceWikiMacroResources;
144655
144846
  exports2.requestDownload = requestDownload;
144656
144847
  exports2.resetListStart = resetListStart;
144657
144848
  exports2.saveAs = saveAs$1;
@@ -144700,6 +144891,7 @@ ${JSON.stringify(error2, null, 2)}`);
144700
144891
  exports2.updateOpAttributes = updateOpAttributes;
144701
144892
  exports2.updateTextBlockContent = updateTextBlockContent;
144702
144893
  exports2.updateUrl = updateUrl;
144894
+ exports2.wikiMacroToText = wikiMacroToText;
144703
144895
  for (const k in tinyTypedEmitter) {
144704
144896
  if (k !== "default" && !exports2.hasOwnProperty(k))
144705
144897
  Object.defineProperty(exports2, k, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ones-editor/editor",
3
- "version": "5.0.5",
3
+ "version": "5.0.6-beta.p8216.1",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "dependencies": {