@ones-editor/editor 3.0.15-beta.2 → 3.0.15-beta.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.
@@ -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
@@ -83156,6 +83156,9 @@ ${docStr}
83156
83156
  const blocks = this.docObject.blocks[containerId];
83157
83157
  const blockData = blocks[updateIndex];
83158
83158
  assert(logger$C, !blockData.deleted, `block has already deleted: ${containerId}/${blockIndex}`);
83159
+ if (!Array.isArray(blockData.text)) {
83160
+ blockData.text = [];
83161
+ }
83159
83162
  const text2 = blockData.text;
83160
83163
  assert(logger$C, text2, `no text for block: ${JSON.stringify(blockData)}`);
83161
83164
  const newText = updateBlockText(text2, actions2, user);
@@ -86607,7 +86610,7 @@ ${docStr}
86607
86610
  },
86608
86611
  convertTo: convertTo$6
86609
86612
  };
86610
- const MERMAID_SCRIPTS = "https://cdn.jsdelivr.net/npm/mermaid@9.0.1/dist/mermaid.min.js";
86613
+ const MERMAID_SCRIPTS = "https://cdn.jsdelivr.net/npm/mermaid@11.15.0/dist/mermaid.min.js";
86611
86614
  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"];
86612
86615
  const PLANTUML_PRE = "https://www.plantuml.com/plantuml/svg";
86613
86616
  const GRAPH_MIN_HEIGHT = 150;
@@ -87324,22 +87327,17 @@ ${docStr}
87324
87327
  const mermaid = window.mermaid;
87325
87328
  assert(logger$l, mermaid, "Resource load failed: Mermaid javaScript resource injection failed");
87326
87329
  const id = genId();
87327
- const promise = new Promise((resolve, reject) => {
87328
- try {
87329
- mermaid.mermaidAPI.render(id, code, (svgCode2) => {
87330
- setTimeout(() => {
87331
- resolve(svgCode2);
87332
- });
87333
- });
87334
- } catch (err) {
87335
- err.message = err.str;
87336
- const delId = `d${id}`;
87337
- const el = document.getElementById(delId);
87338
- el == null ? void 0 : el.remove();
87339
- reject(err);
87340
- }
87341
- });
87342
- const svgCode = await promise;
87330
+ let svgCode = "";
87331
+ try {
87332
+ const result = await mermaid.render(id, code);
87333
+ svgCode = typeof result === "string" ? result : result.svg;
87334
+ } catch (err) {
87335
+ err.message = err.str || err.message;
87336
+ const delId = `d${id}`;
87337
+ const el = document.getElementById(delId);
87338
+ el == null ? void 0 : el.remove();
87339
+ throw err;
87340
+ }
87343
87341
  const domParser = new DOMParser();
87344
87342
  const xml = domParser.parseFromString(svgCode, "image/svg+xml");
87345
87343
  const xmlSvg = xml.firstChild;
@@ -93090,58 +93088,138 @@ ${data2.plantumlText}
93090
93088
  StatusBox
93091
93089
  ];
93092
93090
  const safetyOffset = 2;
93091
+ function isPointInElement(elem, x, y) {
93092
+ const rect = elem.getBoundingClientRect();
93093
+ return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom;
93094
+ }
93095
+ function getImagePreviewTarget(elem) {
93096
+ if (elem instanceof HTMLImageElement && elem.classList.contains("editor-image")) {
93097
+ return elem;
93098
+ }
93099
+ const imageContainer = elem.closest(".image-container[data-src]");
93100
+ if (imageContainer instanceof HTMLElement) {
93101
+ const img = imageContainer.querySelector("img.editor-image");
93102
+ return img instanceof HTMLElement ? img : imageContainer;
93103
+ }
93104
+ const imageBox = elem.closest('span[data-type="editor-box"][data-box-type="image"]');
93105
+ if (imageBox instanceof HTMLElement) {
93106
+ const img = imageBox.querySelector("img");
93107
+ return img instanceof HTMLElement ? img : imageBox;
93108
+ }
93109
+ return null;
93110
+ }
93111
+ function getScrollSnapshots(elem) {
93112
+ const snapshots = [{
93113
+ element: null,
93114
+ isWindow: true,
93115
+ scrollTop: window.scrollY,
93116
+ scrollLeft: window.scrollX
93117
+ }];
93118
+ const added = /* @__PURE__ */ new Set();
93119
+ let current = elem;
93120
+ while (current) {
93121
+ const style2 = window.getComputedStyle(current);
93122
+ const overflow = `${style2.overflow}${style2.overflowY}${style2.overflowX}`;
93123
+ const canScroll = current.scrollHeight > current.clientHeight || current.scrollWidth > current.clientWidth;
93124
+ if (canScroll || /(auto|scroll|overlay)/.test(overflow)) {
93125
+ snapshots.push({
93126
+ element: current,
93127
+ isWindow: false,
93128
+ scrollTop: current.scrollTop,
93129
+ scrollLeft: current.scrollLeft
93130
+ });
93131
+ added.add(current);
93132
+ }
93133
+ current = current.parentElement;
93134
+ }
93135
+ const scrollingElement = document.scrollingElement;
93136
+ if (scrollingElement instanceof HTMLElement && !added.has(scrollingElement)) {
93137
+ snapshots.push({
93138
+ element: scrollingElement,
93139
+ isWindow: false,
93140
+ scrollTop: scrollingElement.scrollTop,
93141
+ scrollLeft: scrollingElement.scrollLeft
93142
+ });
93143
+ }
93144
+ return snapshots;
93145
+ }
93093
93146
  class ImagePreviewHandler {
93094
93147
  constructor(editor, onPreview) {
93095
93148
  __publicField(this, "lastClickPosition");
93096
- __publicField(this, "handleImagePreview", (e2) => {
93097
- var _a;
93098
- const elem = e2.target;
93099
- if (!(elem instanceof HTMLElement)) {
93100
- return;
93149
+ __publicField(this, "lastPreviewCandidates", []);
93150
+ __publicField(this, "lastScrollSnapshots", []);
93151
+ __publicField(this, "previewFromCandidates", (candidates) => {
93152
+ if (!candidates.length) {
93153
+ return false;
93101
93154
  }
93102
- const imageBox = getParentBox(elem);
93103
- const boxType = imageBox && getBoxTypeFromElement(imageBox);
93104
- if (imageBox && elem instanceof HTMLImageElement && boxType === "image") {
93105
- const imgData = this.editor.getBoxData(imageBox);
93106
- if (!imgData.link) {
93107
- this.onPreview(this.editor, elem);
93108
- return;
93155
+ for (const elem of candidates) {
93156
+ if (this.previewFromImageBox(elem)) {
93157
+ return true;
93158
+ }
93159
+ if (this.previewFromImageContainer(elem)) {
93160
+ return true;
93161
+ }
93162
+ if (this.previewFromEmbedBlock(elem)) {
93163
+ return true;
93109
93164
  }
93110
93165
  }
93111
- const block = getParentBlock(elem);
93112
- if (!block) {
93113
- return;
93114
- }
93115
- if (!isEmbedBlock(block)) {
93166
+ return false;
93167
+ });
93168
+ __publicField(this, "captureScrollSnapshots", () => {
93169
+ this.lastScrollSnapshots = getScrollSnapshots(this.editor.rootElement);
93170
+ });
93171
+ __publicField(this, "restoreScrollSnapshots", () => {
93172
+ if (!this.lastScrollSnapshots.length) {
93116
93173
  return;
93117
93174
  }
93118
- if (getEmbedType(block) === "image") {
93119
- const imageData = this.editor.getBlockData(block);
93120
- if (imageData.link) {
93121
- return;
93122
- }
93123
- }
93124
- if (elem.hasAttribute("data-src")) {
93125
- const img = elem.querySelector("img");
93126
- if (img) {
93127
- this.onPreview(this.editor, img);
93175
+ this.lastScrollSnapshots.forEach((snapshot2) => {
93176
+ var _a;
93177
+ if (snapshot2.isWindow) {
93178
+ window.scrollTo(snapshot2.scrollLeft, snapshot2.scrollTop);
93128
93179
  return;
93129
93180
  }
93130
- }
93131
- if (elem.tagName === "IMG" && ((_a = elem.parentElement) == null ? void 0 : _a.hasAttribute("data-src"))) {
93132
- this.onPreview(this.editor, elem);
93133
- }
93181
+ (_a = snapshot2.element) == null ? void 0 : _a.scrollTo(snapshot2.scrollLeft, snapshot2.scrollTop);
93182
+ });
93183
+ });
93184
+ __publicField(this, "scheduleRestoreScrollSnapshots", () => {
93185
+ this.restoreScrollSnapshots();
93186
+ window.requestAnimationFrame(() => {
93187
+ this.restoreScrollSnapshots();
93188
+ window.requestAnimationFrame(this.restoreScrollSnapshots);
93189
+ });
93190
+ window.setTimeout(this.restoreScrollSnapshots, 50);
93191
+ window.setTimeout(this.restoreScrollSnapshots, 200);
93192
+ });
93193
+ __publicField(this, "handleImagePreview", (e2) => {
93194
+ const candidates = this.getPreviewCandidateElements(e2);
93195
+ this.previewFromCandidates(candidates);
93134
93196
  });
93135
93197
  __publicField(this, "handleMouseUp", (e2) => {
93136
- if (!this.lastClickPosition)
93198
+ if (!this.lastClickPosition) {
93137
93199
  return;
93200
+ }
93138
93201
  const { x, y } = this.lastClickPosition;
93139
93202
  const deltaX = Math.abs(e2.x - x);
93140
93203
  const deltaY = Math.abs(e2.y - y);
93204
+ const previewCandidates = this.lastPreviewCandidates;
93141
93205
  this.lastClickPosition = void 0;
93206
+ this.lastPreviewCandidates = [];
93142
93207
  if (deltaX < safetyOffset && deltaY < safetyOffset) {
93208
+ this.scheduleRestoreScrollSnapshots();
93209
+ if (this.previewFromCandidates(previewCandidates)) {
93210
+ window.setTimeout(() => {
93211
+ this.lastScrollSnapshots = [];
93212
+ }, 250);
93213
+ return;
93214
+ }
93143
93215
  this.handleImagePreview(e2);
93216
+ this.scheduleRestoreScrollSnapshots();
93217
+ window.setTimeout(() => {
93218
+ this.lastScrollSnapshots = [];
93219
+ }, 250);
93220
+ return;
93144
93221
  }
93222
+ this.lastScrollSnapshots = [];
93145
93223
  });
93146
93224
  __publicField(this, "handleMouseDown", (e2) => {
93147
93225
  const { x, y } = e2;
@@ -93158,9 +93236,16 @@ ${data2.plantumlText}
93158
93236
  return;
93159
93237
  }
93160
93238
  this.lastClickPosition = { x, y };
93239
+ this.lastPreviewCandidates = this.getPreviewCandidateElements(e2);
93240
+ if (this.lastPreviewCandidates.some((candidate) => this.isPreviewableImageElement(candidate))) {
93241
+ this.captureScrollSnapshots();
93242
+ e2.preventDefault();
93243
+ e2.stopPropagation();
93244
+ this.scheduleRestoreScrollSnapshots();
93245
+ }
93161
93246
  });
93162
93247
  __publicField(this, "destroy", () => {
93163
- this.editor.rootElement.removeEventListener("mousedown", this.handleMouseDown);
93248
+ this.editor.rootElement.removeEventListener("mousedown", this.handleMouseDown, { capture: true });
93164
93249
  this.editor.rootElement.removeEventListener("mouseup", this.handleMouseUp);
93165
93250
  });
93166
93251
  this.editor = editor;
@@ -93168,6 +93253,131 @@ ${data2.plantumlText}
93168
93253
  editor.rootElement.addEventListener("mousedown", this.handleMouseDown, { capture: true });
93169
93254
  editor.rootElement.addEventListener("mouseup", this.handleMouseUp);
93170
93255
  }
93256
+ getPreviewCandidateElements(e2) {
93257
+ var _a, _b;
93258
+ const candidates = [];
93259
+ const addCandidate = (target) => {
93260
+ if (!(target instanceof HTMLElement)) {
93261
+ return;
93262
+ }
93263
+ if (!this.editor.rootElement.contains(target)) {
93264
+ return;
93265
+ }
93266
+ if (candidates.includes(target)) {
93267
+ return;
93268
+ }
93269
+ candidates.push(target);
93270
+ };
93271
+ const addElementStackHit = (target) => {
93272
+ if (!(target instanceof HTMLElement)) {
93273
+ return;
93274
+ }
93275
+ if (!this.editor.rootElement.contains(target)) {
93276
+ return;
93277
+ }
93278
+ addCandidate(getImagePreviewTarget(target));
93279
+ };
93280
+ if (typeof document !== "undefined") {
93281
+ (_a = document.elementsFromPoint) == null ? void 0 : _a.call(document, e2.clientX, e2.clientY).forEach(addElementStackHit);
93282
+ addElementStackHit(document.elementFromPoint(e2.clientX, e2.clientY));
93283
+ }
93284
+ this.editor.rootElement.querySelectorAll('img.editor-image, span[data-type="editor-box"][data-box-type="image"] img').forEach((elem) => {
93285
+ if (elem instanceof HTMLElement && isPointInElement(elem, e2.clientX, e2.clientY)) {
93286
+ addCandidate(elem);
93287
+ }
93288
+ });
93289
+ this.editor.rootElement.querySelectorAll(".image-container[data-src]").forEach((elem) => {
93290
+ if (elem instanceof HTMLElement && isPointInElement(elem, e2.clientX, e2.clientY)) {
93291
+ addCandidate(elem);
93292
+ }
93293
+ });
93294
+ addCandidate(e2.target);
93295
+ (_b = e2.composedPath) == null ? void 0 : _b.call(e2).forEach(addCandidate);
93296
+ return candidates;
93297
+ }
93298
+ previewFromImageBox(elem) {
93299
+ const imageBox = getParentBox(elem);
93300
+ const boxType = imageBox && getBoxTypeFromElement(imageBox);
93301
+ const img = elem instanceof HTMLImageElement ? elem : imageBox == null ? void 0 : imageBox.querySelector("img");
93302
+ if (!imageBox || boxType !== "image" || !img) {
93303
+ return false;
93304
+ }
93305
+ const imgData = this.editor.getBoxData(imageBox);
93306
+ if (!imgData.link) {
93307
+ this.onPreview(this.editor, img);
93308
+ }
93309
+ return true;
93310
+ }
93311
+ previewFromImageContainer(elem) {
93312
+ const imageContainer = elem.closest(".image-container[data-src]");
93313
+ if (!(imageContainer instanceof HTMLElement)) {
93314
+ return false;
93315
+ }
93316
+ if (!this.editor.rootElement.contains(imageContainer)) {
93317
+ return false;
93318
+ }
93319
+ const block = getParentBlock(imageContainer);
93320
+ if (block && isEmbedBlock(block) && getEmbedType(block) === "image") {
93321
+ const imageData = this.editor.getBlockData(block);
93322
+ if (imageData.link) {
93323
+ return true;
93324
+ }
93325
+ }
93326
+ const img = elem instanceof HTMLImageElement ? elem : imageContainer.querySelector("img");
93327
+ if (img) {
93328
+ this.onPreview(this.editor, img);
93329
+ }
93330
+ return true;
93331
+ }
93332
+ previewFromEmbedBlock(elem) {
93333
+ var _a;
93334
+ const block = getParentBlock(elem);
93335
+ if (!block) {
93336
+ return false;
93337
+ }
93338
+ if (!isEmbedBlock(block)) {
93339
+ return false;
93340
+ }
93341
+ if (getEmbedType(block) === "image") {
93342
+ const imageData = this.editor.getBlockData(block);
93343
+ if (imageData.link) {
93344
+ return true;
93345
+ }
93346
+ }
93347
+ if (elem.hasAttribute("data-src")) {
93348
+ const img = elem.querySelector("img");
93349
+ if (img) {
93350
+ this.onPreview(this.editor, img);
93351
+ }
93352
+ return true;
93353
+ }
93354
+ if (elem.tagName === "IMG" && ((_a = elem.parentElement) == null ? void 0 : _a.hasAttribute("data-src"))) {
93355
+ this.onPreview(this.editor, elem);
93356
+ return true;
93357
+ }
93358
+ return false;
93359
+ }
93360
+ isPreviewableImageElement(elem) {
93361
+ const imageBox = getParentBox(elem);
93362
+ const boxType = imageBox && getBoxTypeFromElement(imageBox);
93363
+ const imageBoxImg = elem instanceof HTMLImageElement ? elem : imageBox == null ? void 0 : imageBox.querySelector("img");
93364
+ if (imageBox && boxType === "image" && imageBoxImg) {
93365
+ const imgData = this.editor.getBoxData(imageBox);
93366
+ return !imgData.link;
93367
+ }
93368
+ const imageContainer = elem.closest(".image-container[data-src]");
93369
+ if (imageContainer instanceof HTMLElement) {
93370
+ const block = getParentBlock(imageContainer);
93371
+ if (block && isEmbedBlock(block) && getEmbedType(block) === "image") {
93372
+ const imageData = this.editor.getBlockData(block);
93373
+ if (imageData.link) {
93374
+ return false;
93375
+ }
93376
+ }
93377
+ return Boolean(elem instanceof HTMLImageElement ? elem : imageContainer.querySelector("img"));
93378
+ }
93379
+ return false;
93380
+ }
93171
93381
  }
93172
93382
  function createHistoryEditor(root2, doc2, options) {
93173
93383
  var _a, _b, _c, _d, _e;
@@ -96960,7 +97170,7 @@ ${JSON.stringify(error2, null, 2)}`);
96960
97170
  }
96961
97171
  }
96962
97172
  });
96963
- editor.version = "3.0.15-beta.2";
97173
+ editor.version = "3.0.15-beta.4";
96964
97174
  return editor;
96965
97175
  }
96966
97176
  function isDoc(doc2) {
@@ -97094,7 +97304,7 @@ ${JSON.stringify(error2, null, 2)}`);
97094
97304
  OnesEditorDropTarget.register(editor);
97095
97305
  OnesEditorTocProvider.register(editor);
97096
97306
  OnesEditorExclusiveBlock.register(editor);
97097
- editor.version = "3.0.15-beta.2";
97307
+ editor.version = "3.0.15-beta.4";
97098
97308
  return editor;
97099
97309
  }
97100
97310
  async function showDocVersions(editor, options, serverUrl) {
@@ -97229,7 +97439,7 @@ ${JSON.stringify(error2, null, 2)}`);
97229
97439
  }
97230
97440
  }
97231
97441
  });
97232
- editor.version = "3.0.15-beta.2";
97442
+ editor.version = "3.0.15-beta.4";
97233
97443
  return editor;
97234
97444
  }
97235
97445
  const emojis$1 = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ones-editor/editor",
3
- "version": "3.0.15-beta.2",
3
+ "version": "3.0.15-beta.4",
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",