@ones-editor/editor 3.0.15-beta.1 → 3.0.15-beta.3

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
@@ -28750,6 +28750,11 @@ var __publicField = (obj, key, value) => {
28750
28750
  addClass(editor.rootContainer, "select-all");
28751
28751
  selectParentContainer(editor, getFirstChildBlock(editor.rootContainer));
28752
28752
  }
28753
+ function editorClearAll(editor) {
28754
+ selectParentContainer(editor, getFirstChildBlock(editor.rootContainer));
28755
+ editorClearSelectedContents(editor);
28756
+ removeClass(editor.rootContainer, "select-all");
28757
+ }
28753
28758
  function editorSelectWord(editor) {
28754
28759
  const old = editor.selection.range.focus;
28755
28760
  if (!old.isSimple())
@@ -83151,6 +83156,9 @@ ${docStr}
83151
83156
  const blocks = this.docObject.blocks[containerId];
83152
83157
  const blockData = blocks[updateIndex];
83153
83158
  assert(logger$C, !blockData.deleted, `block has already deleted: ${containerId}/${blockIndex}`);
83159
+ if (!Array.isArray(blockData.text)) {
83160
+ blockData.text = [];
83161
+ }
83154
83162
  const text2 = blockData.text;
83155
83163
  assert(logger$C, text2, `no text for block: ${JSON.stringify(blockData)}`);
83156
83164
  const newText = updateBlockText(text2, actions2, user);
@@ -93085,58 +93093,138 @@ ${data2.plantumlText}
93085
93093
  StatusBox
93086
93094
  ];
93087
93095
  const safetyOffset = 2;
93096
+ function isPointInElement(elem, x, y) {
93097
+ const rect = elem.getBoundingClientRect();
93098
+ return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom;
93099
+ }
93100
+ function getImagePreviewTarget(elem) {
93101
+ if (elem instanceof HTMLImageElement && elem.classList.contains("editor-image")) {
93102
+ return elem;
93103
+ }
93104
+ const imageContainer = elem.closest(".image-container[data-src]");
93105
+ if (imageContainer instanceof HTMLElement) {
93106
+ const img = imageContainer.querySelector("img.editor-image");
93107
+ return img instanceof HTMLElement ? img : imageContainer;
93108
+ }
93109
+ const imageBox = elem.closest('span[data-type="editor-box"][data-box-type="image"]');
93110
+ if (imageBox instanceof HTMLElement) {
93111
+ const img = imageBox.querySelector("img");
93112
+ return img instanceof HTMLElement ? img : imageBox;
93113
+ }
93114
+ return null;
93115
+ }
93116
+ function getScrollSnapshots(elem) {
93117
+ const snapshots = [{
93118
+ element: null,
93119
+ isWindow: true,
93120
+ scrollTop: window.scrollY,
93121
+ scrollLeft: window.scrollX
93122
+ }];
93123
+ const added = /* @__PURE__ */ new Set();
93124
+ let current = elem;
93125
+ while (current) {
93126
+ const style2 = window.getComputedStyle(current);
93127
+ const overflow = `${style2.overflow}${style2.overflowY}${style2.overflowX}`;
93128
+ const canScroll = current.scrollHeight > current.clientHeight || current.scrollWidth > current.clientWidth;
93129
+ if (canScroll || /(auto|scroll|overlay)/.test(overflow)) {
93130
+ snapshots.push({
93131
+ element: current,
93132
+ isWindow: false,
93133
+ scrollTop: current.scrollTop,
93134
+ scrollLeft: current.scrollLeft
93135
+ });
93136
+ added.add(current);
93137
+ }
93138
+ current = current.parentElement;
93139
+ }
93140
+ const scrollingElement = document.scrollingElement;
93141
+ if (scrollingElement instanceof HTMLElement && !added.has(scrollingElement)) {
93142
+ snapshots.push({
93143
+ element: scrollingElement,
93144
+ isWindow: false,
93145
+ scrollTop: scrollingElement.scrollTop,
93146
+ scrollLeft: scrollingElement.scrollLeft
93147
+ });
93148
+ }
93149
+ return snapshots;
93150
+ }
93088
93151
  class ImagePreviewHandler {
93089
93152
  constructor(editor, onPreview) {
93090
93153
  __publicField(this, "lastClickPosition");
93091
- __publicField(this, "handleImagePreview", (e2) => {
93092
- var _a;
93093
- const elem = e2.target;
93094
- if (!(elem instanceof HTMLElement)) {
93095
- return;
93154
+ __publicField(this, "lastPreviewCandidates", []);
93155
+ __publicField(this, "lastScrollSnapshots", []);
93156
+ __publicField(this, "previewFromCandidates", (candidates) => {
93157
+ if (!candidates.length) {
93158
+ return false;
93096
93159
  }
93097
- const imageBox = getParentBox(elem);
93098
- const boxType = imageBox && getBoxTypeFromElement(imageBox);
93099
- if (imageBox && elem instanceof HTMLImageElement && boxType === "image") {
93100
- const imgData = this.editor.getBoxData(imageBox);
93101
- if (!imgData.link) {
93102
- this.onPreview(this.editor, elem);
93103
- return;
93160
+ for (const elem of candidates) {
93161
+ if (this.previewFromImageBox(elem)) {
93162
+ return true;
93163
+ }
93164
+ if (this.previewFromImageContainer(elem)) {
93165
+ return true;
93166
+ }
93167
+ if (this.previewFromEmbedBlock(elem)) {
93168
+ return true;
93104
93169
  }
93105
93170
  }
93106
- const block = getParentBlock(elem);
93107
- if (!block) {
93108
- return;
93109
- }
93110
- if (!isEmbedBlock(block)) {
93171
+ return false;
93172
+ });
93173
+ __publicField(this, "captureScrollSnapshots", () => {
93174
+ this.lastScrollSnapshots = getScrollSnapshots(this.editor.rootElement);
93175
+ });
93176
+ __publicField(this, "restoreScrollSnapshots", () => {
93177
+ if (!this.lastScrollSnapshots.length) {
93111
93178
  return;
93112
93179
  }
93113
- if (getEmbedType(block) === "image") {
93114
- const imageData = this.editor.getBlockData(block);
93115
- if (imageData.link) {
93116
- return;
93117
- }
93118
- }
93119
- if (elem.hasAttribute("data-src")) {
93120
- const img = elem.querySelector("img");
93121
- if (img) {
93122
- this.onPreview(this.editor, img);
93180
+ this.lastScrollSnapshots.forEach((snapshot2) => {
93181
+ var _a;
93182
+ if (snapshot2.isWindow) {
93183
+ window.scrollTo(snapshot2.scrollLeft, snapshot2.scrollTop);
93123
93184
  return;
93124
93185
  }
93125
- }
93126
- if (elem.tagName === "IMG" && ((_a = elem.parentElement) == null ? void 0 : _a.hasAttribute("data-src"))) {
93127
- this.onPreview(this.editor, elem);
93128
- }
93186
+ (_a = snapshot2.element) == null ? void 0 : _a.scrollTo(snapshot2.scrollLeft, snapshot2.scrollTop);
93187
+ });
93188
+ });
93189
+ __publicField(this, "scheduleRestoreScrollSnapshots", () => {
93190
+ this.restoreScrollSnapshots();
93191
+ window.requestAnimationFrame(() => {
93192
+ this.restoreScrollSnapshots();
93193
+ window.requestAnimationFrame(this.restoreScrollSnapshots);
93194
+ });
93195
+ window.setTimeout(this.restoreScrollSnapshots, 50);
93196
+ window.setTimeout(this.restoreScrollSnapshots, 200);
93197
+ });
93198
+ __publicField(this, "handleImagePreview", (e2) => {
93199
+ const candidates = this.getPreviewCandidateElements(e2);
93200
+ this.previewFromCandidates(candidates);
93129
93201
  });
93130
93202
  __publicField(this, "handleMouseUp", (e2) => {
93131
- if (!this.lastClickPosition)
93203
+ if (!this.lastClickPosition) {
93132
93204
  return;
93205
+ }
93133
93206
  const { x, y } = this.lastClickPosition;
93134
93207
  const deltaX = Math.abs(e2.x - x);
93135
93208
  const deltaY = Math.abs(e2.y - y);
93209
+ const previewCandidates = this.lastPreviewCandidates;
93136
93210
  this.lastClickPosition = void 0;
93211
+ this.lastPreviewCandidates = [];
93137
93212
  if (deltaX < safetyOffset && deltaY < safetyOffset) {
93213
+ this.scheduleRestoreScrollSnapshots();
93214
+ if (this.previewFromCandidates(previewCandidates)) {
93215
+ window.setTimeout(() => {
93216
+ this.lastScrollSnapshots = [];
93217
+ }, 250);
93218
+ return;
93219
+ }
93138
93220
  this.handleImagePreview(e2);
93221
+ this.scheduleRestoreScrollSnapshots();
93222
+ window.setTimeout(() => {
93223
+ this.lastScrollSnapshots = [];
93224
+ }, 250);
93225
+ return;
93139
93226
  }
93227
+ this.lastScrollSnapshots = [];
93140
93228
  });
93141
93229
  __publicField(this, "handleMouseDown", (e2) => {
93142
93230
  const { x, y } = e2;
@@ -93153,9 +93241,16 @@ ${data2.plantumlText}
93153
93241
  return;
93154
93242
  }
93155
93243
  this.lastClickPosition = { x, y };
93244
+ this.lastPreviewCandidates = this.getPreviewCandidateElements(e2);
93245
+ if (this.lastPreviewCandidates.some((candidate) => this.isPreviewableImageElement(candidate))) {
93246
+ this.captureScrollSnapshots();
93247
+ e2.preventDefault();
93248
+ e2.stopPropagation();
93249
+ this.scheduleRestoreScrollSnapshots();
93250
+ }
93156
93251
  });
93157
93252
  __publicField(this, "destroy", () => {
93158
- this.editor.rootElement.removeEventListener("mousedown", this.handleMouseDown);
93253
+ this.editor.rootElement.removeEventListener("mousedown", this.handleMouseDown, { capture: true });
93159
93254
  this.editor.rootElement.removeEventListener("mouseup", this.handleMouseUp);
93160
93255
  });
93161
93256
  this.editor = editor;
@@ -93163,6 +93258,131 @@ ${data2.plantumlText}
93163
93258
  editor.rootElement.addEventListener("mousedown", this.handleMouseDown, { capture: true });
93164
93259
  editor.rootElement.addEventListener("mouseup", this.handleMouseUp);
93165
93260
  }
93261
+ getPreviewCandidateElements(e2) {
93262
+ var _a, _b;
93263
+ const candidates = [];
93264
+ const addCandidate = (target) => {
93265
+ if (!(target instanceof HTMLElement)) {
93266
+ return;
93267
+ }
93268
+ if (!this.editor.rootElement.contains(target)) {
93269
+ return;
93270
+ }
93271
+ if (candidates.includes(target)) {
93272
+ return;
93273
+ }
93274
+ candidates.push(target);
93275
+ };
93276
+ const addElementStackHit = (target) => {
93277
+ if (!(target instanceof HTMLElement)) {
93278
+ return;
93279
+ }
93280
+ if (!this.editor.rootElement.contains(target)) {
93281
+ return;
93282
+ }
93283
+ addCandidate(getImagePreviewTarget(target));
93284
+ };
93285
+ if (typeof document !== "undefined") {
93286
+ (_a = document.elementsFromPoint) == null ? void 0 : _a.call(document, e2.clientX, e2.clientY).forEach(addElementStackHit);
93287
+ addElementStackHit(document.elementFromPoint(e2.clientX, e2.clientY));
93288
+ }
93289
+ this.editor.rootElement.querySelectorAll('img.editor-image, span[data-type="editor-box"][data-box-type="image"] img').forEach((elem) => {
93290
+ if (elem instanceof HTMLElement && isPointInElement(elem, e2.clientX, e2.clientY)) {
93291
+ addCandidate(elem);
93292
+ }
93293
+ });
93294
+ this.editor.rootElement.querySelectorAll(".image-container[data-src]").forEach((elem) => {
93295
+ if (elem instanceof HTMLElement && isPointInElement(elem, e2.clientX, e2.clientY)) {
93296
+ addCandidate(elem);
93297
+ }
93298
+ });
93299
+ addCandidate(e2.target);
93300
+ (_b = e2.composedPath) == null ? void 0 : _b.call(e2).forEach(addCandidate);
93301
+ return candidates;
93302
+ }
93303
+ previewFromImageBox(elem) {
93304
+ const imageBox = getParentBox(elem);
93305
+ const boxType = imageBox && getBoxTypeFromElement(imageBox);
93306
+ const img = elem instanceof HTMLImageElement ? elem : imageBox == null ? void 0 : imageBox.querySelector("img");
93307
+ if (!imageBox || boxType !== "image" || !img) {
93308
+ return false;
93309
+ }
93310
+ const imgData = this.editor.getBoxData(imageBox);
93311
+ if (!imgData.link) {
93312
+ this.onPreview(this.editor, img);
93313
+ }
93314
+ return true;
93315
+ }
93316
+ previewFromImageContainer(elem) {
93317
+ const imageContainer = elem.closest(".image-container[data-src]");
93318
+ if (!(imageContainer instanceof HTMLElement)) {
93319
+ return false;
93320
+ }
93321
+ if (!this.editor.rootElement.contains(imageContainer)) {
93322
+ return false;
93323
+ }
93324
+ const block = getParentBlock(imageContainer);
93325
+ if (block && isEmbedBlock(block) && getEmbedType(block) === "image") {
93326
+ const imageData = this.editor.getBlockData(block);
93327
+ if (imageData.link) {
93328
+ return true;
93329
+ }
93330
+ }
93331
+ const img = elem instanceof HTMLImageElement ? elem : imageContainer.querySelector("img");
93332
+ if (img) {
93333
+ this.onPreview(this.editor, img);
93334
+ }
93335
+ return true;
93336
+ }
93337
+ previewFromEmbedBlock(elem) {
93338
+ var _a;
93339
+ const block = getParentBlock(elem);
93340
+ if (!block) {
93341
+ return false;
93342
+ }
93343
+ if (!isEmbedBlock(block)) {
93344
+ return false;
93345
+ }
93346
+ if (getEmbedType(block) === "image") {
93347
+ const imageData = this.editor.getBlockData(block);
93348
+ if (imageData.link) {
93349
+ return true;
93350
+ }
93351
+ }
93352
+ if (elem.hasAttribute("data-src")) {
93353
+ const img = elem.querySelector("img");
93354
+ if (img) {
93355
+ this.onPreview(this.editor, img);
93356
+ }
93357
+ return true;
93358
+ }
93359
+ if (elem.tagName === "IMG" && ((_a = elem.parentElement) == null ? void 0 : _a.hasAttribute("data-src"))) {
93360
+ this.onPreview(this.editor, elem);
93361
+ return true;
93362
+ }
93363
+ return false;
93364
+ }
93365
+ isPreviewableImageElement(elem) {
93366
+ const imageBox = getParentBox(elem);
93367
+ const boxType = imageBox && getBoxTypeFromElement(imageBox);
93368
+ const imageBoxImg = elem instanceof HTMLImageElement ? elem : imageBox == null ? void 0 : imageBox.querySelector("img");
93369
+ if (imageBox && boxType === "image" && imageBoxImg) {
93370
+ const imgData = this.editor.getBoxData(imageBox);
93371
+ return !imgData.link;
93372
+ }
93373
+ const imageContainer = elem.closest(".image-container[data-src]");
93374
+ if (imageContainer instanceof HTMLElement) {
93375
+ const block = getParentBlock(imageContainer);
93376
+ if (block && isEmbedBlock(block) && getEmbedType(block) === "image") {
93377
+ const imageData = this.editor.getBlockData(block);
93378
+ if (imageData.link) {
93379
+ return false;
93380
+ }
93381
+ }
93382
+ return Boolean(elem instanceof HTMLImageElement ? elem : imageContainer.querySelector("img"));
93383
+ }
93384
+ return false;
93385
+ }
93166
93386
  }
93167
93387
  function createHistoryEditor(root2, doc2, options) {
93168
93388
  var _a, _b, _c, _d, _e;
@@ -96955,7 +97175,7 @@ ${JSON.stringify(error2, null, 2)}`);
96955
97175
  }
96956
97176
  }
96957
97177
  });
96958
- editor.version = "3.0.15-beta.1";
97178
+ editor.version = "3.0.15-beta.3";
96959
97179
  return editor;
96960
97180
  }
96961
97181
  function isDoc(doc2) {
@@ -97089,7 +97309,7 @@ ${JSON.stringify(error2, null, 2)}`);
97089
97309
  OnesEditorDropTarget.register(editor);
97090
97310
  OnesEditorTocProvider.register(editor);
97091
97311
  OnesEditorExclusiveBlock.register(editor);
97092
- editor.version = "3.0.15-beta.1";
97312
+ editor.version = "3.0.15-beta.3";
97093
97313
  return editor;
97094
97314
  }
97095
97315
  async function showDocVersions(editor, options, serverUrl) {
@@ -97224,7 +97444,7 @@ ${JSON.stringify(error2, null, 2)}`);
97224
97444
  }
97225
97445
  }
97226
97446
  });
97227
- editor.version = "3.0.15-beta.1";
97447
+ editor.version = "3.0.15-beta.3";
97228
97448
  return editor;
97229
97449
  }
97230
97450
  const emojis$1 = {
@@ -143375,6 +143595,7 @@ ${JSON.stringify(error2, null, 2)}`);
143375
143595
  exports2.editorAutoBreakTextBlock = editorAutoBreakTextBlock;
143376
143596
  exports2.editorAutoInsertBlock = editorAutoInsertBlock;
143377
143597
  exports2.editorBreakTextBlock = editorBreakTextBlock;
143598
+ exports2.editorClearAll = editorClearAll;
143378
143599
  exports2.editorClearCompositionText = editorClearCompositionText;
143379
143600
  exports2.editorClearSelectedContents = editorClearSelectedContents;
143380
143601
  exports2.editorCopyBlock = editorCopyBlock;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ones-editor/editor",
3
- "version": "3.0.15-beta.1",
3
+ "version": "3.0.15-beta.3",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "dependencies": {