@ones-editor/editor 3.0.12-beta.4 → 3.0.14-beta.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.
@@ -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
@@ -7798,38 +7798,6 @@ div.editor-root:not(.readonly) div.editor-content div[data-type=editor-container
7798
7798
  }
7799
7799
  div.editor-root.readonly div.editor-content div[data-type=editor-container] .embed-block[data-embed-type=drawio] [data-src] {
7800
7800
  cursor: zoom-in;
7801
- }
7802
- .command-item.menu-item[data-id=insert-drawio] {
7803
- width: 100%;
7804
- }
7805
- .command-item.menu-item[data-id=insert-drawio] .uml-menu-item {
7806
- color: #2d2d2e;
7807
- user-select: none;
7808
- transition: background 20ms ease-in 0s;
7809
- width: 100%;
7810
- height: 32px;
7811
- padding: 6px 10px;
7812
- display: flex;
7813
- align-items: center;
7814
- box-sizing: border-box;
7815
- }
7816
- .command-item.menu-item[data-id=insert-drawio] .uml-menu-item .menu-item-icon {
7817
- display: flex;
7818
- align-items: center;
7819
- justify-content: center;
7820
- height: 24px;
7821
- width: 24px;
7822
- margin-right: 5px;
7823
- margin-top: -2px;
7824
- display: flex;
7825
- flex-direction: column;
7826
- justify-content: center;
7827
- }
7828
- .command-item.menu-item[data-id=insert-drawio] .uml-menu-item .menu-item-name {
7829
- white-space: nowrap;
7830
- flex-grow: 1;
7831
- display: flex;
7832
- align-items: center;
7833
7801
  }.embed-block[data-embed-type=mermaid],
7834
7802
  .embed-block[data-embed-type=flowchart],
7835
7803
  .embed-block[data-embed-type=plantuml] {
@@ -41832,9 +41800,7 @@ ${codeText}
41832
41800
  }
41833
41801
  }
41834
41802
  if (item.element) {
41835
- if (!item.useDefaultClickHandler) {
41836
- return;
41837
- }
41803
+ return;
41838
41804
  }
41839
41805
  this.emit("click", this, item, elem);
41840
41806
  if (item.clickToClose === false) {
@@ -83112,6 +83078,9 @@ ${docStr}
83112
83078
  const blocks = this.docObject.blocks[containerId];
83113
83079
  const blockData = blocks[updateIndex];
83114
83080
  assert(logger$C, !blockData.deleted, `block has already deleted: ${containerId}/${blockIndex}`);
83081
+ if (!Array.isArray(blockData.text)) {
83082
+ blockData.text = [];
83083
+ }
83115
83084
  const text2 = blockData.text;
83116
83085
  assert(logger$C, text2, `no text for block: ${JSON.stringify(blockData)}`);
83117
83086
  const newText = updateBlockText(text2, actions2, user);
@@ -88431,12 +88400,7 @@ ${data2.flowchartText}
88431
88400
  function getBlockProperties$3(editor, block) {
88432
88401
  const properties = getStandardEmbedBlockProperties(editor, block, {
88433
88402
  handleExecuteCommand: (editor2, block2, item) => {
88434
- var _a, _b;
88435
88403
  if (item.id === "edit") {
88436
- const drawioOptions = getDrawioOptions(editor2);
88437
- if (((_b = (_a = drawioOptions.callbacks) == null ? void 0 : _a.onBeforeInsertDrawio) == null ? void 0 : _b.call(_a, editor2)) === false) {
88438
- return true;
88439
- }
88440
88404
  const data2 = editor2.getBlockData(block2).embedData;
88441
88405
  editGraph(editor2, block2, data2, false);
88442
88406
  return true;
@@ -88624,51 +88588,30 @@ ${data2.flowchartText}
88624
88588
  update(editor, embed, data2);
88625
88589
  return true;
88626
88590
  }
88627
- function getOptions$3(editor) {
88628
- var _a;
88629
- const drawioOptions = getDrawioOptions(editor);
88630
- const suffixElem = (_a = drawioOptions.menuItemSuffix) == null ? void 0 : _a.call(drawioOptions, editor);
88631
- const element = suffixElem ? () => {
88632
- const root2 = createElement("div", ["uml-menu-item"], null);
88633
- const iconElem = createElement("div", ["menu-item-icon"], root2);
88634
- iconElem.innerHTML = UmlIcon;
88635
- createElement("div", ["menu-item-name"], root2, i18n$1.t("drawio.title"));
88636
- root2.appendChild(suffixElem);
88637
- return root2;
88638
- } : void 0;
88639
- const insertEmbedCommandItems = [];
88640
- if (drawioOptions.visible !== false) {
88641
- insertEmbedCommandItems.push({
88591
+ function getOptions$3() {
88592
+ return {
88593
+ name: "UML",
88594
+ insertEmbedCommandItems: [{
88642
88595
  id: "insert-drawio",
88643
88596
  name: i18n$1.t("drawio.title"),
88644
88597
  icon: UmlIcon,
88645
88598
  group: "common",
88646
88599
  order: 600,
88647
- subText: getShortcutById("insert-drawio"),
88648
- element,
88649
- useDefaultClickHandler: true
88650
- });
88651
- }
88652
- return {
88653
- name: "UML",
88654
- insertEmbedCommandItems,
88655
- handleInsertEmptyEmbed: async (editor2, options) => {
88656
- var _a2, _b;
88657
- if (((_b = (_a2 = drawioOptions.callbacks) == null ? void 0 : _a2.onBeforeInsertDrawio) == null ? void 0 : _b.call(_a2, editor2)) === false) {
88658
- return null;
88659
- }
88600
+ subText: getShortcutById("insert-drawio")
88601
+ }],
88602
+ handleInsertEmptyEmbed: async (editor, options) => {
88660
88603
  const embedData = {
88661
88604
  src: "",
88662
88605
  xml: "",
88663
88606
  xmlSvg: ""
88664
88607
  };
88665
- const block = editor2.insertEmbed(options.containerId, options.blockIndex, "drawio", embedData);
88608
+ const block = editor.insertEmbed(options.containerId, options.blockIndex, "drawio", embedData);
88666
88609
  setTimeout(() => {
88667
88610
  const blockContent = getBlockContent(block);
88668
88611
  const embed = blockContent.querySelector(":scope > .editor-drawio-content");
88669
88612
  assert(logger$i, embed, "embed should not be null");
88670
- const blockData = editor2.getBlockData(block);
88671
- editDrawIO(editor2, block, embed, blockData.embedData);
88613
+ const blockData = editor.getBlockData(block);
88614
+ editDrawIO(editor, block, embed, blockData.embedData);
88672
88615
  }, 100);
88673
88616
  return block;
88674
88617
  }
@@ -93046,58 +92989,138 @@ ${data2.plantumlText}
93046
92989
  StatusBox
93047
92990
  ];
93048
92991
  const safetyOffset = 2;
92992
+ function isPointInElement(elem, x, y) {
92993
+ const rect = elem.getBoundingClientRect();
92994
+ return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom;
92995
+ }
92996
+ function getImagePreviewTarget(elem) {
92997
+ if (elem instanceof HTMLImageElement && elem.classList.contains("editor-image")) {
92998
+ return elem;
92999
+ }
93000
+ const imageContainer = elem.closest(".image-container[data-src]");
93001
+ if (imageContainer instanceof HTMLElement) {
93002
+ const img = imageContainer.querySelector("img.editor-image");
93003
+ return img instanceof HTMLElement ? img : imageContainer;
93004
+ }
93005
+ const imageBox = elem.closest('span[data-type="editor-box"][data-box-type="image"]');
93006
+ if (imageBox instanceof HTMLElement) {
93007
+ const img = imageBox.querySelector("img");
93008
+ return img instanceof HTMLElement ? img : imageBox;
93009
+ }
93010
+ return null;
93011
+ }
93012
+ function getScrollSnapshots(elem) {
93013
+ const snapshots = [{
93014
+ element: null,
93015
+ isWindow: true,
93016
+ scrollTop: window.scrollY,
93017
+ scrollLeft: window.scrollX
93018
+ }];
93019
+ const added = /* @__PURE__ */ new Set();
93020
+ let current = elem;
93021
+ while (current) {
93022
+ const style2 = window.getComputedStyle(current);
93023
+ const overflow = `${style2.overflow}${style2.overflowY}${style2.overflowX}`;
93024
+ const canScroll = current.scrollHeight > current.clientHeight || current.scrollWidth > current.clientWidth;
93025
+ if (canScroll || /(auto|scroll|overlay)/.test(overflow)) {
93026
+ snapshots.push({
93027
+ element: current,
93028
+ isWindow: false,
93029
+ scrollTop: current.scrollTop,
93030
+ scrollLeft: current.scrollLeft
93031
+ });
93032
+ added.add(current);
93033
+ }
93034
+ current = current.parentElement;
93035
+ }
93036
+ const scrollingElement = document.scrollingElement;
93037
+ if (scrollingElement instanceof HTMLElement && !added.has(scrollingElement)) {
93038
+ snapshots.push({
93039
+ element: scrollingElement,
93040
+ isWindow: false,
93041
+ scrollTop: scrollingElement.scrollTop,
93042
+ scrollLeft: scrollingElement.scrollLeft
93043
+ });
93044
+ }
93045
+ return snapshots;
93046
+ }
93049
93047
  class ImagePreviewHandler {
93050
93048
  constructor(editor, onPreview) {
93051
93049
  __publicField(this, "lastClickPosition");
93052
- __publicField(this, "handleImagePreview", (e2) => {
93053
- var _a;
93054
- const elem = e2.target;
93055
- if (!(elem instanceof HTMLElement)) {
93056
- return;
93050
+ __publicField(this, "lastPreviewCandidates", []);
93051
+ __publicField(this, "lastScrollSnapshots", []);
93052
+ __publicField(this, "previewFromCandidates", (candidates) => {
93053
+ if (!candidates.length) {
93054
+ return false;
93057
93055
  }
93058
- const imageBox = getParentBox(elem);
93059
- const boxType = imageBox && getBoxTypeFromElement(imageBox);
93060
- if (imageBox && elem instanceof HTMLImageElement && boxType === "image") {
93061
- const imgData = this.editor.getBoxData(imageBox);
93062
- if (!imgData.link) {
93063
- this.onPreview(this.editor, elem);
93064
- return;
93056
+ for (const elem of candidates) {
93057
+ if (this.previewFromImageBox(elem)) {
93058
+ return true;
93059
+ }
93060
+ if (this.previewFromImageContainer(elem)) {
93061
+ return true;
93062
+ }
93063
+ if (this.previewFromEmbedBlock(elem)) {
93064
+ return true;
93065
93065
  }
93066
93066
  }
93067
- const block = getParentBlock(elem);
93068
- if (!block) {
93069
- return;
93070
- }
93071
- if (!isEmbedBlock(block)) {
93067
+ return false;
93068
+ });
93069
+ __publicField(this, "captureScrollSnapshots", () => {
93070
+ this.lastScrollSnapshots = getScrollSnapshots(this.editor.rootElement);
93071
+ });
93072
+ __publicField(this, "restoreScrollSnapshots", () => {
93073
+ if (!this.lastScrollSnapshots.length) {
93072
93074
  return;
93073
93075
  }
93074
- if (getEmbedType(block) === "image") {
93075
- const imageData = this.editor.getBlockData(block);
93076
- if (imageData.link) {
93077
- return;
93078
- }
93079
- }
93080
- if (elem.hasAttribute("data-src")) {
93081
- const img = elem.querySelector("img");
93082
- if (img) {
93083
- this.onPreview(this.editor, img);
93076
+ this.lastScrollSnapshots.forEach((snapshot2) => {
93077
+ var _a;
93078
+ if (snapshot2.isWindow) {
93079
+ window.scrollTo(snapshot2.scrollLeft, snapshot2.scrollTop);
93084
93080
  return;
93085
93081
  }
93086
- }
93087
- if (elem.tagName === "IMG" && ((_a = elem.parentElement) == null ? void 0 : _a.hasAttribute("data-src"))) {
93088
- this.onPreview(this.editor, elem);
93089
- }
93082
+ (_a = snapshot2.element) == null ? void 0 : _a.scrollTo(snapshot2.scrollLeft, snapshot2.scrollTop);
93083
+ });
93084
+ });
93085
+ __publicField(this, "scheduleRestoreScrollSnapshots", () => {
93086
+ this.restoreScrollSnapshots();
93087
+ window.requestAnimationFrame(() => {
93088
+ this.restoreScrollSnapshots();
93089
+ window.requestAnimationFrame(this.restoreScrollSnapshots);
93090
+ });
93091
+ window.setTimeout(this.restoreScrollSnapshots, 50);
93092
+ window.setTimeout(this.restoreScrollSnapshots, 200);
93093
+ });
93094
+ __publicField(this, "handleImagePreview", (e2) => {
93095
+ const candidates = this.getPreviewCandidateElements(e2);
93096
+ this.previewFromCandidates(candidates);
93090
93097
  });
93091
93098
  __publicField(this, "handleMouseUp", (e2) => {
93092
- if (!this.lastClickPosition)
93099
+ if (!this.lastClickPosition) {
93093
93100
  return;
93101
+ }
93094
93102
  const { x, y } = this.lastClickPosition;
93095
93103
  const deltaX = Math.abs(e2.x - x);
93096
93104
  const deltaY = Math.abs(e2.y - y);
93105
+ const previewCandidates = this.lastPreviewCandidates;
93097
93106
  this.lastClickPosition = void 0;
93107
+ this.lastPreviewCandidates = [];
93098
93108
  if (deltaX < safetyOffset && deltaY < safetyOffset) {
93109
+ this.scheduleRestoreScrollSnapshots();
93110
+ if (this.previewFromCandidates(previewCandidates)) {
93111
+ window.setTimeout(() => {
93112
+ this.lastScrollSnapshots = [];
93113
+ }, 250);
93114
+ return;
93115
+ }
93099
93116
  this.handleImagePreview(e2);
93117
+ this.scheduleRestoreScrollSnapshots();
93118
+ window.setTimeout(() => {
93119
+ this.lastScrollSnapshots = [];
93120
+ }, 250);
93121
+ return;
93100
93122
  }
93123
+ this.lastScrollSnapshots = [];
93101
93124
  });
93102
93125
  __publicField(this, "handleMouseDown", (e2) => {
93103
93126
  const { x, y } = e2;
@@ -93114,9 +93137,16 @@ ${data2.plantumlText}
93114
93137
  return;
93115
93138
  }
93116
93139
  this.lastClickPosition = { x, y };
93140
+ this.lastPreviewCandidates = this.getPreviewCandidateElements(e2);
93141
+ if (this.lastPreviewCandidates.some((candidate) => this.isPreviewableImageElement(candidate))) {
93142
+ this.captureScrollSnapshots();
93143
+ e2.preventDefault();
93144
+ e2.stopPropagation();
93145
+ this.scheduleRestoreScrollSnapshots();
93146
+ }
93117
93147
  });
93118
93148
  __publicField(this, "destroy", () => {
93119
- this.editor.rootElement.removeEventListener("mousedown", this.handleMouseDown);
93149
+ this.editor.rootElement.removeEventListener("mousedown", this.handleMouseDown, { capture: true });
93120
93150
  this.editor.rootElement.removeEventListener("mouseup", this.handleMouseUp);
93121
93151
  });
93122
93152
  this.editor = editor;
@@ -93124,6 +93154,131 @@ ${data2.plantumlText}
93124
93154
  editor.rootElement.addEventListener("mousedown", this.handleMouseDown, { capture: true });
93125
93155
  editor.rootElement.addEventListener("mouseup", this.handleMouseUp);
93126
93156
  }
93157
+ getPreviewCandidateElements(e2) {
93158
+ var _a, _b;
93159
+ const candidates = [];
93160
+ const addCandidate = (target) => {
93161
+ if (!(target instanceof HTMLElement)) {
93162
+ return;
93163
+ }
93164
+ if (!this.editor.rootElement.contains(target)) {
93165
+ return;
93166
+ }
93167
+ if (candidates.includes(target)) {
93168
+ return;
93169
+ }
93170
+ candidates.push(target);
93171
+ };
93172
+ const addElementStackHit = (target) => {
93173
+ if (!(target instanceof HTMLElement)) {
93174
+ return;
93175
+ }
93176
+ if (!this.editor.rootElement.contains(target)) {
93177
+ return;
93178
+ }
93179
+ addCandidate(getImagePreviewTarget(target));
93180
+ };
93181
+ if (typeof document !== "undefined") {
93182
+ (_a = document.elementsFromPoint) == null ? void 0 : _a.call(document, e2.clientX, e2.clientY).forEach(addElementStackHit);
93183
+ addElementStackHit(document.elementFromPoint(e2.clientX, e2.clientY));
93184
+ }
93185
+ this.editor.rootElement.querySelectorAll('img.editor-image, span[data-type="editor-box"][data-box-type="image"] img').forEach((elem) => {
93186
+ if (elem instanceof HTMLElement && isPointInElement(elem, e2.clientX, e2.clientY)) {
93187
+ addCandidate(elem);
93188
+ }
93189
+ });
93190
+ this.editor.rootElement.querySelectorAll(".image-container[data-src]").forEach((elem) => {
93191
+ if (elem instanceof HTMLElement && isPointInElement(elem, e2.clientX, e2.clientY)) {
93192
+ addCandidate(elem);
93193
+ }
93194
+ });
93195
+ addCandidate(e2.target);
93196
+ (_b = e2.composedPath) == null ? void 0 : _b.call(e2).forEach(addCandidate);
93197
+ return candidates;
93198
+ }
93199
+ previewFromImageBox(elem) {
93200
+ const imageBox = getParentBox(elem);
93201
+ const boxType = imageBox && getBoxTypeFromElement(imageBox);
93202
+ const img = elem instanceof HTMLImageElement ? elem : imageBox == null ? void 0 : imageBox.querySelector("img");
93203
+ if (!imageBox || boxType !== "image" || !img) {
93204
+ return false;
93205
+ }
93206
+ const imgData = this.editor.getBoxData(imageBox);
93207
+ if (!imgData.link) {
93208
+ this.onPreview(this.editor, img);
93209
+ }
93210
+ return true;
93211
+ }
93212
+ previewFromImageContainer(elem) {
93213
+ const imageContainer = elem.closest(".image-container[data-src]");
93214
+ if (!(imageContainer instanceof HTMLElement)) {
93215
+ return false;
93216
+ }
93217
+ if (!this.editor.rootElement.contains(imageContainer)) {
93218
+ return false;
93219
+ }
93220
+ const block = getParentBlock(imageContainer);
93221
+ if (block && isEmbedBlock(block) && getEmbedType(block) === "image") {
93222
+ const imageData = this.editor.getBlockData(block);
93223
+ if (imageData.link) {
93224
+ return true;
93225
+ }
93226
+ }
93227
+ const img = elem instanceof HTMLImageElement ? elem : imageContainer.querySelector("img");
93228
+ if (img) {
93229
+ this.onPreview(this.editor, img);
93230
+ }
93231
+ return true;
93232
+ }
93233
+ previewFromEmbedBlock(elem) {
93234
+ var _a;
93235
+ const block = getParentBlock(elem);
93236
+ if (!block) {
93237
+ return false;
93238
+ }
93239
+ if (!isEmbedBlock(block)) {
93240
+ return false;
93241
+ }
93242
+ if (getEmbedType(block) === "image") {
93243
+ const imageData = this.editor.getBlockData(block);
93244
+ if (imageData.link) {
93245
+ return true;
93246
+ }
93247
+ }
93248
+ if (elem.hasAttribute("data-src")) {
93249
+ const img = elem.querySelector("img");
93250
+ if (img) {
93251
+ this.onPreview(this.editor, img);
93252
+ }
93253
+ return true;
93254
+ }
93255
+ if (elem.tagName === "IMG" && ((_a = elem.parentElement) == null ? void 0 : _a.hasAttribute("data-src"))) {
93256
+ this.onPreview(this.editor, elem);
93257
+ return true;
93258
+ }
93259
+ return false;
93260
+ }
93261
+ isPreviewableImageElement(elem) {
93262
+ const imageBox = getParentBox(elem);
93263
+ const boxType = imageBox && getBoxTypeFromElement(imageBox);
93264
+ const imageBoxImg = elem instanceof HTMLImageElement ? elem : imageBox == null ? void 0 : imageBox.querySelector("img");
93265
+ if (imageBox && boxType === "image" && imageBoxImg) {
93266
+ const imgData = this.editor.getBoxData(imageBox);
93267
+ return !imgData.link;
93268
+ }
93269
+ const imageContainer = elem.closest(".image-container[data-src]");
93270
+ if (imageContainer instanceof HTMLElement) {
93271
+ const block = getParentBlock(imageContainer);
93272
+ if (block && isEmbedBlock(block) && getEmbedType(block) === "image") {
93273
+ const imageData = this.editor.getBlockData(block);
93274
+ if (imageData.link) {
93275
+ return false;
93276
+ }
93277
+ }
93278
+ return Boolean(elem instanceof HTMLImageElement ? elem : imageContainer.querySelector("img"));
93279
+ }
93280
+ return false;
93281
+ }
93127
93282
  }
93128
93283
  function createHistoryEditor(root2, doc2, options) {
93129
93284
  var _a, _b, _c, _d, _e;
@@ -96917,7 +97072,7 @@ ${JSON.stringify(error2, null, 2)}`);
96917
97072
  }
96918
97073
  }
96919
97074
  });
96920
- editor.version = "3.0.12-beta.4";
97075
+ editor.version = "3.0.14-beta.1";
96921
97076
  return editor;
96922
97077
  }
96923
97078
  function isDoc(doc2) {
@@ -97051,7 +97206,7 @@ ${JSON.stringify(error2, null, 2)}`);
97051
97206
  OnesEditorDropTarget.register(editor);
97052
97207
  OnesEditorTocProvider.register(editor);
97053
97208
  OnesEditorExclusiveBlock.register(editor);
97054
- editor.version = "3.0.12-beta.4";
97209
+ editor.version = "3.0.14-beta.1";
97055
97210
  return editor;
97056
97211
  }
97057
97212
  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.12-beta.4",
3
+ "version": "3.0.14-beta.1",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "dependencies": {