diff --git a/package-lock.json b/package-lock.json index 54bbc70..eac2f85 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 9514632..e7468de 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/editor/ColorPicker.svelte b/src/lib/editor/ColorPicker.svelte new file mode 100644 index 0000000..ad001ba --- /dev/null +++ b/src/lib/editor/ColorPicker.svelte @@ -0,0 +1,773 @@ + + +
+
+ + +
+ {#if open && view === 'palette'} +
+ +
+ {#each COLOR_PALETTE as row} +
+ {#each row as c} + + {/each} +
+ {/each} +
+
+ + +
+
+ {/if} + {#if open && view === 'custom'} + + {/if} +
+ + diff --git a/src/lib/editor/ToolbarExpanded.svelte b/src/lib/editor/ToolbarExpanded.svelte index 61d7b6a..a6c7518 100644 --- a/src/lib/editor/ToolbarExpanded.svelte +++ b/src/lib/editor/ToolbarExpanded.svelte @@ -1,6 +1,7 @@ @@ -1009,98 +716,41 @@
-
-
- - -
- {#if colorOpen} -
- -
- {#each COLOR_PALETTE as row} -
- {#each row as c} - - {/each} -
- {/each} -
-
- - -
-
- {/if} - {#if moreColorsOpen} - - {/if} -
- + onColorPickerOpen('font')} + onApply={(c, r) => editor?.chain().focus().setTextSelection(r).setColor(c).run()} + onClear={(r) => editor?.chain().focus().setTextSelection(r).unsetColor().run()} + > + {#snippet icon()} + + {/snippet} + + 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()} + + {/snippet} +
@@ -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); - } diff --git a/src/lib/editor/extensions.ts b/src/lib/editor/extensions.ts index 77098f7..bed0d4b 100644 --- a/src/lib/editor/extensions.ts +++ b/src/lib/editor/extensions.ts @@ -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] }), diff --git a/src/lib/export/odt.ts b/src/lib/export/odt.ts index 53a4ed9..cc50185 100644 --- a/src/lib/export/odt.ts +++ b/src/lib/export/odt.ts @@ -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 / 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\nllo" → "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( + /]*>[\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 { 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], { type: 'application/vnd.oasis.opendocument.text' }); const url = URL.createObjectURL(blob);