@usememos/editor 26.10.4 → 26.10.6

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
@@ -4005,21 +4005,27 @@ function representableInline(inline3) {
4005
4005
  endRun();
4006
4006
  for (const [from, to] of removals) spans = setMarkInRange(spans, from, to, mark, false);
4007
4007
  }
4008
- return dropLineEndSpaces(spans);
4008
+ return dropLineEdgeSpaces(spans);
4009
4009
  }
4010
4010
  var isPlainSpan = (s) => !s.link && !s.image && !s.token && !s.marks.includes("code") && !s.marks.includes("html");
4011
- function dropLineEndSpaces(spans) {
4011
+ function dropLineEdgeSpaces(spans) {
4012
4012
  const text = spans.map((s) => s.text).join("");
4013
- if (!/[ \t](\n|$)/.test(text)) return spans;
4013
+ if (!/[ \t](\n|$)|(^|\n)[ \t]/.test(text)) return spans;
4014
4014
  const plain2 = [];
4015
4015
  for (const s of spans) for (let i = 0; i < s.text.length; i++) plain2.push(isPlainSpan(s));
4016
4016
  const removals = [];
4017
4017
  let lineStart = 0;
4018
4018
  for (let i = 0; i <= text.length; i++) {
4019
4019
  if (i < text.length && text[i] !== "\n") continue;
4020
+ const space = (k) => (text[k] === " " || text[k] === " ") && plain2[k];
4021
+ let to = lineStart;
4022
+ while (to < i && space(to)) to++;
4020
4023
  let from = i;
4021
- while (from > lineStart && (text[from - 1] === " " || text[from - 1] === " ") && plain2[from - 1]) from--;
4022
- if (from < i && /[^ \t]/.test(text.slice(lineStart, from))) removals.push([from, i]);
4024
+ while (from > lineStart && space(from - 1)) from--;
4025
+ if (to < from) {
4026
+ if (to > lineStart) removals.push([lineStart, to]);
4027
+ if (from < i) removals.push([from, i]);
4028
+ }
4023
4029
  lineStart = i + 1;
4024
4030
  }
4025
4031
  let out = spans;
@@ -6363,6 +6369,10 @@ var BlockHandle = class {
6363
6369
  this.el.style.left = `${Math.round(rect.left - (this.el.offsetWidth || 20) - 6 - offset.x)}px`;
6364
6370
  this.el.style.top = `${Math.round(rect.top + firstLine / 2 - size / 2 - offset.y)}px`;
6365
6371
  }
6372
+ /** True when `node` is the handle or inside it. */
6373
+ contains(node) {
6374
+ return this.el.contains(node);
6375
+ }
6366
6376
  hide() {
6367
6377
  this.cancelHide();
6368
6378
  this.el.hidden = true;
@@ -6741,6 +6751,7 @@ var Editor = class {
6741
6751
  this.listen(root.ownerDocument, "selectionchange", () => this.onSelectionChange());
6742
6752
  this.listen(root.ownerDocument, "mouseup", () => this.onMouseUp());
6743
6753
  this.listen(root.ownerDocument, "scroll", () => this.onScroll(), true);
6754
+ this.listen(root.ownerDocument, "pointerdown", (e) => this.onDocumentPointerDown(e), true);
6744
6755
  this.paint(true, true);
6745
6756
  this.afterRender();
6746
6757
  for (const ext of this.extensions) {
@@ -6967,8 +6978,13 @@ var Editor = class {
6967
6978
  applyRuntime(options) {
6968
6979
  const root = this.root;
6969
6980
  if ("placeholder" in options) {
6970
- if (options.placeholder) root.style.setProperty("--mbe-placeholder", JSON.stringify(options.placeholder));
6971
- else root.style.removeProperty("--mbe-placeholder");
6981
+ if (options.placeholder) {
6982
+ root.style.setProperty("--mbe-placeholder", JSON.stringify(options.placeholder));
6983
+ root.setAttribute("aria-placeholder", options.placeholder);
6984
+ } else {
6985
+ root.style.removeProperty("--mbe-placeholder");
6986
+ root.removeAttribute("aria-placeholder");
6987
+ }
6972
6988
  this.runtime = { ...this.runtime, placeholder: options.placeholder };
6973
6989
  }
6974
6990
  if ("attributes" in options) {
@@ -8419,6 +8435,8 @@ var Editor = class {
8419
8435
  const start = startOf(block3);
8420
8436
  this.current = { ...this.state, selection: { anchor: start, focus: start } };
8421
8437
  this.focus();
8438
+ } else if (!this.hasFocus()) {
8439
+ this.focus();
8422
8440
  }
8423
8441
  this.blockMenu.setSections(this.blockMenuSections(blockId));
8424
8442
  const rect = anchor ?? this.blockElement(blockId)?.getBoundingClientRect();
@@ -8429,6 +8447,13 @@ var Editor = class {
8429
8447
  this.blockMenu.hide();
8430
8448
  this.syncActiveDescendant();
8431
8449
  }
8450
+ /** A press anywhere but the block menu or the block handle closes the menu, in the editor's text too. */
8451
+ onDocumentPointerDown(e) {
8452
+ if (!this.blockMenu.isOpen) return;
8453
+ const target = e.target;
8454
+ if (target && (this.blockMenu.popup.el.contains(target) || this.handle.contains(target))) return;
8455
+ this.closeBlockMenu();
8456
+ }
8432
8457
  // -------------------------------------------------------------------------
8433
8458
  // Toolbar and link popover (see ui/toolbar.ts)
8434
8459
  /** @internal Opens the link popover for the selection, as Mod+K does. */