feat: text highlighting

This commit is contained in:
Steffen Becker committed 2026-06-06 12:19:05 +02:00
1 parent 1ca69b09cb
commit 355f12afd7
6 files changed
+898 -747

No files matched your search

+14
View File
@@ -14,6 +14,7 @@
"@tiptap/extension-document": "^3.23.6",
"@tiptap/extension-font-family": "^3.23.6",
"@tiptap/extension-heading": "^3.23.6",
"@tiptap/extension-highlight": "3.23.6",
"@tiptap/extension-history": "^3.23.6",
"@tiptap/extension-italic": "^3.23.6",
"@tiptap/extension-list-item": "^3.23.6",
@@ -520,6 +521,19 @@
"@tiptap/core": "3.23.6"
}
},
"node_modules/@tiptap/extension-highlight": {
"version": "3.23.6",
"resolved": "https://registry.npmjs.org/@tiptap/extension-highlight/-/extension-highlight-3.23.6.tgz",
"integrity": "sha512-GWv6R4iZzVrNeB33qtJDBt/BfVjuc5S/sUF/VDFAtK8h86bg4cEm1tGLEsui4T3+vij/t+P+eoGhkzF6NGogdg==",
"license": "MIT",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/ueberdosis"
},
"peerDependencies": {
"@tiptap/core": "3.23.6"
}
},
"node_modules/@tiptap/extension-history": {
"version": "3.23.6",
"resolved": "https://registry.npmjs.org/@tiptap/extension-history/-/extension-history-3.23.6.tgz",
+1
View File
@@ -22,6 +22,7 @@
"@tiptap/extension-document": "^3.23.6",
"@tiptap/extension-font-family": "^3.23.6",
"@tiptap/extension-heading": "^3.23.6",
"@tiptap/extension-highlight": "3.23.6",
"@tiptap/extension-history": "^3.23.6",
"@tiptap/extension-italic": "^3.23.6",
"@tiptap/extension-list-item": "^3.23.6",
+773
View File
@@ -0,0 +1,773 @@
<script lang="ts">
import type { Editor } from '@tiptap/core';
import type { Snippet } from 'svelte';
import { onDestroy, untrack } from 'svelte';
let {
editor,
currentColor,
defaultColor,
title,
chevronTitle,
open = $bindable(false),
onApply,
onClear,
onOpen,
icon,
}: {
editor: Editor | null;
// Uniform color of the selection. null = no color anywhere; '' = mixed.
currentColor: string | null;
defaultColor: string;
title: string;
chevronTitle: string;
open?: boolean;
onApply: (color: string, range: { from: number; to: number }) => void;
onClear: (range: { from: number; to: number }) => void;
onOpen?: () => void;
icon: Snippet;
} = $props();
// Row 1 = greyscale, row 2 = standard hues. Rows 3–7 are generated per column
// as a light→dark gradient over the same hue, so each column reads as a shade ramp.
const COLUMN_HUES: { h: number; sBase: number }[] = [
{ h: 0, sBase: 0.85 }, // red (crimson side)
{ h: 8, sBase: 1.00 }, // red
{ h: 35, sBase: 1.00 }, // orange
{ h: 55, sBase: 1.00 }, // yellow
{ h: 88, sBase: 0.55 }, // yellow-green
{ h: 146, sBase: 1.00 }, // green
{ h: 195, sBase: 1.00 }, // cyan
{ h: 210, sBase: 1.00 }, // blue
{ h: 225, sBase: 1.00 }, // deep blue
{ h: 275, sBase: 0.65 }, // purple
];
const SHADE_STOPS: { sScale: number; v: number }[] = [
{ sScale: 0.25, v: 1.00 },
{ sScale: 0.50, v: 0.95 },
{ sScale: 0.75, v: 0.82 },
{ sScale: 1.00, v: 0.55 },
{ sScale: 1.00, v: 0.30 },
];
const COLOR_PALETTE: string[][] = [
['#000000', '#1A1A1A', '#333333', '#4D4D4D', '#666666', '#808080', '#999999', '#B3B3B3', '#CCCCCC', '#FFFFFF'],
['#C00000', '#FF0000', '#FFC000', '#FFFF00', '#92D050', '#00B050', '#00B0F0', '#0070C0', '#002060', '#7030A0'],
...SHADE_STOPS.map(stop =>
COLUMN_HUES.map(({ h, sBase }) => hsvToHex(h, sBase * stop.sScale, stop.v)),
),
];
// 'palette' = swatch grid; 'custom' = HSV picker dialog. Both live under `open`,
// so the parent can close the whole popover via the bindable `open` prop.
let view = $state<'palette' | 'custom'>('palette');
// Seeded once from the prop (constant per instance); the user's later picks
// own this value, so we read the initial prop without tracking it.
let lastColor = $state<string>(untrack(() => defaultColor));
const eyeDropperSupported =
typeof window !== 'undefined' && 'EyeDropper' in window;
let pickerH = $state<number>(0);
let pickerS = $state<number>(1);
let pickerV = $state<number>(0.75);
let stagedColor = $derived(hsvToHex(pickerH, pickerS, pickerV));
let savedFrom: number | null = null;
let savedTo: number | null = null;
function clamp01(n: number): number {
return n < 0 ? 0 : n > 1 ? 1 : n;
}
function hsvToHex(h: number, s: number, v: number): string {
const c = v * s;
const hh = ((h % 360) + 360) % 360 / 60;
const x = c * (1 - Math.abs((hh % 2) - 1));
let r = 0, g = 0, b = 0;
if (hh < 1) { r = c; g = x; b = 0; }
else if (hh < 2) { r = x; g = c; b = 0; }
else if (hh < 3) { r = 0; g = c; b = x; }
else if (hh < 4) { r = 0; g = x; b = c; }
else if (hh < 5) { r = x; g = 0; b = c; }
else { r = c; g = 0; b = x; }
const m = v - c;
const hex = (n: number) => Math.round((n + m) * 255).toString(16).padStart(2, '0');
return `#${hex(r)}${hex(g)}${hex(b)}`.toUpperCase();
}
function hexToHsv(hex: string): { h: number; s: number; v: number } | null {
const m = /^#([0-9a-f]{6})$/i.exec(hex.trim());
if (!m) return null;
const r = parseInt(m[1].slice(0, 2), 16) / 255;
const g = parseInt(m[1].slice(2, 4), 16) / 255;
const b = parseInt(m[1].slice(4, 6), 16) / 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const d = max - min;
let h = 0;
if (d !== 0) {
if (max === r) h = ((g - b) / d) % 6;
else if (max === g) h = (b - r) / d + 2;
else h = (r - g) / d + 4;
h *= 60;
if (h < 0) h += 360;
}
const s = max === 0 ? 0 : d / max;
return { h, s, v: max };
}
function openPicker() {
if (!editor) return;
onOpen?.();
savedFrom = editor.state.selection.from;
savedTo = editor.state.selection.to;
if (open) {
open = false;
} else {
view = 'palette';
open = true;
}
}
function applyColor(color: string) {
if (!editor) return;
open = false;
view = 'palette';
lastColor = color;
const from = savedFrom ?? editor.state.selection.from;
const to = savedTo ?? editor.state.selection.to;
savedFrom = null;
savedTo = null;
onApply(color, { from, to });
}
function quickApplyColor() {
if (!editor) return;
// Save selection here too because the main button doesn't go through openPicker.
if (savedFrom === null) {
savedFrom = editor.state.selection.from;
savedTo = editor.state.selection.to;
}
applyColor(lastColor);
}
function clearColor() {
if (!editor) return;
open = false;
view = 'palette';
const from = savedFrom ?? editor.state.selection.from;
const to = savedTo ?? editor.state.selection.to;
savedFrom = null;
savedTo = null;
onClear({ from, to });
}
function openMoreColors() {
const seed = (currentColor && currentColor !== '') ? currentColor : lastColor;
const hsv = hexToHsv(seed) ?? { h: 0, s: 1, v: 0.75 };
pickerH = hsv.h;
pickerS = hsv.s;
pickerV = hsv.v;
view = 'custom';
}
function confirmMoreColors() {
applyColor(stagedColor);
}
function cancelMoreColors() {
open = false;
view = 'palette';
}
async function pickFromPage() {
open = false;
view = 'palette';
if (eyeDropperSupported) {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result = await new (window as any).EyeDropper().open();
applyColor(String(result.sRGBHex).toUpperCase());
} catch {
savedFrom = null;
savedTo = null;
}
return;
}
startDomPickMode();
}
let domPickActive = $state(false);
let hoveredEl: HTMLElement | null = null;
let prevOutline = '';
let prevOutlineOffset = '';
function getEditorRoot(): HTMLElement | null {
return (editor?.view?.dom as HTMLElement | undefined) ?? null;
}
function rgbToHex(rgb: string): string | null {
const m = /^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(rgb);
if (!m) return null;
const hex = (n: number) =>
Math.max(0, Math.min(255, n)).toString(16).padStart(2, '0');
return `#${hex(+m[1])}${hex(+m[2])}${hex(+m[3])}`.toUpperCase();
}
function setHover(el: HTMLElement) {
if (hoveredEl === el) return;
clearHover();
hoveredEl = el;
prevOutline = el.style.outline;
prevOutlineOffset = el.style.outlineOffset;
el.style.outline = '2px solid var(--color-primary)';
el.style.outlineOffset = '-2px';
}
function clearHover() {
if (!hoveredEl) return;
hoveredEl.style.outline = prevOutline;
hoveredEl.style.outlineOffset = prevOutlineOffset;
hoveredEl = null;
}
function startDomPickMode() {
const root = getEditorRoot();
if (!root) return;
domPickActive = true;
root.style.cursor = 'crosshair';
root.addEventListener('mousemove', onPickMove, true);
root.addEventListener('mousedown', onPickMouseDown, true);
window.addEventListener('keydown', onPickKey, true);
window.addEventListener('mousedown', onOutsideMouseDown, true);
}
function stopDomPickMode() {
if (!domPickActive) return;
domPickActive = false;
const root = getEditorRoot();
if (root) {
root.style.cursor = '';
root.removeEventListener('mousemove', onPickMove, true);
root.removeEventListener('mousedown', onPickMouseDown, true);
}
window.removeEventListener('keydown', onPickKey, true);
window.removeEventListener('mousedown', onOutsideMouseDown, true);
clearHover();
}
onDestroy(stopDomPickMode);
function onPickMove(e: MouseEvent) {
const t = e.target;
if (t instanceof HTMLElement) setHover(t);
}
function onPickMouseDown(e: MouseEvent) {
e.preventDefault();
e.stopPropagation();
const t = e.target;
let hex: string | null = null;
if (t instanceof HTMLElement) {
hex = rgbToHex(window.getComputedStyle(t).color);
}
stopDomPickMode();
if (hex) applyColor(hex);
else { savedFrom = null; savedTo = null; }
}
function onPickKey(e: KeyboardEvent) {
if (e.key !== 'Escape') return;
e.preventDefault();
stopDomPickMode();
savedFrom = null;
savedTo = null;
}
function onOutsideMouseDown(e: MouseEvent) {
const root = getEditorRoot();
if (root && !root.contains(e.target as Node)) {
stopDomPickMode();
savedFrom = null;
savedTo = null;
}
}
function onSvPointerDown(e: PointerEvent) {
// Keep the editor's text selection visible by stopping the browser from
// shifting focus / collapsing the selection on this click.
e.preventDefault();
const el = e.currentTarget as HTMLElement;
const rect = el.getBoundingClientRect();
const update = (ev: PointerEvent) => {
pickerS = clamp01((ev.clientX - rect.left) / rect.width);
pickerV = clamp01(1 - (ev.clientY - rect.top) / rect.height);
};
el.setPointerCapture(e.pointerId);
update(e);
const move = (ev: PointerEvent) => update(ev);
const up = (ev: PointerEvent) => {
el.releasePointerCapture(ev.pointerId);
el.removeEventListener('pointermove', move);
el.removeEventListener('pointerup', up);
el.removeEventListener('pointercancel', up);
};
el.addEventListener('pointermove', move);
el.addEventListener('pointerup', up);
el.addEventListener('pointercancel', up);
}
function onHuePointerDown(e: PointerEvent) {
e.preventDefault();
const el = e.currentTarget as HTMLElement;
const rect = el.getBoundingClientRect();
const update = (ev: PointerEvent) => {
pickerH = clamp01((ev.clientY - rect.top) / rect.height) * 360;
};
el.setPointerCapture(e.pointerId);
update(e);
const move = (ev: PointerEvent) => update(ev);
const up = (ev: PointerEvent) => {
el.releasePointerCapture(ev.pointerId);
el.removeEventListener('pointermove', move);
el.removeEventListener('pointerup', up);
el.removeEventListener('pointercancel', up);
};
el.addEventListener('pointermove', move);
el.addEventListener('pointerup', up);
el.addEventListener('pointercancel', up);
}
function onHexInput(e: Event) {
const value = (e.target as HTMLInputElement).value;
const hsv = hexToHsv(value);
if (!hsv) return;
pickerH = hsv.h;
pickerS = hsv.s;
pickerV = hsv.v;
}
function colorPickerClickOutside(node: HTMLElement) {
function handler(e: MouseEvent) {
if (!node.contains(e.target as Node)) {
open = false;
view = 'palette';
}
}
window.addEventListener('mousedown', handler);
return { destroy() { window.removeEventListener('mousedown', handler); } };
}
</script>
<div class="color-picker" use:colorPickerClickOutside>
<div class="color-split">
<button class="color-main" onclick={quickApplyColor} {title}>
{@render icon()}
<span class="color-bar" style="background: {lastColor}"></span>
</button>
<button class="color-chevron" onclick={openPicker} tabindex="-1" title={chevronTitle}>
<svg width="8" height="8" viewBox="0 0 8 8" fill="none" aria-hidden="true">
<path d="M1 2.5l3 3 3-3" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
</div>
{#if open && view === 'palette'}
<div class="color-dropdown">
<button
class="color-automatic"
class:active={currentColor === null}
onclick={clearColor}
>Automatic</button>
<div class="color-grid">
{#each COLOR_PALETTE as row}
<div class="color-row">
{#each row as c}
<button
class="color-cell"
class:active={currentColor === c}
style="background: {c}"
title={c}
onclick={() => applyColor(c)}
aria-label={c}
></button>
{/each}
</div>
{/each}
</div>
<div class="color-extras">
<button class="color-more-trigger" onclick={openMoreColors}>More colors…</button>
<button
class="color-pipette-trigger"
onclick={pickFromPage}
title="Pick a color from the page"
aria-label="Pick a color from the page"
>
<svg width="20" height="20" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<g transform="rotate(-45 8 8)" stroke="currentColor" stroke-width="1.1" stroke-linejoin="round" stroke-linecap="round">
<rect x="6.25" y="1.5" width="3.5" height="2.6" rx="1" fill="currentColor" stroke="none" />
<rect x="5" y="4.2" width="6" height="1.6" rx="0.4" />
<path d="M6.75 5.8 L6.75 11.4 L8 13.7 L9.25 11.4 L9.25 5.8 Z" />
</g>
</svg>
</button>
</div>
</div>
{/if}
{#if open && view === 'custom'}
<div class="color-window" role="dialog" aria-label="Choose color">
<div class="color-window-body">
<div
class="sv-square"
style="--hue: {pickerH}"
onpointerdown={onSvPointerDown}
role="presentation"
>
<div class="sv-thumb" style="left: {pickerS * 100}%; top: {(1 - pickerV) * 100}%"></div>
</div>
<div
class="hue-slider"
onpointerdown={onHuePointerDown}
role="presentation"
>
<div class="hue-thumb" style="top: {(pickerH / 360) * 100}%"></div>
</div>
</div>
<div class="color-window-readout">
<span class="color-window-swatch" style="background: {stagedColor}"></span>
<input class="color-window-hex" type="text" value={stagedColor} oninput={onHexInput} maxlength="7" />
</div>
<div class="color-more-actions">
<button class="color-cancel" onclick={cancelMoreColors}>Cancel</button>
<button class="color-confirm" onclick={confirmMoreColors}>OK</button>
</div>
</div>
{/if}
</div>
<style>
.color-picker {
position: relative;
}
.color-split {
display: inline-flex;
align-items: stretch;
height: 2rem;
border: 1px solid var(--color-border);
border-radius: var(--radius);
background: var(--color-surface);
overflow: hidden;
transition: border-color 0.15s;
}
.color-split:hover {
border-color: var(--color-primary);
}
.color-main {
display: inline-flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 1px;
min-width: 1.7rem;
height: 100%;
padding: 0 0.35rem;
border: none;
border-radius: 0;
background: transparent;
color: var(--color-text);
cursor: pointer;
}
.color-main:hover {
background: var(--color-btn-hover);
}
.color-bar {
display: block;
width: 14px;
height: 3px;
border-radius: 1px;
}
.color-chevron {
display: inline-flex;
align-items: center;
justify-content: center;
width: 18px;
min-width: unset;
height: 100%;
padding: 0;
border: none;
border-left: 1px solid var(--color-border);
border-radius: 0;
background: transparent;
color: var(--color-text);
cursor: pointer;
}
.color-chevron:hover {
background: var(--color-btn-hover);
}
.color-dropdown {
position: absolute;
top: calc(100% + 3px);
left: 0;
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: 6px;
display: flex;
flex-direction: column;
gap: 4px;
}
.color-automatic {
display: block;
width: 100%;
padding: 0.35rem 0.6rem;
border: 1px solid var(--color-border);
border-radius: calc(var(--radius) - 2px);
background: transparent;
color: var(--color-text);
font-size: 0.8rem;
font-family: var(--font-sans);
text-align: center;
cursor: pointer;
min-width: unset;
height: auto;
transition: background 0.1s;
}
.color-automatic:hover {
background: var(--color-btn-hover);
}
.color-automatic.active {
background: var(--color-primary);
color: white;
border-color: var(--color-primary);
}
.color-grid {
display: flex;
flex-direction: column;
gap: 2px;
}
.color-row {
display: flex;
gap: 2px;
}
.color-cell {
width: 18px;
height: 18px;
min-width: unset;
padding: 0;
border: 1px solid var(--color-border);
border-radius: 2px;
cursor: pointer;
transition: transform 0.05s;
}
.color-cell:hover {
transform: scale(1.15);
}
.color-cell.active {
outline: 2px solid var(--color-primary);
outline-offset: 1px;
}
.color-more-trigger {
display: block;
width: 100%;
padding: 0.35rem 0.6rem;
border: 1px solid var(--color-border);
border-radius: calc(var(--radius) - 2px);
background: transparent;
color: var(--color-text);
font-size: 0.8rem;
font-family: var(--font-sans);
text-align: center;
cursor: pointer;
min-width: unset;
height: auto;
transition: background 0.1s;
}
.color-more-trigger:hover {
background: var(--color-btn-hover);
}
.color-extras {
display: flex;
gap: 4px;
}
.color-extras .color-more-trigger {
flex: 1;
width: auto;
}
.color-pipette-trigger {
display: inline-flex;
align-items: center;
justify-content: center;
width: 34px;
padding: 0;
border: 1px solid var(--color-border);
border-radius: calc(var(--radius) - 2px);
background: transparent;
color: var(--color-text);
cursor: pointer;
min-width: unset;
height: auto;
transition: background 0.1s;
}
.color-pipette-trigger:hover {
background: var(--color-btn-hover);
}
.color-window {
position: absolute;
top: calc(100% + 3px);
left: 0;
width: 210px;
padding: 8px;
display: flex;
flex-direction: column;
gap: 8px;
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;
}
.color-window-body {
display: flex;
gap: 8px;
}
.sv-square {
position: relative;
width: 160px;
height: 160px;
cursor: crosshair;
border: 1px solid var(--color-border);
background:
linear-gradient(to top, #000, transparent),
linear-gradient(to right, #fff, hsl(var(--hue, 0), 100%, 50%));
touch-action: none;
}
.sv-thumb {
position: absolute;
width: 12px;
height: 12px;
border-radius: 50%;
border: 2px solid white;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.6);
transform: translate(-50%, -50%);
pointer-events: none;
}
.hue-slider {
position: relative;
width: 18px;
height: 160px;
cursor: pointer;
border: 1px solid var(--color-border);
background: linear-gradient(
to bottom,
#f00 0%, #ff0 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%
);
touch-action: none;
}
.hue-thumb {
position: absolute;
left: -3px;
width: 22px;
height: 6px;
border: 1px solid white;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.6);
transform: translateY(-50%);
pointer-events: none;
}
.color-window-readout {
display: flex;
align-items: center;
gap: 6px;
}
.color-window-swatch {
display: block;
width: 28px;
height: 24px;
border: 1px solid var(--color-border);
border-radius: 3px;
flex-shrink: 0;
}
.color-window-hex {
flex: 1;
min-width: 0;
height: 24px;
padding: 0 0.4rem;
border: 1px solid var(--color-border);
border-radius: 3px;
background: var(--color-surface);
color: var(--color-text);
font-size: 0.8rem;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
outline: none;
}
.color-window-hex:focus {
border-color: var(--color-primary);
}
.color-more-actions {
display: flex;
gap: 4px;
justify-content: flex-end;
}
.color-confirm,
.color-cancel {
padding: 0.25rem 0.6rem;
border-radius: calc(var(--radius) - 2px);
font-size: 0.75rem;
font-family: var(--font-sans);
cursor: pointer;
min-width: unset;
height: auto;
transition: background 0.1s, border-color 0.15s;
}
.color-cancel {
border: 1px solid var(--color-border);
background: transparent;
color: var(--color-text);
}
.color-cancel:hover {
background: var(--color-btn-hover);
}
.color-confirm {
border: 1px solid var(--color-primary);
background: var(--color-primary);
color: white;
}
.color-confirm:hover {
filter: brightness(1.1);
}
</style>
+68 -746
View File
@@ -1,6 +1,7 @@
<script lang="ts">
import type { Editor } from '@tiptap/core';
import { onDestroy, onMount } from 'svelte';
import { onMount } from 'svelte';
import ColorPicker from './ColorPicker.svelte';
import {
CANDIDATE_FONTS,
detectAvailableFonts,
@@ -245,108 +246,35 @@
}
});
// Row 1 = greyscale, row 2 = standard hues. Rows 3–7 are generated per column
// as a light→dark gradient over the same hue, so each column reads as a shade ramp.
const COLUMN_HUES: { h: number; sBase: number }[] = [
{ h: 0, sBase: 0.85 }, // red (crimson side)
{ h: 8, sBase: 1.00 }, // red
{ h: 35, sBase: 1.00 }, // orange
{ h: 55, sBase: 1.00 }, // yellow
{ h: 88, sBase: 0.55 }, // yellow-green
{ h: 146, sBase: 1.00 }, // green
{ h: 195, sBase: 1.00 }, // cyan
{ h: 210, sBase: 1.00 }, // blue
{ h: 225, sBase: 1.00 }, // deep blue
{ h: 275, sBase: 0.65 }, // purple
];
const SHADE_STOPS: { sScale: number; v: number }[] = [
{ sScale: 0.25, v: 1.00 },
{ sScale: 0.50, v: 0.95 },
{ sScale: 0.75, v: 0.82 },
{ sScale: 1.00, v: 0.55 },
{ sScale: 1.00, v: 0.30 },
];
const COLOR_PALETTE: string[][] = [
['#000000', '#1A1A1A', '#333333', '#4D4D4D', '#666666', '#808080', '#999999', '#B3B3B3', '#CCCCCC', '#FFFFFF'],
['#C00000', '#FF0000', '#FFC000', '#FFFF00', '#92D050', '#00B050', '#00B0F0', '#0070C0', '#002060', '#7030A0'],
...SHADE_STOPS.map(stop =>
COLUMN_HUES.map(({ h, sBase }) => hsvToHex(h, sBase * stop.sScale, stop.v)),
),
];
// Uniform color of the selection. null = no color anywhere; '' = mixed.
let currentColor = $derived.by<string | null>(() => {
// Uniform color of the selection for a given mark. null = no color anywhere; '' = mixed.
function uniformMarkColor(markName: string): string | null {
if (tick < 0 || !editor) return null;
const { from, to, empty } = editor.state.selection;
if (empty) {
const marks = editor.state.storedMarks ?? editor.state.selection.$head.marks();
return marks.find(m => m.type.name === 'textStyle')?.attrs.color ?? null;
return marks.find(m => m.type.name === markName)?.attrs.color ?? null;
}
let color: string | null | undefined;
let mixed = false;
editor.state.doc.nodesBetween(from, to, (node) => {
if (mixed || !node.isText) return;
const c = node.marks.find(m => m.type.name === 'textStyle')?.attrs.color ?? null;
const c = node.marks.find(m => m.type.name === markName)?.attrs.color ?? null;
if (color === undefined) color = c;
else if (color !== c) mixed = true;
});
return mixed ? '' : (color ?? null);
});
}
// Font color lives on the textStyle mark (fontColor.ts); highlight on the
// dedicated `highlight` mark (multicolor → exports to fo:background-color).
let currentFontColor = $derived(uniformMarkColor('textStyle'));
let currentHighlightColor = $derived(uniformMarkColor('highlight'));
let fontOpen = $state(false);
let sizeOpen = $state(false);
let lineHeightOpen = $state(false);
let alignOpen = $state(false);
let colorOpen = $state(false);
let lastColor = $state<string>('#C00000');
let moreColorsOpen = $state(false);
const eyeDropperSupported =
typeof window !== 'undefined' && 'EyeDropper' in window;
let pickerH = $state<number>(0);
let pickerS = $state<number>(1);
let pickerV = $state<number>(0.75);
let stagedColor = $derived(hsvToHex(pickerH, pickerS, pickerV));
function clamp01(n: number): number {
return n < 0 ? 0 : n > 1 ? 1 : n;
}
function hsvToHex(h: number, s: number, v: number): string {
const c = v * s;
const hh = ((h % 360) + 360) % 360 / 60;
const x = c * (1 - Math.abs((hh % 2) - 1));
let r = 0, g = 0, b = 0;
if (hh < 1) { r = c; g = x; b = 0; }
else if (hh < 2) { r = x; g = c; b = 0; }
else if (hh < 3) { r = 0; g = c; b = x; }
else if (hh < 4) { r = 0; g = x; b = c; }
else if (hh < 5) { r = x; g = 0; b = c; }
else { r = c; g = 0; b = x; }
const m = v - c;
const hex = (n: number) => Math.round((n + m) * 255).toString(16).padStart(2, '0');
return `#${hex(r)}${hex(g)}${hex(b)}`.toUpperCase();
}
function hexToHsv(hex: string): { h: number; s: number; v: number } | null {
const m = /^#([0-9a-f]{6})$/i.exec(hex.trim());
if (!m) return null;
const r = parseInt(m[1].slice(0, 2), 16) / 255;
const g = parseInt(m[1].slice(2, 4), 16) / 255;
const b = parseInt(m[1].slice(4, 6), 16) / 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const d = max - min;
let h = 0;
if (d !== 0) {
if (max === r) h = ((g - b) / d) % 6;
else if (max === g) h = (b - r) / d + 2;
else h = (r - g) / d + 4;
h *= 60;
if (h < 0) h += 360;
}
const s = max === 0 ? 0 : d / max;
return { h, s, v: max };
}
let fontColorOpen = $state(false);
let highlightColorOpen = $state(false);
let sizeInputFocused = $state(false);
let sizeInputValue = $state('');
let savedFrom: number | null = null;
@@ -364,7 +292,8 @@
sizeInputFocused = false;
lineHeightOpen = false;
alignOpen = false;
colorOpen = false;
fontColorOpen = false;
highlightColorOpen = false;
// Save selection before the picker button steals focus.
savedFrom = editor.state.selection.from;
savedTo = editor.state.selection.to;
@@ -411,7 +340,8 @@
fontOpen = false;
lineHeightOpen = false;
alignOpen = false;
colorOpen = false;
fontColorOpen = false;
highlightColorOpen = false;
savedFrom = editor.state.selection.from;
savedTo = editor.state.selection.to;
(e.target as HTMLInputElement).select();
@@ -439,7 +369,8 @@
fontOpen = false;
lineHeightOpen = false;
alignOpen = false;
colorOpen = false;
fontColorOpen = false;
highlightColorOpen = false;
if (!sizeInputFocused) {
savedFrom = editor.state.selection.from;
savedTo = editor.state.selection.to;
@@ -470,7 +401,8 @@
sizeOpen = false;
sizeInputFocused = false;
alignOpen = false;
colorOpen = false;
fontColorOpen = false;
highlightColorOpen = false;
lineHeightOpen = !lineHeightOpen;
}
@@ -508,7 +440,8 @@
sizeOpen = false;
sizeInputFocused = false;
alignOpen = false;
colorOpen = false;
fontColorOpen = false;
highlightColorOpen = false;
spaceBeforeOpen = false;
spaceAfterOpen = false;
if (axis === 'before') spaceBeforeFocused = true; else spaceAfterFocused = true;
@@ -573,7 +506,8 @@
sizeOpen = false;
sizeInputFocused = false;
lineHeightOpen = false;
colorOpen = false;
fontColorOpen = false;
highlightColorOpen = false;
alignOpen = !alignOpen;
}
@@ -663,7 +597,8 @@
sizeOpen = false;
sizeInputFocused = false;
lineHeightOpen = false;
colorOpen = false;
fontColorOpen = false;
highlightColorOpen = false;
alignOpen = false;
layoutOpen = !layoutOpen;
if (layoutOpen) syncMarginInputs();
@@ -677,245 +612,17 @@
return { destroy() { window.removeEventListener('mousedown', handler); } };
}
function openColorPicker() {
if (!editor) return;
// The color pickers (font + highlight) live in ColorPicker.svelte. When one
// opens it asks the parent (via the ColorPicker `onOpen` prop) to close the
// sibling toolbar dropdowns and the other color picker.
function onColorPickerOpen(which: 'font' | 'highlight') {
fontOpen = false;
sizeOpen = false;
sizeInputFocused = false;
lineHeightOpen = false;
alignOpen = false;
moreColorsOpen = false;
savedFrom = editor.state.selection.from;
savedTo = editor.state.selection.to;
colorOpen = !colorOpen;
}
function applyColor(color: string) {
if (!editor) return;
colorOpen = false;
moreColorsOpen = false;
lastColor = color;
const from = savedFrom ?? editor.state.selection.from;
const to = savedTo ?? editor.state.selection.to;
savedFrom = null;
savedTo = null;
editor.chain().focus().setTextSelection({ from, to }).setColor(color).run();
}
function quickApplyColor() {
if (!editor) return;
// Save selection here too because the main button doesn't go through openColorPicker.
if (savedFrom === null) {
savedFrom = editor.state.selection.from;
savedTo = editor.state.selection.to;
}
applyColor(lastColor);
}
function clearColor() {
if (!editor) return;
colorOpen = false;
moreColorsOpen = false;
const from = savedFrom ?? editor.state.selection.from;
const to = savedTo ?? editor.state.selection.to;
savedFrom = null;
savedTo = null;
editor.chain().focus().setTextSelection({ from, to }).unsetColor().run();
}
function openMoreColors() {
const seed = (currentColor && currentColor !== '') ? currentColor : lastColor;
const hsv = hexToHsv(seed) ?? { h: 0, s: 1, v: 0.75 };
pickerH = hsv.h;
pickerS = hsv.s;
pickerV = hsv.v;
colorOpen = false;
moreColorsOpen = true;
}
function confirmMoreColors() {
applyColor(stagedColor);
}
function cancelMoreColors() {
moreColorsOpen = false;
}
async function pickFromPage() {
colorOpen = false;
moreColorsOpen = false;
if (eyeDropperSupported) {
try {
const result = await new (window as any).EyeDropper().open();
applyColor(String(result.sRGBHex).toUpperCase());
} catch {
savedFrom = null;
savedTo = null;
}
return;
}
startDomPickMode();
}
let domPickActive = $state(false);
let hoveredEl: HTMLElement | null = null;
let prevOutline = '';
let prevOutlineOffset = '';
function getEditorRoot(): HTMLElement | null {
return (editor?.view?.dom as HTMLElement | undefined) ?? null;
}
function rgbToHex(rgb: string): string | null {
const m = /^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(rgb);
if (!m) return null;
const hex = (n: number) =>
Math.max(0, Math.min(255, n)).toString(16).padStart(2, '0');
return `#${hex(+m[1])}${hex(+m[2])}${hex(+m[3])}`.toUpperCase();
}
function setHover(el: HTMLElement) {
if (hoveredEl === el) return;
clearHover();
hoveredEl = el;
prevOutline = el.style.outline;
prevOutlineOffset = el.style.outlineOffset;
el.style.outline = '2px solid var(--color-primary)';
el.style.outlineOffset = '-2px';
}
function clearHover() {
if (!hoveredEl) return;
hoveredEl.style.outline = prevOutline;
hoveredEl.style.outlineOffset = prevOutlineOffset;
hoveredEl = null;
}
function startDomPickMode() {
const root = getEditorRoot();
if (!root) return;
domPickActive = true;
root.style.cursor = 'crosshair';
root.addEventListener('mousemove', onPickMove, true);
root.addEventListener('mousedown', onPickMouseDown, true);
window.addEventListener('keydown', onPickKey, true);
window.addEventListener('mousedown', onOutsideMouseDown, true);
}
function stopDomPickMode() {
if (!domPickActive) return;
domPickActive = false;
const root = getEditorRoot();
if (root) {
root.style.cursor = '';
root.removeEventListener('mousemove', onPickMove, true);
root.removeEventListener('mousedown', onPickMouseDown, true);
}
window.removeEventListener('keydown', onPickKey, true);
window.removeEventListener('mousedown', onOutsideMouseDown, true);
clearHover();
}
onDestroy(stopDomPickMode);
function onPickMove(e: MouseEvent) {
const t = e.target;
if (t instanceof HTMLElement) setHover(t);
}
function onPickMouseDown(e: MouseEvent) {
e.preventDefault();
e.stopPropagation();
const t = e.target;
let hex: string | null = null;
if (t instanceof HTMLElement) {
hex = rgbToHex(window.getComputedStyle(t).color);
}
stopDomPickMode();
if (hex) applyColor(hex);
else { savedFrom = null; savedTo = null; }
}
function onPickKey(e: KeyboardEvent) {
if (e.key !== 'Escape') return;
e.preventDefault();
stopDomPickMode();
savedFrom = null;
savedTo = null;
}
function onOutsideMouseDown(e: MouseEvent) {
const root = getEditorRoot();
if (root && !root.contains(e.target as Node)) {
stopDomPickMode();
savedFrom = null;
savedTo = null;
}
}
function onSvPointerDown(e: PointerEvent) {
// Keep the editor's text selection visible by stopping the browser from
// shifting focus / collapsing the selection on this click.
e.preventDefault();
const el = e.currentTarget as HTMLElement;
const rect = el.getBoundingClientRect();
const update = (ev: PointerEvent) => {
pickerS = clamp01((ev.clientX - rect.left) / rect.width);
pickerV = clamp01(1 - (ev.clientY - rect.top) / rect.height);
};
el.setPointerCapture(e.pointerId);
update(e);
const move = (ev: PointerEvent) => update(ev);
const up = (ev: PointerEvent) => {
el.releasePointerCapture(ev.pointerId);
el.removeEventListener('pointermove', move);
el.removeEventListener('pointerup', up);
el.removeEventListener('pointercancel', up);
};
el.addEventListener('pointermove', move);
el.addEventListener('pointerup', up);
el.addEventListener('pointercancel', up);
}
function onHuePointerDown(e: PointerEvent) {
e.preventDefault();
const el = e.currentTarget as HTMLElement;
const rect = el.getBoundingClientRect();
const update = (ev: PointerEvent) => {
pickerH = clamp01((ev.clientY - rect.top) / rect.height) * 360;
};
el.setPointerCapture(e.pointerId);
update(e);
const move = (ev: PointerEvent) => update(ev);
const up = (ev: PointerEvent) => {
el.releasePointerCapture(ev.pointerId);
el.removeEventListener('pointermove', move);
el.removeEventListener('pointerup', up);
el.removeEventListener('pointercancel', up);
};
el.addEventListener('pointermove', move);
el.addEventListener('pointerup', up);
el.addEventListener('pointercancel', up);
}
function onHexInput(e: Event) {
const value = (e.target as HTMLInputElement).value;
const hsv = hexToHsv(value);
if (!hsv) return;
pickerH = hsv.h;
pickerS = hsv.s;
pickerV = hsv.v;
}
function colorPickerClickOutside(node: HTMLElement) {
function handler(e: MouseEvent) {
if (!node.contains(e.target as Node)) {
colorOpen = false;
moreColorsOpen = false;
}
}
window.addEventListener('mousedown', handler);
return { destroy() { window.removeEventListener('mousedown', handler); } };
if (which === 'font') highlightColorOpen = false;
else fontColorOpen = false;
}
</script>
@@ -1009,98 +716,41 @@
<div class="toolbar-separator"></div>
<div class="toolbar-group">
<div class="color-picker" use:colorPickerClickOutside>
<div class="color-split">
<button class="color-main" onclick={quickApplyColor} title="Font color">
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<text x="3" y="11" font-size="10" font-weight="bold" font-family="sans-serif" fill="currentColor">A</text>
</svg>
<span class="color-bar" style="background: {lastColor}"></span>
</button>
<button class="color-chevron" onclick={openColorPicker} tabindex="-1" title="Choose font color">
<svg width="8" height="8" viewBox="0 0 8 8" fill="none" aria-hidden="true">
<path d="M1 2.5l3 3 3-3" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
</div>
{#if colorOpen}
<div class="color-dropdown">
<button
class="color-automatic"
class:active={currentColor === null}
onclick={clearColor}
>Automatic</button>
<div class="color-grid">
{#each COLOR_PALETTE as row}
<div class="color-row">
{#each row as c}
<button
class="color-cell"
class:active={currentColor === c}
style="background: {c}"
title={c}
onclick={() => applyColor(c)}
aria-label={c}
></button>
{/each}
</div>
{/each}
</div>
<div class="color-extras">
<button class="color-more-trigger" onclick={openMoreColors}>More colors…</button>
<button
class="color-pipette-trigger"
onclick={pickFromPage}
title="Pick a color from the page"
aria-label="Pick a color from the page"
>
<svg width="20" height="20" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<g transform="rotate(-45 8 8)" stroke="currentColor" stroke-width="1.1" stroke-linejoin="round" stroke-linecap="round">
<rect x="6.25" y="1.5" width="3.5" height="2.6" rx="1" fill="currentColor" stroke="none" />
<rect x="5" y="4.2" width="6" height="1.6" rx="0.4" />
<path d="M6.75 5.8 L6.75 11.4 L8 13.7 L9.25 11.4 L9.25 5.8 Z" />
</g>
</svg>
</button>
</div>
</div>
{/if}
{#if moreColorsOpen}
<div class="color-window" role="dialog" aria-label="Choose color">
<div class="color-window-body">
<div
class="sv-square"
style="--hue: {pickerH}"
onpointerdown={onSvPointerDown}
role="presentation"
>
<div class="sv-thumb" style="left: {pickerS * 100}%; top: {(1 - pickerV) * 100}%"></div>
</div>
<div
class="hue-slider"
onpointerdown={onHuePointerDown}
role="presentation"
>
<div class="hue-thumb" style="top: {(pickerH / 360) * 100}%"></div>
</div>
</div>
<div class="color-window-readout">
<span class="color-window-swatch" style="background: {stagedColor}"></span>
<input class="color-window-hex" type="text" value={stagedColor} oninput={onHexInput} maxlength="7" />
</div>
<div class="color-more-actions">
<button class="color-cancel" onclick={cancelMoreColors}>Cancel</button>
<button class="color-confirm" onclick={confirmMoreColors}>OK</button>
</div>
</div>
{/if}
</div>
<button disabled title="Highlight color (coming soon)">
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<rect x="2" y="2" width="12" height="12" rx="2" fill="none" stroke="currentColor" stroke-width="1.5"/>
<rect x="4" y="4" width="8" height="8" rx="1" fill="currentColor" opacity="0.3"/>
</svg>
</button>
<ColorPicker
{editor}
currentColor={currentFontColor}
defaultColor="#C00000"
title="Font color"
chevronTitle="Choose font color"
bind:open={fontColorOpen}
onOpen={() => onColorPickerOpen('font')}
onApply={(c, r) => editor?.chain().focus().setTextSelection(r).setColor(c).run()}
onClear={(r) => editor?.chain().focus().setTextSelection(r).unsetColor().run()}
>
{#snippet icon()}
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<text x="3" y="11" font-size="10" font-weight="bold" font-family="sans-serif" fill="currentColor">A</text>
</svg>
{/snippet}
</ColorPicker>
<ColorPicker
{editor}
currentColor={currentHighlightColor}
defaultColor="#FFFF00"
title="Highlight color"
chevronTitle="Choose highlight color"
bind:open={highlightColorOpen}
onOpen={() => onColorPickerOpen('highlight')}
onApply={(c, r) => editor?.chain().focus().setTextSelection(r).setHighlight({ color: c }).run()}
onClear={(r) => editor?.chain().focus().setTextSelection(r).unsetHighlight().run()}
>
{#snippet icon()}
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<rect x="2" y="2" width="12" height="12" rx="2" fill="none" stroke="currentColor" stroke-width="1.5"/>
<rect x="4" y="4" width="8" height="8" rx="1" fill="currentColor" opacity="0.3"/>
</svg>
{/snippet}
</ColorPicker>
</div>
<div class="toolbar-separator"></div>
@@ -2060,332 +1710,4 @@
color: white;
}
.color-picker {
position: relative;
}
.color-split {
display: inline-flex;
align-items: stretch;
height: 2rem;
border: 1px solid var(--color-border);
border-radius: var(--radius);
background: var(--color-surface);
overflow: hidden;
transition: border-color 0.15s;
}
.color-split:hover {
border-color: var(--color-primary);
}
.color-main {
display: inline-flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 1px;
min-width: 1.7rem;
height: 100%;
padding: 0 0.35rem;
border: none;
border-radius: 0;
background: transparent;
color: var(--color-text);
cursor: pointer;
}
.color-main:hover {
background: var(--color-btn-hover);
}
.color-bar {
display: block;
width: 14px;
height: 3px;
border-radius: 1px;
}
.color-chevron {
display: inline-flex;
align-items: center;
justify-content: center;
width: 18px;
min-width: unset;
height: 100%;
padding: 0;
border: none;
border-left: 1px solid var(--color-border);
border-radius: 0;
background: transparent;
color: var(--color-text);
cursor: pointer;
}
.color-chevron:hover {
background: var(--color-btn-hover);
}
.color-dropdown {
position: absolute;
top: calc(100% + 3px);
left: 0;
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: 6px;
display: flex;
flex-direction: column;
gap: 4px;
}
.color-automatic {
display: block;
width: 100%;
padding: 0.35rem 0.6rem;
border: 1px solid var(--color-border);
border-radius: calc(var(--radius) - 2px);
background: transparent;
color: var(--color-text);
font-size: 0.8rem;
font-family: var(--font-sans);
text-align: center;
cursor: pointer;
min-width: unset;
height: auto;
transition: background 0.1s;
}
.color-automatic:hover {
background: var(--color-btn-hover);
}
.color-automatic.active {
background: var(--color-primary);
color: white;
border-color: var(--color-primary);
}
.color-grid {
display: flex;
flex-direction: column;
gap: 2px;
}
.color-row {
display: flex;
gap: 2px;
}
.color-cell {
width: 18px;
height: 18px;
min-width: unset;
padding: 0;
border: 1px solid var(--color-border);
border-radius: 2px;
cursor: pointer;
transition: transform 0.05s;
}
.color-cell:hover {
transform: scale(1.15);
}
.color-cell.active {
outline: 2px solid var(--color-primary);
outline-offset: 1px;
}
.color-more-trigger {
display: block;
width: 100%;
padding: 0.35rem 0.6rem;
border: 1px solid var(--color-border);
border-radius: calc(var(--radius) - 2px);
background: transparent;
color: var(--color-text);
font-size: 0.8rem;
font-family: var(--font-sans);
text-align: center;
cursor: pointer;
min-width: unset;
height: auto;
transition: background 0.1s;
}
.color-more-trigger:hover {
background: var(--color-btn-hover);
}
.color-extras {
display: flex;
gap: 4px;
}
.color-extras .color-more-trigger {
flex: 1;
width: auto;
}
.color-pipette-trigger {
display: inline-flex;
align-items: center;
justify-content: center;
width: 34px;
padding: 0;
border: 1px solid var(--color-border);
border-radius: calc(var(--radius) - 2px);
background: transparent;
color: var(--color-text);
cursor: pointer;
min-width: unset;
height: auto;
transition: background 0.1s;
}
.color-pipette-trigger:hover {
background: var(--color-btn-hover);
}
.color-window {
position: absolute;
top: calc(100% + 3px);
left: 0;
width: 210px;
padding: 8px;
display: flex;
flex-direction: column;
gap: 8px;
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;
}
.color-window-body {
display: flex;
gap: 8px;
}
.sv-square {
position: relative;
width: 160px;
height: 160px;
cursor: crosshair;
border: 1px solid var(--color-border);
background:
linear-gradient(to top, #000, transparent),
linear-gradient(to right, #fff, hsl(var(--hue, 0), 100%, 50%));
touch-action: none;
}
.sv-thumb {
position: absolute;
width: 12px;
height: 12px;
border-radius: 50%;
border: 2px solid white;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.6);
transform: translate(-50%, -50%);
pointer-events: none;
}
.hue-slider {
position: relative;
width: 18px;
height: 160px;
cursor: pointer;
border: 1px solid var(--color-border);
background: linear-gradient(
to bottom,
#f00 0%, #ff0 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%
);
touch-action: none;
}
.hue-thumb {
position: absolute;
left: -3px;
width: 22px;
height: 6px;
border: 1px solid white;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.6);
transform: translateY(-50%);
pointer-events: none;
}
.color-window-readout {
display: flex;
align-items: center;
gap: 6px;
}
.color-window-swatch {
display: block;
width: 28px;
height: 24px;
border: 1px solid var(--color-border);
border-radius: 3px;
flex-shrink: 0;
}
.color-window-hex {
flex: 1;
min-width: 0;
height: 24px;
padding: 0 0.4rem;
border: 1px solid var(--color-border);
border-radius: 3px;
background: var(--color-surface);
color: var(--color-text);
font-size: 0.8rem;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
outline: none;
}
.color-window-hex:focus {
border-color: var(--color-primary);
}
.color-more-actions {
display: flex;
gap: 4px;
justify-content: flex-end;
}
.color-confirm,
.color-cancel {
padding: 0.25rem 0.6rem;
border-radius: calc(var(--radius) - 2px);
font-size: 0.75rem;
font-family: var(--font-sans);
cursor: pointer;
min-width: unset;
height: auto;
transition: background 0.1s, border-color 0.15s;
}
.color-cancel {
border: 1px solid var(--color-border);
background: transparent;
color: var(--color-text);
}
.color-cancel:hover {
background: var(--color-btn-hover);
}
.color-confirm {
border: 1px solid var(--color-primary);
background: var(--color-primary);
color: white;
}
.color-confirm:hover {
filter: brightness(1.1);
}
</style>
+6
View File
@@ -4,6 +4,7 @@ import Text from '@tiptap/extension-text';
import Bold from '@tiptap/extension-bold';
import Italic from '@tiptap/extension-italic';
import Underline from '@tiptap/extension-underline';
import Highlight from '@tiptap/extension-highlight';
import { TextStyle, FontFamily, FontSize } from '@tiptap/extension-text-style';
import { FontWeight } from './fontWeight';
import { FontColor } from './fontColor';
@@ -31,6 +32,11 @@ export const extensions = [
FontSize,
FontWeight,
FontColor,
// multicolor stores the chosen color on the `highlight` mark's `color` attr;
// odf-kit exports that natively to fo:background-color (round-trips with
// LibreOffice and Word). See src/lib/export/odt.ts applyRuns for the
// custom-attr-paragraph export path.
Highlight.configure({ multicolor: true }),
LineHeight,
ParagraphSpacing,
Heading.configure({ levels: [1, 2, 3] }),
+36 -1
View File
@@ -280,10 +280,44 @@ function applyRuns(p: ParagraphBuilder, content: TiptapNode[] = []) {
const c = normalizeColor(String(tsm.attrs.color));
if (c) fmt.color = c;
}
// Text highlight (background). odf-kit maps highlightColor → fo:background-color
// natively for normal paragraphs; this covers the custom-attr-paragraph path
// (CUST_P/CUST_H), which bypasses odf-kit's own mark handling.
const hl = marks.find(m => m.type === 'highlight');
if (hl?.attrs?.color) {
const c = normalizeColor(String(hl.attrs.color));
if (c) fmt.highlightColor = c;
}
p.addText(node.text, Object.keys(fmt).length ? fmt : undefined);
}
}
// odf-kit's XML builder serializes a multi-child element's children on separate
// lines, joined by "\n" (core/xml.js). Inside a <text:p>/<text:h> that newline
// is significant character data, so LibreOffice/Word collapse it to a space —
// inserting a spurious space wherever a run boundary falls mid-word (e.g. a
// highlight, bold, or colour that starts inside a word splits the text node, and
// the two runs serialize as "He\n<text:span>llo</text:span>" → "He llo").
//
// We strip the bare "\n" separators inside every paragraph/heading element.
// Only the newline characters are removed; any real space in the run text sits
// adjacent to (not inside) the "\n" and is preserved, so word-boundary spacing
// (e.g. "Hello " + bold "world") still round-trips correctly.
function collapseRunWhitespace(odtBytes: Uint8Array): Uint8Array {
const files = unzipSync(odtBytes);
const contentBytes = files['content.xml'];
if (!contentBytes) return odtBytes;
let content = strFromU8(contentBytes);
content = content.replace(
/<text:(p|h)\b[^>]*>[\s\S]*?<\/text:\1>/g,
(block) => block.replace(/\n/g, ''),
);
files['content.xml'] = strToU8(content);
return rezipOdt(files);
}
export async function exportToOdt(editor: Editor, margins: PageMargins = DEFAULT_MARGINS, orientation: Orientation = 'portrait'): Promise<void> {
const raw = editor.getJSON() as TiptapNode;
const json = injectCustomTypes(raw);
@@ -336,7 +370,8 @@ export async function exportToOdt(editor: Editor, margins: PageMargins = DEFAULT
const listStyles: ListItemStyle[] = [];
collectListItemStyles(raw, listStyles);
const styledLists = applyListItemStyles(odt as Uint8Array, listStyles);
const finalBytes = rewriteStylesXml(styledLists);
const cleaned = collapseRunWhitespace(styledLists);
const finalBytes = rewriteStylesXml(cleaned);
const blob = new Blob([finalBytes as Uint8Array<ArrayBuffer>], { type: 'application/vnd.oasis.opendocument.text' });
const url = URL.createObjectURL(blob);