diff --git a/Packages/editor/src/DocwellEditor.vue b/Packages/editor/src/DocwellEditor.vue
index 45614fa..a19806a 100644
--- a/Packages/editor/src/DocwellEditor.vue
+++ b/Packages/editor/src/DocwellEditor.vue
@@ -8,6 +8,7 @@ import { DwButton, DwIcon, type IconName } from "@docwell/ui";
import { computed, nextTick, onBeforeUnmount, ref, watch } from "vue";
import { aiActions, type AiAction, type AiHost } from "./ai";
import { copyBlockLink } from "./blocklinks";
+import { currentConversion, turnIntoActions, turnIntoTarget, type BlockAction } from "./blockmenu";
import { palette, type PaletteColor } from "./colors";
import { docwellExtensions, type CollabUser } from "./extensions";
import { findKey } from "./find";
@@ -174,8 +175,23 @@ onBeforeUnmount(() => editor.value?.destroy());
// The selection toolbar: formatting for editors, Comment for anyone allowed.
type Tool = { id: string; icon: IconName; label: string; active: () => boolean; run: () => void };
/** The colour or alignment choices open under the toolbar, if any. */
-const panel = ref<"color" | "align" | "ai" | null>(null);
-const togglePanel = (p: "color" | "align" | "ai") => (panel.value = panel.value === p ? null : p);
+const panel = ref<"turn" | "color" | "align" | "ai" | null>(null);
+const togglePanel = (p: "turn" | "color" | "align" | "ai") => (panel.value = panel.value === p ? null : p);
+
+/** "Turn into" for the block the selection is in: what it is now and what it can become. Null where it has no say. */
+function turnInto(e: Editor): { current: string | null; label: string; actions: BlockAction[] } | null {
+ const pos = turnIntoTarget(e);
+ if (pos < 0) return null;
+ const actions = turnIntoActions(e, pos, labels.value);
+ if (!actions.length) return null;
+ const current = currentConversion(e, pos);
+ const label = (current && labels.value.slash[current]?.label) || labels.value.blockMenu.turnInto;
+ return { current, label, actions };
+}
+function turn(a: BlockAction) {
+ a.run?.();
+ panel.value = null;
+}
const alignment = (e: Editor) =>
String(e.getAttributes("paragraph").textAlign ?? e.getAttributes("heading").textAlign ?? "left");
@@ -648,6 +664,22 @@ defineExpose({ editor, openFind });
>
+
{{ labels.format.comment }}
+
+
+
({
new Plugin({
key: new PluginKey("blockLinks"),
view: (view) => new BlockHandle(view, editor, labels, link, onCopied),
+ props: {
+ // Marks the block the cursor is in; the stylesheet shows it only while the editor has focus.
+ decorations: (state) => {
+ const pos = currentBlock(state);
+ if (pos < 0) return null;
+ const node = state.doc.nodeAt(pos)!;
+ return DecorationSet.create(state.doc, [
+ Decoration.node(pos, pos + node.nodeSize, { class: "dw-block-current" }),
+ ]);
+ },
+ },
}),
];
},
@@ -191,7 +202,19 @@ const linkGlyph =
const gripGlyph =
'';
-/** The buttons that follow the pointer from block to block: copy link, and the grip. */
+/** Where the top-level block holding the whole selection starts, or -1. */
+function currentBlock(state: EditorState): number {
+ const { $from, $to } = state.selection;
+ if (state.selection instanceof NodeSelection && $from.depth === 0) return $from.pos;
+ if ($from.depth < 1 || $to.depth < 1) return -1;
+ const start = $from.before(1);
+ return start === $to.before(1) ? start : -1;
+}
+
+/**
+ * The buttons beside a block: copy link, and the grip. They follow the pointer
+ * from block to block, and otherwise stay on the block the cursor is in.
+ */
class BlockHandle {
private gutter = document.createElement("div");
private linkButton = document.createElement("button");
@@ -232,12 +255,14 @@ class BlockHandle {
grip.draggable = true;
grip.addEventListener("click", this.openMenu);
grip.addEventListener("dragstart", this.dragStart);
- grip.addEventListener("dragend", () => this.hide());
+ grip.addEventListener("dragend", () => this.rest());
if (link) g.append(b);
g.append(grip);
view.dom.addEventListener("mousemove", this.move);
view.dom.addEventListener("mouseleave", this.leave);
+ view.dom.addEventListener("focus", this.rest);
+ view.dom.addEventListener("blur", this.blur);
}
/** The element around the editor, which the buttons sit in. Hosts may move the editor after creating it. */
@@ -250,48 +275,73 @@ class BlockHandle {
return host;
}
+ /** Whether the pointer is over the text or the buttons. */
+ private hovering = false;
+
private hide = () => {
if (this.closeMenu) return;
this.gutter.hidden = true;
this.pos = -1;
};
+ /** With the pointer elsewhere: back to the block the cursor is in, if the editor has focus. */
+ private rest = () => {
+ if (this.closeMenu || this.hovering) return;
+ const pos = this.view.hasFocus() ? currentBlock(this.view.state) : -1;
+ if (pos < 0 || !this.place(pos)) this.hide();
+ };
+
+ private blur = (e: FocusEvent) => {
+ // Clicking a button moves focus onto it; the buttons stay.
+ if (e.relatedTarget instanceof Node && this.gutter.contains(e.relatedTarget)) return;
+ this.rest();
+ };
+
// Moving from the text onto the buttons keeps them.
private leave = (e: MouseEvent) => {
const to = e.relatedTarget as Node | null;
if (to && (this.gutter.contains(to) || this.view.dom.contains(to))) return;
- this.hide();
+ this.hovering = false;
+ this.rest();
};
private move = (e: MouseEvent) => {
if (this.closeMenu) return;
+ this.hovering = true;
// The top-level block under the pointer.
let el = e.target as HTMLElement | null;
while (el && el.parentElement !== this.view.dom) el = el.parentElement;
- const host = this.host;
- if (!el || !host) return this.hide();
+ if (!el) return;
let pos: number;
try {
pos = this.view.posAtDOM(el, 0);
} catch {
- return this.hide();
+ return;
}
const $pos = this.view.state.doc.resolve(Math.min(pos, this.view.state.doc.content.size));
const start = $pos.depth >= 1 ? $pos.before(1) : pos;
+ if (!this.place(start)) this.hide();
+ };
+
+ /** Shows the buttons beside the top-level block at `start`, centred on its height. */
+ private place(start: number): boolean {
+ const host = this.host;
const node = this.view.state.doc.nodeAt(start);
- if (!node) return this.hide();
+ const el = node && this.view.nodeDOM(start);
+ if (!host || !node || !(el instanceof HTMLElement)) return false;
// Readers can only copy links that exist; only editors can add an id.
const canLink = !!this.link && linkable.includes(node.type.name) && (!!node.attrs.blockId || this.view.editable);
const canEdit = this.view.editable;
- if (!canLink && !canEdit) return this.hide();
+ if (!canLink && !canEdit) return false;
this.linkButton.hidden = !canLink;
this.grip.hidden = !canEdit;
this.pos = start;
const box = el.getBoundingClientRect();
const hostBox = host.getBoundingClientRect();
- this.gutter.style.top = `${box.top - hostBox.top + Math.min(box.height / 2, 14) - 11}px`;
+ this.gutter.style.top = `${box.top - hostBox.top + box.height / 2 - 11}px`;
this.gutter.hidden = false;
- };
+ return true;
+ }
private openMenu = () => {
if (this.closeMenu) {
@@ -307,7 +357,7 @@ class BlockHandle {
this.closeMenu = openBlockMenu(this.grip, actions, this.labels.blockMenu.open, () => {
this.closeMenu = null;
this.grip.setAttribute("aria-expanded", "false");
- this.hide();
+ this.rest();
});
};
@@ -330,8 +380,10 @@ class BlockHandle {
}
update() {
- // Positions move as people type; the next pointer move finds the block again.
- if (this.pos >= this.view.state.doc.content.size) this.hide();
+ if (this.closeMenu) return;
+ // Blocks move and grow as people type; keep the buttons on theirs.
+ if (!this.hovering) return this.rest();
+ if (this.pos < 0 || this.pos >= this.view.state.doc.content.size || !this.place(this.pos)) this.hide();
}
destroy() {
@@ -340,5 +392,7 @@ class BlockHandle {
this.gutter.remove();
this.view.dom.removeEventListener("mousemove", this.move);
this.view.dom.removeEventListener("mouseleave", this.leave);
+ this.view.dom.removeEventListener("focus", this.rest);
+ this.view.dom.removeEventListener("blur", this.blur);
}
}
diff --git a/Packages/editor/src/blockmenu.ts b/Packages/editor/src/blockmenu.ts
index b3981f4..27f3b81 100644
--- a/Packages/editor/src/blockmenu.ts
+++ b/Packages/editor/src/blockmenu.ts
@@ -54,9 +54,87 @@ function withoutIds(json: JSONContent): JSONContent {
return out;
}
+const listTypes = ["bulletList", "orderedList", "taskList"];
+
/**
- * The actions for the top-level block at pos: turn into, move, duplicate,
- * copy, delete, plus block-specific ones (line numbers, PDF pages).
+ * The block "Turn into" acts on for the selection: the list around the
+ * cursor's line, or else its own paragraph, heading, quote or code block.
+ * -1 when the selection spans blocks or sits somewhere else (a table cell).
+ */
+export function turnIntoTarget(editor: Editor): number {
+ const { $from, $to } = editor.state.selection;
+ if (!$from.parent.isTextblock) return -1;
+ let depth = $from.depth;
+ if (depth >= 3 && listTypes.includes($from.node(depth - 2).type.name)) depth -= 2;
+ if (depth >= 2 && $from.node(depth - 1).type.name === "blockquote") depth -= 1;
+ const node = $from.node(depth);
+ if (!convertible.includes(node.type.name)) return -1;
+ const pos = $from.before(depth);
+ return $to.pos <= pos + node.nodeSize ? pos : -1;
+}
+
+/** Which conversion id the block at pos is now, for a tick beside it. */
+export function currentConversion(editor: Editor, pos: number): string | null {
+ const node = editor.state.doc.nodeAt(pos);
+ if (!node) return null;
+ const $pos = editor.state.doc.resolve(pos);
+ for (let d = $pos.depth; d >= 1; d--) if ($pos.node(d).type.name === "callout") return "callout";
+ switch (node.type.name) {
+ case "paragraph":
+ return "text";
+ case "heading":
+ return `h${node.attrs.level}`;
+ case "bulletList":
+ return "bullet";
+ case "orderedList":
+ return "ordered";
+ case "taskList":
+ return "todo";
+ case "blockquote":
+ return "quote";
+ case "codeBlock":
+ return "code";
+ }
+ return null;
+}
+
+/** The kinds of block the block at pos can turn into, each with what turns it. */
+export function turnIntoActions(editor: Editor, pos: number, labels: EditorLabels): BlockAction[] {
+ const node = editor.state.doc.nodeAt(pos);
+ if (!node || !convertible.includes(node.type.name)) return [];
+ const end = pos + node.nodeSize;
+ return conversions.map(([id, icon, make]) => ({
+ id,
+ icon,
+ label: labels.slash[id]?.label ?? id,
+ run: () => {
+ if (id === "callout") {
+ // A callout holds any block, so the block goes in as it is (a
+ // heading stays a heading); a quote gives it its contents.
+ if (node.type.name === "callout" || currentConversion(editor, pos) === "callout") return;
+ const inner = node.type.name === "blockquote" ? node.content : Fragment.from(node);
+ const callout = editor.schema.nodes.callout!.create(null, inner);
+ const tr = editor.state.tr.replaceWith(pos, end, callout);
+ editor.view.dispatch(tr.setSelection(Selection.near(tr.doc.resolve(pos + 1))).scrollIntoView());
+ editor.commands.focus();
+ return;
+ }
+ // Select the block's text from its first to its last line (lists and
+ // callouts hold it deeper down), then make plain paragraphs of it, so
+ // every kind starts from the same place.
+ const doc = editor.state.doc;
+ const from = Selection.findFrom(doc.resolve(pos), 1, true)?.from ?? pos + 1;
+ const to = Selection.findFrom(doc.resolve(end), -1, true)?.to ?? end - 1;
+ const chain = editor.chain().focus().setTextSelection({ from, to }).clearNodes();
+ make(chain).run();
+ },
+ }));
+}
+
+/**
+ * The actions for the top-level block at pos: move, duplicate, copy, delete,
+ * plus block-specific ones (line numbers, PDF pages). Turning a block into
+ * another kind is on the selection toolbar.
*/
export function blockActions(editor: Editor, pos: number, labels: EditorLabels, copyLink?: () => void): BlockAction[] {
const node = editor.state.doc.nodeAt(pos);
@@ -66,39 +144,6 @@ export function blockActions(editor: Editor, pos: number, labels: EditorLabels,
const index = editor.state.doc.resolve(pos).index(0);
const out: BlockAction[] = [];
- if (convertible.includes(node.type.name)) {
- out.push({
- id: "turnInto",
- label: m.turnInto,
- icon: "text",
- children: conversions.map(([id, icon, make]) => ({
- id,
- icon,
- label: labels.slash[id]?.label ?? id,
- run: () => {
- if (id === "callout") {
- // A callout holds any block, so the block goes in as it is (a
- // heading stays a heading); a quote gives it its contents.
- if (node.type.name === "callout") return;
- const inner = node.type.name === "blockquote" ? node.content : Fragment.from(node);
- const callout = editor.schema.nodes.callout!.create(null, inner);
- const tr = editor.state.tr.replaceWith(pos, end, callout);
- editor.view.dispatch(tr.setSelection(Selection.near(tr.doc.resolve(pos + 1))).scrollIntoView());
- editor.commands.focus();
- return;
- }
- // Select the block's text from its first to its last line (lists and
- // callouts hold it deeper down), then make plain paragraphs of it, so
- // every kind starts from the same place.
- const doc = editor.state.doc;
- const from = Selection.findFrom(doc.resolve(pos), 1, true)?.from ?? pos + 1;
- const to = Selection.findFrom(doc.resolve(end), -1, true)?.to ?? end - 1;
- const chain = editor.chain().focus().setTextSelection({ from, to }).clearNodes();
- make(chain).run();
- },
- })),
- });
- }
if (["bulletList", "orderedList", "taskList"].includes(node.type.name)) {
out.push({
id: "groupAsDatabase",
diff --git a/Packages/editor/src/blocks.test.ts b/Packages/editor/src/blocks.test.ts
index c4f37a5..2a3730d 100644
--- a/Packages/editor/src/blocks.test.ts
+++ b/Packages/editor/src/blocks.test.ts
@@ -2,7 +2,7 @@
import { Editor, type JSONContent } from "@tiptap/core";
import { afterEach, describe, expect, it } from "vitest";
-import { blockActions } from "./blockmenu";
+import { blockActions, currentConversion, turnIntoActions, turnIntoTarget } from "./blockmenu";
import { showsLineNumbers } from "./codeblock";
import { docwellExtensions } from "./extensions";
import { englishLabels } from "./labels";
@@ -18,7 +18,7 @@ function setup(content: string | JSONContent, opts: Parameters (e.getJSON().content ?? []).map((n) => n.type);
const action = (e: Editor, pos: number, id: string) => {
- const all = blockActions(e, pos, englishLabels);
+ const all = [...blockActions(e, pos, englishLabels), ...turnIntoActions(e, pos, englishLabels)];
return all.find((a) => a.id === id) ?? all.flatMap((a) => a.children ?? []).find((a) => a.id === id);
};
@@ -187,6 +187,29 @@ describe("blockActions", () => {
expect(e.getJSON().content![2]!.content!.map((n) => n.type)).toEqual(["paragraph"]);
});
+ it("leaves turning into to the selection toolbar", () => {
+ const e = setup(doc);
+ expect(blockActions(e, 0, englishLabels).map((a) => a.id)).not.toContain("turnInto");
+ });
+
+ it("finds the block the toolbar turns into, and what it is now", () => {
+ const e = setup("
Title
a
b
one
two
");
+ const at = (from: number, to = from) => {
+ e.commands.setTextSelection({ from, to });
+ const pos = turnIntoTarget(e);
+ return pos < 0 ? null : currentConversion(e, pos);
+ };
+ expect(at(2)).toBe("h2");
+ const list = e.state.doc.child(0).nodeSize;
+ // The cursor in a list item turns the whole list.
+ expect(at(list + 3)).toBe("bullet");
+ expect(turnIntoTarget(e)).toBe(list);
+ const para = list + e.state.doc.child(1).nodeSize;
+ expect(at(para + 1)).toBe("text");
+ // A selection over two blocks has no single block to turn.
+ expect(at(para + 1, para + e.state.doc.child(2).nodeSize + 2)).toBeNull();
+ });
+
it("deletes a block", () => {
const e = setup(doc);
action(e, 0, "delete")!.run!();
diff --git a/Packages/editor/src/editor.css b/Packages/editor/src/editor.css
index bd3230e..361e835 100644
--- a/Packages/editor/src/editor.css
+++ b/Packages/editor/src/editor.css
@@ -677,10 +677,18 @@
}
.dw-block-gutter {
position: absolute;
- right: calc(100% + 6px);
+ /* The padding covers the gap to the text, so the pointer can cross it. */
+ right: 100%;
+ padding-right: 6px;
display: flex;
gap: 2px;
}
+/* The block the cursor is in. */
+.dw-prose.ProseMirror-focused > .dw-block-current {
+ border-radius: var(--radius-sm);
+ background: var(--color-surface-hover);
+ box-shadow: 0 0 0 4px var(--color-surface-hover);
+}
.dw-block-gutter[hidden],
.dw-block-handle[hidden] {
display: none;
diff --git a/Packages/ui/src/DwTreeItem.vue b/Packages/ui/src/DwTreeItem.vue
index 2a33ae6..ca99799 100644
--- a/Packages/ui/src/DwTreeItem.vue
+++ b/Packages/ui/src/DwTreeItem.vue
@@ -6,6 +6,7 @@ import type { IconName } from "./icons";
// One row of the sidebar page tree (the design system's PageTreeItem), nested
// 16px per depth on surface-sunken. The default slot is the row's main control
// (usually a link holding the title); the actions slot shows on hover and focus.
+// The icon slot replaces the icon, e.g. with a button that changes it.
withDefaults(
defineProps<{
title?: string;
@@ -42,7 +43,7 @@ const emit = defineEmits<{ toggle: [] }>();
>
-
+ {{ title }}
diff --git a/Web-App/frontend/src/components/AppLayout.vue b/Web-App/frontend/src/components/AppLayout.vue
index e0d3c96..baee536 100644
--- a/Web-App/frontend/src/components/AppLayout.vue
+++ b/Web-App/frontend/src/components/AppLayout.vue
@@ -1,6 +1,6 @@
+
+
+
+