feat: add table toolbar

This commit is contained in:
Steffen Becker committed 2026-06-06 16:09:48 +02:00
1 parent b56d1f53fe
commit 631e6aa5e9
3 files changed
+245 -2

No files matched your search

+72 -2
View File
@@ -4,6 +4,7 @@
import { Slice, Fragment } from 'prosemirror-model';
import type { Node as PmNode, MarkType } from 'prosemirror-model';
import { extensions } from './extensions';
import TableToolbar from './TableToolbar.svelte';
import { saveDocument, loadDocument } from '../storage/autosave';
import { applyMarginVars, DEFAULT_MARGINS, type PageMargins } from '../storage/pageMargins';
import { applyOrientationVars, type Orientation } from '../storage/pageOrientation';
@@ -56,6 +57,59 @@
return (Number.isFinite(ph) ? ph : 1123) + 20;
}
// --- Floating table-editing toolbar ---
// Shown when the selection is inside a table; positioned just above that table.
let tableUi = $state<{ visible: boolean; top: number; left: number }>({ visible: false, top: 0, left: 0 });
let tableUiRaf = 0;
// The DOM element of the table containing the current selection, or null.
// nodeDOM(before(table)) returns the wrapper div the table node view renders.
function activeTableDOM(ed: Editor): HTMLElement | null {
const resolved = ed.state.selection.$from;
for (let d = resolved.depth; d > 0; d--) {
if (resolved.node(d).type.name === 'table') {
try {
const dom = ed.view.nodeDOM(resolved.before(d));
return dom instanceof HTMLElement ? dom : null;
} catch {
return null;
}
}
}
return null;
}
function recomputeTableUi() {
const ed = editor;
if (!ed || !editorContainer) {
if (tableUi.visible) tableUi = { ...tableUi, visible: false };
return;
}
const dom = activeTableDOM(ed);
if (!dom) {
if (tableUi.visible) tableUi = { ...tableUi, visible: false };
return;
}
// Position in the editor container's content space: viewport delta + scroll.
// getBoundingClientRect and scrollTop share the same (zoom-affected) scale,
// so this stays aligned across zoom; the toolbar itself is a non-zoomed
// sibling of .paper, so it renders at a constant size.
const tRect = dom.getBoundingClientRect();
const cRect = editorContainer.getBoundingClientRect();
tableUi = {
visible: true,
top: tRect.top - cRect.top + editorContainer.scrollTop,
left: tRect.left - cRect.left + editorContainer.scrollLeft,
};
}
// Defer to the next frame so the DOM reflects the latest transaction /
// pagination spacers before we measure (mirrors the page-break code).
function scheduleTableUi() {
cancelAnimationFrame(tableUiRaf);
tableUiRaf = requestAnimationFrame(recomputeTableUi);
}
// Throttle the value actually written to the DOM to one update per animation frame,
// so rapid slider events don't trigger 50+ layout/paint cycles per second.
let appliedZoom = $state(untrack(() => zoom));
@@ -88,6 +142,8 @@
$effect(() => {
appliedZoom; // track to fire after the pre-effect / DOM update
// Zoom changes the table's rendered position — re-place the floating toolbar.
scheduleTableUi();
if (pendingAnchorDocY === null || !editorContainer || !element) return;
const docY = pendingAnchorDocY;
pendingAnchorDocY = null;
@@ -129,6 +185,8 @@
function onPageCount(e: Event) {
numPages = (e as CustomEvent<{ numPages: number }>).detail.numPages;
updateCurrentPage();
// Pagination spacers can shift a table's position — re-place the toolbar.
scheduleTableUi();
}
function applyFontToFragment(frag: Fragment, textStyleType: MarkType, font: string): Fragment {
@@ -169,6 +227,9 @@
},
onTransaction: () => {
tick++;
// Covers both doc and selection changes (entering/leaving a table,
// adding/removing rows or columns).
scheduleTableUi();
},
onSelectionUpdate: ({ editor: e }) => {
// Use the editor instance passed by TipTap directly — avoids any Svelte
@@ -188,18 +249,27 @@
});
element.addEventListener('pm-pagecount', onPageCount);
editorContainer.addEventListener('scroll', updateCurrentPage);
editorContainer.addEventListener('scroll', onEditorScroll);
});
function onEditorScroll() {
updateCurrentPage();
scheduleTableUi();
}
onDestroy(() => {
if (zoomRaf !== null) cancelAnimationFrame(zoomRaf);
cancelAnimationFrame(marginRecalcRaf);
cancelAnimationFrame(tableUiRaf);
editor?.destroy();
element?.removeEventListener('pm-pagecount', onPageCount);
editorContainer?.removeEventListener('scroll', updateCurrentPage);
editorContainer?.removeEventListener('scroll', onEditorScroll);
});
</script>
<div class="editor" bind:this={editorContainer}>
<div bind:this={element} class="paper" class:show-formatting-marks={showFormattingMarks} style="zoom: {appliedZoom / 100}"></div>
{#if tableUi.visible}
<TableToolbar {editor} top={tableUi.top} left={tableUi.left} />
{/if}
</div>
+170
View File
@@ -0,0 +1,170 @@
<script lang="ts">
import type { Editor, ChainedCommands } from '@tiptap/core';
let {
editor,
top,
left,
}: {
editor: Editor | null;
top: number;
left: number;
} = $props();
// Run a table command without disturbing the cell selection: preventDefault on
// mousedown keeps focus/selection in the editor, then .focus() in the chain
// re-anchors to that selection so the row/column lands at the active cell.
function run(cmd: (chain: ChainedCommands) => ChainedCommands) {
if (!editor) return;
cmd(editor.chain().focus()).run();
}
</script>
<div
class="table-toolbar"
style="top: {top}px; left: {left}px;"
role="toolbar"
tabindex="-1"
aria-label="Table editing"
onmousedown={(e) => e.preventDefault()}
>
<button
class="tt-btn"
title="Insert row above"
aria-label="Insert row above"
onclick={() => run((c) => c.addRowBefore())}
>
<svg width="16" height="16" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<rect x="3" y="9" width="12" height="6" rx="1" stroke="currentColor" stroke-width="1.3"/>
<line x1="9" y1="9" x2="9" y2="15" stroke="currentColor" stroke-width="1.1"/>
<path d="M9 2v4M7 4l2-2 2 2" stroke="currentColor" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
<button
class="tt-btn"
title="Insert row below"
aria-label="Insert row below"
onclick={() => run((c) => c.addRowAfter())}
>
<svg width="16" height="16" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<rect x="3" y="3" width="12" height="6" rx="1" stroke="currentColor" stroke-width="1.3"/>
<line x1="9" y1="3" x2="9" y2="9" stroke="currentColor" stroke-width="1.1"/>
<path d="M9 16v-4M7 14l2 2 2-2" stroke="currentColor" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
<button
class="tt-btn"
title="Insert column left"
aria-label="Insert column left"
onclick={() => run((c) => c.addColumnBefore())}
>
<svg width="16" height="16" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<rect x="9" y="3" width="6" height="12" rx="1" stroke="currentColor" stroke-width="1.3"/>
<line x1="9" y1="9" x2="15" y2="9" stroke="currentColor" stroke-width="1.1"/>
<path d="M2 9h4M4 7l-2 2 2 2" stroke="currentColor" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
<button
class="tt-btn"
title="Insert column right"
aria-label="Insert column right"
onclick={() => run((c) => c.addColumnAfter())}
>
<svg width="16" height="16" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<rect x="3" y="3" width="6" height="12" rx="1" stroke="currentColor" stroke-width="1.3"/>
<line x1="9" y1="9" x2="3" y2="9" stroke="currentColor" stroke-width="1.1"/>
<path d="M16 9h-4M14 7l2 2-2 2" stroke="currentColor" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
<span class="tt-sep"></span>
<button
class="tt-btn tt-danger"
title="Delete row"
aria-label="Delete row"
onclick={() => run((c) => c.deleteRow())}
>
<svg width="16" height="16" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<rect x="3" y="6" width="12" height="6" rx="1" stroke="currentColor" stroke-width="1.3"/>
<line x1="6" y1="9" x2="12" y2="9" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"/>
</svg>
</button>
<button
class="tt-btn tt-danger"
title="Delete column"
aria-label="Delete column"
onclick={() => run((c) => c.deleteColumn())}
>
<svg width="16" height="16" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<rect x="6" y="3" width="6" height="12" rx="1" stroke="currentColor" stroke-width="1.3"/>
<line x1="9" y1="6" x2="9" y2="12" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"/>
</svg>
</button>
<button
class="tt-btn tt-danger"
title="Delete table"
aria-label="Delete table"
onclick={() => run((c) => c.deleteTable())}
>
<svg width="16" height="16" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<path d="M3.5 5h11M7 5V3.5h4V5M5 5l.7 9.5a1 1 0 0 0 1 .9h4.6a1 1 0 0 0 1-.9L13 5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
</div>
<style>
.table-toolbar {
position: absolute;
/* Sit just above the table's top-left corner. */
transform: translateY(calc(-100% - 6px));
z-index: 150;
display: flex;
align-items: center;
gap: 1px;
padding: 3px;
background: var(--color-surface);
border: 1px solid var(--color-border);
border-radius: var(--radius);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.14);
/* The toolbar is a sibling of the zoomed .paper, so it renders at a constant
size regardless of editor zoom. */
}
.tt-btn {
display: inline-flex;
align-items: center;
justify-content: center;
width: 1.7rem;
height: 1.7rem;
min-width: unset;
padding: 0;
border: none;
border-radius: calc(var(--radius) - 2px);
background: transparent;
color: var(--color-text);
cursor: pointer;
transition: background 0.12s, color 0.12s;
}
.tt-btn:hover {
background: var(--color-btn-hover);
}
.tt-danger:hover {
background: #fdecea;
color: #c0392b;
}
.tt-sep {
width: 1px;
height: 1.1rem;
margin: 0 2px;
background: var(--color-border);
}
</style>
+3
View File
@@ -2,6 +2,9 @@
flex: 1;
padding: 2rem 1rem 3rem; /* extra bottom padding clears the fixed status bar */
overflow: auto;
/* Positioning context for the floating table toolbar (TableToolbar.svelte),
which is an absolutely-positioned child that scrolls with the content. */
position: relative;
}
/* A4 paper container (794px = 210mm at 96dpi by default; --user-page-width is