From 9b14d1654f1664df96b84af0d739758a5daf4a36 Mon Sep 17 00:00:00 2001 From: Steffen Becker Date: Sun, 16 Aug 2026 14:01:30 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20the=20style=20gallery=20lists=20heading?= =?UTF-8?q?=20levels=201=E2=80=935=20until=20asked=20for=20the=20rest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both reference products define ten levels but only offer the first five; visibleStyles() filters styleOrder for the gallery in either chrome, with a session-only 'show all styles' toggle in the menu. A deeper level a block already uses stays listed, so the caret always has an active style. --- src/lib/components/Toolbar.svelte | 20 ++++++++++++++++--- .../ribbon/controls/StyleGallery.svelte | 15 +++++++++++--- src/lib/i18n/locales/de.ts | 1 + src/lib/i18n/locales/en.ts | 1 + src/lib/styles/CLAUDE.md | 3 +++ src/lib/styles/sheet.svelte.ts | 11 ++++++++++ src/lib/styles/styleSheet.ts | 12 +++++++++++ tests/unit/style-resolve.test.ts | 11 +++++++++- 8 files changed, 67 insertions(+), 7 deletions(-) diff --git a/src/lib/components/Toolbar.svelte b/src/lib/components/Toolbar.svelte index 1c1ec51..12934e5 100644 --- a/src/lib/components/Toolbar.svelte +++ b/src/lib/components/Toolbar.svelte @@ -9,9 +9,9 @@ import { cellRegionText } from '../editor/extensions/tableStyle'; import { blockStyleName } from '../editor/extensions/paragraphStyle'; import { activeCharacterStyle } from '../editor/extensions/characterStyle'; - import { DEFAULT_STYLE, headingStyleName, resolveStyle, styleOrder, type StyleFamily } from '../styles/styleSheet'; + import { DEFAULT_STYLE, headingStyleName, resolveStyle, visibleStyles, type StyleFamily } from '../styles/styleSheet'; import { MAX_HEADING_LEVEL } from '../export/odt'; - import { styleSheet } from '../styles/sheet.svelte'; + import { showAllStyles, styleSheet, toggleAllStyles } from '../styles/sheet.svelte'; import { t } from '../i18n/i18n.svelte'; import { withShortcut } from '../i18n/shortcut'; import { shortcutHint, type ShortcutId } from '../editor/shortcuts'; @@ -31,7 +31,6 @@ // The document's named paragraph styles (LibreOffice model): the gallery lists the // registry, assigning one only sets the style — hard formatting stays, as in Word/LO. let sheet = $derived(styleSheet()); - let galleryStyles = $derived(styleOrder(sheet)); // Character styles apply to the selected run, not the block (LibreOffice's second family). let charStyles = $derived(Object.values(sheet.character ?? {})); let currentCharStyle = $derived.by(() => { @@ -75,6 +74,8 @@ return blockStyleName(editor.state.selection.$from.parent as never); }); + let galleryStyles = $derived(visibleStyles(sheet, showAllStyles(), currentStyle)); + // Only the styles the Shortcuts extension binds carry a hint. function styleShortcut(name: string): string | undefined { if (name === DEFAULT_STYLE) return shortcutHint('styleStandard'); @@ -247,6 +248,14 @@ {styleLabel(s.name)} {/each} + {#if charStyles.length} {#each charStyles as c} @@ -503,6 +512,11 @@ overflow: hidden; } + .show-all { + font-size: 0.8rem; + color: var(--color-text-muted); + } + .char-label { margin-top: 2px; border-top: 1px solid var(--color-border); diff --git a/src/lib/components/ribbon/controls/StyleGallery.svelte b/src/lib/components/ribbon/controls/StyleGallery.svelte index 045c8e3..0f13e5a 100644 --- a/src/lib/components/ribbon/controls/StyleGallery.svelte +++ b/src/lib/components/ribbon/controls/StyleGallery.svelte @@ -4,9 +4,9 @@ import { anchored, clickOutside, isMenuOpen, toggleMenu, closeMenu } from '../menu.svelte'; import { blockStyleName } from '../../../editor/extensions/paragraphStyle'; import { activeCharacterStyle } from '../../../editor/extensions/characterStyle'; - import { DEFAULT_STYLE, headingStyleName, resolveStyle, styleOrder, type StyleFamily } from '../../../styles/styleSheet'; + import { DEFAULT_STYLE, headingStyleName, resolveStyle, visibleStyles, type StyleFamily } from '../../../styles/styleSheet'; import { MAX_HEADING_LEVEL } from '../../../export/odt'; - import { styleSheet } from '../../../styles/sheet.svelte'; + import { showAllStyles, styleSheet, toggleAllStyles } from '../../../styles/sheet.svelte'; import { t } from '../../../i18n/i18n.svelte'; import { shortcutHint, type ShortcutId } from '../../../editor/shortcuts'; @@ -19,7 +19,6 @@ const ID = 'styleGallery'; let sheet = $derived(styleSheet()); - let paraStyles = $derived(styleOrder(sheet)); let charStyles = $derived(Object.values(sheet.character ?? {})); let current = $derived.by(() => { @@ -27,6 +26,8 @@ return blockStyleName(editor.state.selection.$from.parent as never); }); + let paraStyles = $derived(visibleStyles(sheet, showAllStyles(), current)); + let currentChar = $derived.by(() => { if (tick < 0 || !editor) return null; return activeCharacterStyle(editor.state as never); @@ -113,6 +114,9 @@ {#if styleShortcut(s.name)}{styleShortcut(s.name)}{/if} {/each} + {#if charStyles.length}
{t().styles.characterStyles}
{#each charStyles as c} @@ -213,6 +217,11 @@ .gallery-more:hover { background: var(--w-hover); } + .show-all { + color: var(--w-text-dim); + font-size: 11px; + } + .gallery-menu { max-height: 380px; min-width: 220px; diff --git a/src/lib/i18n/locales/de.ts b/src/lib/i18n/locales/de.ts index b60cbbc..d47f191 100644 --- a/src/lib/i18n/locales/de.ts +++ b/src/lib/i18n/locales/de.ts @@ -151,6 +151,7 @@ const de: Messages = { styles: { title: 'Formatvorlagen', manage: 'Vorlagen verwalten…', + showAll: 'Alle Formatvorlagen anzeigen', characterStyles: 'Zeichenvorlagen', paragraphStyles: 'Absatzvorlagen', newStyle: 'Neu', diff --git a/src/lib/i18n/locales/en.ts b/src/lib/i18n/locales/en.ts index 1f28cba..a2bff9b 100644 --- a/src/lib/i18n/locales/en.ts +++ b/src/lib/i18n/locales/en.ts @@ -155,6 +155,7 @@ const en = { styles: { title: 'Styles', manage: 'Manage styles…', + showAll: 'Show all styles', characterStyles: 'Character styles', paragraphStyles: 'Paragraph styles', newStyle: 'New', diff --git a/src/lib/styles/CLAUDE.md b/src/lib/styles/CLAUDE.md index dcb92ab..731e109 100644 --- a/src/lib/styles/CLAUDE.md +++ b/src/lib/styles/CLAUDE.md @@ -29,6 +29,9 @@ holds it as a reactive singleton persisted to `edentext-styles` (same shape as ` `clearDirectFormatting()` (Ctrl+M, also the ToolbarExpanded eraser button) drops marks and the style-governed block attrs but keeps the style and hyperlinks. `blockStyleName(node)` resolves a block's style (own → heading level → Standard). +- **The gallery lists `visibleStyles`, not `styleOrder`**: both reference products define ten + heading levels but offer the first `GALLERY_HEADING_LEVELS` (5); the rest need the menu's "show + all styles" (`showAllStyles`/`toggleAllStyles`, session-only) or a block already using them. - **Rendering**: `Editor.svelte` writes `styleCss(sheet)` into a `#document-styles` element on every change (then a `FORCE_PAGE_RECALC` so pagination re-measures). One rule per style keyed by `data-style`, plus `hN:not([data-style])` fallbacks for imported headings, plus a **text-only diff --git a/src/lib/styles/sheet.svelte.ts b/src/lib/styles/sheet.svelte.ts index 8292b7d..7e72689 100644 --- a/src/lib/styles/sheet.svelte.ts +++ b/src/lib/styles/sheet.svelte.ts @@ -28,6 +28,17 @@ export function setStyleSheet(next: StyleSheet): void { localStorage.setItem(STORAGE_KEY, JSON.stringify({ v: STYLE_SHEET_VERSION, ...next })); } +// Gallery-only, not persisted: whether it lists the deep heading levels too. +let allStyles = $state(false); + +export function showAllStyles(): boolean { + return allStyles; +} + +export function toggleAllStyles(): void { + allStyles = !allStyles; +} + // Replace one style (add or edit) — everything using it re-renders via styleCss. export function putStyle(style: Style, family: StyleFamily = 'paragraph'): void { const key = family === 'table' ? 'paragraph' : family; diff --git a/src/lib/styles/styleSheet.ts b/src/lib/styles/styleSheet.ts index fd27395..84329ae 100644 --- a/src/lib/styles/styleSheet.ts +++ b/src/lib/styles/styleSheet.ts @@ -164,6 +164,18 @@ export function styleOrder(sheet: StyleSheet, withAbstract = false, family: Styl return out; } +// Both reference products define ten heading levels but list only the first five in +// their gallery; the rest wait behind "show all styles" (the manager always has them). +export const GALLERY_HEADING_LEVELS = 5; + +// The gallery's slice of styleOrder. A deeper heading the document already uses stays +// listed, or the block under the caret would show no active style. +export function visibleStyles(sheet: StyleSheet, showAll = false, current?: string): Style[] { + return styleOrder(sheet).filter( + (s) => showAll || !s.outlineLevel || s.outlineLevel <= GALLERY_HEADING_LEVELS || s.name === current, + ); +} + // Styles that exist only to be inherited from. export function isAbstractStyle(name: string): boolean { return name === HEADING_PARENT; diff --git a/tests/unit/style-resolve.test.ts b/tests/unit/style-resolve.test.ts index 6224c9d..11271da 100644 --- a/tests/unit/style-resolve.test.ts +++ b/tests/unit/style-resolve.test.ts @@ -1,6 +1,6 @@ import { describe, it, expect } from 'vitest'; import { - builtinStyleSheet, mergeStoredSheet, resolveStyle, styleCss, styleOrder, DEFAULT_STYLE, + builtinStyleSheet, mergeStoredSheet, resolveStyle, styleCss, styleOrder, visibleStyles, DEFAULT_STYLE, STYLE_SHEET_VERSION, type StyleSheet, } from '../../src/lib/styles/styleSheet'; import { HEADING_STYLE_OVERRIDES } from '../../src/lib/export/odt'; @@ -103,6 +103,15 @@ describe('persisted style sheet', () => { expect(mergeStoredSheet(edited).paragraph['Heading'].text.fontFamily).toBe('Georgia'); }); + it('lists headings 1–5 in the gallery, the rest only on request or in use', () => { + const sheet = builtinStyleSheet(); + const names = (styles: { name: string }[]) => styles.map((s) => s.name); + expect(names(visibleStyles(sheet))).toContain('Heading 5'); + expect(names(visibleStyles(sheet))).not.toContain('Heading 6'); + expect(names(visibleStyles(sheet, true))).toContain('Heading 10'); + expect(names(visibleStyles(sheet, false, 'Heading 7'))).toContain('Heading 7'); + }); + it('falls back to the built-ins for junk', () => { expect(mergeStoredSheet(null).paragraph['Standard'].text.fontSizePt).toBe(12); expect(mergeStoredSheet({ v: 2 }).paragraph['Heading 1'].text.fontSizePt).toBe(18);