@ones-editor/editor 4.0.7-beta.ONES22303263.1 → 4.0.7-beta.ONES22303310.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
@@ -26485,6 +26485,114 @@ var __publicField = (obj, key, value) => {
26485
26485
  };
26486
26486
  }
26487
26487
  }
26488
+ const controllers = /* @__PURE__ */ new WeakMap();
26489
+ const noop$1 = () => {
26490
+ };
26491
+ class FocusRestoration {
26492
+ constructor(input2, restore) {
26493
+ __publicField(this, "generation", 0);
26494
+ __publicField(this, "owner", null);
26495
+ __publicField(this, "destroyed", false);
26496
+ __publicField(this, "activeScope");
26497
+ __publicField(this, "timers", /* @__PURE__ */ new Set());
26498
+ this.input = input2;
26499
+ this.restore = restore;
26500
+ controllers.set(input2, this);
26501
+ }
26502
+ canFocus() {
26503
+ var _a;
26504
+ return !this.destroyed && !this.owner && !((_a = this.activeScope) == null ? void 0 : _a.cancelled) && this.input.isConnected;
26505
+ }
26506
+ hasOtherInput() {
26507
+ const active = this.input.ownerDocument.activeElement;
26508
+ return active !== this.input && !!(active == null ? void 0 : active.matches('input, textarea, select, [contenteditable="true"], [contenteditable=""]'));
26509
+ }
26510
+ schedule(callback, delay = 0, protectUserFocus = false) {
26511
+ var _a;
26512
+ return this.scheduleInScope(callback, delay, protectUserFocus, (_a = this.activeScope) != null ? _a : this.createScope());
26513
+ }
26514
+ createScope() {
26515
+ return { cancelled: false, timers: /* @__PURE__ */ new Set() };
26516
+ }
26517
+ scheduleInScope(callback, delay, protectUserFocus, scope) {
26518
+ if (scope.cancelled || !this.canFocus()) {
26519
+ return noop$1;
26520
+ }
26521
+ const generation = this.generation;
26522
+ const timer = setTimeout(() => {
26523
+ this.timers.delete(timer);
26524
+ scope.timers.delete(timer);
26525
+ if (!scope.cancelled && generation === this.generation && this.canFocus() && (!protectUserFocus || !this.hasOtherInput())) {
26526
+ const previousScope = this.activeScope;
26527
+ this.activeScope = scope;
26528
+ try {
26529
+ callback();
26530
+ } finally {
26531
+ this.activeScope = previousScope;
26532
+ }
26533
+ }
26534
+ }, delay);
26535
+ this.timers.add(timer);
26536
+ scope.timers.add(timer);
26537
+ return () => {
26538
+ scope.cancelled = true;
26539
+ scope.timers.forEach((pending) => {
26540
+ clearTimeout(pending);
26541
+ this.timers.delete(pending);
26542
+ });
26543
+ scope.timers.clear();
26544
+ };
26545
+ }
26546
+ request(delay = 0) {
26547
+ return this.scheduleInScope(this.restore, delay, !clientType.isMobile, this.createScope());
26548
+ }
26549
+ cancelPending() {
26550
+ if (this.activeScope) {
26551
+ this.activeScope.cancelled = true;
26552
+ }
26553
+ this.generation += 1;
26554
+ this.timers.forEach((timer) => clearTimeout(timer));
26555
+ this.timers.clear();
26556
+ }
26557
+ acquire() {
26558
+ this.cancelPending();
26559
+ const owner = {};
26560
+ this.owner = owner;
26561
+ return () => {
26562
+ if (this.owner === owner) {
26563
+ this.cancelPending();
26564
+ this.owner = null;
26565
+ }
26566
+ };
26567
+ }
26568
+ destroy() {
26569
+ this.destroyed = true;
26570
+ this.cancelPending();
26571
+ this.owner = null;
26572
+ controllers.delete(this.input);
26573
+ }
26574
+ }
26575
+ function requestInputFocus(input2, delay = 0) {
26576
+ var _a, _b;
26577
+ return (_b = (_a = controllers.get(input2)) == null ? void 0 : _a.request(delay)) != null ? _b : noop$1;
26578
+ }
26579
+ function requestEditorFocus(editor, delay = 0) {
26580
+ if (editor.destroyed) {
26581
+ return noop$1;
26582
+ }
26583
+ if (clientType.isMobile && delay === 0) {
26584
+ editor.focus();
26585
+ return noop$1;
26586
+ }
26587
+ return requestInputFocus(editor.input.inputElement, delay);
26588
+ }
26589
+ function acquireEditorFocus(editor) {
26590
+ var _a, _b;
26591
+ if (clientType.isMobile || editor.destroyed) {
26592
+ return noop$1;
26593
+ }
26594
+ return (_b = (_a = controllers.get(editor.input.inputElement)) == null ? void 0 : _a.acquire()) != null ? _b : noop$1;
26595
+ }
26488
26596
  const logger$3V = getLogger("editor-input");
26489
26597
  class EditorInput$1 {
26490
26598
  constructor(editor, callbacks) {
@@ -26494,6 +26602,7 @@ var __publicField = (obj, key, value) => {
26494
26602
  __publicField(this, "callbacks");
26495
26603
  __publicField(this, "inputElement");
26496
26604
  __publicField(this, "focused", false);
26605
+ __publicField(this, "focusRestoration");
26497
26606
  __publicField(this, "handleBlur", () => {
26498
26607
  this.editor.emit("blur", this.editor);
26499
26608
  this.callbacks.onBlur();
@@ -26515,12 +26624,20 @@ var __publicField = (obj, key, value) => {
26515
26624
  const isInDisableScrollMask = this.isInDisableScrollMask(target);
26516
26625
  if (target && (isInDisableScrollMask || rootElement === target || rootElement.contains(target))) {
26517
26626
  if (!isInDisableScrollMask && !isInBlockTools(target)) {
26518
- setTimeout(() => {
26627
+ const focus = () => {
26628
+ if (this.editor.destroyed || !this.focusRestoration.canFocus()) {
26629
+ return;
26630
+ }
26519
26631
  this.editor.emit("focus", this.editor);
26520
26632
  this.callbacks.onFocus();
26521
26633
  this.editor.input.focus();
26522
26634
  this.focused = true;
26523
- });
26635
+ };
26636
+ if (clientType.isMobile) {
26637
+ setTimeout(focus);
26638
+ } else {
26639
+ this.focusRestoration.schedule(focus);
26640
+ }
26524
26641
  }
26525
26642
  return;
26526
26643
  }
@@ -26655,6 +26772,11 @@ var __publicField = (obj, key, value) => {
26655
26772
  input2.addEventListener("compositionstart", this.handleCompositionstart);
26656
26773
  input2.addEventListener("compositionend", this.handleCompositionend);
26657
26774
  this.inputElement = input2;
26775
+ this.focusRestoration = new FocusRestoration(input2, () => {
26776
+ if (!this.editor.destroyed) {
26777
+ this.editor.focus(clientType.isMobile ? void 0 : { preventScroll: true });
26778
+ }
26779
+ });
26658
26780
  if (clientType.isMobile) {
26659
26781
  document.addEventListener("touchend", this.handleDocumentClick);
26660
26782
  } else {
@@ -26677,6 +26799,7 @@ var __publicField = (obj, key, value) => {
26677
26799
  return !!(elem == null ? void 0 : elem.closest("div[data-command-bar-id]")) || !!(elem == null ? void 0 : elem.closest("[data-keep-editor-focus]")) || !!(elem == null ? void 0 : elem.closest(".data-keep-editor-focus"));
26678
26800
  }
26679
26801
  destroy() {
26802
+ this.focusRestoration.destroy();
26680
26803
  this.callbacks.destroy();
26681
26804
  if (clientType.isMobile) {
26682
26805
  document.removeEventListener("touchend", this.handleDocumentClick);
@@ -26701,24 +26824,36 @@ var __publicField = (obj, key, value) => {
26701
26824
  return this.inputElement;
26702
26825
  }
26703
26826
  focus(options) {
26827
+ if (this.editor.destroyed || !this.focusRestoration.canFocus()) {
26828
+ return;
26829
+ }
26704
26830
  if (!this.editor.selection.caret.visible()) {
26705
26831
  this.editor.selection.caret.update();
26706
26832
  }
26707
26833
  if (clientType.isMobile) {
26708
26834
  this.inputElement.focus(options);
26709
26835
  this.callbacks.onFocus();
26836
+ this.focused = true;
26710
26837
  } else {
26711
- setTimeout(() => {
26838
+ this.focusRestoration.schedule(() => {
26839
+ if (this.editor.destroyed) {
26840
+ return;
26841
+ }
26712
26842
  if (document.activeElement !== this.inputElement) {
26713
- setTimeout(() => {
26714
- this.editor.emit("focus", this.editor);
26843
+ this.focusRestoration.schedule(() => {
26844
+ if (!this.editor.destroyed && document.activeElement === this.inputElement) {
26845
+ this.editor.emit("focus", this.editor);
26846
+ }
26715
26847
  });
26716
26848
  }
26717
26849
  this.inputElement.focus(options);
26850
+ if (this.editor.destroyed || !this.focusRestoration.canFocus()) {
26851
+ return;
26852
+ }
26718
26853
  this.callbacks.onFocus();
26719
- });
26854
+ this.focused = true;
26855
+ }, 0, (options == null ? void 0 : options.preventScroll) === true);
26720
26856
  }
26721
- this.focused = true;
26722
26857
  }
26723
26858
  isInDisableScrollMask(target) {
26724
26859
  let elem = target;
@@ -32282,7 +32417,7 @@ ${codeText}
32282
32417
  editor.doc.endBatchUpdate();
32283
32418
  }
32284
32419
  if (!clientType.isMobile) {
32285
- editor.focus();
32420
+ requestEditorFocus(editor);
32286
32421
  }
32287
32422
  return rangeResult;
32288
32423
  });
@@ -42594,7 +42729,9 @@ ${codeText}
42594
42729
  });
42595
42730
  __publicField(this, "tippyInstance", null);
42596
42731
  __publicField(this, "_target", null);
42732
+ __publicField(this, "cancelFocus");
42597
42733
  __publicField(this, "focusTimer", null);
42734
+ __publicField(this, "focusRestoreGeneration", 0);
42598
42735
  __publicField(this, "handleBeforeClose", (event) => {
42599
42736
  var _a;
42600
42737
  const focusOwner = this.getFocusOwner();
@@ -42614,6 +42751,9 @@ ${codeText}
42614
42751
  this.clearFocusTimer();
42615
42752
  }
42616
42753
  });
42754
+ __publicField(this, "handleWindowBlur", () => {
42755
+ this.clearFocusTimer();
42756
+ });
42617
42757
  __publicField(this, "setProps", (props) => {
42618
42758
  var _a;
42619
42759
  (_a = this.tippyInstance) == null ? void 0 : _a.setProps(props);
@@ -42632,20 +42772,25 @@ ${codeText}
42632
42772
  if (!parentEditor || event && !this.isFocusOwner(event.target)) {
42633
42773
  return;
42634
42774
  }
42635
- document.addEventListener("mousedown", this.handleFocusInteraction);
42636
- document.addEventListener("focusin", this.handleFocusInteraction);
42775
+ const generation = this.focusRestoreGeneration;
42776
+ document.addEventListener("mousedown", this.handleFocusInteraction, true);
42777
+ document.addEventListener("focusin", this.handleFocusInteraction, true);
42778
+ window.addEventListener("blur", this.handleWindowBlur);
42637
42779
  this.focusTimer = setTimeout(() => {
42780
+ if (generation !== this.focusRestoreGeneration) {
42781
+ return;
42782
+ }
42638
42783
  this.clearFocusTimer();
42639
42784
  const input2 = parentEditor.querySelector(".editor-input");
42640
- if (!target.isConnected || !parentEditor.isConnected || !(input2 == null ? void 0 : input2.isConnected)) {
42785
+ if (this._target !== target || !target.isConnected || !parentEditor.isConnected || !(input2 == null ? void 0 : input2.isConnected) || !document.hasFocus()) {
42641
42786
  return;
42642
42787
  }
42643
42788
  const activeElement = document.activeElement;
42644
42789
  if (parentEditor.contains(activeElement)) {
42645
42790
  return;
42646
42791
  }
42647
- if (!activeElement || activeElement === document.body || this.isInCommandBar(activeElement)) {
42648
- input2.focus();
42792
+ if (!activeElement || activeElement === document.body || activeElement === document.documentElement || this.isInCommandBar(activeElement)) {
42793
+ this.cancelFocus = requestInputFocus(input2);
42649
42794
  }
42650
42795
  }, 100);
42651
42796
  }
@@ -42655,12 +42800,17 @@ ${codeText}
42655
42800
  return target instanceof Node && (!!(parentEditor == null ? void 0 : parentEditor.contains(target)) || this.isInCommandBar(target));
42656
42801
  }
42657
42802
  clearFocusTimer() {
42803
+ var _a;
42804
+ this.focusRestoreGeneration += 1;
42805
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
42806
+ this.cancelFocus = void 0;
42658
42807
  if (this.focusTimer !== null) {
42659
42808
  clearTimeout(this.focusTimer);
42660
42809
  this.focusTimer = null;
42661
42810
  }
42662
- document.removeEventListener("mousedown", this.handleFocusInteraction);
42663
- document.removeEventListener("focusin", this.handleFocusInteraction);
42811
+ document.removeEventListener("mousedown", this.handleFocusInteraction, true);
42812
+ document.removeEventListener("focusin", this.handleFocusInteraction, true);
42813
+ window.removeEventListener("blur", this.handleWindowBlur);
42664
42814
  }
42665
42815
  manualShow(target, options) {
42666
42816
  var _a, _b, _c, _d;
@@ -42713,7 +42863,7 @@ ${codeText}
42713
42863
  }
42714
42864
  close(reason, e2) {
42715
42865
  var _a, _b;
42716
- if (reason && reason !== "clickBackground" && reason !== "cancelBar" && reason !== "clickItem") {
42866
+ if (reason === "clickBackground" || reason && reason !== "cancelBar" && reason !== "clickItem") {
42717
42867
  this.clearFocusTimer();
42718
42868
  }
42719
42869
  super.close(reason);
@@ -42726,11 +42876,12 @@ ${codeText}
42726
42876
  }
42727
42877
  }
42728
42878
  destroy() {
42729
- var _a;
42879
+ var _a, _b;
42730
42880
  this.clearFocusTimer();
42731
42881
  (_a = this.tippyInstance) == null ? void 0 : _a.destroy();
42732
42882
  this.tippyInstance = null;
42733
42883
  super.destroy();
42884
+ (_b = this.cancelFocus) == null ? void 0 : _b.call(this);
42734
42885
  }
42735
42886
  getCommandBarRoot() {
42736
42887
  return this.content.closest("[data-tippy-root]");
@@ -44856,19 +45007,22 @@ ${codeText}
44856
45007
  class EditorAutoSuggestMenu extends AutoSuggestMenu {
44857
45008
  constructor(editor, editorOptions) {
44858
45009
  super(editorOptions);
45010
+ __publicField(this, "cancelFocus");
44859
45011
  __publicField(this, "handleClose", () => {
44860
- var _a, _b;
45012
+ var _a, _b, _c;
45013
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
44861
45014
  if (shouldFocusToEditor(this.editor)) {
44862
- this.editor.focus();
45015
+ this.cancelFocus = requestEditorFocus(this.editor);
44863
45016
  }
44864
- (_b = (_a = this.options).onClose) == null ? void 0 : _b.call(_a);
45017
+ (_c = (_b = this.options).onClose) == null ? void 0 : _c.call(_b);
44865
45018
  });
44866
45019
  this.editor = editor;
44867
45020
  this.editorOptions = editorOptions;
44868
45021
  }
44869
45022
  async popupInEditor(containerId, blockIndex, offset) {
44870
- var _a, _b;
44871
- if (((_b = (_a = this.editorOptions).onShow) == null ? void 0 : _b.call(_a, this, containerId, blockIndex, offset)) === false) {
45023
+ var _a, _b, _c;
45024
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
45025
+ if (((_c = (_b = this.editorOptions).onShow) == null ? void 0 : _c.call(_b, this, containerId, blockIndex, offset)) === false) {
44872
45026
  return;
44873
45027
  }
44874
45028
  if (this.options.showLoading) {
@@ -44886,6 +45040,11 @@ ${codeText}
44886
45040
  const anchor2 = createBlockAnchor(this.editor, block, "editor-auto-suggest-menu", rect);
44887
45041
  super.popup(anchor2, { noEvent: true });
44888
45042
  }
45043
+ destroy() {
45044
+ var _a;
45045
+ super.destroy();
45046
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
45047
+ }
44889
45048
  }
44890
45049
  const logger$2G = getLogger("auto-suggest-menu");
44891
45050
  class OnesEditorAutoSuggest {
@@ -48762,6 +48921,8 @@ ${codeText}
48762
48921
  }
48763
48922
  function resetScrollbarCore(scrollCore, scrollWrap) {
48764
48923
  const scrollContainer = getScrollContainer(scrollWrap);
48924
+ if (!scrollContainer || !getContainerOption(scrollCore, getContainer(scrollContainer)))
48925
+ return;
48765
48926
  const scrollbarContainer = getScrollbarContainer(scrollContainer);
48766
48927
  const { scrollWidth, clientWidth } = getElementScrollSize(scrollContainer);
48767
48928
  const { paddingLeft, paddingRight } = getScrollWrapPaddingSize(scrollWrap);
@@ -48778,7 +48939,7 @@ ${codeText}
48778
48939
  }
48779
48940
  function debounceByArgs(func, wait) {
48780
48941
  const debouncers = /* @__PURE__ */ new Map();
48781
- return function(...args) {
48942
+ const debounced = function(...args) {
48782
48943
  var _a;
48783
48944
  const scrollWrap = args[1];
48784
48945
  let key = (scrollWrap == null ? void 0 : scrollWrap.genId) || "";
@@ -48790,17 +48951,30 @@ ${codeText}
48790
48951
  if (!debouncers.has(key)) {
48791
48952
  debouncers.set(key, debounce__default.default(
48792
48953
  (...innerArgs) => {
48793
- const result = func.apply(this, innerArgs);
48794
- if (scrollWrap)
48795
- scrollWrap.genId = void 0;
48796
- debouncers.delete(key);
48797
- return result;
48954
+ try {
48955
+ return func.apply(this, innerArgs);
48956
+ } finally {
48957
+ if (scrollWrap)
48958
+ scrollWrap.genId = void 0;
48959
+ debouncers.delete(key);
48960
+ }
48798
48961
  },
48799
48962
  wait
48800
48963
  ));
48801
48964
  }
48802
48965
  return (_a = debouncers.get(key)) == null ? void 0 : _a.apply(this, args);
48803
48966
  };
48967
+ debounced.cancel = (scrollWrap) => {
48968
+ var _a, _b;
48969
+ const target = scrollWrap;
48970
+ const key = target.genId;
48971
+ if (!key)
48972
+ return;
48973
+ (_b = (_a = debouncers.get(key)) == null ? void 0 : _a.cancel) == null ? void 0 : _b.call(_a);
48974
+ debouncers.delete(key);
48975
+ target.genId = void 0;
48976
+ };
48977
+ return debounced;
48804
48978
  }
48805
48979
  const resetScrollbar = debounceByArgs(resetScrollbarCore, 50);
48806
48980
  function resetScrollbarLeft(scrollContainer, scrollbarContainer) {
@@ -49295,6 +49469,7 @@ ${codeText}
49295
49469
  }
49296
49470
  unbindCore(scrollContainer, scrollWrap) {
49297
49471
  var _a, _b, _c, _d, _e, _f;
49472
+ resetScrollbar.cancel(scrollWrap);
49298
49473
  if (!this.boundContainers.delete(scrollContainer))
49299
49474
  return;
49300
49475
  const scrollbarContainer = getScrollbarContainer(scrollContainer);
@@ -54227,6 +54402,7 @@ ${codeText}
54227
54402
  });
54228
54403
  __publicField(this, "close", () => {
54229
54404
  this.linkPopup.close("cancelBar");
54405
+ this.emit("close");
54230
54406
  });
54231
54407
  __publicField(this, "createItems", (content) => [{
54232
54408
  id: "link-popup",
@@ -54248,7 +54424,6 @@ ${codeText}
54248
54424
  this.emit("finished");
54249
54425
  });
54250
54426
  __publicField(this, "handleClose", () => {
54251
- this.emit("close");
54252
54427
  this.close();
54253
54428
  });
54254
54429
  __publicField(this, "handleShown", () => {
@@ -54277,8 +54452,13 @@ ${codeText}
54277
54452
  this.linkPopup = new MobileBottomMenu(this.createItems(content), void 0, {
54278
54453
  id: "link-popup",
54279
54454
  header: this.createHeader(),
54280
- disableSelectedScroll: true
54455
+ disableSelectedScroll: true,
54456
+ beforeClose: () => {
54457
+ this.handleClose();
54458
+ return true;
54459
+ }
54281
54460
  });
54461
+ this.linkPopup.addListener("close", () => this.emit("close"));
54282
54462
  }
54283
54463
  destroy() {
54284
54464
  var _a, _b;
@@ -54297,11 +54477,11 @@ ${codeText}
54297
54477
  this.emit("finished");
54298
54478
  });
54299
54479
  __publicField(this, "handleClose", () => {
54300
- this.emit("close");
54301
54480
  this.close();
54302
54481
  });
54303
54482
  __publicField(this, "close", () => {
54304
54483
  this.linkPopup.close("cancelBar");
54484
+ this.emit("close");
54305
54485
  });
54306
54486
  __publicField(this, "disabledSubmit", () => {
54307
54487
  var _a;
@@ -54321,8 +54501,13 @@ ${codeText}
54321
54501
  this.content = content;
54322
54502
  this.options = options;
54323
54503
  this.linkPopup = new Popup(this.createPopupContent(), {
54324
- id: "link-popup"
54504
+ id: "link-popup",
54505
+ beforeClose: () => {
54506
+ this.handleClose();
54507
+ return true;
54508
+ }
54325
54509
  });
54510
+ this.linkPopup.addListener("close", () => this.emit("close"));
54326
54511
  }
54327
54512
  createPopupContent() {
54328
54513
  const container = createElement("div", ["link-popup-content"], null);
@@ -54355,46 +54540,85 @@ ${codeText}
54355
54540
  __publicField(this, "onFinished", null);
54356
54541
  __publicField(this, "onClose", null);
54357
54542
  __publicField(this, "onBeforeFormatUrl", null);
54543
+ __publicField(this, "session", 0);
54544
+ __publicField(this, "submitting", false);
54545
+ __publicField(this, "focusTimer");
54546
+ __publicField(this, "releaseFocus");
54358
54547
  __publicField(this, "handleOk", async () => {
54359
- var _a;
54548
+ if (!this.linkPopup || this.submitting || this.editor.destroyed) {
54549
+ return;
54550
+ }
54551
+ const session = this.session;
54552
+ const form = this.form;
54553
+ const onFinished = this.onFinished;
54554
+ const beforeFormatUrl = this.onBeforeFormatUrl;
54555
+ this.submitting = true;
54360
54556
  try {
54361
- await this.checkValue();
54362
- const { linkText, linkUrl } = this.form;
54557
+ await this.checkValue(form);
54558
+ if (!this.isCurrent(session)) {
54559
+ return;
54560
+ }
54561
+ const { linkText, linkUrl } = form;
54363
54562
  const inputValue = linkUrl.getInputValue();
54364
- let link2 = "";
54365
- if (this.onBeforeFormatUrl && this.onBeforeFormatUrl(inputValue) !== false) {
54366
- link2 = this.onBeforeFormatUrl(inputValue);
54367
- } else {
54368
- link2 = this.formatUrl(inputValue);
54563
+ const formatted = beforeFormatUrl == null ? void 0 : beforeFormatUrl(inputValue);
54564
+ const link2 = typeof formatted === "string" ? formatted : this.formatUrl(inputValue);
54565
+ const text2 = linkText.getInputValue() || link2;
54566
+ if (!this.isCurrent(session)) {
54567
+ return;
54369
54568
  }
54370
- (_a = this.onFinished) == null ? void 0 : _a.call(this, linkText.getInputValue() || link2, link2);
54371
54569
  this._destroy();
54570
+ onFinished == null ? void 0 : onFinished(text2, link2);
54372
54571
  } catch (error2) {
54572
+ if (this.isCurrent(session)) {
54573
+ this.submitting = false;
54574
+ }
54373
54575
  }
54374
54576
  });
54375
54577
  __publicField(this, "handleClose", () => {
54376
- var _a;
54377
- (_a = this.onClose) == null ? void 0 : _a.call(this);
54578
+ if (!this.linkPopup) {
54579
+ return;
54580
+ }
54581
+ const onClose = this.onClose;
54378
54582
  this._destroy();
54583
+ if (!this.editor.destroyed) {
54584
+ onClose == null ? void 0 : onClose();
54585
+ }
54379
54586
  });
54380
54587
  __publicField(this, "onShow", () => {
54381
- setTimeout(() => {
54382
- var _a;
54383
- (_a = this.form.linkUrl) == null ? void 0 : _a.getField().focus();
54588
+ var _a;
54589
+ const session = this.session;
54590
+ const field = (_a = this.form.linkUrl) == null ? void 0 : _a.getField();
54591
+ clearTimeout(this.focusTimer);
54592
+ this.focusTimer = setTimeout(() => {
54593
+ if (this.isCurrent(session) && (field == null ? void 0 : field.isConnected)) {
54594
+ field.focus(clientType.isMobile ? void 0 : { preventScroll: true });
54595
+ }
54384
54596
  }, 400);
54385
54597
  });
54386
54598
  __publicField(this, "showPopup", (options) => {
54387
- var _a, _b, _c, _d, _e;
54599
+ const session = this.session;
54388
54600
  if (clientType.isMobile) {
54389
54601
  this.linkPopup = new MobileLinkPopup(this.editor, this.createPopupContent(options.linkText, options.linkUrl), options);
54390
54602
  } else {
54391
54603
  this.linkPopup = new LinkPopup(this.createPopupContent(options.linkText, options.linkUrl), options);
54392
54604
  }
54393
- (_a = this.linkPopup) == null ? void 0 : _a.addListener("show", this.onShow);
54394
- (_b = this.linkPopup) == null ? void 0 : _b.manualShow();
54395
- (_c = this.linkPopup) == null ? void 0 : _c.disabledSubmit();
54396
- (_d = this.linkPopup) == null ? void 0 : _d.addListener("finished", this.handleOk);
54397
- (_e = this.linkPopup) == null ? void 0 : _e.addListener("close", this.handleClose);
54605
+ this.linkPopup.addListener("show", () => {
54606
+ if (this.isCurrent(session)) {
54607
+ this.onShow();
54608
+ }
54609
+ });
54610
+ this.linkPopup.addListener("finished", () => {
54611
+ if (this.isCurrent(session)) {
54612
+ this.handleOk();
54613
+ }
54614
+ });
54615
+ this.linkPopup.addListener("close", () => {
54616
+ if (this.isCurrent(session)) {
54617
+ this.handleClose();
54618
+ }
54619
+ });
54620
+ this.linkPopup.disabledSubmit();
54621
+ this.linkPopup.manualShow();
54398
54622
  });
54399
54623
  __publicField(this, "onChange", () => {
54400
54624
  var _a;
@@ -54406,11 +54630,14 @@ ${codeText}
54406
54630
  return;
54407
54631
  }
54408
54632
  if (event.key === "Enter") {
54409
- (_a = this.form.linkUrl) == null ? void 0 : _a.getField().focus();
54633
+ (_a = this.form.linkUrl) == null ? void 0 : _a.getField().focus(clientType.isMobile ? void 0 : { preventScroll: true });
54634
+ event.preventDefault();
54410
54635
  event.stopPropagation();
54411
54636
  return;
54412
54637
  }
54413
54638
  if (event.key === "Escape") {
54639
+ event.preventDefault();
54640
+ event.stopPropagation();
54414
54641
  this.handleClose();
54415
54642
  }
54416
54643
  });
@@ -54424,13 +54651,18 @@ ${codeText}
54424
54651
  return;
54425
54652
  }
54426
54653
  if (event.key === "Escape") {
54654
+ event.preventDefault();
54655
+ event.stopPropagation();
54427
54656
  this.handleClose();
54428
54657
  }
54429
54658
  });
54430
54659
  this.editor = editor;
54431
54660
  }
54432
- async checkValue() {
54433
- const { linkUrl } = this.form;
54661
+ isCurrent(session) {
54662
+ return session === this.session && !!this.linkPopup && !this.editor.destroyed;
54663
+ }
54664
+ async checkValue(form) {
54665
+ const { linkUrl } = form;
54434
54666
  if (!linkUrl.getInputValue()) {
54435
54667
  linkUrl.setFieldError(i18n$1.t("link.urlPlaceholder"));
54436
54668
  throw new Error(i18n$1.t("link.urlPlaceholder"));
@@ -54448,14 +54680,20 @@ ${codeText}
54448
54680
  return link2;
54449
54681
  }
54450
54682
  editLink(options) {
54451
- this.showPopup(options);
54683
+ this._destroy();
54684
+ if (this.editor.destroyed) {
54685
+ return;
54686
+ }
54687
+ this.releaseFocus = acquireEditorFocus(this.editor);
54452
54688
  const { onFinished, onClose, onBeforeFormatUrl } = options;
54453
54689
  this.onFinished = onFinished;
54454
- if (onClose) {
54455
- this.onClose = onClose;
54456
- }
54457
- if (onBeforeFormatUrl) {
54458
- this.onBeforeFormatUrl = onBeforeFormatUrl;
54690
+ this.onClose = onClose != null ? onClose : null;
54691
+ this.onBeforeFormatUrl = onBeforeFormatUrl != null ? onBeforeFormatUrl : null;
54692
+ try {
54693
+ this.showPopup(options);
54694
+ } catch (error2) {
54695
+ this._destroy();
54696
+ throw error2;
54459
54697
  }
54460
54698
  }
54461
54699
  createPopupContent(linkText, linkUrl) {
@@ -54484,12 +54722,21 @@ ${codeText}
54484
54722
  return content;
54485
54723
  }
54486
54724
  _destroy() {
54487
- var _a, _b;
54725
+ this.session += 1;
54726
+ clearTimeout(this.focusTimer);
54727
+ this.focusTimer = void 0;
54728
+ const popup = this.linkPopup;
54729
+ const releaseFocus = this.releaseFocus;
54730
+ this.linkPopup = null;
54731
+ this.releaseFocus = void 0;
54732
+ this.submitting = false;
54488
54733
  this.onFinished = null;
54489
54734
  this.onClose = null;
54735
+ this.onBeforeFormatUrl = null;
54490
54736
  this.form = {};
54491
- (_a = this.linkPopup) == null ? void 0 : _a.removeAllListeners();
54492
- (_b = this.linkPopup) == null ? void 0 : _b.destroy();
54737
+ popup == null ? void 0 : popup.removeAllListeners();
54738
+ popup == null ? void 0 : popup.destroy();
54739
+ releaseFocus == null ? void 0 : releaseFocus();
54493
54740
  }
54494
54741
  destroy() {
54495
54742
  this._destroy();
@@ -54529,14 +54776,14 @@ ${codeText}
54529
54776
  const text2 = [op1, op2];
54530
54777
  editor.insertTextToBlock(block, 0, text2);
54531
54778
  editor.selection.selectBlock(block, getTextLength(text2));
54532
- editor.focus();
54779
+ requestEditorFocus(editor);
54533
54780
  } catch (error2) {
54534
54781
  logger$20.error(error2);
54535
54782
  }
54536
54783
  });
54537
54784
  };
54538
54785
  const onClose = () => {
54539
- editor.focus();
54786
+ requestEditorFocus(editor);
54540
54787
  };
54541
54788
  editLink(editor, {
54542
54789
  target: block,
@@ -54568,21 +54815,21 @@ ${codeText}
54568
54815
  if (isCollapsed) {
54569
54816
  newText = insertText(editor.getBlockText(block), end, linkText, { link: linkUrl });
54570
54817
  editor.setBlockText(block, newText);
54571
- editor.focus();
54818
+ requestEditorFocus(editor);
54572
54819
  return;
54573
54820
  }
54574
54821
  newText = replaceText(editor.getBlockText(block), start, end, linkText);
54575
54822
  newText = rangeAddAttribute(start, linkText.length, newText, "link", linkUrl);
54576
54823
  editor.setBlockText(block, newText);
54577
54824
  editor.selection.selectBlock(block, start + linkText.length);
54578
- editor.focus();
54825
+ requestEditorFocus(editor);
54579
54826
  } catch (error2) {
54580
54827
  logger$20.error(error2);
54581
54828
  }
54582
54829
  });
54583
54830
  };
54584
54831
  const onClose = () => {
54585
- editor.focus();
54832
+ requestEditorFocus(editor);
54586
54833
  };
54587
54834
  editLink(editor, {
54588
54835
  target: anchor2,
@@ -54831,14 +55078,14 @@ ${codeText}
54831
55078
  const newText = insertText(deleted, range.start, linkText, { link: linkUrl });
54832
55079
  editor.setBlockText(block, newText);
54833
55080
  editor.selection.selectBlock(block, range.start + linkText.length);
54834
- editor.focus();
55081
+ requestEditorFocus(editor);
54835
55082
  } catch (error2) {
54836
55083
  logger$1$.error(error2);
54837
55084
  }
54838
55085
  };
54839
55086
  const onClose = () => {
54840
55087
  editor.selection.selectBlock(block, range.end);
54841
- editor.focus();
55088
+ requestEditorFocus(editor);
54842
55089
  };
54843
55090
  const rect = getBlockClientRects(editor, block, editor.selection.range)[0];
54844
55091
  if (!rect) {
@@ -55340,6 +55587,7 @@ ${codeText}
55340
55587
  __publicField(this, "_disableTextToolbar", false);
55341
55588
  __publicField(this, "_lastClickTime", 0);
55342
55589
  __publicField(this, "_disabled", false);
55590
+ __publicField(this, "cancelFocus");
55343
55591
  __publicField(this, "isInToolbar", (event) => this.toolbar.isInCommandBar(event.target));
55344
55592
  __publicField(this, "handleButtonClick", (toolbar2, item) => {
55345
55593
  const tooltip = this.editor.findCustom("editor-tooltip");
@@ -55363,17 +55611,23 @@ ${codeText}
55363
55611
  }
55364
55612
  this._lastClickTime = Date.now();
55365
55613
  });
55366
- __publicField(this, "handleClose", () => {
55367
- var _a;
55614
+ __publicField(this, "handleClose", (toolbar2) => {
55615
+ var _a, _b;
55616
+ if (toolbar2.closeReason === "clickBackground" || !this.editor.rootElement.isConnected) {
55617
+ return;
55618
+ }
55619
+ const focusedElement = document.activeElement;
55620
+ if (focusedElement && focusedElement !== document.body && focusedElement !== document.documentElement && !this.editor.rootElement.contains(focusedElement) && !toolbar2.isInCommandBar(focusedElement)) {
55621
+ return;
55622
+ }
55368
55623
  if (shouldFocusToEditor(this.editor) && !clientType.isMobile) {
55369
55624
  const activeElement = (_a = document.getSelection()) == null ? void 0 : _a.focusNode;
55370
55625
  if (activeElement && isInBlockTools(activeElement)) {
55371
55626
  logger$1Z.debug("focus is in block tools, ignore focus");
55372
55627
  return;
55373
55628
  }
55374
- this.editor.focus({
55375
- preventScroll: true
55376
- });
55629
+ (_b = this.cancelFocus) == null ? void 0 : _b.call(this);
55630
+ this.cancelFocus = requestEditorFocus(this.editor);
55377
55631
  }
55378
55632
  });
55379
55633
  __publicField(this, "handleClosing", (bar2, event) => {
@@ -55502,8 +55756,10 @@ ${codeText}
55502
55756
  this.toolbar.close("selectionChange");
55503
55757
  }
55504
55758
  destroy() {
55759
+ var _a;
55505
55760
  this.unbindEvents();
55506
55761
  this.toolbar.destroy();
55762
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
55507
55763
  this.editor.removeListener("selectionChanged", this.handleSelectionChange);
55508
55764
  OnesEditorHoveringBlock.get(this.editor).removeListener("childChange", this.handleHoveringBlockChildChange);
55509
55765
  }
@@ -73985,10 +74241,11 @@ ${codeText}
73985
74241
  createTableBorderBar(editor, tableBlock);
73986
74242
  }
73987
74243
  function hideTableBorderBar(editor, tableBlock) {
74244
+ var _a, _b;
73988
74245
  assert(logger$14, isTableBlock(tableBlock), "invalid table block");
73989
- const tools = getTableTools(editor, tableBlock);
74246
+ const tools = (_a = getBlockContent(tableBlock).querySelector("div[data-type=table-content]")) == null ? void 0 : _a.querySelector(":scope >div[data-type=table-tools]");
73990
74247
  const blockTools = getBlockTools(tableBlock);
73991
- const topTableBorderBar = tools.querySelectorAll(".table-border-bar-container");
74248
+ const topTableBorderBar = (_b = tools == null ? void 0 : tools.querySelectorAll(".table-border-bar-container")) != null ? _b : [];
73992
74249
  const leftTableBorderBar = blockTools.querySelectorAll(".table-border-bar-container");
73993
74250
  topTableBorderBar.forEach((elem) => {
73994
74251
  editor.domEvents.removeEventListener(elem, "click", handleBorderBarClicked);
@@ -96935,14 +97192,13 @@ ${JSON.stringify(error2, null, 2)}`);
96935
97192
  __publicField(this, "toolbar");
96936
97193
  __publicField(this, "toolbarFullScreen");
96937
97194
  __publicField(this, "root");
97195
+ __publicField(this, "cancelFocus");
96938
97196
  __publicField(this, "handleClick", (bar2, item) => {
96939
97197
  const command = item;
96940
97198
  if (command.onClick) {
96941
97199
  command.onClick(this.editor, command, bar2);
96942
97200
  this.handleSelectionChanged();
96943
- setTimeout(() => {
96944
- this.editor.focus();
96945
- });
97201
+ this.restoreFocus();
96946
97202
  return;
96947
97203
  }
96948
97204
  const findParent = (item2, id) => {
@@ -96988,9 +97244,7 @@ ${JSON.stringify(error2, null, 2)}`);
96988
97244
  id: "main-toolbar"
96989
97245
  });
96990
97246
  this.toolbar.addListener("closing", () => {
96991
- setTimeout(() => {
96992
- editor.focus();
96993
- }, 100);
97247
+ this.restoreFocus(100);
96994
97248
  return true;
96995
97249
  });
96996
97250
  this.toolbar.addListener("click", this.handleClick);
@@ -97021,11 +97275,18 @@ ${JSON.stringify(error2, null, 2)}`);
97021
97275
  });
97022
97276
  }
97023
97277
  }
97024
- destroy() {
97278
+ restoreFocus(delay = 0) {
97025
97279
  var _a;
97280
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
97281
+ this.cancelFocus = requestEditorFocus(this.editor, delay);
97282
+ }
97283
+ destroy() {
97284
+ var _a, _b, _c;
97285
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
97026
97286
  this.toolbar.destroy();
97027
97287
  this.editor.removeListener("selectionChanged", this.handleSelectionChanged);
97028
- (_a = this.toolbarFullScreen) == null ? void 0 : _a.destroy();
97288
+ (_b = this.toolbarFullScreen) == null ? void 0 : _b.destroy();
97289
+ (_c = this.cancelFocus) == null ? void 0 : _c.call(this);
97029
97290
  this.root.remove();
97030
97291
  }
97031
97292
  setItems(items) {
@@ -97617,7 +97878,7 @@ ${JSON.stringify(error2, null, 2)}`);
97617
97878
  }
97618
97879
  }
97619
97880
  });
97620
- editor.version = "4.0.7-beta.ONES22303263.1";
97881
+ editor.version = "4.0.7-beta.ONES22303310.1";
97621
97882
  return editor;
97622
97883
  }
97623
97884
  function isDoc(doc2) {
@@ -97753,7 +98014,7 @@ ${JSON.stringify(error2, null, 2)}`);
97753
98014
  OnesEditorDropTarget.register(editor);
97754
98015
  OnesEditorTocProvider.register(editor);
97755
98016
  OnesEditorExclusiveBlock.register(editor);
97756
- editor.version = "4.0.7-beta.ONES22303263.1";
98017
+ editor.version = "4.0.7-beta.ONES22303310.1";
97757
98018
  return editor;
97758
98019
  } catch (error2) {
97759
98020
  try {
@@ -97896,7 +98157,7 @@ ${JSON.stringify(error2, null, 2)}`);
97896
98157
  }
97897
98158
  }
97898
98159
  });
97899
- editor.version = "4.0.7-beta.ONES22303263.1";
98160
+ editor.version = "4.0.7-beta.ONES22303310.1";
97900
98161
  return editor;
97901
98162
  }
97902
98163
  const emojis$1 = {
@@ -143722,6 +143983,7 @@ ${JSON.stringify(error2, null, 2)}`);
143722
143983
  exports2.FindReplaceItem = FindReplaceItem;
143723
143984
  exports2.FitIcon = FitIcon;
143724
143985
  exports2.FixedToolbar = FixedToolbar;
143986
+ exports2.FocusRestoration = FocusRestoration;
143725
143987
  exports2.FontBoldItem = FontBoldItem;
143726
143988
  exports2.FontItalicItem = FontItalicItem;
143727
143989
  exports2.FontStrikethroughItem = FontStrikethroughItem;
@@ -143920,6 +144182,7 @@ ${JSON.stringify(error2, null, 2)}`);
143920
144182
  exports2.YoukuIcon = YoukuIcon;
143921
144183
  exports2.ZERO_WIDTH_SPACE_CHAR = ZERO_WIDTH_SPACE_CHAR;
143922
144184
  exports2.abbreviation = abbreviation;
144185
+ exports2.acquireEditorFocus = acquireEditorFocus;
143923
144186
  exports2.activeComment = activeComment;
143924
144187
  exports2.addAttribute = addAttribute;
143925
144188
  exports2.addClass = addClass;
@@ -144405,6 +144668,8 @@ ${JSON.stringify(error2, null, 2)}`);
144405
144668
  exports2.replaceAllText = replaceAllText;
144406
144669
  exports2.replaceText = replaceText;
144407
144670
  exports2.requestDownload = requestDownload;
144671
+ exports2.requestEditorFocus = requestEditorFocus;
144672
+ exports2.requestInputFocus = requestInputFocus;
144408
144673
  exports2.resetListStart = resetListStart;
144409
144674
  exports2.saveAs = saveAs$1;
144410
144675
  exports2.scrollBarIntoView = scrollBarIntoView;