From 5b11cdf3f657759a0af016111f75049670ff0601 Mon Sep 17 00:00:00 2001 From: Steffen Becker Date: Mon, 3 Aug 2026 09:42:07 +0200 Subject: [PATCH] fix: styles manager --- src/lib/components/StyleManagerDialog.svelte | 69 ++++++++++++++------ src/lib/i18n/locales/de.ts | 1 - src/lib/i18n/locales/en.ts | 1 - src/lib/styles/styleSheet.ts | 38 ++++++++--- tests/unit/style-resolve.test.ts | 12 ++++ 5 files changed, 90 insertions(+), 31 deletions(-) diff --git a/src/lib/components/StyleManagerDialog.svelte b/src/lib/components/StyleManagerDialog.svelte index f18b503..27b2947 100644 --- a/src/lib/components/StyleManagerDialog.svelte +++ b/src/lib/components/StyleManagerDialog.svelte @@ -17,6 +17,10 @@ let { open = $bindable(false), editor }: { open?: boolean; editor: Editor | null } = $props(); const ALIGNMENTS: AlignValue[] = ['left', 'center', 'right', 'justify']; + // Beyond this the indent would push the name out of the 14rem pane, so deeper + // chains keep stacking at the last level. + const MAX_INDENT_DEPTH = 6; + const indentRem = (depth: number) => 0.6 + Math.min(depth, MAX_INDENT_DEPTH) * 0.8; let dialogEl: HTMLDialogElement | null = $state(null); let family = $state('paragraph'); @@ -30,11 +34,7 @@ let isChar = $derived(family === 'character'); let styles = $derived(isChar ? sheet.character : sheet.paragraph); // Indented by inheritance depth, so the chain is visible. - let rows = $derived( - isChar - ? Object.values(sheet.character).map((s) => ({ style: s, depth: depthOf(s) })) - : styleOrder(sheet, true).map((s) => ({ style: s, depth: depthOf(s) })), - ); + let rows = $derived(styleOrder(sheet, true, family).map((s) => ({ style: s, depth: depthOf(s) }))); let current = $derived(isChar ? selectedChar : selected); let style = $derived(styles[current] ?? Object.values(styles)[0]); let resolved = $derived(resolveStyle(sheet, style?.name, family)); @@ -196,16 +196,24 @@ selectedChar = name; } - function rename() { - const next = prompt(t().styles.renamePrompt, style.name)?.trim(); - if (!next || next === style.name || styles[next]) return; - const from = style.name; + // Renaming happens in place in the list — via double-click or the Rename button. + let editingName = $state(null); + + function commitRename(from: string, next: string) { + editingName = null; + next = next.trim(); + if (!next || next === from || styles[next]) return; renameStyle(from, next, family); // Retag every block that referenced the old name. retag(from, next); select(next); } + function focusSelect(el: HTMLInputElement) { + el.focus(); + el.select(); + } + // Re-point every block (or run) that referenced `from`; null drops the reference. function retag(from: string, to: string | null) { editor?.commands.command(({ tr, state, dispatch }) => { @@ -265,16 +273,33 @@
    {#each rows as { style: s, depth } (s.name)}
  • - + {#if editingName === s.name} + editingName === s.name && commitRename(s.name, e.currentTarget.value)} + onkeydown={(e) => { + if (e.key === 'Enter') e.currentTarget.blur(); + // Escape would otherwise close the whole dialog. + else if (e.key === 'Escape') { e.preventDefault(); editingName = null; } + }} + /> + {:else} + + {/if}
  • {/each}
@@ -402,7 +427,7 @@ {#if style.builtin} {:else} - + {/if} @@ -504,8 +529,12 @@ cursor: pointer; } .entry:hover { background: var(--color-btn-hover); } + .rename { padding-right: 0.6rem; outline: 1px solid var(--color-primary); outline-offset: -1px; } .entry.active { background: var(--color-primary); color: white; } + /* A long name is clipped instead of overflowing the pane (the title shows it in full). */ + .name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .badge { + flex: none; font-size: 0.6rem; text-transform: uppercase; letter-spacing: 0.05em; diff --git a/src/lib/i18n/locales/de.ts b/src/lib/i18n/locales/de.ts index a9b1647..52b9234 100644 --- a/src/lib/i18n/locales/de.ts +++ b/src/lib/i18n/locales/de.ts @@ -94,7 +94,6 @@ const de: Messages = { updateFromSelection: 'Aus Auswahl aktualisieren', rename: 'Umbenennen…', reset: 'Auf Standard zurücksetzen', - renamePrompt: 'Neuer Name der Vorlage:', newName: 'Vorlage', }, history: { diff --git a/src/lib/i18n/locales/en.ts b/src/lib/i18n/locales/en.ts index 6d942f8..6818159 100644 --- a/src/lib/i18n/locales/en.ts +++ b/src/lib/i18n/locales/en.ts @@ -98,7 +98,6 @@ const en = { updateFromSelection: 'Update from selection', rename: 'Rename…', reset: 'Reset to default', - renamePrompt: 'New style name:', newName: 'Style', }, history: { diff --git a/src/lib/styles/styleSheet.ts b/src/lib/styles/styleSheet.ts index 27700ca..8eba258 100644 --- a/src/lib/styles/styleSheet.ts +++ b/src/lib/styles/styleSheet.ts @@ -106,15 +106,35 @@ export function builtinStyleSheet(): StyleSheet { return { paragraph, character }; } -// The gallery order: built-ins as listed above, user styles after them. `Heading` is an -// abstract parent (LibreOffice's, holding what all levels share) — never assignable, so -// the gallery hides it; the style manager passes withAbstract to edit it. -export function styleOrder(sheet: StyleSheet, withAbstract = false): Style[] { - const names = Object.keys(sheet.paragraph); - const hidden = (n: string) => !withAbstract && n === HEADING_PARENT; - const builtinOrder = BUILTINS.map(b => b.name).filter(n => names.includes(n) && !hidden(n)); - const rest = names.filter(n => !builtinOrder.includes(n) && !hidden(n)).sort(); - return [...builtinOrder, ...rest].map(n => sheet.paragraph[n]); +// Inheritance order: every style directly followed by its own children, so the manager's +// indent matches the tree. Siblings sort built-ins first (as listed above), then by name. +// `Heading` is an abstract parent (LibreOffice's, holding what all levels share) — never +// assignable, so the gallery hides it (its children stay); the manager passes withAbstract. +export function styleOrder(sheet: StyleSheet, withAbstract = false, family: StyleFamily = 'paragraph'): Style[] { + const styles = family === 'character' ? sheet.character : sheet.paragraph; + const rank = new Map((family === 'character' ? CHAR_BUILTINS : BUILTINS).map((b, i) => [b.name, i])); + const order = (a: string, b: string) => (rank.get(a) ?? 1e9) - (rank.get(b) ?? 1e9) || a.localeCompare(b); + // A style whose parent is missing from the sheet hangs at the root. + const parentOf = (n: string) => (styles[n].parent && styles[styles[n].parent!] ? styles[n].parent : null); + const out: Style[] = []; + const seen = new Set(); + const emit = (parent: string | null) => { + for (const name of Object.keys(styles).filter(n => parentOf(n) === parent).sort(order)) { + if (seen.has(name)) continue; + seen.add(name); + if (withAbstract || name !== HEADING_PARENT) out.push(styles[name]); + emit(name); + } + }; + emit(null); + // A parent cycle reaches no root; list its members anyway. + for (const name of Object.keys(styles).sort(order)) { + if (seen.has(name)) continue; + seen.add(name); + out.push(styles[name]); + emit(name); + } + return out; } // Styles that exist only to be inherited from. diff --git a/tests/unit/style-resolve.test.ts b/tests/unit/style-resolve.test.ts index 36c03ee..6224c9d 100644 --- a/tests/unit/style-resolve.test.ts +++ b/tests/unit/style-resolve.test.ts @@ -67,6 +67,18 @@ describe('named paragraph styles', () => { expect(names).toContain('Heading 5'); expect(names).toContain('Quotations'); }); + + it('lists every style directly after its parent', () => { + const sheet = builtinStyleSheet(); + for (const [name, parent] of [['warum', DEFAULT_STYLE], ['hallo', DEFAULT_STYLE], ['ordnung', 'warum']]) { + sheet.paragraph[name] = { name, parent, next: null, para: {}, text: {} }; + } + const names = styleOrder(sheet, true).map((s) => s.name); + expect(names.indexOf('ordnung')).toBe(names.indexOf('warum') + 1); + expect(names.indexOf('hallo')).toBeLessThan(names.indexOf('warum')); + // Built-ins keep their listed order ahead of the user styles. + expect(names.slice(0, 3)).toEqual([DEFAULT_STYLE, 'Heading', 'Heading 1']); + }); }); // The sheet lives in localStorage, so a released change to the built-ins has to reach