fix: correct font export

This commit is contained in:
Steffen Becker committed 2026-05-26 20:24:02 +02:00
1 parent 1b4bdd2f01
commit de45c009aa
2 files changed
+35

No files matched your search

+33
View File
@@ -1,10 +1,14 @@
<script lang="ts">
import { onMount, onDestroy } from 'svelte';
import { Editor } from '@tiptap/core';
import { Slice, Fragment } from 'prosemirror-model';
import type { Node as PmNode, MarkType } from 'prosemirror-model';
import { extensions } from './extensions';
import { saveDocument, loadDocument } from '../storage/autosave';
import '../../styles/editor.css';
const DEFAULT_EDITOR_FONT = 'Georgia'; // must match ToolbarExpanded.svelte
let { editor = $bindable(), tick = $bindable(0), currentPage = $bindable(1), numPages = $bindable(1), zoom = 100 }: {
editor: Editor | null; tick: number; currentPage: number; numPages: number; zoom: number;
} = $props();
@@ -47,6 +51,25 @@
updateCurrentPage();
}
function applyFontToFragment(frag: Fragment, textStyleType: MarkType, font: string): Fragment {
const nodes: PmNode[] = [];
frag.forEach((node: PmNode) => {
if (node.isText) {
const existingTS = node.marks.find(m => m.type === textStyleType);
if (existingTS?.attrs.fontFamily) {
nodes.push(node);
} else {
const newAttrs = { ...(existingTS?.attrs ?? {}), fontFamily: font };
const otherMarks = node.marks.filter(m => m.type !== textStyleType);
nodes.push(node.mark([...otherMarks, textStyleType.create(newAttrs)]));
}
} else {
nodes.push(node.copy(applyFontToFragment(node.content, textStyleType, font)));
}
});
return Fragment.fromArray(nodes);
}
onMount(() => {
const saved = loadDocument();
@@ -54,6 +77,16 @@
element,
extensions,
content: saved || undefined,
editorProps: {
transformPasted(slice, view) {
const textStyleType = view.state.schema.marks.textStyle;
if (!textStyleType) return slice;
const cursorMarks = view.state.storedMarks ?? view.state.selection.$head.marks();
const cursorFont = cursorMarks.find(m => m.type === textStyleType)?.attrs.fontFamily as string | undefined;
const font = cursorFont ?? DEFAULT_EDITOR_FONT;
return new Slice(applyFontToFragment(slice.content, textStyleType, font), slice.openStart, slice.openEnd);
},
},
onTransaction: () => {
tick++;
},
+2
View File
@@ -8,6 +8,8 @@ import { tiptapToOdt, type TiptapNode, type TextFormatting, type OdtDocument, ty
const LH_P = '__lh_p__';
const LH_H = '__lh_h__';
const DEFAULT_FONT = 'Georgia'; // must match DEFAULT_EDITOR_FONT in ToolbarExpanded.svelte
function injectLineHeightTypes(node: TiptapNode, inList = false): TiptapNode {
// Don't rename paragraphs inside list items — tiptapToOdt walks them by
// type === "paragraph" to build list content; renaming breaks that.