@ai-matrx/rich-editor 0.2.11 → 0.3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.1
4
+ ### A block goes above only from the very start of its paragraph (G15, 2026-10-07)
5
+
6
+ - `blockSide` / `nearestSide` / `blockSideOfSelection` (`"nearest"`): a caret at offset 0 of a non-empty block → BEFORE it; anywhere else → AFTER. The 0.3.0 "first half → before" rule counted characters, so a right-click 89% along the first visual line of a wrapped paragraph put the block above it.
7
+
8
+ Consumer action: none; hosts already pass `"nearest"`.
9
+
10
+ ## 0.3.0
11
+ ### A block lands on the side of the click; a trailing space stays typed (G11B, 2026-10-07)
12
+
13
+ - `insertText(text, where)` takes `"nearest"` (new `BlockPlacement` type, `core/text-insertion`): from the start or the first half of the caret's paragraph the block goes BEFORE it, otherwise AFTER. `insertAtRichCaret(…, "block")` uses it. New helpers `blockSide`, `nearestSide` (plain text) and `blockSideOfSelection` (visual).
14
+ - A space typed at the end of a paragraph or heading survives the editor rebuilding from its own text — typing after "six." used to give "six.Then".
15
+ - A reference/action card in Write takes the whole line on a phone; its grip and tools wrap under it.
16
+
17
+ Consumer action: none required; a host passing `"before" | "after"` keeps that exact behaviour.
18
+
3
19
  ## 0.2.11
4
20
  ### Bold (and every inline mark) toggles like a real editor
5
21
 
@@ -18,10 +18,18 @@
18
18
  */
19
19
  import { type Node as PMNode, type Schema } from "@tiptap/pm/model";
20
20
  import type { Transaction } from "@tiptap/pm/state";
21
+ import { type BlockPlacement } from "./text-insertion.js";
21
22
  /** The top-level boundary a block goes to, relative to [from, to]. */
22
23
  export declare function blockInsertPosition(doc: PMNode, from: number, to: number, where: "before" | "after"): number;
24
+ /**
25
+ * The side of its block a selection belongs on (`blockSide`): a collapsed
26
+ * caret at the very start of a textblock → "before"; anything else — the
27
+ * middle, the end, an empty block, a selected chip, ⌘A, a position between
28
+ * blocks — → "after".
29
+ */
30
+ export declare function blockSideOfSelection(doc: PMNode, from: number, to: number): "before" | "after";
23
31
  /**
24
32
  * Insert markdown as its own block(s) before / after the selection's block.
25
33
  * Throws nothing it can avoid; returns false when the text parses to nothing.
26
34
  */
27
- export declare function insertMarkdownBlock(tr: Transaction, schema: Schema, text: string, where: "before" | "after"): boolean;
35
+ export declare function insertMarkdownBlock(tr: Transaction, schema: Schema, text: string, where: BlockPlacement): boolean;
@@ -1,15 +1,23 @@
1
1
  import { Fragment, Slice } from "@tiptap/pm/model";
2
2
  import { Selection } from "@tiptap/pm/state";
3
3
  import { markdownToSlice } from "./paste-markdown.js";
4
+ import { blockSide } from "./text-insertion.js";
4
5
  function blockInsertPosition(doc, from, to, where) {
5
6
  const clamp = (pos) => Math.max(0, Math.min(pos, doc.content.size));
6
7
  const $pos = doc.resolve(clamp(where === "before" ? from : to));
7
8
  if ($pos.depth === 0) return $pos.pos;
8
9
  return where === "before" ? $pos.before(1) : $pos.after(1);
9
10
  }
11
+ function blockSideOfSelection(doc, from, to) {
12
+ if (from !== to) return "after";
13
+ const $pos = doc.resolve(Math.max(0, Math.min(from, doc.content.size)));
14
+ if ($pos.depth === 0 || !$pos.parent.isTextblock) return "after";
15
+ return blockSide($pos.parentOffset, $pos.parent.content.size);
16
+ }
10
17
  function insertMarkdownBlock(tr, schema, text, where) {
11
18
  const { from, to } = tr.selection;
12
- const at = blockInsertPosition(tr.doc, from, to, where);
19
+ const side = where === "nearest" ? blockSideOfSelection(tr.doc, from, to) : where;
20
+ const at = blockInsertPosition(tr.doc, from, to, side);
13
21
  const parsed = markdownToSlice(text, schema);
14
22
  if (parsed.size === 0) return false;
15
23
  const blocks = new Slice(Fragment.from(parsed.content), 0, 0);
@@ -20,5 +28,6 @@ function insertMarkdownBlock(tr, schema, text, where) {
20
28
  }
21
29
  export {
22
30
  blockInsertPosition,
31
+ blockSideOfSelection,
23
32
  insertMarkdownBlock
24
33
  };
@@ -453,7 +453,8 @@ function parseProseBlock(block, schema, adjacency, nextId, linkDefinitions) {
453
453
  if (!last || children.length === segmentStart) {
454
454
  return { children: [], lockedReason: "markdown the parser could not map" };
455
455
  }
456
- last.trail += segment.after;
456
+ const kept = keepTrailingSpaces(last, segment.after, state);
457
+ last.trail += segment.after.slice(kept);
457
458
  }
458
459
  children.forEach(
459
460
  (child, index) => adjacency.set(child.id, { trail: child.trail, next: children[index + 1]?.id ?? null })
@@ -494,6 +495,30 @@ function trimSegmentEnd(text, after) {
494
495
  const tail = /[ \t]+$/.exec(text)?.[0] ?? "";
495
496
  return tail ? { text: text.slice(0, text.length - tail.length), after: tail + after } : { text, after };
496
497
  }
498
+ function keepTrailingSpaces(last, after, state) {
499
+ const tail = /^[ \t]+/.exec(after)?.[0] ?? "";
500
+ if (!tail || last.lockedReason !== null || last.trail !== "") return 0;
501
+ const type = last.json.type;
502
+ if (type !== "paragraph" && type !== "heading") return 0;
503
+ const content = [...last.json.content ?? []];
504
+ const end = content[content.length - 1];
505
+ if (end?.type === "text" && !end.marks?.length) {
506
+ content[content.length - 1] = { ...end, text: `${end.text ?? ""}${tail}` };
507
+ } else {
508
+ content.push({ type: "text", text: tail });
509
+ }
510
+ const json = { ...last.json, content };
511
+ try {
512
+ const node = state.schema.nodeFromJSON(json);
513
+ node.check();
514
+ if (serializeBlock(node, createSerializeContext(new Map(state.adjacency))) !== last.raw + tail) return 0;
515
+ } catch {
516
+ return 0;
517
+ }
518
+ last.json = json;
519
+ last.raw += tail;
520
+ return tail.length;
521
+ }
497
522
  function parseTopToken(token, state) {
498
523
  const { schema, islands, adjacency, nextId } = state;
499
524
  const { body, trail } = splitTrail(token.raw);
@@ -11,4 +11,23 @@ export declare function insertTextByRef(textareaRef: HTMLTextAreaElement | null,
11
11
  * surface (textarea, the rich editor's source view, a contentEditable text
12
12
  * node) uses this one rule.
13
13
  */
14
- export declare function blockBoundary(text: string, from: number, to: number, where?: "before" | "after"): number;
14
+ export declare function blockBoundary(text: string, from: number, to: number, where?: BlockPlacement): number;
15
+ /**
16
+ * Where a block goes relative to the caret's block: `before` it, `after` it, or
17
+ * `nearest` — before it from the very start of the block, after it otherwise.
18
+ */
19
+ export type BlockPlacement = "before" | "after" | "nearest";
20
+ /**
21
+ * 🚨 A BLOCK GOES ABOVE ONLY FROM THE VERY START OF ITS BLOCK. A matrx fence is
22
+ * block-level, so it never sits mid-sentence: a caret at offset 0 of a
23
+ * non-empty block (a right-click left of its first character lands there too)
24
+ * puts the block BEFORE it; anywhere else (the middle, the end, an empty block)
25
+ * puts it AFTER. The old "first half → before" rule measured characters, so a
26
+ * right-click at 89% along the first visual line of a wrapped paragraph put the
27
+ * block ABOVE it (G15 review, 2026-10-07). `offset` is the caret's place inside
28
+ * its block, `length` the block's size. ONE rule for the visual editor, the
29
+ * source view and every plain-text field.
30
+ */
31
+ export declare function blockSide(offset: number, length: number): "before" | "after";
32
+ /** `blockSide` for the caret's LINE in plain text (a non-empty selection keeps "after"). */
33
+ export declare function nearestSide(text: string, from: number, to: number): "before" | "after";
@@ -40,6 +40,7 @@ function insertTextByRef(textareaRef, text) {
40
40
  return insertTextAtTextareaCursor(textareaRef, text);
41
41
  }
42
42
  function blockBoundary(text, from, to, where = "after") {
43
+ if (where === "nearest") where = nearestSide(text, from, to);
43
44
  if (where === "after") {
44
45
  const newline2 = text.indexOf("\n", to);
45
46
  return newline2 === -1 ? text.length : newline2;
@@ -48,8 +49,21 @@ function blockBoundary(text, from, to, where = "after") {
48
49
  const newline = text.lastIndexOf("\n", from - 1);
49
50
  return newline === -1 ? 0 : newline + 1;
50
51
  }
52
+ function blockSide(offset, length) {
53
+ if (length <= 0) return "after";
54
+ return offset <= 0 ? "before" : "after";
55
+ }
56
+ function nearestSide(text, from, to) {
57
+ if (from !== to) return "after";
58
+ const start = from <= 0 ? 0 : text.lastIndexOf("\n", from - 1) + 1;
59
+ const newline = text.indexOf("\n", from);
60
+ const end = newline === -1 ? text.length : newline;
61
+ return blockSide(from - start, end - start);
62
+ }
51
63
  export {
52
64
  blockBoundary,
65
+ blockSide,
53
66
  insertTextAtTextareaCursor,
54
- insertTextByRef
67
+ insertTextByRef,
68
+ nearestSide
55
69
  };
@@ -4,6 +4,7 @@ import type { SourceFeature } from "@ai-matrx/agents/generated/source-attributio
4
4
  import type { ContentSource } from "@ai-matrx/rich-content/rich-document/types";
5
5
  import { type SavePlan } from "../core/save-plan.js";
6
6
  import type { CaretContext } from "../core/caret-context.js";
7
+ import type { BlockPlacement } from "../core/text-insertion.js";
7
8
  import type { DeclaredVariable } from "../core/variables.js";
8
9
  import { type ImagePolicyDeclaration } from "@ai-matrx/rich-content/levels/prose/remote-image-policy";
9
10
  import type { AnnotationRecord } from "@ai-matrx/rich-content/rich-document/host-types";
@@ -20,7 +21,7 @@ export interface RichEditorController {
20
21
  /** Put text at the caret, replacing the selection (markdown stays markdown). */
21
22
  replaceSelection: (text: string) => void;
22
23
  /** Insert a new block before / after the selection; false when it could not. */
23
- insertText: (text: string, where: "before" | "after") => boolean;
24
+ insertText: (text: string, where: BlockPlacement) => boolean;
24
25
  /**
25
26
  * Replace the WHOLE text (the context menu's full-content `onTextReplace`
26
27
  * contract: Cut / Paste / an AI result's Replace / an agent's
@@ -2,10 +2,11 @@
2
2
  * THE context menu's insert-at-the-caret for a host that drives a RichEditor.
3
3
  *
4
4
  * `inline` text replaces the selection where it is. A `block` (a reference
5
- * fence) goes through `insertText(…, "after")`, which places it on its own
6
- * paragraph at the END of the caret's block — never inside a word (G5 review,
7
- * 2026-10-02: "of" became "o" + block + "f"). One helper so the three note
8
- * hosts (desktop, content editor, mobile) cannot drift.
5
+ * fence) goes through `insertText(…, "nearest")`: its own paragraph, never
6
+ * inside a word (G5 review, 2026-10-02: "of" became "o" + block + "f"), on the
7
+ * side of the caret's block the click was nearer to — BEFORE it from its start
8
+ * or first half, AFTER it otherwise (G11B review, 2026-10-07). One helper so
9
+ * the three note hosts (desktop, content editor, mobile) cannot drift.
9
10
  */
10
11
  import type { RichEditorController } from "./RichEditorImpl.js";
11
12
  export declare function insertAtRichCaret(rich: RichEditorController | null, text: string, placement?: "inline" | "block"): boolean;
@@ -1,6 +1,6 @@
1
1
  function insertAtRichCaret(rich, text, placement = "inline") {
2
2
  if (!rich) return false;
3
- if (placement === "block") return rich.insertText(text, "after");
3
+ if (placement === "block") return rich.insertText(text, "nearest");
4
4
  rich.replaceSelection(text);
5
5
  return true;
6
6
  }
@@ -44,7 +44,7 @@ import {
44
44
  setFindHighlights,
45
45
  setIslandRendering
46
46
  } from "./live-preview.js";
47
- import { blockBoundary } from "../core/text-insertion.js";
47
+ import { blockBoundary, nearestSide } from "../core/text-insertion.js";
48
48
  const RAW_THEME = EditorView.theme({
49
49
  ".cm-content, .cm-line": {
50
50
  fontFamily: "var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace)",
@@ -300,8 +300,10 @@ function SourceEditor({
300
300
  const v = view.current;
301
301
  if (!v) return false;
302
302
  const { from, to } = v.state.selection.main;
303
- const at = blockBoundary(v.state.doc.toString(), from, to, where);
304
- const insert = where === "before" ? `${text}
303
+ const doc = v.state.doc.toString();
304
+ const side = where === "nearest" ? nearestSide(doc, from, to) : where;
305
+ const at = blockBoundary(doc, from, to, side);
306
+ const insert = side === "before" ? `${text}
305
307
 
306
308
  ` : `
307
309
 
@@ -1,4 +1,5 @@
1
1
  import { type Ref } from "react";
2
+ import type { BlockPlacement } from "../core/text-insertion.js";
2
3
  import { type VisualLoadStats } from "../core/visual-document.js";
3
4
  import { type CaretCarry, type CaretContext } from "../core/caret-context.js";
4
5
  import type { FindOptions } from "../core/find-replace.js";
@@ -19,7 +20,7 @@ export interface EditorViewHandle {
19
20
  /** Replace the selection with literal text (markdown typed by an AI stays markdown). */
20
21
  replaceSelection: (text: string) => void;
21
22
  /** Insert a block before / after the selection; false when it could not. */
22
- insertText: (text: string, where: "before" | "after") => boolean;
23
+ insertText: (text: string, where: BlockPlacement) => boolean;
23
24
  /** Flush pending text now (before a save or a view switch). */
24
25
  flush: () => string;
25
26
  /**
@@ -61,7 +61,7 @@ function IslandBlockView({ node, updateAttributes, deleteNode, selected, editor,
61
61
  NodeViewWrapper,
62
62
  {
63
63
  className: cn(
64
- "rich-editor-island rich-editor-reference group/island relative my-1 flex min-w-0 items-start gap-1 rounded-md transition-colors",
64
+ "rich-editor-island rich-editor-reference group/island relative my-1 flex min-w-0 flex-wrap items-center gap-1 rounded-md transition-colors sm:flex-nowrap sm:items-start",
65
65
  selected && "ring-1 ring-primary/40"
66
66
  ),
67
67
  "data-island-type": "matrx",
@@ -71,13 +71,13 @@ function IslandBlockView({ node, updateAttributes, deleteNode, selected, editor,
71
71
  "span",
72
72
  {
73
73
  "data-drag-handle": true,
74
- className: "mt-1.5 shrink-0 cursor-grab touch-none text-muted-foreground/60 opacity-100 hover:text-foreground sm:opacity-0 sm:group-hover/island:opacity-100",
74
+ className: "shrink-0 cursor-grab touch-none text-muted-foreground/60 opacity-100 hover:text-foreground sm:mt-1.5 sm:opacity-0 sm:group-hover/island:opacity-100",
75
75
  "aria-hidden": true,
76
76
  children: /* @__PURE__ */ jsx(GripVertical, { className: "h-3.5 w-3.5" })
77
77
  }
78
78
  ),
79
- /* @__PURE__ */ jsx("div", { className: "min-w-0 flex-1", children: /* @__PURE__ */ jsx(IslandPreview, { raw, islandType }) }),
80
- /* @__PURE__ */ jsxs("div", { className: "flex shrink-0 items-center gap-0.5 text-muted-foreground opacity-100 sm:opacity-0 sm:transition-opacity sm:group-hover/island:opacity-100 sm:group-focus-within/island:opacity-100", children: [
79
+ /* @__PURE__ */ jsx("div", { className: "order-first min-w-0 basis-full sm:order-none sm:basis-0 sm:flex-1", "data-island-body": "", children: /* @__PURE__ */ jsx(IslandPreview, { raw, islandType }) }),
80
+ /* @__PURE__ */ jsxs("div", { className: "ml-auto flex shrink-0 items-center gap-0.5 text-muted-foreground opacity-100 sm:ml-0 sm:opacity-0 sm:transition-opacity sm:group-hover/island:opacity-100 sm:group-focus-within/island:opacity-100", children: [
81
81
  /* @__PURE__ */ jsx("button", { type: "button", className: "rounded p-1 hover:bg-muted hover:text-foreground pointer-coarse:flex pointer-coarse:h-11 pointer-coarse:w-11 pointer-coarse:items-center pointer-coarse:justify-center", onClick: copy, title: `Copy this ${meta.label.toLowerCase()}`, children: copied ? /* @__PURE__ */ jsx(Check, { className: "h-3.5 w-3.5" }) : /* @__PURE__ */ jsx(Copy, { className: "h-3.5 w-3.5" }) }),
82
82
  !readOnly && /* @__PURE__ */ jsx(
83
83
  "button",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-matrx/rich-editor",
3
- "version": "0.2.11",
3
+ "version": "0.3.1",
4
4
  "description": "The AI Matrx editor: Visual (Tiptap), Source (CodeMirror), Preview, the markdown formatting command layer, and edit-in-place — over @ai-matrx/rich-content. Consumers that only render never install it.",
5
5
  "type": "module",
6
6
  "license": "MIT",