fix: styles manager

This commit is contained in:
Steffen Becker committed 2026-08-03 09:42:07 +02:00
1 parent 51584a98ea
commit 5b11cdf3f6
5 files changed
+90 -31

No files matched your search

+49 -20
View File
@@ -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<StyleFamily>('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<string | null>(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 @@
<ul class="list">
{#each rows as { style: s, depth } (s.name)}
<li>
<button
class="entry"
class:active={current === s.name}
style="padding-left: {0.6 + depth * 0.8}rem"
onclick={() => select(s.name)}
>
{s.name}
{#if isAbstractStyle(s.name)}<span class="badge">{t().styles.abstract}</span>
{:else if !s.builtin}<span class="badge">{t().styles.custom}</span>{/if}
</button>
{#if editingName === s.name}
<input
class="entry rename"
style="padding-left: {indentRem(depth)}rem"
value={s.name}
use:focusSelect
onblur={(e) => 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}
<button
class="entry"
class:active={current === s.name}
style="padding-left: {indentRem(depth)}rem"
title={s.name}
onclick={() => select(s.name)}
ondblclick={() => { if (!s.builtin) editingName = s.name; }}
>
<span class="name">{s.name}</span>
{#if isAbstractStyle(s.name)}<span class="badge">{t().styles.abstract}</span>
{:else if !s.builtin}<span class="badge">{t().styles.custom}</span>{/if}
</button>
{/if}
</li>
{/each}
</ul>
@@ -402,7 +427,7 @@
{#if style.builtin}
<button onclick={() => resetStyle(style.name, family)}>{t().styles.reset}</button>
{:else}
<button onclick={rename}>{t().styles.rename}</button>
<button onclick={() => (editingName = style.name)}>{t().styles.rename}</button>
<button class="danger" onclick={remove} disabled={style.name === HEADING_PARENT}>{t().common.remove}</button>
{/if}
</footer>
@@ -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;
-1
View File
@@ -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: {
-1
View File
@@ -98,7 +98,6 @@ const en = {
updateFromSelection: 'Update from selection',
rename: 'Rename…',
reset: 'Reset to default',
renamePrompt: 'New style name:',
newName: 'Style',
},
history: {
+29 -9
View File
@@ -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<string>();
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.
+12
View File
@@ -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