feat: add special charts

This commit is contained in:
Steffen Becker committed 2026-06-24 21:49:59 +02:00
1 parent 9082c05526
commit f9df5280f8
3 files changed
+659

No files matched your search

+322
View File
@@ -0,0 +1,322 @@
<script lang="ts">
import type { Editor } from '@tiptap/core';
import { CHAR_CATEGORIES, type SpecialChar } from './specialChars';
let {
editor,
open = $bindable(false),
onOpen,
onInsert,
}: {
editor: Editor | null;
open?: boolean;
onOpen?: () => void;
// Mirrors TablePicker's onInsert: the parent runs the editor command against
// the saved selection range (focus is stolen by the popover).
onInsert: (char: string, range: { from: number; to: number }) => void;
} = $props();
const RECENT_CHARS_KEY = 'odf-editor-recent-chars';
const MAX_RECENT = 12;
// De-duplicated flat index across all categories, for search and name lookup.
const flatIndex: SpecialChar[] = (() => {
const seen = new Set<string>();
const out: SpecialChar[] = [];
for (const cat of CHAR_CATEGORIES) {
for (const c of cat.chars) {
if (!seen.has(c.char)) { seen.add(c.char); out.push(c); }
}
}
return out;
})();
const nameByChar = new Map(flatIndex.map((e) => [e.char, e.name]));
let activeIndex = $state(0);
let query = $state('');
let recentChars = $state<string[]>(loadRecents());
let savedFrom: number | null = null;
let savedTo: number | null = null;
function loadRecents(): string[] {
try {
const raw = localStorage.getItem(RECENT_CHARS_KEY);
if (!raw) return [];
const parsed: unknown = JSON.parse(raw);
if (!Array.isArray(parsed)) return [];
return parsed.filter((x): x is string => typeof x === 'string').slice(0, MAX_RECENT);
} catch { return []; }
}
function saveRecents(chars: string[]) {
try { localStorage.setItem(RECENT_CHARS_KEY, JSON.stringify(chars)); } catch { /* quota or disabled */ }
}
function hexOf(char: string): string {
return (char.codePointAt(0) ?? 0).toString(16).toUpperCase().padStart(4, '0');
}
function titleOf(char: string): string {
const name = nameByChar.get(char);
return `${name ? name + ' ' : ''}(U+${hexOf(char)})`;
}
// Search results when the query is non-empty, otherwise the active category.
// Matches a name substring, the literal glyph, or a hex codepoint prefix.
let displayed = $derived.by(() => {
const raw = query.trim();
if (!raw) return CHAR_CATEGORIES[activeIndex].chars;
const q = raw.toLowerCase();
const hexQ = q.replace(/^u\+/, '').replace(/^0x/, '').replace(/\s+/g, '');
const isHex = /^[0-9a-f]+$/.test(hexQ);
return flatIndex.filter((e) =>
e.name.includes(q) ||
e.char === raw ||
(isHex && hexOf(e.char).toLowerCase().startsWith(hexQ)),
);
});
function openPicker() {
if (!editor) return;
onOpen?.();
if (open) {
open = false;
return;
}
savedFrom = editor.state.selection.from;
savedTo = editor.state.selection.to;
query = '';
open = true;
}
// Insert at the saved range, then keep the popover open and advance the saved
// caret to just after the glyph so consecutive picks type out in sequence.
function pick(char: string) {
if (!editor) return;
const from = savedFrom ?? editor.state.selection.from;
const to = savedTo ?? editor.state.selection.to;
onInsert(char, { from, to });
savedFrom = savedTo = editor.state.selection.to;
const next = [char, ...recentChars.filter((c) => c !== char)].slice(0, MAX_RECENT);
recentChars = next;
saveRecents(next);
}
function onKeydown(e: KeyboardEvent) {
if (e.key === 'Escape') {
open = false;
(e.currentTarget as HTMLElement).blur();
}
}
function charPickerClickOutside(node: HTMLElement) {
function handler(e: MouseEvent) {
if (!node.contains(e.target as Node)) open = false;
}
window.addEventListener('mousedown', handler);
return { destroy() { window.removeEventListener('mousedown', handler); } };
}
</script>
<div class="char-picker" use:charPickerClickOutside>
<button class="char-trigger" onclick={openPicker} title="Insert special character" aria-pressed={open}>
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<text x="8" y="12.5" font-size="14" font-weight="700" font-family="var(--font-serif, serif)" fill="currentColor" text-anchor="middle">Ω</text>
</svg>
</button>
{#if open}
<div class="char-dropdown" role="dialog" aria-label="Insert special character" tabindex="-1" onkeydown={onKeydown}>
<!-- svelte-ignore a11y_autofocus -->
<input
type="text"
class="char-search"
bind:value={query}
placeholder="Search by name or code…"
aria-label="Search special characters"
autofocus
/>
{#if !query && recentChars.length > 0}
<div class="char-section-label">Recent</div>
<div class="char-grid">
{#each recentChars as c (c)}
<button class="char-cell" title={titleOf(c)} onclick={() => pick(c)}>{c}</button>
{/each}
</div>
{/if}
{#if !query}
<div class="char-tabs" role="tablist">
{#each CHAR_CATEGORIES as cat, i}
<button
class="char-tab"
class:active={i === activeIndex}
role="tab"
aria-selected={i === activeIndex}
onclick={() => (activeIndex = i)}
>{cat.name}</button>
{/each}
</div>
{/if}
{#if displayed.length > 0}
<div class="char-grid main">
{#each displayed as entry (entry.char)}
<button class="char-cell" title={titleOf(entry.char)} onclick={() => pick(entry.char)}>{entry.char}</button>
{/each}
</div>
{:else}
<div class="char-empty">No matching characters</div>
{/if}
</div>
{/if}
</div>
<style>
.char-picker {
position: relative;
}
.char-trigger {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 2rem;
height: 2rem;
padding: 0 0.5rem;
border: none;
border-radius: var(--radius);
background: transparent;
color: var(--color-text);
cursor: pointer;
transition: background 0.15s;
}
.char-trigger:hover {
background: var(--color-btn-hover);
}
.char-trigger[aria-pressed='true'] {
background: var(--color-primary);
color: white;
}
.char-dropdown {
position: absolute;
top: calc(100% + 3px);
right: 0;
width: 312px;
background: var(--color-surface);
border: 1px solid var(--color-border);
border-radius: var(--radius);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
z-index: 200;
padding: 8px;
}
.char-search {
width: 100%;
height: 1.9rem;
padding: 0 0.5rem;
border: 1px solid var(--color-border);
border-radius: var(--radius);
background: var(--color-surface);
color: var(--color-text);
font-size: 0.82rem;
font-family: var(--font-sans);
outline: none;
transition: border-color 0.15s;
}
.char-search:hover,
.char-search:focus {
border-color: var(--color-primary);
}
.char-section-label {
padding: 0.45rem 0.1rem 0.2rem;
font-size: 0.65rem;
text-transform: uppercase;
letter-spacing: 0.06em;
color: var(--color-text-muted);
font-family: var(--font-sans);
user-select: none;
}
.char-tabs {
display: flex;
flex-wrap: wrap;
gap: 2px;
margin: 6px 0;
padding-bottom: 6px;
border-bottom: 1px solid var(--color-border);
}
.char-tab {
padding: 0.2rem 0.45rem;
border: 1px solid transparent;
border-radius: var(--radius);
background: transparent;
color: var(--color-text-muted);
font-size: 0.72rem;
font-family: var(--font-sans);
cursor: pointer;
transition: background 0.1s, color 0.1s;
}
.char-tab:hover {
background: var(--color-btn-hover);
color: var(--color-text);
}
.char-tab.active {
background: var(--color-primary);
color: white;
}
.char-grid {
display: grid;
grid-template-columns: repeat(10, 1fr);
gap: 2px;
}
.char-grid.main {
max-height: 220px;
overflow-y: auto;
}
.char-cell {
display: inline-flex;
align-items: center;
justify-content: center;
aspect-ratio: 1;
min-width: unset;
padding: 0;
border: 1px solid var(--color-border);
border-radius: 3px;
background: var(--color-surface);
color: var(--color-text);
font-family: var(--font-serif, serif);
font-size: 1rem;
line-height: 1;
cursor: pointer;
transition: background 0.1s, border-color 0.1s;
}
.char-cell:hover {
background: var(--color-primary);
border-color: var(--color-primary);
color: white;
}
.char-empty {
padding: 1rem 0.5rem;
text-align: center;
font-size: 0.8rem;
font-family: var(--font-sans);
color: var(--color-text-muted);
}
</style>
+28
View File
@@ -3,6 +3,7 @@
import { onMount } from 'svelte';
import ColorPicker from './ColorPicker.svelte';
import TablePicker from './TablePicker.svelte';
import SpecialCharPicker from './SpecialCharPicker.svelte';
import {
CANDIDATE_FONTS,
detectAvailableFonts,
@@ -643,6 +644,7 @@
fontColorOpen = false;
highlightColorOpen = false;
layoutOpen = false;
specialCharOpen = false;
}
function insertTable(rows: number, cols: number, range: { from: number; to: number }) {
@@ -650,6 +652,26 @@
editor.chain().focus().setTextSelection(range).insertTable({ rows, cols, withHeaderRow: false }).run();
}
// --- Special character insertion (SpecialCharPicker.svelte) ---
let specialCharOpen = $state(false);
// Close the sibling dropdowns when the special-character picker opens.
function onSpecialCharPickerOpen() {
fontOpen = false;
sizeOpen = false;
sizeInputFocused = false;
lineHeightOpen = false;
fontColorOpen = false;
highlightColorOpen = false;
layoutOpen = false;
tableOpen = false;
}
function insertSpecialChar(char: string, range: { from: number; to: number }) {
if (!editor) return;
editor.chain().focus().setTextSelection(range).insertContent(char).run();
}
// --- Image insertion (image.ts) ---
let imageInput = $state<HTMLInputElement | null>(null);
@@ -1113,6 +1135,12 @@
onOpen={onTablePickerOpen}
onInsert={insertTable}
/>
<SpecialCharPicker
{editor}
bind:open={specialCharOpen}
onOpen={onSpecialCharPickerOpen}
onInsert={insertSpecialChar}
/>
<button
onclick={() => imageInput?.click()}
disabled={!!hfActive}
+309
View File
@@ -0,0 +1,309 @@
// Curated catalog for the special-character picker (SpecialCharPicker.svelte).
// Glyphs are chosen to render in Liberation Serif / Times New Roman so the
// on-screen preview matches the exported .odt. `name` is lowercase for search.
export interface SpecialChar {
char: string;
/** Lowercase descriptive name, matched as a substring by the search box. */
name: string;
}
export interface CharCategory {
name: string;
chars: SpecialChar[];
}
export const CHAR_CATEGORIES: CharCategory[] = [
{
name: 'Punctuation',
chars: [
{ char: '—', name: 'em dash' },
{ char: '–', name: 'en dash' },
{ char: '‐', name: 'hyphen' },
{ char: '…', name: 'ellipsis horizontal' },
{ char: '·', name: 'middle dot interpunct' },
{ char: '•', name: 'bullet' },
{ char: '‣', name: 'triangular bullet' },
{ char: '¡', name: 'inverted exclamation mark' },
{ char: '¿', name: 'inverted question mark' },
{ char: '§', name: 'section sign' },
{ char: '¶', name: 'pilcrow paragraph sign' },
{ char: '†', name: 'dagger' },
{ char: '‡', name: 'double dagger' },
{ char: '‖', name: 'double vertical line' },
{ char: '¦', name: 'broken bar' },
{ char: '«', name: 'left guillemet angle quote' },
{ char: '»', name: 'right guillemet angle quote' },
{ char: '‹', name: 'single left angle quote' },
{ char: '›', name: 'single right angle quote' },
{ char: '“', name: 'left double quotation mark' },
{ char: '”', name: 'right double quotation mark' },
{ char: '„', name: 'double low-9 quotation mark' },
{ char: '‘', name: 'left single quotation mark' },
{ char: '’', name: 'right single quotation mark apostrophe' },
{ char: '‚', name: 'single low-9 quotation mark' },
{ char: '′', name: 'prime minutes feet' },
{ char: '″', name: 'double prime seconds inches' },
{ char: '‴', name: 'triple prime' },
{ char: '※', name: 'reference mark' },
{ char: '⁂', name: 'asterism' },
],
},
{
name: 'Currency',
chars: [
{ char: '€', name: 'euro' },
{ char: '£', name: 'pound sterling' },
{ char: '¥', name: 'yen yuan' },
{ char: '¢', name: 'cent' },
{ char: '$', name: 'dollar' },
{ char: '¤', name: 'currency sign generic' },
{ char: '₩', name: 'won' },
{ char: '₹', name: 'indian rupee' },
{ char: '₽', name: 'ruble' },
{ char: '₺', name: 'turkish lira' },
{ char: '₴', name: 'hryvnia' },
{ char: '₦', name: 'naira' },
{ char: '₱', name: 'peso' },
{ char: '฿', name: 'baht' },
{ char: '₪', name: 'new shekel' },
{ char: '₫', name: 'dong' },
{ char: '₨', name: 'rupee sign' },
],
},
{
name: 'Math',
chars: [
{ char: '±', name: 'plus minus' },
{ char: '∓', name: 'minus or plus' },
{ char: '×', name: 'multiplication times' },
{ char: '÷', name: 'division obelus' },
{ char: '−', name: 'minus sign' },
{ char: '≠', name: 'not equal to' },
{ char: '≤', name: 'less than or equal to' },
{ char: '≥', name: 'greater than or equal to' },
{ char: '≈', name: 'almost equal approximately' },
{ char: '≡', name: 'identical to equivalent' },
{ char: '∝', name: 'proportional to' },
{ char: '∞', name: 'infinity' },
{ char: '√', name: 'square root radical' },
{ char: '∛', name: 'cube root' },
{ char: '∑', name: 'n-ary summation sigma' },
{ char: '∏', name: 'n-ary product pi' },
{ char: '∫', name: 'integral' },
{ char: '∂', name: 'partial differential' },
{ char: '∆', name: 'increment delta' },
{ char: '∇', name: 'nabla del gradient' },
{ char: '∈', name: 'element of' },
{ char: '∉', name: 'not an element of' },
{ char: '⊂', name: 'subset of' },
{ char: '⊃', name: 'superset of' },
{ char: '∪', name: 'union' },
{ char: '∩', name: 'intersection' },
{ char: '∧', name: 'logical and' },
{ char: '∨', name: 'logical or' },
{ char: '¬', name: 'not sign negation' },
{ char: '∀', name: 'for all universal' },
{ char: '∃', name: 'there exists' },
{ char: '∅', name: 'empty set' },
{ char: '∥', name: 'parallel to' },
{ char: '⊥', name: 'perpendicular up tack' },
{ char: '∠', name: 'angle' },
{ char: '°', name: 'degree' },
{ char: '′', name: 'prime' },
{ char: 'µ', name: 'micro mu' },
{ char: '‰', name: 'per mille' },
{ char: '‱', name: 'per ten thousand basis point' },
{ char: '½', name: 'one half fraction' },
{ char: '⅓', name: 'one third fraction' },
{ char: '⅔', name: 'two thirds fraction' },
{ char: '¼', name: 'one quarter fraction' },
{ char: '¾', name: 'three quarters fraction' },
{ char: '⅛', name: 'one eighth fraction' },
{ char: '¹', name: 'superscript one' },
{ char: '²', name: 'superscript two squared' },
{ char: '³', name: 'superscript three cubed' },
{ char: '⁰', name: 'superscript zero' },
],
},
{
name: 'Arrows',
chars: [
{ char: '←', name: 'leftwards arrow left' },
{ char: '→', name: 'rightwards arrow right' },
{ char: '↑', name: 'upwards arrow up' },
{ char: '↓', name: 'downwards arrow down' },
{ char: '↔', name: 'left right arrow horizontal' },
{ char: '↕', name: 'up down arrow vertical' },
{ char: '↖', name: 'north west arrow up left' },
{ char: '↗', name: 'north east arrow up right' },
{ char: '↘', name: 'south east arrow down right' },
{ char: '↙', name: 'south west arrow down left' },
{ char: '⇐', name: 'leftwards double arrow' },
{ char: '⇒', name: 'rightwards double arrow implies' },
{ char: '⇑', name: 'upwards double arrow' },
{ char: '⇓', name: 'downwards double arrow' },
{ char: '⇔', name: 'left right double arrow iff' },
{ char: '↩', name: 'leftwards arrow with hook' },
{ char: '↪', name: 'rightwards arrow with hook' },
{ char: '↻', name: 'clockwise open circle arrow' },
{ char: '↺', name: 'anticlockwise open circle arrow' },
{ char: '✓', name: 'check mark tick' },
{ char: '✗', name: 'ballot x cross' },
],
},
{
name: 'Greek',
chars: [
{ char: 'Α', name: 'capital alpha' },
{ char: 'Β', name: 'capital beta' },
{ char: 'Γ', name: 'capital gamma' },
{ char: 'Δ', name: 'capital delta' },
{ char: 'Ε', name: 'capital epsilon' },
{ char: 'Ζ', name: 'capital zeta' },
{ char: 'Η', name: 'capital eta' },
{ char: 'Θ', name: 'capital theta' },
{ char: 'Ι', name: 'capital iota' },
{ char: 'Κ', name: 'capital kappa' },
{ char: 'Λ', name: 'capital lambda' },
{ char: 'Μ', name: 'capital mu' },
{ char: 'Ν', name: 'capital nu' },
{ char: 'Ξ', name: 'capital xi' },
{ char: 'Ο', name: 'capital omicron' },
{ char: 'Π', name: 'capital pi' },
{ char: 'Ρ', name: 'capital rho' },
{ char: 'Σ', name: 'capital sigma' },
{ char: 'Τ', name: 'capital tau' },
{ char: 'Υ', name: 'capital upsilon' },
{ char: 'Φ', name: 'capital phi' },
{ char: 'Χ', name: 'capital chi' },
{ char: 'Ψ', name: 'capital psi' },
{ char: 'Ω', name: 'capital omega' },
{ char: 'α', name: 'alpha' },
{ char: 'β', name: 'beta' },
{ char: 'γ', name: 'gamma' },
{ char: 'δ', name: 'delta' },
{ char: 'ε', name: 'epsilon' },
{ char: 'ζ', name: 'zeta' },
{ char: 'η', name: 'eta' },
{ char: 'θ', name: 'theta' },
{ char: 'ι', name: 'iota' },
{ char: 'κ', name: 'kappa' },
{ char: 'λ', name: 'lambda' },
{ char: 'μ', name: 'mu' },
{ char: 'ν', name: 'nu' },
{ char: 'ξ', name: 'xi' },
{ char: 'ο', name: 'omicron' },
{ char: 'π', name: 'pi' },
{ char: 'ρ', name: 'rho' },
{ char: 'σ', name: 'sigma' },
{ char: 'ς', name: 'final sigma' },
{ char: 'τ', name: 'tau' },
{ char: 'υ', name: 'upsilon' },
{ char: 'φ', name: 'phi' },
{ char: 'χ', name: 'chi' },
{ char: 'ψ', name: 'psi' },
{ char: 'ω', name: 'omega' },
],
},
{
name: 'Latin',
chars: [
{ char: 'À', name: 'capital a grave' },
{ char: 'Á', name: 'capital a acute' },
{ char: 'Â', name: 'capital a circumflex' },
{ char: 'Ã', name: 'capital a tilde' },
{ char: 'Ä', name: 'capital a umlaut diaeresis' },
{ char: 'Å', name: 'capital a ring' },
{ char: 'Æ', name: 'capital ae ligature' },
{ char: 'Ç', name: 'capital c cedilla' },
{ char: 'È', name: 'capital e grave' },
{ char: 'É', name: 'capital e acute' },
{ char: 'Ê', name: 'capital e circumflex' },
{ char: 'Ë', name: 'capital e umlaut diaeresis' },
{ char: 'Ì', name: 'capital i grave' },
{ char: 'Í', name: 'capital i acute' },
{ char: 'Î', name: 'capital i circumflex' },
{ char: 'Ï', name: 'capital i umlaut diaeresis' },
{ char: 'Ñ', name: 'capital n tilde' },
{ char: 'Ò', name: 'capital o grave' },
{ char: 'Ó', name: 'capital o acute' },
{ char: 'Ô', name: 'capital o circumflex' },
{ char: 'Õ', name: 'capital o tilde' },
{ char: 'Ö', name: 'capital o umlaut diaeresis' },
{ char: 'Ø', name: 'capital o slash stroke' },
{ char: 'Œ', name: 'capital oe ligature' },
{ char: 'Ù', name: 'capital u grave' },
{ char: 'Ú', name: 'capital u acute' },
{ char: 'Û', name: 'capital u circumflex' },
{ char: 'Ü', name: 'capital u umlaut diaeresis' },
{ char: 'Ý', name: 'capital y acute' },
{ char: 'Ÿ', name: 'capital y umlaut diaeresis' },
{ char: 'à', name: 'a grave' },
{ char: 'á', name: 'a acute' },
{ char: 'â', name: 'a circumflex' },
{ char: 'ã', name: 'a tilde' },
{ char: 'ä', name: 'a umlaut diaeresis' },
{ char: 'å', name: 'a ring' },
{ char: 'æ', name: 'ae ligature' },
{ char: 'ç', name: 'c cedilla' },
{ char: 'è', name: 'e grave' },
{ char: 'é', name: 'e acute' },
{ char: 'ê', name: 'e circumflex' },
{ char: 'ë', name: 'e umlaut diaeresis' },
{ char: 'ì', name: 'i grave' },
{ char: 'í', name: 'i acute' },
{ char: 'î', name: 'i circumflex' },
{ char: 'ï', name: 'i umlaut diaeresis' },
{ char: 'ñ', name: 'n tilde enye' },
{ char: 'ò', name: 'o grave' },
{ char: 'ó', name: 'o acute' },
{ char: 'ô', name: 'o circumflex' },
{ char: 'õ', name: 'o tilde' },
{ char: 'ö', name: 'o umlaut diaeresis' },
{ char: 'ø', name: 'o slash stroke' },
{ char: 'œ', name: 'oe ligature' },
{ char: 'ù', name: 'u grave' },
{ char: 'ú', name: 'u acute' },
{ char: 'û', name: 'u circumflex' },
{ char: 'ü', name: 'u umlaut diaeresis' },
{ char: 'ý', name: 'y acute' },
{ char: 'ÿ', name: 'y umlaut diaeresis' },
{ char: 'ß', name: 'sharp s eszett' },
],
},
{
name: 'Symbols',
chars: [
{ char: '©', name: 'copyright' },
{ char: '®', name: 'registered trademark' },
{ char: '™', name: 'trademark tm' },
{ char: '℗', name: 'sound recording copyright' },
{ char: '№', name: 'numero number sign' },
{ char: '℃', name: 'degrees celsius' },
{ char: '℉', name: 'degrees fahrenheit' },
{ char: 'Ω', name: 'ohm' },
{ char: '°', name: 'degree sign' },
{ char: '℠', name: 'service mark sm' },
{ char: '★', name: 'black star filled' },
{ char: '☆', name: 'white star outline' },
{ char: '♠', name: 'spade suit' },
{ char: '♣', name: 'club suit' },
{ char: '♥', name: 'heart suit' },
{ char: '♦', name: 'diamond suit' },
{ char: '♪', name: 'eighth note music' },
{ char: '♫', name: 'beamed eighth notes music' },
{ char: '✔', name: 'heavy check mark' },
{ char: '✘', name: 'heavy ballot x' },
{ char: '☑', name: 'ballot box with check' },
{ char: '☐', name: 'ballot box empty' },
{ char: '✓', name: 'tick checkmark' },
{ char: '☞', name: 'pointing hand right' },
{ char: '✉', name: 'envelope mail' },
{ char: '☎', name: 'telephone' },
{ char: '⚑', name: 'flag' },
{ char: '⚘', name: 'flower' },
{ char: ' ', name: 'non-breaking space nbsp' },
],
},
];