Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ Changelog ist die Upgrade-Anleitung für die Tools.

## [Unreleased]

### @basicbar/ui (→ wird `ui/v0.12.0`)

- `RichTextEditor` bekommt die Prop `tools` (Typ `RichTextTool[]`, Standard
`RICH_TEXT_TOOLS`, also alle Werkzeuge wie bisher). Ein weggelassenes
Werkzeug hat keinen Knopf, kein Tastenkürzel und keine Eingaberegel;
eingefügter Inhalt dieser Art kommt ohne die Formatierung an (eine
eingefügte H2 wird zum Absatz). So bietet ein Feld, dessen Backend nur
Fett und Listen behält, genau das an, z. B.
`tools={["bold", "bulletList", "orderedList"]}`. Bilder bleiben an
`onUploadImage` gebunden. Erprobt für orientierBAR (Studienaufbau je Fach).
- Migration: keine; ohne `tools` verhält sich der Editor wie bisher.

### Doku für die Tools (kein Release)

- Upgrade-Leitfaden [Vite 8, React 19, Django 6.1 und
Expand Down
10 changes: 10 additions & 0 deletions packages/ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ Unterstreichen/Durchgestrichen sind nicht registriert (TipTap-`underline`/
erzwingt `rel="noopener"` ohnehin) — eine Formatierung, die der Sanitizer
später wieder entfernt, soll der Nutzer erst gar nicht setzen können.

**Werkzeuge einschränken:** `tools` legt fest, welche Formatierung ein Feld
anbietet (Standard: alle aus `RICH_TEXT_TOOLS`). Was fehlt, hat weder Knopf
noch Tastenkürzel, und eingefügter Inhalt dieser Art verliert die
Formatierung. Sinnvoll, wenn der Sanitizer eines Felds weniger behält als das
volle Subset, z. B. nur Fett und Listen:

```tsx
<RichTextEditor value={v} onChange={setV} tools={["bold", "bulletList", "orderedList"]} />
```

**Zugänglicher Name:** der Editor braucht immer entweder `ariaLabel` (kein
sichtbares Label) oder `labelledBy` (Id eines bereits vorhandenen sichtbaren
`<label>`-Elements, z. B. `TranslatableField`s `labelId` — siehe unten).
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@basicbar/ui",
"version": "0.11.2",
"version": "0.12.0",
"description": "Design-System-Basis der virtUOS -bar-Tools: Tailwind-Preset, Basis-Styles, Theme (Dark Mode), i18n-Bootstrap, contentLang, TranslatableField, Preferences, SegmentedControl, RichText; RichTextEditor (TipTap) als eigener Entry",
"license": "Apache-2.0",
"author": "Universität Osnabrück (virtUOS)",
Expand Down
153 changes: 97 additions & 56 deletions packages/ui/src/RichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ function trackPosition(editor: Editor, pos: number) {
return tracked;
}

/** A formatting tool of the editor; images are governed by `onUploadImage`. */
export type RichTextTool = "bold" | "italic" | "heading" | "bulletList" | "orderedList" | "link";

/** Every formatting tool, the default of `tools`. */
export const RICH_TEXT_TOOLS: readonly RichTextTool[] = [
"bold", "italic", "heading", "bulletList", "orderedList", "link",
];

export interface RichTextEditorProps {
value: string;
onChange: (html: string) => void;
Expand Down Expand Up @@ -123,6 +131,13 @@ export interface RichTextEditorProps {
* saving or for a viewer without edit rights. Default `true`. (Pure
* display of stored HTML is `RichText`, which costs no TipTap.) */
editable?: boolean;
/** Formatting this field offers; default all of `RICH_TEXT_TOOLS`. A tool
* left out has no toolbar button, no keyboard shortcut and no input rule,
* and pasted content of that kind arrives without it (e.g. a pasted H2
* becomes a paragraph) — so a field whose backend keeps only bold and
* lists can offer exactly that. Read once when the editor mounts. Images
* stay governed by `onUploadImage`. */
tools?: readonly RichTextTool[];
}

function ToolbarButton({
Expand Down Expand Up @@ -172,7 +187,10 @@ export function RichTextEditor({
describedBy,
id,
editable = true,
tools = RICH_TEXT_TOOLS,
}: RichTextEditorProps) {
const has = (tool: RichTextTool) => tools.includes(tool);
const lists = has("bulletList") || has("orderedList");
const { t } = useTranslation();
const { dialog: promptDialog, prompt } = usePromptDialog();
// Detail of the last failed upload ("" = no message), shown under the
Expand Down Expand Up @@ -306,7 +324,12 @@ export function RichTextEditor({
editable,
extensions: [
StarterKit.configure({
heading: { levels: [2, 3] },
heading: has("heading") ? { levels: [2, 3] } : false,
italic: has("italic") ? undefined : false,
bulletList: has("bulletList") ? undefined : false,
orderedList: has("orderedList") ? undefined : false,
listItem: lists ? undefined : false,
listKeymap: lists ? undefined : false,
blockquote: false,
codeBlock: false,
code: false,
Expand All @@ -319,16 +342,20 @@ export function RichTextEditor({
underline: false,
strike: false,
}),
PlainBold,
...(has("bold") ? [PlainBold] : []),
// Links may point anywhere; the backend forces rel="noopener" itself,
// so we don't render one here (any value we chose would just be
// overwritten). We do not open a new tab (target), and we do not
// auto-link typed URLs (toolbar only).
Link.configure({
openOnClick: false,
autolink: false,
HTMLAttributes: { target: null, rel: null },
}),
...(has("link")
? [
Link.configure({
openOnClick: false,
autolink: false,
HTMLAttributes: { target: null, rel: null },
}),
]
: []),
// Kept unconditionally so existing <img> content survives even when
// this instance has no onUploadImage (view-only fields, other tools'
// legacy content, …) — only insertion is gated on onUploadImage.
Expand Down Expand Up @@ -531,55 +558,69 @@ export function RichTextEditor({
// Wraps onto further rows on narrow screens instead of a horizontal
// scroller whose hidden buttons had no affordance.
<div className="flex flex-wrap gap-1 border-b border-slate-200 dark:border-slate-800 px-2 py-1">
<ToolbarButton
label={t("Bold")}
active={editor.isActive("bold")}
onClick={() => editor.chain().focus().toggleBold().run()}
>
<BoldIcon aria-hidden className="h-4 w-4" />
</ToolbarButton>
<ToolbarButton
label={t("Italic")}
active={editor.isActive("italic")}
onClick={() => editor.chain().focus().toggleItalic().run()}
>
<ItalicIcon aria-hidden className="h-4 w-4" />
</ToolbarButton>
<ToolbarButton
label={t("Heading (large)")}
active={editor.isActive("heading", { level: 2 })}
onClick={() => editor.chain().focus().toggleHeading({ level: 2 }).run()}
>
<Heading2 aria-hidden className="h-4 w-4" />
</ToolbarButton>
<ToolbarButton
label={t("Heading (small)")}
active={editor.isActive("heading", { level: 3 })}
onClick={() => editor.chain().focus().toggleHeading({ level: 3 }).run()}
>
<Heading3 aria-hidden className="h-4 w-4" />
</ToolbarButton>
<ToolbarButton
label={t("Bulleted list")}
active={editor.isActive("bulletList")}
onClick={() => editor.chain().focus().toggleBulletList().run()}
>
<List aria-hidden className="h-4 w-4" />
</ToolbarButton>
<ToolbarButton
label={t("Numbered list")}
active={editor.isActive("orderedList")}
onClick={() => editor.chain().focus().toggleOrderedList().run()}
>
<ListOrdered aria-hidden className="h-4 w-4" />
</ToolbarButton>
<ToolbarButton
label={t("Link")}
active={editor.isActive("link")}
onClick={() => void setLink()}
>
<LinkIcon aria-hidden className="h-4 w-4" />
</ToolbarButton>
{has("bold") && (
<ToolbarButton
label={t("Bold")}
active={editor.isActive("bold")}
onClick={() => editor.chain().focus().toggleBold().run()}
>
<BoldIcon aria-hidden className="h-4 w-4" />
</ToolbarButton>
)}
{has("italic") && (
<ToolbarButton
label={t("Italic")}
active={editor.isActive("italic")}
onClick={() => editor.chain().focus().toggleItalic().run()}
>
<ItalicIcon aria-hidden className="h-4 w-4" />
</ToolbarButton>
)}
{has("heading") && (
<ToolbarButton
label={t("Heading (large)")}
active={editor.isActive("heading", { level: 2 })}
onClick={() => editor.chain().focus().toggleHeading({ level: 2 }).run()}
>
<Heading2 aria-hidden className="h-4 w-4" />
</ToolbarButton>
)}
{has("heading") && (
<ToolbarButton
label={t("Heading (small)")}
active={editor.isActive("heading", { level: 3 })}
onClick={() => editor.chain().focus().toggleHeading({ level: 3 }).run()}
>
<Heading3 aria-hidden className="h-4 w-4" />
</ToolbarButton>
)}
{has("bulletList") && (
<ToolbarButton
label={t("Bulleted list")}
active={editor.isActive("bulletList")}
onClick={() => editor.chain().focus().toggleBulletList().run()}
>
<List aria-hidden className="h-4 w-4" />
</ToolbarButton>
)}
{has("orderedList") && (
<ToolbarButton
label={t("Numbered list")}
active={editor.isActive("orderedList")}
onClick={() => editor.chain().focus().toggleOrderedList().run()}
>
<ListOrdered aria-hidden className="h-4 w-4" />
</ToolbarButton>
)}
{has("link") && (
<ToolbarButton
label={t("Link")}
active={editor.isActive("link")}
onClick={() => void setLink()}
>
<LinkIcon aria-hidden className="h-4 w-4" />
</ToolbarButton>
)}
{onUploadImage && (
<label
className="flex cursor-pointer items-center rounded px-2 py-1 text-sm text-slate-600 dark:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-800 focus-within:ring-2 focus-within:ring-brand-600 focus-within:ring-offset-1"
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/rich-text-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
* dependency only lands in bundles that actually render the editor — the main
* entry stays free of it. `RichText` (the renderer) has no such dependency
* and lives in the main entry. */
export { RichTextEditor } from "./RichTextEditor";
export type { InsertedImage, RichTextEditorProps } from "./RichTextEditor";
export { RICH_TEXT_TOOLS, RichTextEditor } from "./RichTextEditor";
export type { InsertedImage, RichTextEditorProps, RichTextTool } from "./RichTextEditor";