dorfteich/apps/web/src/editor/SectionStyleMenu.tsx
Claude Fable 5 e32f961047
All checks were successful
CI / Lint, typecheck, test (push) Successful in 2m54s
CI / Build container images (push) Has been skipped
CD / Build and push images (push) Successful in 3m9s
CD / Deploy to Test (push) Successful in 11s
CD / Smoke tests against Test (push) Successful in 1m9s
CD / Promote to Int (push) Successful in 10s
CI / Auth e2e pack (push) Successful in 3m57s
CI / Import/export fidelity gate (push) Successful in 43s
Complete section-style plugins: CSS gate, injection, picker, export (#75)
Second half of #75 on top of the section node (2e96173/784f21d):

- Install gate for section_style CSS (plugin-css.ts): every rule must be
  scoped under one of the plugin's own .dt-style-<pluginId>-<styleId>
  classes (enforced, not rewritten — grouping at-rules checked inside,
  @font-face/@keyframes exempt, statement at-rules rejected); positioning
  out of the content flow (anything but static/relative) is rejected as an
  overlay vector; "</style" is rejected as a breakout vector for inlined
  embedding. Hostile fixtures from the acceptance list are pinned in
  plugin-css.test.ts.
- Web: usePondPlugins loads the pond's active plugins once per visit;
  SectionStyleSheets links each active style plugin's immutable
  styles.css; SectionStyleMenu (toolbar) wraps/restyles/unwraps with a
  picker fed from the plugins' i18n titles. Sections show a faint dashed
  hint while editing so unstyled (plugin-disabled) sections stay findable.
- PDF export: PluginsService.sectionStyleCssForPond inlines the pond's
  active section-style CSS into the Gotenberg HTML, so styled sections
  survive the network-isolated render; covered in export.service.db.test.
- Reference plugin packages/plugins/section-styles-basic (callout, info,
  warning, colored-box; theme-neutral semi-transparent backgrounds), a
  workspace package whose tests validate it against the SDK schema and
  whose real files run through the api install gate.
- e2e section-styles.spec.ts: install → wrap → computed background in edit
  and read mode → unwrap → neutral fallback after disabling the plugin.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EwZ4jR4KFAPvpjWevfUGX1
2026-07-11 11:43:36 +02:00

93 lines
3.2 KiB
TypeScript

import type { Editor } from '@tiptap/core';
import { useEditorState } from '@tiptap/react';
import { useTranslation } from 'react-i18next';
import type { SectionStyleOption } from '../plugins/use-pond-plugins';
/** The option's label in the UI language, falling back through English to the
* raw id (a manifest always carries de and en, so the id is a last resort). */
function optionLabel(option: SectionStyleOption, language: string): string {
const base = language.split('-')[0] ?? language;
return option.title[base] ?? option.title.en ?? option.styleId;
}
/**
* Toolbar control for styled sections (issue #75): a picker over the section
* styles the pond's active `section_style` plugins declare. Selecting a style
* wraps the selection (or restyles the surrounding section); the dedicated
* button unwraps. Hidden entirely when no style plugin is active — the
* feature only exists where an admin turned a plugin on.
*/
export function SectionStyleMenu({
editor,
options,
}: {
editor: Editor;
options: SectionStyleOption[];
}): React.JSX.Element | null {
const { t, i18n } = useTranslation('editor');
const active = useEditorState({
editor,
selector: ({ editor: e }) => {
if (!e.isActive('section')) return null;
const attrs = e.getAttributes('section');
return { pluginId: String(attrs.pluginId ?? ''), styleId: String(attrs.styleId ?? '') };
},
});
if (options.length === 0) return null;
const activeKey = active ? `${active.pluginId}/${active.styleId}` : '';
function applyStyle(key: string): void {
const [pluginId, styleId] = key.split('/');
if (!pluginId || !styleId) return;
const chain = editor.chain().focus();
if (active) {
chain.updateAttributes('section', { pluginId, styleId }).run();
} else {
chain.wrapInSection({ pluginId, styleId }).run();
}
}
return (
<div className="editor-toolbar__group editor-toolbar__section-menu">
<label className="editor-toolbar__section-label">
<span className="visually-hidden">{t('toolbar.section.label')}</span>
<select
className="editor-toolbar__section-select"
title={t('toolbar.section.label')}
value={activeKey}
// Keep the editor selection: without this the select steals focus
// and the wrap applies to a collapsed selection at the wrong spot.
onMouseDown={(event) => event.stopPropagation()}
onChange={(event) => applyStyle(event.target.value)}
>
<option value="" disabled>
{t('toolbar.section.none')}
</option>
{options.map((option) => {
const key = `${option.pluginId}/${option.styleId}`;
return (
<option key={key} value={key}>
{optionLabel(option, i18n.language)}
</option>
);
})}
</select>
</label>
<button
type="button"
className="toolbar-button"
title={t('toolbar.section.remove')}
aria-label={t('toolbar.section.remove')}
disabled={!active}
onMouseDown={(event) => event.preventDefault()}
onClick={() => editor.chain().focus().unwrapSection().run()}
>
</button>
</div>
);
}