@ones-editor/editor 4.0.7-beta.h1474.1 → 4.0.7-beta.h1474.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.
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 {
@@ -54227,6 +54386,7 @@ ${codeText}
54227
54386
  });
54228
54387
  __publicField(this, "close", () => {
54229
54388
  this.linkPopup.close("cancelBar");
54389
+ this.emit("close");
54230
54390
  });
54231
54391
  __publicField(this, "createItems", (content) => [{
54232
54392
  id: "link-popup",
@@ -54248,7 +54408,6 @@ ${codeText}
54248
54408
  this.emit("finished");
54249
54409
  });
54250
54410
  __publicField(this, "handleClose", () => {
54251
- this.emit("close");
54252
54411
  this.close();
54253
54412
  });
54254
54413
  __publicField(this, "handleShown", () => {
@@ -54277,8 +54436,13 @@ ${codeText}
54277
54436
  this.linkPopup = new MobileBottomMenu(this.createItems(content), void 0, {
54278
54437
  id: "link-popup",
54279
54438
  header: this.createHeader(),
54280
- disableSelectedScroll: true
54439
+ disableSelectedScroll: true,
54440
+ beforeClose: () => {
54441
+ this.handleClose();
54442
+ return true;
54443
+ }
54281
54444
  });
54445
+ this.linkPopup.addListener("close", () => this.emit("close"));
54282
54446
  }
54283
54447
  destroy() {
54284
54448
  var _a, _b;
@@ -54297,11 +54461,11 @@ ${codeText}
54297
54461
  this.emit("finished");
54298
54462
  });
54299
54463
  __publicField(this, "handleClose", () => {
54300
- this.emit("close");
54301
54464
  this.close();
54302
54465
  });
54303
54466
  __publicField(this, "close", () => {
54304
54467
  this.linkPopup.close("cancelBar");
54468
+ this.emit("close");
54305
54469
  });
54306
54470
  __publicField(this, "disabledSubmit", () => {
54307
54471
  var _a;
@@ -54321,8 +54485,13 @@ ${codeText}
54321
54485
  this.content = content;
54322
54486
  this.options = options;
54323
54487
  this.linkPopup = new Popup(this.createPopupContent(), {
54324
- id: "link-popup"
54488
+ id: "link-popup",
54489
+ beforeClose: () => {
54490
+ this.handleClose();
54491
+ return true;
54492
+ }
54325
54493
  });
54494
+ this.linkPopup.addListener("close", () => this.emit("close"));
54326
54495
  }
54327
54496
  createPopupContent() {
54328
54497
  const container = createElement("div", ["link-popup-content"], null);
@@ -54355,46 +54524,85 @@ ${codeText}
54355
54524
  __publicField(this, "onFinished", null);
54356
54525
  __publicField(this, "onClose", null);
54357
54526
  __publicField(this, "onBeforeFormatUrl", null);
54527
+ __publicField(this, "session", 0);
54528
+ __publicField(this, "submitting", false);
54529
+ __publicField(this, "focusTimer");
54530
+ __publicField(this, "releaseFocus");
54358
54531
  __publicField(this, "handleOk", async () => {
54359
- var _a;
54532
+ if (!this.linkPopup || this.submitting || this.editor.destroyed) {
54533
+ return;
54534
+ }
54535
+ const session = this.session;
54536
+ const form = this.form;
54537
+ const onFinished = this.onFinished;
54538
+ const beforeFormatUrl = this.onBeforeFormatUrl;
54539
+ this.submitting = true;
54360
54540
  try {
54361
- await this.checkValue();
54362
- const { linkText, linkUrl } = this.form;
54541
+ await this.checkValue(form);
54542
+ if (!this.isCurrent(session)) {
54543
+ return;
54544
+ }
54545
+ const { linkText, linkUrl } = form;
54363
54546
  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);
54547
+ const formatted = beforeFormatUrl == null ? void 0 : beforeFormatUrl(inputValue);
54548
+ const link2 = typeof formatted === "string" ? formatted : this.formatUrl(inputValue);
54549
+ const text2 = linkText.getInputValue() || link2;
54550
+ if (!this.isCurrent(session)) {
54551
+ return;
54369
54552
  }
54370
- (_a = this.onFinished) == null ? void 0 : _a.call(this, linkText.getInputValue() || link2, link2);
54371
54553
  this._destroy();
54554
+ onFinished == null ? void 0 : onFinished(text2, link2);
54372
54555
  } catch (error2) {
54556
+ if (this.isCurrent(session)) {
54557
+ this.submitting = false;
54558
+ }
54373
54559
  }
54374
54560
  });
54375
54561
  __publicField(this, "handleClose", () => {
54376
- var _a;
54377
- (_a = this.onClose) == null ? void 0 : _a.call(this);
54562
+ if (!this.linkPopup) {
54563
+ return;
54564
+ }
54565
+ const onClose = this.onClose;
54378
54566
  this._destroy();
54567
+ if (!this.editor.destroyed) {
54568
+ onClose == null ? void 0 : onClose();
54569
+ }
54379
54570
  });
54380
54571
  __publicField(this, "onShow", () => {
54381
- setTimeout(() => {
54382
- var _a;
54383
- (_a = this.form.linkUrl) == null ? void 0 : _a.getField().focus();
54572
+ var _a;
54573
+ const session = this.session;
54574
+ const field = (_a = this.form.linkUrl) == null ? void 0 : _a.getField();
54575
+ clearTimeout(this.focusTimer);
54576
+ this.focusTimer = setTimeout(() => {
54577
+ if (this.isCurrent(session) && (field == null ? void 0 : field.isConnected)) {
54578
+ field.focus(clientType.isMobile ? void 0 : { preventScroll: true });
54579
+ }
54384
54580
  }, 400);
54385
54581
  });
54386
54582
  __publicField(this, "showPopup", (options) => {
54387
- var _a, _b, _c, _d, _e;
54583
+ const session = this.session;
54388
54584
  if (clientType.isMobile) {
54389
54585
  this.linkPopup = new MobileLinkPopup(this.editor, this.createPopupContent(options.linkText, options.linkUrl), options);
54390
54586
  } else {
54391
54587
  this.linkPopup = new LinkPopup(this.createPopupContent(options.linkText, options.linkUrl), options);
54392
54588
  }
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);
54589
+ this.linkPopup.addListener("show", () => {
54590
+ if (this.isCurrent(session)) {
54591
+ this.onShow();
54592
+ }
54593
+ });
54594
+ this.linkPopup.addListener("finished", () => {
54595
+ if (this.isCurrent(session)) {
54596
+ this.handleOk();
54597
+ }
54598
+ });
54599
+ this.linkPopup.addListener("close", () => {
54600
+ if (this.isCurrent(session)) {
54601
+ this.handleClose();
54602
+ }
54603
+ });
54604
+ this.linkPopup.disabledSubmit();
54605
+ this.linkPopup.manualShow();
54398
54606
  });
54399
54607
  __publicField(this, "onChange", () => {
54400
54608
  var _a;
@@ -54406,11 +54614,14 @@ ${codeText}
54406
54614
  return;
54407
54615
  }
54408
54616
  if (event.key === "Enter") {
54409
- (_a = this.form.linkUrl) == null ? void 0 : _a.getField().focus();
54617
+ (_a = this.form.linkUrl) == null ? void 0 : _a.getField().focus(clientType.isMobile ? void 0 : { preventScroll: true });
54618
+ event.preventDefault();
54410
54619
  event.stopPropagation();
54411
54620
  return;
54412
54621
  }
54413
54622
  if (event.key === "Escape") {
54623
+ event.preventDefault();
54624
+ event.stopPropagation();
54414
54625
  this.handleClose();
54415
54626
  }
54416
54627
  });
@@ -54424,13 +54635,18 @@ ${codeText}
54424
54635
  return;
54425
54636
  }
54426
54637
  if (event.key === "Escape") {
54638
+ event.preventDefault();
54639
+ event.stopPropagation();
54427
54640
  this.handleClose();
54428
54641
  }
54429
54642
  });
54430
54643
  this.editor = editor;
54431
54644
  }
54432
- async checkValue() {
54433
- const { linkUrl } = this.form;
54645
+ isCurrent(session) {
54646
+ return session === this.session && !!this.linkPopup && !this.editor.destroyed;
54647
+ }
54648
+ async checkValue(form) {
54649
+ const { linkUrl } = form;
54434
54650
  if (!linkUrl.getInputValue()) {
54435
54651
  linkUrl.setFieldError(i18n$1.t("link.urlPlaceholder"));
54436
54652
  throw new Error(i18n$1.t("link.urlPlaceholder"));
@@ -54448,14 +54664,20 @@ ${codeText}
54448
54664
  return link2;
54449
54665
  }
54450
54666
  editLink(options) {
54451
- this.showPopup(options);
54667
+ this._destroy();
54668
+ if (this.editor.destroyed) {
54669
+ return;
54670
+ }
54671
+ this.releaseFocus = acquireEditorFocus(this.editor);
54452
54672
  const { onFinished, onClose, onBeforeFormatUrl } = options;
54453
54673
  this.onFinished = onFinished;
54454
- if (onClose) {
54455
- this.onClose = onClose;
54456
- }
54457
- if (onBeforeFormatUrl) {
54458
- this.onBeforeFormatUrl = onBeforeFormatUrl;
54674
+ this.onClose = onClose != null ? onClose : null;
54675
+ this.onBeforeFormatUrl = onBeforeFormatUrl != null ? onBeforeFormatUrl : null;
54676
+ try {
54677
+ this.showPopup(options);
54678
+ } catch (error2) {
54679
+ this._destroy();
54680
+ throw error2;
54459
54681
  }
54460
54682
  }
54461
54683
  createPopupContent(linkText, linkUrl) {
@@ -54484,12 +54706,21 @@ ${codeText}
54484
54706
  return content;
54485
54707
  }
54486
54708
  _destroy() {
54487
- var _a, _b;
54709
+ this.session += 1;
54710
+ clearTimeout(this.focusTimer);
54711
+ this.focusTimer = void 0;
54712
+ const popup = this.linkPopup;
54713
+ const releaseFocus = this.releaseFocus;
54714
+ this.linkPopup = null;
54715
+ this.releaseFocus = void 0;
54716
+ this.submitting = false;
54488
54717
  this.onFinished = null;
54489
54718
  this.onClose = null;
54719
+ this.onBeforeFormatUrl = null;
54490
54720
  this.form = {};
54491
- (_a = this.linkPopup) == null ? void 0 : _a.removeAllListeners();
54492
- (_b = this.linkPopup) == null ? void 0 : _b.destroy();
54721
+ popup == null ? void 0 : popup.removeAllListeners();
54722
+ popup == null ? void 0 : popup.destroy();
54723
+ releaseFocus == null ? void 0 : releaseFocus();
54493
54724
  }
54494
54725
  destroy() {
54495
54726
  this._destroy();
@@ -54529,14 +54760,14 @@ ${codeText}
54529
54760
  const text2 = [op1, op2];
54530
54761
  editor.insertTextToBlock(block, 0, text2);
54531
54762
  editor.selection.selectBlock(block, getTextLength(text2));
54532
- editor.focus();
54763
+ requestEditorFocus(editor);
54533
54764
  } catch (error2) {
54534
54765
  logger$20.error(error2);
54535
54766
  }
54536
54767
  });
54537
54768
  };
54538
54769
  const onClose = () => {
54539
- editor.focus();
54770
+ requestEditorFocus(editor);
54540
54771
  };
54541
54772
  editLink(editor, {
54542
54773
  target: block,
@@ -54568,21 +54799,21 @@ ${codeText}
54568
54799
  if (isCollapsed) {
54569
54800
  newText = insertText(editor.getBlockText(block), end, linkText, { link: linkUrl });
54570
54801
  editor.setBlockText(block, newText);
54571
- editor.focus();
54802
+ requestEditorFocus(editor);
54572
54803
  return;
54573
54804
  }
54574
54805
  newText = replaceText(editor.getBlockText(block), start, end, linkText);
54575
54806
  newText = rangeAddAttribute(start, linkText.length, newText, "link", linkUrl);
54576
54807
  editor.setBlockText(block, newText);
54577
54808
  editor.selection.selectBlock(block, start + linkText.length);
54578
- editor.focus();
54809
+ requestEditorFocus(editor);
54579
54810
  } catch (error2) {
54580
54811
  logger$20.error(error2);
54581
54812
  }
54582
54813
  });
54583
54814
  };
54584
54815
  const onClose = () => {
54585
- editor.focus();
54816
+ requestEditorFocus(editor);
54586
54817
  };
54587
54818
  editLink(editor, {
54588
54819
  target: anchor2,
@@ -54831,14 +55062,14 @@ ${codeText}
54831
55062
  const newText = insertText(deleted, range.start, linkText, { link: linkUrl });
54832
55063
  editor.setBlockText(block, newText);
54833
55064
  editor.selection.selectBlock(block, range.start + linkText.length);
54834
- editor.focus();
55065
+ requestEditorFocus(editor);
54835
55066
  } catch (error2) {
54836
55067
  logger$1$.error(error2);
54837
55068
  }
54838
55069
  };
54839
55070
  const onClose = () => {
54840
55071
  editor.selection.selectBlock(block, range.end);
54841
- editor.focus();
55072
+ requestEditorFocus(editor);
54842
55073
  };
54843
55074
  const rect = getBlockClientRects(editor, block, editor.selection.range)[0];
54844
55075
  if (!rect) {
@@ -55340,6 +55571,7 @@ ${codeText}
55340
55571
  __publicField(this, "_disableTextToolbar", false);
55341
55572
  __publicField(this, "_lastClickTime", 0);
55342
55573
  __publicField(this, "_disabled", false);
55574
+ __publicField(this, "cancelFocus");
55343
55575
  __publicField(this, "isInToolbar", (event) => this.toolbar.isInCommandBar(event.target));
55344
55576
  __publicField(this, "handleButtonClick", (toolbar2, item) => {
55345
55577
  const tooltip = this.editor.findCustom("editor-tooltip");
@@ -55363,17 +55595,23 @@ ${codeText}
55363
55595
  }
55364
55596
  this._lastClickTime = Date.now();
55365
55597
  });
55366
- __publicField(this, "handleClose", () => {
55367
- var _a;
55598
+ __publicField(this, "handleClose", (toolbar2) => {
55599
+ var _a, _b;
55600
+ if (toolbar2.closeReason === "clickBackground" || !this.editor.rootElement.isConnected) {
55601
+ return;
55602
+ }
55603
+ const focusedElement = document.activeElement;
55604
+ if (focusedElement && focusedElement !== document.body && focusedElement !== document.documentElement && !this.editor.rootElement.contains(focusedElement) && !toolbar2.isInCommandBar(focusedElement)) {
55605
+ return;
55606
+ }
55368
55607
  if (shouldFocusToEditor(this.editor) && !clientType.isMobile) {
55369
55608
  const activeElement = (_a = document.getSelection()) == null ? void 0 : _a.focusNode;
55370
55609
  if (activeElement && isInBlockTools(activeElement)) {
55371
55610
  logger$1Z.debug("focus is in block tools, ignore focus");
55372
55611
  return;
55373
55612
  }
55374
- this.editor.focus({
55375
- preventScroll: true
55376
- });
55613
+ (_b = this.cancelFocus) == null ? void 0 : _b.call(this);
55614
+ this.cancelFocus = requestEditorFocus(this.editor);
55377
55615
  }
55378
55616
  });
55379
55617
  __publicField(this, "handleClosing", (bar2, event) => {
@@ -55502,8 +55740,10 @@ ${codeText}
55502
55740
  this.toolbar.close("selectionChange");
55503
55741
  }
55504
55742
  destroy() {
55743
+ var _a;
55505
55744
  this.unbindEvents();
55506
55745
  this.toolbar.destroy();
55746
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
55507
55747
  this.editor.removeListener("selectionChanged", this.handleSelectionChange);
55508
55748
  OnesEditorHoveringBlock.get(this.editor).removeListener("childChange", this.handleHoveringBlockChildChange);
55509
55749
  }
@@ -96935,14 +97175,13 @@ ${JSON.stringify(error2, null, 2)}`);
96935
97175
  __publicField(this, "toolbar");
96936
97176
  __publicField(this, "toolbarFullScreen");
96937
97177
  __publicField(this, "root");
97178
+ __publicField(this, "cancelFocus");
96938
97179
  __publicField(this, "handleClick", (bar2, item) => {
96939
97180
  const command = item;
96940
97181
  if (command.onClick) {
96941
97182
  command.onClick(this.editor, command, bar2);
96942
97183
  this.handleSelectionChanged();
96943
- setTimeout(() => {
96944
- this.editor.focus();
96945
- });
97184
+ this.restoreFocus();
96946
97185
  return;
96947
97186
  }
96948
97187
  const findParent = (item2, id) => {
@@ -96988,9 +97227,7 @@ ${JSON.stringify(error2, null, 2)}`);
96988
97227
  id: "main-toolbar"
96989
97228
  });
96990
97229
  this.toolbar.addListener("closing", () => {
96991
- setTimeout(() => {
96992
- editor.focus();
96993
- }, 100);
97230
+ this.restoreFocus(100);
96994
97231
  return true;
96995
97232
  });
96996
97233
  this.toolbar.addListener("click", this.handleClick);
@@ -97021,11 +97258,18 @@ ${JSON.stringify(error2, null, 2)}`);
97021
97258
  });
97022
97259
  }
97023
97260
  }
97024
- destroy() {
97261
+ restoreFocus(delay = 0) {
97025
97262
  var _a;
97263
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
97264
+ this.cancelFocus = requestEditorFocus(this.editor, delay);
97265
+ }
97266
+ destroy() {
97267
+ var _a, _b, _c;
97268
+ (_a = this.cancelFocus) == null ? void 0 : _a.call(this);
97026
97269
  this.toolbar.destroy();
97027
97270
  this.editor.removeListener("selectionChanged", this.handleSelectionChanged);
97028
- (_a = this.toolbarFullScreen) == null ? void 0 : _a.destroy();
97271
+ (_b = this.toolbarFullScreen) == null ? void 0 : _b.destroy();
97272
+ (_c = this.cancelFocus) == null ? void 0 : _c.call(this);
97029
97273
  this.root.remove();
97030
97274
  }
97031
97275
  setItems(items) {
@@ -97617,7 +97861,7 @@ ${JSON.stringify(error2, null, 2)}`);
97617
97861
  }
97618
97862
  }
97619
97863
  });
97620
- editor.version = "4.0.7-beta.h1474.1";
97864
+ editor.version = "4.0.7-beta.h1474.3";
97621
97865
  return editor;
97622
97866
  }
97623
97867
  function isDoc(doc2) {
@@ -97753,7 +97997,7 @@ ${JSON.stringify(error2, null, 2)}`);
97753
97997
  OnesEditorDropTarget.register(editor);
97754
97998
  OnesEditorTocProvider.register(editor);
97755
97999
  OnesEditorExclusiveBlock.register(editor);
97756
- editor.version = "4.0.7-beta.h1474.1";
98000
+ editor.version = "4.0.7-beta.h1474.3";
97757
98001
  return editor;
97758
98002
  } catch (error2) {
97759
98003
  try {
@@ -97896,7 +98140,7 @@ ${JSON.stringify(error2, null, 2)}`);
97896
98140
  }
97897
98141
  }
97898
98142
  });
97899
- editor.version = "4.0.7-beta.h1474.1";
98143
+ editor.version = "4.0.7-beta.h1474.3";
97900
98144
  return editor;
97901
98145
  }
97902
98146
  const emojis$1 = {
@@ -143722,6 +143966,7 @@ ${JSON.stringify(error2, null, 2)}`);
143722
143966
  exports2.FindReplaceItem = FindReplaceItem;
143723
143967
  exports2.FitIcon = FitIcon;
143724
143968
  exports2.FixedToolbar = FixedToolbar;
143969
+ exports2.FocusRestoration = FocusRestoration;
143725
143970
  exports2.FontBoldItem = FontBoldItem;
143726
143971
  exports2.FontItalicItem = FontItalicItem;
143727
143972
  exports2.FontStrikethroughItem = FontStrikethroughItem;
@@ -143920,6 +144165,7 @@ ${JSON.stringify(error2, null, 2)}`);
143920
144165
  exports2.YoukuIcon = YoukuIcon;
143921
144166
  exports2.ZERO_WIDTH_SPACE_CHAR = ZERO_WIDTH_SPACE_CHAR;
143922
144167
  exports2.abbreviation = abbreviation;
144168
+ exports2.acquireEditorFocus = acquireEditorFocus;
143923
144169
  exports2.activeComment = activeComment;
143924
144170
  exports2.addAttribute = addAttribute;
143925
144171
  exports2.addClass = addClass;
@@ -144405,6 +144651,8 @@ ${JSON.stringify(error2, null, 2)}`);
144405
144651
  exports2.replaceAllText = replaceAllText;
144406
144652
  exports2.replaceText = replaceText;
144407
144653
  exports2.requestDownload = requestDownload;
144654
+ exports2.requestEditorFocus = requestEditorFocus;
144655
+ exports2.requestInputFocus = requestInputFocus;
144408
144656
  exports2.resetListStart = resetListStart;
144409
144657
  exports2.saveAs = saveAs$1;
144410
144658
  exports2.scrollBarIntoView = scrollBarIntoView;