@ones-editor/editor 3.0.13 → 3.0.14-beta.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.
@@ -3,7 +3,18 @@ export declare class ImagePreviewHandler implements OnesEditorCustom {
3
3
  private editor;
4
4
  private onPreview;
5
5
  private lastClickPosition;
6
+ private lastPreviewCandidates;
7
+ private lastScrollSnapshots;
6
8
  constructor(editor: OnesEditor, onPreview: (editor: OnesEditor, img: HTMLImageElement) => void);
9
+ getPreviewCandidateElements(e: MouseEvent): HTMLElement[];
10
+ previewFromImageBox(elem: HTMLElement): boolean;
11
+ previewFromImageContainer(elem: HTMLElement): boolean;
12
+ previewFromEmbedBlock(elem: HTMLElement): boolean;
13
+ isPreviewableImageElement(elem: HTMLElement): boolean;
14
+ previewFromCandidates: (candidates: HTMLElement[]) => boolean;
15
+ captureScrollSnapshots: () => void;
16
+ restoreScrollSnapshots: () => void;
17
+ scheduleRestoreScrollSnapshots: () => void;
7
18
  handleImagePreview: (e: MouseEvent) => void;
8
19
  handleMouseUp: (e: MouseEvent) => void;
9
20
  handleMouseDown: (e: MouseEvent) => void;
package/dist/index.js CHANGED
@@ -83078,6 +83078,9 @@ ${docStr}
83078
83078
  const blocks = this.docObject.blocks[containerId];
83079
83079
  const blockData = blocks[updateIndex];
83080
83080
  assert(logger$C, !blockData.deleted, `block has already deleted: ${containerId}/${blockIndex}`);
83081
+ if (!Array.isArray(blockData.text)) {
83082
+ blockData.text = [];
83083
+ }
83081
83084
  const text2 = blockData.text;
83082
83085
  assert(logger$C, text2, `no text for block: ${JSON.stringify(blockData)}`);
83083
83086
  const newText = updateBlockText(text2, actions2, user);
@@ -86529,7 +86532,7 @@ ${docStr}
86529
86532
  },
86530
86533
  convertTo: convertTo$6
86531
86534
  };
86532
- const MERMAID_SCRIPTS = "https://cdn.jsdelivr.net/npm/mermaid@9.0.1/dist/mermaid.min.js";
86535
+ const MERMAID_SCRIPTS = "https://cdn.jsdelivr.net/npm/mermaid@11.15.0/dist/mermaid.min.js";
86533
86536
  const FLOWCHART_SCRIPTS = ["//cdnjs.cloudflare.com/ajax/libs/raphael/2.3.0/raphael.min.js", "//cdnjs.cloudflare.com/ajax/libs/flowchart/1.15.0/flowchart.min.js"];
86534
86537
  const PLANTUML_PRE = "https://www.plantuml.com/plantuml/svg";
86535
86538
  const GRAPH_MIN_HEIGHT = 150;
@@ -87246,22 +87249,17 @@ ${docStr}
87246
87249
  const mermaid = window.mermaid;
87247
87250
  assert(logger$l, mermaid, "Resource load failed: Mermaid javaScript resource injection failed");
87248
87251
  const id = genId();
87249
- const promise = new Promise((resolve, reject) => {
87250
- try {
87251
- mermaid.mermaidAPI.render(id, code, (svgCode2) => {
87252
- setTimeout(() => {
87253
- resolve(svgCode2);
87254
- });
87255
- });
87256
- } catch (err) {
87257
- err.message = err.str;
87258
- const delId = `d${id}`;
87259
- const el = document.getElementById(delId);
87260
- el == null ? void 0 : el.remove();
87261
- reject(err);
87262
- }
87263
- });
87264
- const svgCode = await promise;
87252
+ let svgCode = "";
87253
+ try {
87254
+ const result = await mermaid.render(id, code);
87255
+ svgCode = typeof result === "string" ? result : result.svg;
87256
+ } catch (err) {
87257
+ err.message = err.str || err.message;
87258
+ const delId = `d${id}`;
87259
+ const el = document.getElementById(delId);
87260
+ el == null ? void 0 : el.remove();
87261
+ throw err;
87262
+ }
87265
87263
  const domParser = new DOMParser();
87266
87264
  const xml = domParser.parseFromString(svgCode, "image/svg+xml");
87267
87265
  const xmlSvg = xml.firstChild;
@@ -92986,58 +92984,138 @@ ${data2.plantumlText}
92986
92984
  StatusBox
92987
92985
  ];
92988
92986
  const safetyOffset = 2;
92987
+ function isPointInElement(elem, x, y) {
92988
+ const rect = elem.getBoundingClientRect();
92989
+ return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom;
92990
+ }
92991
+ function getImagePreviewTarget(elem) {
92992
+ if (elem instanceof HTMLImageElement && elem.classList.contains("editor-image")) {
92993
+ return elem;
92994
+ }
92995
+ const imageContainer = elem.closest(".image-container[data-src]");
92996
+ if (imageContainer instanceof HTMLElement) {
92997
+ const img = imageContainer.querySelector("img.editor-image");
92998
+ return img instanceof HTMLElement ? img : imageContainer;
92999
+ }
93000
+ const imageBox = elem.closest('span[data-type="editor-box"][data-box-type="image"]');
93001
+ if (imageBox instanceof HTMLElement) {
93002
+ const img = imageBox.querySelector("img");
93003
+ return img instanceof HTMLElement ? img : imageBox;
93004
+ }
93005
+ return null;
93006
+ }
93007
+ function getScrollSnapshots(elem) {
93008
+ const snapshots = [{
93009
+ element: null,
93010
+ isWindow: true,
93011
+ scrollTop: window.scrollY,
93012
+ scrollLeft: window.scrollX
93013
+ }];
93014
+ const added = /* @__PURE__ */ new Set();
93015
+ let current = elem;
93016
+ while (current) {
93017
+ const style2 = window.getComputedStyle(current);
93018
+ const overflow = `${style2.overflow}${style2.overflowY}${style2.overflowX}`;
93019
+ const canScroll = current.scrollHeight > current.clientHeight || current.scrollWidth > current.clientWidth;
93020
+ if (canScroll || /(auto|scroll|overlay)/.test(overflow)) {
93021
+ snapshots.push({
93022
+ element: current,
93023
+ isWindow: false,
93024
+ scrollTop: current.scrollTop,
93025
+ scrollLeft: current.scrollLeft
93026
+ });
93027
+ added.add(current);
93028
+ }
93029
+ current = current.parentElement;
93030
+ }
93031
+ const scrollingElement = document.scrollingElement;
93032
+ if (scrollingElement instanceof HTMLElement && !added.has(scrollingElement)) {
93033
+ snapshots.push({
93034
+ element: scrollingElement,
93035
+ isWindow: false,
93036
+ scrollTop: scrollingElement.scrollTop,
93037
+ scrollLeft: scrollingElement.scrollLeft
93038
+ });
93039
+ }
93040
+ return snapshots;
93041
+ }
92989
93042
  class ImagePreviewHandler {
92990
93043
  constructor(editor, onPreview) {
92991
93044
  __publicField(this, "lastClickPosition");
92992
- __publicField(this, "handleImagePreview", (e2) => {
92993
- var _a;
92994
- const elem = e2.target;
92995
- if (!(elem instanceof HTMLElement)) {
92996
- return;
93045
+ __publicField(this, "lastPreviewCandidates", []);
93046
+ __publicField(this, "lastScrollSnapshots", []);
93047
+ __publicField(this, "previewFromCandidates", (candidates) => {
93048
+ if (!candidates.length) {
93049
+ return false;
92997
93050
  }
92998
- const imageBox = getParentBox(elem);
92999
- const boxType = imageBox && getBoxTypeFromElement(imageBox);
93000
- if (imageBox && elem instanceof HTMLImageElement && boxType === "image") {
93001
- const imgData = this.editor.getBoxData(imageBox);
93002
- if (!imgData.link) {
93003
- this.onPreview(this.editor, elem);
93004
- return;
93051
+ for (const elem of candidates) {
93052
+ if (this.previewFromImageBox(elem)) {
93053
+ return true;
93054
+ }
93055
+ if (this.previewFromImageContainer(elem)) {
93056
+ return true;
93057
+ }
93058
+ if (this.previewFromEmbedBlock(elem)) {
93059
+ return true;
93005
93060
  }
93006
93061
  }
93007
- const block = getParentBlock(elem);
93008
- if (!block) {
93009
- return;
93010
- }
93011
- if (!isEmbedBlock(block)) {
93062
+ return false;
93063
+ });
93064
+ __publicField(this, "captureScrollSnapshots", () => {
93065
+ this.lastScrollSnapshots = getScrollSnapshots(this.editor.rootElement);
93066
+ });
93067
+ __publicField(this, "restoreScrollSnapshots", () => {
93068
+ if (!this.lastScrollSnapshots.length) {
93012
93069
  return;
93013
93070
  }
93014
- if (getEmbedType(block) === "image") {
93015
- const imageData = this.editor.getBlockData(block);
93016
- if (imageData.link) {
93017
- return;
93018
- }
93019
- }
93020
- if (elem.hasAttribute("data-src")) {
93021
- const img = elem.querySelector("img");
93022
- if (img) {
93023
- this.onPreview(this.editor, img);
93071
+ this.lastScrollSnapshots.forEach((snapshot2) => {
93072
+ var _a;
93073
+ if (snapshot2.isWindow) {
93074
+ window.scrollTo(snapshot2.scrollLeft, snapshot2.scrollTop);
93024
93075
  return;
93025
93076
  }
93026
- }
93027
- if (elem.tagName === "IMG" && ((_a = elem.parentElement) == null ? void 0 : _a.hasAttribute("data-src"))) {
93028
- this.onPreview(this.editor, elem);
93029
- }
93077
+ (_a = snapshot2.element) == null ? void 0 : _a.scrollTo(snapshot2.scrollLeft, snapshot2.scrollTop);
93078
+ });
93079
+ });
93080
+ __publicField(this, "scheduleRestoreScrollSnapshots", () => {
93081
+ this.restoreScrollSnapshots();
93082
+ window.requestAnimationFrame(() => {
93083
+ this.restoreScrollSnapshots();
93084
+ window.requestAnimationFrame(this.restoreScrollSnapshots);
93085
+ });
93086
+ window.setTimeout(this.restoreScrollSnapshots, 50);
93087
+ window.setTimeout(this.restoreScrollSnapshots, 200);
93088
+ });
93089
+ __publicField(this, "handleImagePreview", (e2) => {
93090
+ const candidates = this.getPreviewCandidateElements(e2);
93091
+ this.previewFromCandidates(candidates);
93030
93092
  });
93031
93093
  __publicField(this, "handleMouseUp", (e2) => {
93032
- if (!this.lastClickPosition)
93094
+ if (!this.lastClickPosition) {
93033
93095
  return;
93096
+ }
93034
93097
  const { x, y } = this.lastClickPosition;
93035
93098
  const deltaX = Math.abs(e2.x - x);
93036
93099
  const deltaY = Math.abs(e2.y - y);
93100
+ const previewCandidates = this.lastPreviewCandidates;
93037
93101
  this.lastClickPosition = void 0;
93102
+ this.lastPreviewCandidates = [];
93038
93103
  if (deltaX < safetyOffset && deltaY < safetyOffset) {
93104
+ this.scheduleRestoreScrollSnapshots();
93105
+ if (this.previewFromCandidates(previewCandidates)) {
93106
+ window.setTimeout(() => {
93107
+ this.lastScrollSnapshots = [];
93108
+ }, 250);
93109
+ return;
93110
+ }
93039
93111
  this.handleImagePreview(e2);
93112
+ this.scheduleRestoreScrollSnapshots();
93113
+ window.setTimeout(() => {
93114
+ this.lastScrollSnapshots = [];
93115
+ }, 250);
93116
+ return;
93040
93117
  }
93118
+ this.lastScrollSnapshots = [];
93041
93119
  });
93042
93120
  __publicField(this, "handleMouseDown", (e2) => {
93043
93121
  const { x, y } = e2;
@@ -93054,9 +93132,16 @@ ${data2.plantumlText}
93054
93132
  return;
93055
93133
  }
93056
93134
  this.lastClickPosition = { x, y };
93135
+ this.lastPreviewCandidates = this.getPreviewCandidateElements(e2);
93136
+ if (this.lastPreviewCandidates.some((candidate) => this.isPreviewableImageElement(candidate))) {
93137
+ this.captureScrollSnapshots();
93138
+ e2.preventDefault();
93139
+ e2.stopPropagation();
93140
+ this.scheduleRestoreScrollSnapshots();
93141
+ }
93057
93142
  });
93058
93143
  __publicField(this, "destroy", () => {
93059
- this.editor.rootElement.removeEventListener("mousedown", this.handleMouseDown);
93144
+ this.editor.rootElement.removeEventListener("mousedown", this.handleMouseDown, { capture: true });
93060
93145
  this.editor.rootElement.removeEventListener("mouseup", this.handleMouseUp);
93061
93146
  });
93062
93147
  this.editor = editor;
@@ -93064,6 +93149,131 @@ ${data2.plantumlText}
93064
93149
  editor.rootElement.addEventListener("mousedown", this.handleMouseDown, { capture: true });
93065
93150
  editor.rootElement.addEventListener("mouseup", this.handleMouseUp);
93066
93151
  }
93152
+ getPreviewCandidateElements(e2) {
93153
+ var _a, _b;
93154
+ const candidates = [];
93155
+ const addCandidate = (target) => {
93156
+ if (!(target instanceof HTMLElement)) {
93157
+ return;
93158
+ }
93159
+ if (!this.editor.rootElement.contains(target)) {
93160
+ return;
93161
+ }
93162
+ if (candidates.includes(target)) {
93163
+ return;
93164
+ }
93165
+ candidates.push(target);
93166
+ };
93167
+ const addElementStackHit = (target) => {
93168
+ if (!(target instanceof HTMLElement)) {
93169
+ return;
93170
+ }
93171
+ if (!this.editor.rootElement.contains(target)) {
93172
+ return;
93173
+ }
93174
+ addCandidate(getImagePreviewTarget(target));
93175
+ };
93176
+ if (typeof document !== "undefined") {
93177
+ (_a = document.elementsFromPoint) == null ? void 0 : _a.call(document, e2.clientX, e2.clientY).forEach(addElementStackHit);
93178
+ addElementStackHit(document.elementFromPoint(e2.clientX, e2.clientY));
93179
+ }
93180
+ this.editor.rootElement.querySelectorAll('img.editor-image, span[data-type="editor-box"][data-box-type="image"] img').forEach((elem) => {
93181
+ if (elem instanceof HTMLElement && isPointInElement(elem, e2.clientX, e2.clientY)) {
93182
+ addCandidate(elem);
93183
+ }
93184
+ });
93185
+ this.editor.rootElement.querySelectorAll(".image-container[data-src]").forEach((elem) => {
93186
+ if (elem instanceof HTMLElement && isPointInElement(elem, e2.clientX, e2.clientY)) {
93187
+ addCandidate(elem);
93188
+ }
93189
+ });
93190
+ addCandidate(e2.target);
93191
+ (_b = e2.composedPath) == null ? void 0 : _b.call(e2).forEach(addCandidate);
93192
+ return candidates;
93193
+ }
93194
+ previewFromImageBox(elem) {
93195
+ const imageBox = getParentBox(elem);
93196
+ const boxType = imageBox && getBoxTypeFromElement(imageBox);
93197
+ const img = elem instanceof HTMLImageElement ? elem : imageBox == null ? void 0 : imageBox.querySelector("img");
93198
+ if (!imageBox || boxType !== "image" || !img) {
93199
+ return false;
93200
+ }
93201
+ const imgData = this.editor.getBoxData(imageBox);
93202
+ if (!imgData.link) {
93203
+ this.onPreview(this.editor, img);
93204
+ }
93205
+ return true;
93206
+ }
93207
+ previewFromImageContainer(elem) {
93208
+ const imageContainer = elem.closest(".image-container[data-src]");
93209
+ if (!(imageContainer instanceof HTMLElement)) {
93210
+ return false;
93211
+ }
93212
+ if (!this.editor.rootElement.contains(imageContainer)) {
93213
+ return false;
93214
+ }
93215
+ const block = getParentBlock(imageContainer);
93216
+ if (block && isEmbedBlock(block) && getEmbedType(block) === "image") {
93217
+ const imageData = this.editor.getBlockData(block);
93218
+ if (imageData.link) {
93219
+ return true;
93220
+ }
93221
+ }
93222
+ const img = elem instanceof HTMLImageElement ? elem : imageContainer.querySelector("img");
93223
+ if (img) {
93224
+ this.onPreview(this.editor, img);
93225
+ }
93226
+ return true;
93227
+ }
93228
+ previewFromEmbedBlock(elem) {
93229
+ var _a;
93230
+ const block = getParentBlock(elem);
93231
+ if (!block) {
93232
+ return false;
93233
+ }
93234
+ if (!isEmbedBlock(block)) {
93235
+ return false;
93236
+ }
93237
+ if (getEmbedType(block) === "image") {
93238
+ const imageData = this.editor.getBlockData(block);
93239
+ if (imageData.link) {
93240
+ return true;
93241
+ }
93242
+ }
93243
+ if (elem.hasAttribute("data-src")) {
93244
+ const img = elem.querySelector("img");
93245
+ if (img) {
93246
+ this.onPreview(this.editor, img);
93247
+ }
93248
+ return true;
93249
+ }
93250
+ if (elem.tagName === "IMG" && ((_a = elem.parentElement) == null ? void 0 : _a.hasAttribute("data-src"))) {
93251
+ this.onPreview(this.editor, elem);
93252
+ return true;
93253
+ }
93254
+ return false;
93255
+ }
93256
+ isPreviewableImageElement(elem) {
93257
+ const imageBox = getParentBox(elem);
93258
+ const boxType = imageBox && getBoxTypeFromElement(imageBox);
93259
+ const imageBoxImg = elem instanceof HTMLImageElement ? elem : imageBox == null ? void 0 : imageBox.querySelector("img");
93260
+ if (imageBox && boxType === "image" && imageBoxImg) {
93261
+ const imgData = this.editor.getBoxData(imageBox);
93262
+ return !imgData.link;
93263
+ }
93264
+ const imageContainer = elem.closest(".image-container[data-src]");
93265
+ if (imageContainer instanceof HTMLElement) {
93266
+ const block = getParentBlock(imageContainer);
93267
+ if (block && isEmbedBlock(block) && getEmbedType(block) === "image") {
93268
+ const imageData = this.editor.getBlockData(block);
93269
+ if (imageData.link) {
93270
+ return false;
93271
+ }
93272
+ }
93273
+ return Boolean(elem instanceof HTMLImageElement ? elem : imageContainer.querySelector("img"));
93274
+ }
93275
+ return false;
93276
+ }
93067
93277
  }
93068
93278
  function createHistoryEditor(root2, doc2, options) {
93069
93279
  var _a, _b, _c, _d, _e;
@@ -96857,7 +97067,7 @@ ${JSON.stringify(error2, null, 2)}`);
96857
97067
  }
96858
97068
  }
96859
97069
  });
96860
- editor.version = "3.0.13";
97070
+ editor.version = "3.0.14-beta.2";
96861
97071
  return editor;
96862
97072
  }
96863
97073
  function isDoc(doc2) {
@@ -96991,7 +97201,7 @@ ${JSON.stringify(error2, null, 2)}`);
96991
97201
  OnesEditorDropTarget.register(editor);
96992
97202
  OnesEditorTocProvider.register(editor);
96993
97203
  OnesEditorExclusiveBlock.register(editor);
96994
- editor.version = "3.0.13";
97204
+ editor.version = "3.0.14-beta.2";
96995
97205
  return editor;
96996
97206
  }
96997
97207
  async function showDocVersions(editor, options, serverUrl) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ones-editor/editor",
3
- "version": "3.0.13",
3
+ "version": "3.0.14-beta.2",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "dependencies": {
@@ -48,7 +48,7 @@
48
48
  "marked-extended-latex": "^1.1.0",
49
49
  "markmap-common": "0.15.3",
50
50
  "markmap-view": "0.15.4",
51
- "mermaid": "^9.1.0",
51
+ "mermaid": "11.15.0",
52
52
  "mime-db": "^1.52.0",
53
53
  "nanoid": "^3.2.0",
54
54
  "ot-json1": "^1.0.2",