@ones-editor/editor 5.0.8-beta.ONES22303263.1 → 5.0.8-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
@@ -26555,6 +26555,114 @@ var __publicField = (obj, key, value) => {
26555
26555
  };
26556
26556
  }
26557
26557
  }
26558
+ const controllers = /* @__PURE__ */ new WeakMap();
26559
+ const noop$1 = () => {
26560
+ };
26561
+ class FocusRestoration {
26562
+ constructor(input2, restore) {
26563
+ __publicField(this, "generation", 0);
26564
+ __publicField(this, "owner", null);
26565
+ __publicField(this, "destroyed", false);
26566
+ __publicField(this, "activeScope");
26567
+ __publicField(this, "timers", /* @__PURE__ */ new Set());
26568
+ this.input = input2;
26569
+ this.restore = restore;
26570
+ controllers.set(input2, this);
26571
+ }
26572
+ canFocus() {
26573
+ var _a;
26574
+ return !this.destroyed && !this.owner && !((_a = this.activeScope) == null ? void 0 : _a.cancelled) && this.input.isConnected;
26575
+ }
26576
+ hasOtherInput() {
26577
+ const active = this.input.ownerDocument.activeElement;
26578
+ return active !== this.input && !!(active == null ? void 0 : active.matches('input, textarea, select, [contenteditable="true"], [contenteditable=""]'));
26579
+ }
26580
+ schedule(callback, delay = 0, protectUserFocus = false) {
26581
+ var _a;
26582
+ return this.scheduleInScope(callback, delay, protectUserFocus, (_a = this.activeScope) != null ? _a : this.createScope());
26583
+ }
26584
+ createScope() {
26585
+ return { cancelled: false, timers: /* @__PURE__ */ new Set() };
26586
+ }
26587
+ scheduleInScope(callback, delay, protectUserFocus, scope) {
26588
+ if (scope.cancelled || !this.canFocus()) {
26589
+ return noop$1;
26590
+ }
26591
+ const generation = this.generation;
26592
+ const timer = setTimeout(() => {
26593
+ this.timers.delete(timer);
26594
+ scope.timers.delete(timer);
26595
+ if (!scope.cancelled && generation === this.generation && this.canFocus() && (!protectUserFocus || !this.hasOtherInput())) {
26596
+ const previousScope = this.activeScope;
26597
+ this.activeScope = scope;
26598
+ try {
26599
+ callback();
26600
+ } finally {
26601
+ this.activeScope = previousScope;
26602
+ }
26603
+ }
26604
+ }, delay);
26605
+ this.timers.add(timer);
26606
+ scope.timers.add(timer);
26607
+ return () => {
26608
+ scope.cancelled = true;
26609
+ scope.timers.forEach((pending) => {
26610
+ clearTimeout(pending);
26611
+ this.timers.delete(pending);
26612
+ });
26613
+ scope.timers.clear();
26614
+ };
26615
+ }
26616
+ request(delay = 0) {
26617
+ return this.scheduleInScope(this.restore, delay, !clientType.isMobile, this.createScope());
26618
+ }
26619
+ cancelPending() {
26620
+ if (this.activeScope) {
26621
+ this.activeScope.cancelled = true;
26622
+ }
26623
+ this.generation += 1;
26624
+ this.timers.forEach((timer) => clearTimeout(timer));
26625
+ this.timers.clear();
26626
+ }
26627
+ acquire() {
26628
+ this.cancelPending();
26629
+ const owner = {};
26630
+ this.owner = owner;
26631
+ return () => {
26632
+ if (this.owner === owner) {
26633
+ this.cancelPending();
26634
+ this.owner = null;
26635
+ }
26636
+ };
26637
+ }
26638
+ destroy() {
26639
+ this.destroyed = true;
26640
+ this.cancelPending();
26641
+ this.owner = null;
26642
+ controllers.delete(this.input);
26643
+ }
26644
+ }
26645
+ function requestInputFocus(input2, delay = 0) {
26646
+ var _a, _b;
26647
+ return (_b = (_a = controllers.get(input2)) == null ? void 0 : _a.request(delay)) != null ? _b : noop$1;
26648
+ }
26649
+ function requestEditorFocus(editor, delay = 0) {
26650
+ if (editor.destroyed) {
26651
+ return noop$1;
26652
+ }
26653
+ if (clientType.isMobile && delay === 0) {
26654
+ editor.focus();
26655
+ return noop$1;
26656
+ }
26657
+ return requestInputFocus(editor.input.inputElement, delay);
26658
+ }
26659
+ function acquireEditorFocus(editor) {
26660
+ var _a, _b;
26661
+ if (clientType.isMobile || editor.destroyed) {
26662
+ return noop$1;
26663
+ }
26664
+ return (_b = (_a = controllers.get(editor.input.inputElement)) == null ? void 0 : _a.acquire()) != null ? _b : noop$1;
26665
+ }
26558
26666
  const logger$3W = getLogger("editor-input");
26559
26667
  class EditorInput$1 {
26560
26668
  constructor(editor, callbacks) {
@@ -26564,6 +26672,7 @@ var __publicField = (obj, key, value) => {
26564
26672
  __publicField(this, "callbacks");
26565
26673
  __publicField(this, "inputElement");
26566
26674
  __publicField(this, "focused", false);
26675
+ __publicField(this, "focusRestoration");
26567
26676
  __publicField(this, "handleBlur", () => {
26568
26677
  this.editor.emit("blur", this.editor);
26569
26678
  this.callbacks.onBlur();
@@ -26585,12 +26694,20 @@ var __publicField = (obj, key, value) => {
26585
26694
  const isInDisableScrollMask = this.isInDisableScrollMask(target);
26586
26695
  if (target && (isInDisableScrollMask || rootElement === target || rootElement.contains(target))) {
26587
26696
  if (!isInDisableScrollMask && !isInBlockTools(target)) {
26588
- setTimeout(() => {
26697
+ const focus = () => {
26698
+ if (this.editor.destroyed || !this.focusRestoration.canFocus()) {
26699
+ return;
26700
+ }
26589
26701
  this.editor.emit("focus", this.editor);
26590
26702
  this.callbacks.onFocus();
26591
26703
  this.editor.input.focus({ preventScroll: true });
26592
26704
  this.focused = true;
26593
- });
26705
+ };
26706
+ if (clientType.isMobile) {
26707
+ setTimeout(focus);
26708
+ } else {
26709
+ this.focusRestoration.schedule(focus);
26710
+ }
26594
26711
  }
26595
26712
  return;
26596
26713
  }
@@ -26725,6 +26842,11 @@ var __publicField = (obj, key, value) => {
26725
26842
  input2.addEventListener("compositionstart", this.handleCompositionstart);
26726
26843
  input2.addEventListener("compositionend", this.handleCompositionend);
26727
26844
  this.inputElement = input2;
26845
+ this.focusRestoration = new FocusRestoration(input2, () => {
26846
+ if (!this.editor.destroyed) {
26847
+ this.editor.focus(clientType.isMobile ? void 0 : { preventScroll: true });
26848
+ }
26849
+ });
26728
26850
  if (clientType.isMobile) {
26729
26851
  document.addEventListener("touchend", this.handleDocumentClick);
26730
26852
  } else {
@@ -26747,6 +26869,7 @@ var __publicField = (obj, key, value) => {
26747
26869
  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"));
26748
26870
  }
26749
26871
  destroy() {
26872
+ this.focusRestoration.destroy();
26750
26873
  this.callbacks.destroy();
26751
26874
  if (clientType.isMobile) {
26752
26875
  document.removeEventListener("touchend", this.handleDocumentClick);
@@ -26771,24 +26894,36 @@ var __publicField = (obj, key, value) => {
26771
26894
  return this.inputElement;
26772
26895
  }
26773
26896
  focus(options) {
26897
+ if (this.editor.destroyed || !this.focusRestoration.canFocus()) {
26898
+ return;
26899
+ }
26774
26900
  if (!this.editor.selection.caret.visible()) {
26775
26901
  this.editor.selection.caret.update();
26776
26902
  }
26777
26903
  if (clientType.isMobile) {
26778
26904
  this.inputElement.focus(options);
26779
26905
  this.callbacks.onFocus();
26906
+ this.focused = true;
26780
26907
  } else {
26781
- setTimeout(() => {
26908
+ this.focusRestoration.schedule(() => {
26909
+ if (this.editor.destroyed) {
26910
+ return;
26911
+ }
26782
26912
  if (document.activeElement !== this.inputElement) {
26783
- setTimeout(() => {
26784
- this.editor.emit("focus", this.editor);
26913
+ this.focusRestoration.schedule(() => {
26914
+ if (!this.editor.destroyed && document.activeElement === this.inputElement) {
26915
+ this.editor.emit("focus", this.editor);
26916
+ }
26785
26917
  });
26786
26918
  }
26787
26919
  this.inputElement.focus(options);
26920
+ if (this.editor.destroyed || !this.focusRestoration.canFocus()) {
26921
+ return;
26922
+ }
26788
26923
  this.callbacks.onFocus();
26789
- });
26924
+ this.focused = true;
26925
+ }, 0, (options == null ? void 0 : options.preventScroll) === true);
26790
26926
  }
26791
- this.focused = true;
26792
26927
  }
26793
26928
  isInDisableScrollMask(target) {
26794
26929
  let elem = target;
@@ -32464,7 +32599,7 @@ ${codeText}
32464
32599
  editor.doc.endBatchUpdate();
32465
32600
  }
32466
32601
  if (!clientType.isMobile) {
32467
- editor.focus();
32602
+ requestEditorFocus(editor);
32468
32603
  }
32469
32604
  return rangeResult;
32470
32605
  });
@@ -42798,7 +42933,9 @@ ${codeText}
42798
42933
  });
42799
42934
  __publicField(this, "tippyInstance", null);
42800
42935
  __publicField(this, "_target", null);
42936
+ __publicField(this, "cancelFocus");
42801
42937
  __publicField(this, "focusTimer", null);
42938
+ __publicField(this, "focusRestoreGeneration", 0);
42802
42939
  __publicField(this, "handleBeforeClose", (event) => {
42803
42940
  var _a;
42804
42941
  const focusOwner = this.getFocusOwner();
@@ -42818,6 +42955,9 @@ ${codeText}
42818
42955
  this.clearFocusTimer();
42819
42956
  }
42820
42957
  });
42958
+ __publicField(this, "handleWindowBlur", () => {
42959
+ this.clearFocusTimer();
42960
+ });
42821
42961
  __publicField(this, "setProps", (props) => {
42822
42962
  var _a;
42823
42963
  (_a = this.tippyInstance) == null ? void 0 : _a.setProps(props);
@@ -42836,20 +42976,25 @@ ${codeText}
42836
42976
  if (!parentEditor || event && !this.isFocusOwner(event.target)) {
42837
42977
  return;
42838
42978
  }
42839
- document.addEventListener("mousedown", this.handleFocusInteraction);
42840
- document.addEventListener("focusin", this.handleFocusInteraction);
42979
+ const generation = this.focusRestoreGeneration;
42980
+ document.addEventListener("mousedown", this.handleFocusInteraction, true);
42981
+ document.addEventListener("focusin", this.handleFocusInteraction, true);
42982
+ window.addEventListener("blur", this.handleWindowBlur);
42841
42983
  this.focusTimer = setTimeout(() => {
42984
+ if (generation !== this.focusRestoreGeneration) {
42985
+ return;
42986
+ }
42842
42987
  this.clearFocusTimer();
42843
42988
  const input2 = parentEditor.querySelector(".editor-input");
42844
- if (!target.isConnected || !parentEditor.isConnected || !(input2 == null ? void 0 : input2.isConnected)) {
42989
+ if (this._target !== target || !target.isConnected || !parentEditor.isConnected || !(input2 == null ? void 0 : input2.isConnected) || !document.hasFocus()) {
42845
42990
  return;
42846
42991
  }
42847
42992
  const activeElement = document.activeElement;
42848
42993
  if (parentEditor.contains(activeElement)) {
42849
42994
  return;
42850
42995
  }
42851
- if (!activeElement || activeElement === document.body || this.isInCommandBar(activeElement)) {
42852
- input2.focus();
42996
+ if (!activeElement || activeElement === document.body || activeElement === document.documentElement || this.isInCommandBar(activeElement)) {
42997
+ this.cancelFocus = requestInputFocus(input2);
42853
42998
  }
42854
42999
  }, 100);
42855
43000
  }
@@ -42859,12 +43004,17 @@ ${codeText}
42859
43004
  return target instanceof Node && (!!(parentEditor == null ? void 0 : parentEditor.contains(target)) || this.isInCommandBar(target));
42860
43005
  }
42861
43006
  clearFocusTimer() {
43007
+ var _a;
43008
+ this.focusRestoreGeneration += 1;
43009
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
43010
+ this.cancelFocus = void 0;
42862
43011
  if (this.focusTimer !== null) {
42863
43012
  clearTimeout(this.focusTimer);
42864
43013
  this.focusTimer = null;
42865
43014
  }
42866
- document.removeEventListener("mousedown", this.handleFocusInteraction);
42867
- document.removeEventListener("focusin", this.handleFocusInteraction);
43015
+ document.removeEventListener("mousedown", this.handleFocusInteraction, true);
43016
+ document.removeEventListener("focusin", this.handleFocusInteraction, true);
43017
+ window.removeEventListener("blur", this.handleWindowBlur);
42868
43018
  }
42869
43019
  manualShow(target, options) {
42870
43020
  var _a, _b, _c, _d;
@@ -42917,7 +43067,7 @@ ${codeText}
42917
43067
  }
42918
43068
  close(reason, e2) {
42919
43069
  var _a, _b;
42920
- if (reason && reason !== "clickBackground" && reason !== "cancelBar" && reason !== "clickItem") {
43070
+ if (reason === "clickBackground" || reason && reason !== "cancelBar" && reason !== "clickItem") {
42921
43071
  this.clearFocusTimer();
42922
43072
  }
42923
43073
  super.close(reason);
@@ -42930,11 +43080,12 @@ ${codeText}
42930
43080
  }
42931
43081
  }
42932
43082
  destroy() {
42933
- var _a;
43083
+ var _a, _b;
42934
43084
  this.clearFocusTimer();
42935
43085
  (_a = this.tippyInstance) == null ? void 0 : _a.destroy();
42936
43086
  this.tippyInstance = null;
42937
43087
  super.destroy();
43088
+ (_b = this.cancelFocus) == null ? void 0 : _b.call(this);
42938
43089
  }
42939
43090
  getCommandBarRoot() {
42940
43091
  return this.content.closest("[data-tippy-root]");
@@ -45060,19 +45211,22 @@ ${codeText}
45060
45211
  class EditorAutoSuggestMenu extends AutoSuggestMenu {
45061
45212
  constructor(editor, editorOptions) {
45062
45213
  super(editorOptions);
45214
+ __publicField(this, "cancelFocus");
45063
45215
  __publicField(this, "handleClose", () => {
45064
- var _a, _b;
45216
+ var _a, _b, _c;
45217
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
45065
45218
  if (shouldFocusToEditor(this.editor)) {
45066
- this.editor.focus();
45219
+ this.cancelFocus = requestEditorFocus(this.editor);
45067
45220
  }
45068
- (_b = (_a = this.options).onClose) == null ? void 0 : _b.call(_a);
45221
+ (_c = (_b = this.options).onClose) == null ? void 0 : _c.call(_b);
45069
45222
  });
45070
45223
  this.editor = editor;
45071
45224
  this.editorOptions = editorOptions;
45072
45225
  }
45073
45226
  async popupInEditor(containerId, blockIndex, offset) {
45074
- var _a, _b;
45075
- if (((_b = (_a = this.editorOptions).onShow) == null ? void 0 : _b.call(_a, this, containerId, blockIndex, offset)) === false) {
45227
+ var _a, _b, _c;
45228
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
45229
+ if (((_c = (_b = this.editorOptions).onShow) == null ? void 0 : _c.call(_b, this, containerId, blockIndex, offset)) === false) {
45076
45230
  return;
45077
45231
  }
45078
45232
  if (this.options.showLoading) {
@@ -45090,6 +45244,11 @@ ${codeText}
45090
45244
  const anchor2 = createBlockAnchor(this.editor, block, "editor-auto-suggest-menu", rect);
45091
45245
  super.popup(anchor2, { noEvent: true });
45092
45246
  }
45247
+ destroy() {
45248
+ var _a;
45249
+ super.destroy();
45250
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
45251
+ }
45093
45252
  }
45094
45253
  const logger$2H = getLogger("auto-suggest-menu");
45095
45254
  class OnesEditorAutoSuggest {
@@ -48966,6 +49125,8 @@ ${codeText}
48966
49125
  }
48967
49126
  function resetScrollbarCore(scrollCore, scrollWrap) {
48968
49127
  const scrollContainer = getScrollContainer(scrollWrap);
49128
+ if (!scrollContainer || !getContainerOption(scrollCore, getContainer(scrollContainer)))
49129
+ return;
48969
49130
  const scrollbarContainer = getScrollbarContainer(scrollContainer);
48970
49131
  const { scrollWidth, clientWidth } = getElementScrollSize(scrollContainer);
48971
49132
  const { paddingLeft, paddingRight } = getScrollWrapPaddingSize(scrollWrap);
@@ -48982,7 +49143,7 @@ ${codeText}
48982
49143
  }
48983
49144
  function debounceByArgs(func, wait) {
48984
49145
  const debouncers = /* @__PURE__ */ new Map();
48985
- return function(...args) {
49146
+ const debounced = function(...args) {
48986
49147
  var _a;
48987
49148
  const scrollWrap = args[1];
48988
49149
  let key = (scrollWrap == null ? void 0 : scrollWrap.genId) || "";
@@ -48994,17 +49155,30 @@ ${codeText}
48994
49155
  if (!debouncers.has(key)) {
48995
49156
  debouncers.set(key, debounce__default.default(
48996
49157
  (...innerArgs) => {
48997
- const result = func.apply(this, innerArgs);
48998
- if (scrollWrap)
48999
- scrollWrap.genId = void 0;
49000
- debouncers.delete(key);
49001
- return result;
49158
+ try {
49159
+ return func.apply(this, innerArgs);
49160
+ } finally {
49161
+ if (scrollWrap)
49162
+ scrollWrap.genId = void 0;
49163
+ debouncers.delete(key);
49164
+ }
49002
49165
  },
49003
49166
  wait
49004
49167
  ));
49005
49168
  }
49006
49169
  return (_a = debouncers.get(key)) == null ? void 0 : _a.apply(this, args);
49007
49170
  };
49171
+ debounced.cancel = (scrollWrap) => {
49172
+ var _a, _b;
49173
+ const target = scrollWrap;
49174
+ const key = target.genId;
49175
+ if (!key)
49176
+ return;
49177
+ (_b = (_a = debouncers.get(key)) == null ? void 0 : _a.cancel) == null ? void 0 : _b.call(_a);
49178
+ debouncers.delete(key);
49179
+ target.genId = void 0;
49180
+ };
49181
+ return debounced;
49008
49182
  }
49009
49183
  const resetScrollbar = debounceByArgs(resetScrollbarCore, 50);
49010
49184
  function resetScrollbarLeft(scrollContainer, scrollbarContainer) {
@@ -49499,6 +49673,7 @@ ${codeText}
49499
49673
  }
49500
49674
  unbindCore(scrollContainer, scrollWrap) {
49501
49675
  var _a, _b, _c, _d, _e, _f;
49676
+ resetScrollbar.cancel(scrollWrap);
49502
49677
  if (!this.boundContainers.delete(scrollContainer))
49503
49678
  return;
49504
49679
  const scrollbarContainer = getScrollbarContainer(scrollContainer);
@@ -54431,6 +54606,7 @@ ${codeText}
54431
54606
  });
54432
54607
  __publicField(this, "close", () => {
54433
54608
  this.linkPopup.close("cancelBar");
54609
+ this.emit("close");
54434
54610
  });
54435
54611
  __publicField(this, "createItems", (content) => [{
54436
54612
  id: "link-popup",
@@ -54452,7 +54628,6 @@ ${codeText}
54452
54628
  this.emit("finished");
54453
54629
  });
54454
54630
  __publicField(this, "handleClose", () => {
54455
- this.emit("close");
54456
54631
  this.close();
54457
54632
  });
54458
54633
  __publicField(this, "handleShown", () => {
@@ -54481,8 +54656,13 @@ ${codeText}
54481
54656
  this.linkPopup = new MobileBottomMenu(this.createItems(content), void 0, {
54482
54657
  id: "link-popup",
54483
54658
  header: this.createHeader(),
54484
- disableSelectedScroll: true
54659
+ disableSelectedScroll: true,
54660
+ beforeClose: () => {
54661
+ this.handleClose();
54662
+ return true;
54663
+ }
54485
54664
  });
54665
+ this.linkPopup.addListener("close", () => this.emit("close"));
54486
54666
  }
54487
54667
  destroy() {
54488
54668
  var _a, _b;
@@ -54501,11 +54681,11 @@ ${codeText}
54501
54681
  this.emit("finished");
54502
54682
  });
54503
54683
  __publicField(this, "handleClose", () => {
54504
- this.emit("close");
54505
54684
  this.close();
54506
54685
  });
54507
54686
  __publicField(this, "close", () => {
54508
54687
  this.linkPopup.close("cancelBar");
54688
+ this.emit("close");
54509
54689
  });
54510
54690
  __publicField(this, "disabledSubmit", () => {
54511
54691
  var _a;
@@ -54525,8 +54705,13 @@ ${codeText}
54525
54705
  this.content = content;
54526
54706
  this.options = options;
54527
54707
  this.linkPopup = new Popup(this.createPopupContent(), {
54528
- id: "link-popup"
54708
+ id: "link-popup",
54709
+ beforeClose: () => {
54710
+ this.handleClose();
54711
+ return true;
54712
+ }
54529
54713
  });
54714
+ this.linkPopup.addListener("close", () => this.emit("close"));
54530
54715
  }
54531
54716
  createPopupContent() {
54532
54717
  const container = createElement("div", ["link-popup-content"], null);
@@ -54559,46 +54744,85 @@ ${codeText}
54559
54744
  __publicField(this, "onFinished", null);
54560
54745
  __publicField(this, "onClose", null);
54561
54746
  __publicField(this, "onBeforeFormatUrl", null);
54747
+ __publicField(this, "session", 0);
54748
+ __publicField(this, "submitting", false);
54749
+ __publicField(this, "focusTimer");
54750
+ __publicField(this, "releaseFocus");
54562
54751
  __publicField(this, "handleOk", async () => {
54563
- var _a;
54752
+ if (!this.linkPopup || this.submitting || this.editor.destroyed) {
54753
+ return;
54754
+ }
54755
+ const session = this.session;
54756
+ const form = this.form;
54757
+ const onFinished = this.onFinished;
54758
+ const beforeFormatUrl = this.onBeforeFormatUrl;
54759
+ this.submitting = true;
54564
54760
  try {
54565
- await this.checkValue();
54566
- const { linkText, linkUrl } = this.form;
54761
+ await this.checkValue(form);
54762
+ if (!this.isCurrent(session)) {
54763
+ return;
54764
+ }
54765
+ const { linkText, linkUrl } = form;
54567
54766
  const inputValue = linkUrl.getInputValue();
54568
- let link2 = "";
54569
- if (this.onBeforeFormatUrl && this.onBeforeFormatUrl(inputValue) !== false) {
54570
- link2 = this.onBeforeFormatUrl(inputValue);
54571
- } else {
54572
- link2 = this.formatUrl(inputValue);
54767
+ const formatted = beforeFormatUrl == null ? void 0 : beforeFormatUrl(inputValue);
54768
+ const link2 = typeof formatted === "string" ? formatted : this.formatUrl(inputValue);
54769
+ const text2 = linkText.getInputValue() || link2;
54770
+ if (!this.isCurrent(session)) {
54771
+ return;
54573
54772
  }
54574
- (_a = this.onFinished) == null ? void 0 : _a.call(this, linkText.getInputValue() || link2, link2);
54575
54773
  this._destroy();
54774
+ onFinished == null ? void 0 : onFinished(text2, link2);
54576
54775
  } catch (error2) {
54776
+ if (this.isCurrent(session)) {
54777
+ this.submitting = false;
54778
+ }
54577
54779
  }
54578
54780
  });
54579
54781
  __publicField(this, "handleClose", () => {
54580
- var _a;
54581
- (_a = this.onClose) == null ? void 0 : _a.call(this);
54782
+ if (!this.linkPopup) {
54783
+ return;
54784
+ }
54785
+ const onClose = this.onClose;
54582
54786
  this._destroy();
54787
+ if (!this.editor.destroyed) {
54788
+ onClose == null ? void 0 : onClose();
54789
+ }
54583
54790
  });
54584
54791
  __publicField(this, "onShow", () => {
54585
- setTimeout(() => {
54586
- var _a;
54587
- (_a = this.form.linkUrl) == null ? void 0 : _a.getField().focus();
54792
+ var _a;
54793
+ const session = this.session;
54794
+ const field = (_a = this.form.linkUrl) == null ? void 0 : _a.getField();
54795
+ clearTimeout(this.focusTimer);
54796
+ this.focusTimer = setTimeout(() => {
54797
+ if (this.isCurrent(session) && (field == null ? void 0 : field.isConnected)) {
54798
+ field.focus(clientType.isMobile ? void 0 : { preventScroll: true });
54799
+ }
54588
54800
  }, 400);
54589
54801
  });
54590
54802
  __publicField(this, "showPopup", (options) => {
54591
- var _a, _b, _c, _d, _e;
54803
+ const session = this.session;
54592
54804
  if (clientType.isMobile) {
54593
54805
  this.linkPopup = new MobileLinkPopup(this.editor, this.createPopupContent(options.linkText, options.linkUrl), options);
54594
54806
  } else {
54595
54807
  this.linkPopup = new LinkPopup(this.createPopupContent(options.linkText, options.linkUrl), options);
54596
54808
  }
54597
- (_a = this.linkPopup) == null ? void 0 : _a.addListener("show", this.onShow);
54598
- (_b = this.linkPopup) == null ? void 0 : _b.manualShow();
54599
- (_c = this.linkPopup) == null ? void 0 : _c.disabledSubmit();
54600
- (_d = this.linkPopup) == null ? void 0 : _d.addListener("finished", this.handleOk);
54601
- (_e = this.linkPopup) == null ? void 0 : _e.addListener("close", this.handleClose);
54809
+ this.linkPopup.addListener("show", () => {
54810
+ if (this.isCurrent(session)) {
54811
+ this.onShow();
54812
+ }
54813
+ });
54814
+ this.linkPopup.addListener("finished", () => {
54815
+ if (this.isCurrent(session)) {
54816
+ this.handleOk();
54817
+ }
54818
+ });
54819
+ this.linkPopup.addListener("close", () => {
54820
+ if (this.isCurrent(session)) {
54821
+ this.handleClose();
54822
+ }
54823
+ });
54824
+ this.linkPopup.disabledSubmit();
54825
+ this.linkPopup.manualShow();
54602
54826
  });
54603
54827
  __publicField(this, "onChange", () => {
54604
54828
  var _a;
@@ -54610,11 +54834,14 @@ ${codeText}
54610
54834
  return;
54611
54835
  }
54612
54836
  if (event.key === "Enter") {
54613
- (_a = this.form.linkUrl) == null ? void 0 : _a.getField().focus();
54837
+ (_a = this.form.linkUrl) == null ? void 0 : _a.getField().focus(clientType.isMobile ? void 0 : { preventScroll: true });
54838
+ event.preventDefault();
54614
54839
  event.stopPropagation();
54615
54840
  return;
54616
54841
  }
54617
54842
  if (event.key === "Escape") {
54843
+ event.preventDefault();
54844
+ event.stopPropagation();
54618
54845
  this.handleClose();
54619
54846
  }
54620
54847
  });
@@ -54628,13 +54855,18 @@ ${codeText}
54628
54855
  return;
54629
54856
  }
54630
54857
  if (event.key === "Escape") {
54858
+ event.preventDefault();
54859
+ event.stopPropagation();
54631
54860
  this.handleClose();
54632
54861
  }
54633
54862
  });
54634
54863
  this.editor = editor;
54635
54864
  }
54636
- async checkValue() {
54637
- const { linkUrl } = this.form;
54865
+ isCurrent(session) {
54866
+ return session === this.session && !!this.linkPopup && !this.editor.destroyed;
54867
+ }
54868
+ async checkValue(form) {
54869
+ const { linkUrl } = form;
54638
54870
  if (!linkUrl.getInputValue()) {
54639
54871
  linkUrl.setFieldError(i18n$1.t("link.urlPlaceholder"));
54640
54872
  throw new Error(i18n$1.t("link.urlPlaceholder"));
@@ -54652,14 +54884,20 @@ ${codeText}
54652
54884
  return link2;
54653
54885
  }
54654
54886
  editLink(options) {
54655
- this.showPopup(options);
54887
+ this._destroy();
54888
+ if (this.editor.destroyed) {
54889
+ return;
54890
+ }
54891
+ this.releaseFocus = acquireEditorFocus(this.editor);
54656
54892
  const { onFinished, onClose, onBeforeFormatUrl } = options;
54657
54893
  this.onFinished = onFinished;
54658
- if (onClose) {
54659
- this.onClose = onClose;
54660
- }
54661
- if (onBeforeFormatUrl) {
54662
- this.onBeforeFormatUrl = onBeforeFormatUrl;
54894
+ this.onClose = onClose != null ? onClose : null;
54895
+ this.onBeforeFormatUrl = onBeforeFormatUrl != null ? onBeforeFormatUrl : null;
54896
+ try {
54897
+ this.showPopup(options);
54898
+ } catch (error2) {
54899
+ this._destroy();
54900
+ throw error2;
54663
54901
  }
54664
54902
  }
54665
54903
  createPopupContent(linkText, linkUrl) {
@@ -54688,12 +54926,21 @@ ${codeText}
54688
54926
  return content;
54689
54927
  }
54690
54928
  _destroy() {
54691
- var _a, _b;
54929
+ this.session += 1;
54930
+ clearTimeout(this.focusTimer);
54931
+ this.focusTimer = void 0;
54932
+ const popup = this.linkPopup;
54933
+ const releaseFocus = this.releaseFocus;
54934
+ this.linkPopup = null;
54935
+ this.releaseFocus = void 0;
54936
+ this.submitting = false;
54692
54937
  this.onFinished = null;
54693
54938
  this.onClose = null;
54939
+ this.onBeforeFormatUrl = null;
54694
54940
  this.form = {};
54695
- (_a = this.linkPopup) == null ? void 0 : _a.removeAllListeners();
54696
- (_b = this.linkPopup) == null ? void 0 : _b.destroy();
54941
+ popup == null ? void 0 : popup.removeAllListeners();
54942
+ popup == null ? void 0 : popup.destroy();
54943
+ releaseFocus == null ? void 0 : releaseFocus();
54697
54944
  }
54698
54945
  destroy() {
54699
54946
  this._destroy();
@@ -54733,14 +54980,14 @@ ${codeText}
54733
54980
  const text2 = [op1, op2];
54734
54981
  editor.insertTextToBlock(block, 0, text2);
54735
54982
  editor.selection.selectBlock(block, getTextLength(text2));
54736
- editor.focus();
54983
+ requestEditorFocus(editor);
54737
54984
  } catch (error2) {
54738
54985
  logger$21.error(error2);
54739
54986
  }
54740
54987
  });
54741
54988
  };
54742
54989
  const onClose = () => {
54743
- editor.focus();
54990
+ requestEditorFocus(editor);
54744
54991
  };
54745
54992
  editLink(editor, {
54746
54993
  target: block,
@@ -54772,21 +55019,21 @@ ${codeText}
54772
55019
  if (isCollapsed) {
54773
55020
  newText = insertText(editor.getBlockText(block), end, linkText, { link: linkUrl });
54774
55021
  editor.setBlockText(block, newText);
54775
- editor.focus();
55022
+ requestEditorFocus(editor);
54776
55023
  return;
54777
55024
  }
54778
55025
  newText = replaceText(editor.getBlockText(block), start, end, linkText);
54779
55026
  newText = rangeAddAttribute(start, linkText.length, newText, "link", linkUrl);
54780
55027
  editor.setBlockText(block, newText);
54781
55028
  editor.selection.selectBlock(block, start + linkText.length);
54782
- editor.focus();
55029
+ requestEditorFocus(editor);
54783
55030
  } catch (error2) {
54784
55031
  logger$21.error(error2);
54785
55032
  }
54786
55033
  });
54787
55034
  };
54788
55035
  const onClose = () => {
54789
- editor.focus();
55036
+ requestEditorFocus(editor);
54790
55037
  };
54791
55038
  editLink(editor, {
54792
55039
  target: anchor2,
@@ -55035,14 +55282,14 @@ ${codeText}
55035
55282
  const newText = insertText(deleted, range.start, linkText, { link: linkUrl });
55036
55283
  editor.setBlockText(block, newText);
55037
55284
  editor.selection.selectBlock(block, range.start + linkText.length);
55038
- editor.focus();
55285
+ requestEditorFocus(editor);
55039
55286
  } catch (error2) {
55040
55287
  logger$20.error(error2);
55041
55288
  }
55042
55289
  };
55043
55290
  const onClose = () => {
55044
55291
  editor.selection.selectBlock(block, range.end);
55045
- editor.focus();
55292
+ requestEditorFocus(editor);
55046
55293
  };
55047
55294
  const rect = getBlockClientRects(editor, block, editor.selection.range)[0];
55048
55295
  if (!rect) {
@@ -55544,6 +55791,7 @@ ${codeText}
55544
55791
  __publicField(this, "_disableTextToolbar", false);
55545
55792
  __publicField(this, "_lastClickTime", 0);
55546
55793
  __publicField(this, "_disabled", false);
55794
+ __publicField(this, "cancelFocus");
55547
55795
  __publicField(this, "isInToolbar", (event) => this.toolbar.isInCommandBar(event.target));
55548
55796
  __publicField(this, "handleButtonClick", (toolbar2, item) => {
55549
55797
  const tooltip = this.editor.findCustom("editor-tooltip");
@@ -55567,17 +55815,23 @@ ${codeText}
55567
55815
  }
55568
55816
  this._lastClickTime = Date.now();
55569
55817
  });
55570
- __publicField(this, "handleClose", () => {
55571
- var _a;
55818
+ __publicField(this, "handleClose", (toolbar2) => {
55819
+ var _a, _b;
55820
+ if (toolbar2.closeReason === "clickBackground" || !this.editor.rootElement.isConnected) {
55821
+ return;
55822
+ }
55823
+ const focusedElement = document.activeElement;
55824
+ if (focusedElement && focusedElement !== document.body && focusedElement !== document.documentElement && !this.editor.rootElement.contains(focusedElement) && !toolbar2.isInCommandBar(focusedElement)) {
55825
+ return;
55826
+ }
55572
55827
  if (shouldFocusToEditor(this.editor) && !clientType.isMobile) {
55573
55828
  const activeElement = (_a = document.getSelection()) == null ? void 0 : _a.focusNode;
55574
55829
  if (activeElement && isInBlockTools(activeElement)) {
55575
55830
  logger$1_.debug("focus is in block tools, ignore focus");
55576
55831
  return;
55577
55832
  }
55578
- this.editor.focus({
55579
- preventScroll: true
55580
- });
55833
+ (_b = this.cancelFocus) == null ? void 0 : _b.call(this);
55834
+ this.cancelFocus = requestEditorFocus(this.editor);
55581
55835
  }
55582
55836
  });
55583
55837
  __publicField(this, "handleClosing", (bar2, event) => {
@@ -55706,8 +55960,10 @@ ${codeText}
55706
55960
  this.toolbar.close("selectionChange");
55707
55961
  }
55708
55962
  destroy() {
55963
+ var _a;
55709
55964
  this.unbindEvents();
55710
55965
  this.toolbar.destroy();
55966
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
55711
55967
  this.editor.removeListener("selectionChanged", this.handleSelectionChange);
55712
55968
  OnesEditorHoveringBlock.get(this.editor).removeListener("childChange", this.handleHoveringBlockChildChange);
55713
55969
  }
@@ -74272,10 +74528,11 @@ ${codeText}
74272
74528
  createTableBorderBar(editor, tableBlock);
74273
74529
  }
74274
74530
  function hideTableBorderBar(editor, tableBlock) {
74531
+ var _a, _b;
74275
74532
  assert(logger$14, isTableBlock(tableBlock), "invalid table block");
74276
- const tools = getTableTools(editor, tableBlock);
74533
+ const tools = (_a = getBlockContent(tableBlock).querySelector("div[data-type=table-content]")) == null ? void 0 : _a.querySelector(":scope >div[data-type=table-tools]");
74277
74534
  const blockTools = getBlockTools(tableBlock);
74278
- const topTableBorderBar = tools.querySelectorAll(".table-border-bar-container");
74535
+ const topTableBorderBar = (_b = tools == null ? void 0 : tools.querySelectorAll(".table-border-bar-container")) != null ? _b : [];
74279
74536
  const leftTableBorderBar = blockTools.querySelectorAll(".table-border-bar-container");
74280
74537
  topTableBorderBar.forEach((elem) => {
74281
74538
  editor.domEvents.removeEventListener(elem, "click", handleBorderBarClicked);
@@ -97316,14 +97573,13 @@ ${JSON.stringify(error2, null, 2)}`);
97316
97573
  __publicField(this, "toolbar");
97317
97574
  __publicField(this, "toolbarFullScreen");
97318
97575
  __publicField(this, "root");
97576
+ __publicField(this, "cancelFocus");
97319
97577
  __publicField(this, "handleClick", (bar2, item) => {
97320
97578
  const command = item;
97321
97579
  if (command.onClick) {
97322
97580
  command.onClick(this.editor, command, bar2);
97323
97581
  this.handleSelectionChanged();
97324
- setTimeout(() => {
97325
- this.editor.focus();
97326
- });
97582
+ this.restoreFocus();
97327
97583
  return;
97328
97584
  }
97329
97585
  const findParent = (item2, id) => {
@@ -97369,9 +97625,7 @@ ${JSON.stringify(error2, null, 2)}`);
97369
97625
  id: "main-toolbar"
97370
97626
  });
97371
97627
  this.toolbar.addListener("closing", () => {
97372
- setTimeout(() => {
97373
- editor.focus();
97374
- }, 100);
97628
+ this.restoreFocus(100);
97375
97629
  return true;
97376
97630
  });
97377
97631
  this.toolbar.addListener("click", this.handleClick);
@@ -97402,11 +97656,18 @@ ${JSON.stringify(error2, null, 2)}`);
97402
97656
  });
97403
97657
  }
97404
97658
  }
97405
- destroy() {
97659
+ restoreFocus(delay = 0) {
97406
97660
  var _a;
97661
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
97662
+ this.cancelFocus = requestEditorFocus(this.editor, delay);
97663
+ }
97664
+ destroy() {
97665
+ var _a, _b, _c;
97666
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
97407
97667
  this.toolbar.destroy();
97408
97668
  this.editor.removeListener("selectionChanged", this.handleSelectionChanged);
97409
- (_a = this.toolbarFullScreen) == null ? void 0 : _a.destroy();
97669
+ (_b = this.toolbarFullScreen) == null ? void 0 : _b.destroy();
97670
+ (_c = this.cancelFocus) == null ? void 0 : _c.call(this);
97410
97671
  this.root.remove();
97411
97672
  }
97412
97673
  setItems(items) {
@@ -98129,7 +98390,7 @@ ${JSON.stringify(error2, null, 2)}`);
98129
98390
  }
98130
98391
  }
98131
98392
  });
98132
- editor.version = "5.0.8-beta.ONES22303263.1";
98393
+ editor.version = "5.0.8-beta.ONES22303310.1";
98133
98394
  return editor;
98134
98395
  }
98135
98396
  function isDoc(doc2) {
@@ -98265,7 +98526,7 @@ ${JSON.stringify(error2, null, 2)}`);
98265
98526
  OnesEditorDropTarget.register(editor);
98266
98527
  OnesEditorTocProvider.register(editor);
98267
98528
  OnesEditorExclusiveBlock.register(editor);
98268
- editor.version = "5.0.8-beta.ONES22303263.1";
98529
+ editor.version = "5.0.8-beta.ONES22303310.1";
98269
98530
  return editor;
98270
98531
  } catch (error2) {
98271
98532
  try {
@@ -98408,7 +98669,7 @@ ${JSON.stringify(error2, null, 2)}`);
98408
98669
  }
98409
98670
  }
98410
98671
  });
98411
- editor.version = "5.0.8-beta.ONES22303263.1";
98672
+ editor.version = "5.0.8-beta.ONES22303310.1";
98412
98673
  return editor;
98413
98674
  }
98414
98675
  const emojis$1 = {
@@ -144234,6 +144495,7 @@ ${JSON.stringify(error2, null, 2)}`);
144234
144495
  exports2.FindReplaceItem = FindReplaceItem;
144235
144496
  exports2.FitIcon = FitIcon;
144236
144497
  exports2.FixedToolbar = FixedToolbar;
144498
+ exports2.FocusRestoration = FocusRestoration;
144237
144499
  exports2.FontBoldItem = FontBoldItem;
144238
144500
  exports2.FontItalicItem = FontItalicItem;
144239
144501
  exports2.FontStrikethroughItem = FontStrikethroughItem;
@@ -144433,6 +144695,7 @@ ${JSON.stringify(error2, null, 2)}`);
144433
144695
  exports2.YoukuIcon = YoukuIcon;
144434
144696
  exports2.ZERO_WIDTH_SPACE_CHAR = ZERO_WIDTH_SPACE_CHAR;
144435
144697
  exports2.abbreviation = abbreviation;
144698
+ exports2.acquireEditorFocus = acquireEditorFocus;
144436
144699
  exports2.activeComment = activeComment;
144437
144700
  exports2.addAttribute = addAttribute;
144438
144701
  exports2.addClass = addClass;
@@ -144925,6 +145188,8 @@ ${JSON.stringify(error2, null, 2)}`);
144925
145188
  exports2.replaceText = replaceText;
144926
145189
  exports2.replaceWikiMacroResources = replaceWikiMacroResources;
144927
145190
  exports2.requestDownload = requestDownload;
145191
+ exports2.requestEditorFocus = requestEditorFocus;
145192
+ exports2.requestInputFocus = requestInputFocus;
144928
145193
  exports2.resetListStart = resetListStart;
144929
145194
  exports2.saveAs = saveAs$1;
144930
145195
  exports2.scrollBarIntoView = scrollBarIntoView;