feat: the style gallery lists heading levels 1–5 until asked for the rest

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.
This commit is contained in:
Steffen Becker committed 2026-08-16 14:01:30 +02:00
1 parent bc98446198
commit 9b14d1654f
8 files changed
+67 -7

No files matched your search

+17 -3
View File
@@ -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<string | null>(() => {
@@ -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)}
</button>
{/each}
<button
class="ol-option style-option show-all"
class:active={showAllStyles()}
onclick={toggleAllStyles}
aria-pressed={showAllStyles()}
>
{t().styles.showAll}
</button>
{#if charStyles.length}
<div class="ol-section-label char-label">{t().styles.characterStyles}</div>
{#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);
@@ -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<string>(() => {
@@ -27,6 +26,8 @@
return blockStyleName(editor.state.selection.$from.parent as never);
});
let paraStyles = $derived(visibleStyles(sheet, showAllStyles(), current));
let currentChar = $derived.by<string | null>(() => {
if (tick < 0 || !editor) return null;
return activeCharacterStyle(editor.state as never);
@@ -113,6 +114,9 @@
{#if styleShortcut(s.name)}<span class="menu-key">{styleShortcut(s.name)}</span>{/if}
</button>
{/each}
<button class="show-all" class:selected={showAllStyles()} aria-pressed={showAllStyles()} onclick={toggleAllStyles}>
{t().styles.showAll}
</button>
{#if charStyles.length}
<div class="rb-menu-label">{t().styles.characterStyles}</div>
{#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;
+1
View File
@@ -151,6 +151,7 @@ const de: Messages = {
styles: {
title: 'Formatvorlagen',
manage: 'Vorlagen verwalten…',
showAll: 'Alle Formatvorlagen anzeigen',
characterStyles: 'Zeichenvorlagen',
paragraphStyles: 'Absatzvorlagen',
newStyle: 'Neu',
+1
View File
@@ -155,6 +155,7 @@ const en = {
styles: {
title: 'Styles',
manage: 'Manage styles…',
showAll: 'Show all styles',
characterStyles: 'Character styles',
paragraphStyles: 'Paragraph styles',
newStyle: 'New',
+3
View File
@@ -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
+11
View File
@@ -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;
+12
View File
@@ -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;
+10 -1
View File
@@ -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);