fix: adjust format to match libreoffice/word
This commit is contained in:
1 parent
e78f127fb8
commit
d297b0b272
5 files changed
+140
-22
No files matched your search
@@ -38,12 +38,13 @@
|
||||
return mixed ? '' : (a ?? 'left');
|
||||
});
|
||||
|
||||
// Must match the first font in --font-serif in editor.css
|
||||
const DEFAULT_EDITOR_FONT = 'Georgia';
|
||||
// Must match the first font in --font-serif in global.css. Bundled as a
|
||||
// webfont so it is always available and matches the exported .odt's font.
|
||||
const DEFAULT_EDITOR_FONT = 'Liberation Serif';
|
||||
|
||||
// Always-shown fonts — render in the picker even when detection fails or is blocked.
|
||||
const WEB_SAFE_FONTS: readonly string[] = [
|
||||
'Arial', 'Verdana', 'Trebuchet MS', 'Georgia', 'Times New Roman', 'Courier New',
|
||||
'Liberation Serif', 'Arial', 'Verdana', 'Trebuchet MS', 'Georgia', 'Times New Roman', 'Courier New',
|
||||
];
|
||||
const WEB_SAFE_SET = new Set<string>(WEB_SAFE_FONTS);
|
||||
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
import { Extension } from '@tiptap/core';
|
||||
|
||||
// ODF/LibreOffice line spacing is a multiple of the font's NATURAL line height
|
||||
// (Liberation Serif ≈ 1.15× em), not of the font size. CSS unitless line-height
|
||||
// multiplies the font size, so to make the on-screen box match what LibreOffice
|
||||
// renders for the same "Single"/"1.5"/"Double" value, we scale by this ratio.
|
||||
// Keep in sync with the base line-height in editor.css (.paper .tiptap).
|
||||
const LINE_HEIGHT_RATIO = 1.15;
|
||||
|
||||
declare module '@tiptap/core' {
|
||||
interface Commands<ReturnType> {
|
||||
lineHeight: {
|
||||
@@ -25,10 +32,17 @@ export const LineHeight = Extension.create({
|
||||
attributes: {
|
||||
lineHeight: {
|
||||
default: null,
|
||||
parseHTML: (element: HTMLElement) => element.style.lineHeight || null,
|
||||
parseHTML: (element: HTMLElement) =>
|
||||
element.getAttribute('data-line-height') || null,
|
||||
renderHTML: (attributes: Record<string, unknown>) => {
|
||||
if (!attributes.lineHeight) return {};
|
||||
return { style: `line-height: ${attributes.lineHeight}` };
|
||||
const raw = String(attributes.lineHeight);
|
||||
const num = parseFloat(raw);
|
||||
const rendered = isNaN(num) ? raw : `${num * LINE_HEIGHT_RATIO}`;
|
||||
return {
|
||||
'data-line-height': raw,
|
||||
style: `line-height: ${rendered}`,
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
+74
-5
@@ -11,8 +11,30 @@ type AlignValue = 'left' | 'center' | 'right' | 'justify';
|
||||
const CUST_P = '__cust_p__';
|
||||
const CUST_H = '__cust_h__';
|
||||
|
||||
const DEFAULT_FONT = 'Georgia'; // must match DEFAULT_EDITOR_FONT in ToolbarExpanded.svelte
|
||||
const DEFAULT_LINE_HEIGHT = 1; // must match line-height in editor.css (.paper .tiptap)
|
||||
// odf-kit emits this as the document's default font (Standard style). The editor
|
||||
// renders the bundled, metric-identical Liberation Serif on screen.
|
||||
const ODFKIT_DEFAULT_FONT = 'Liberation Serif';
|
||||
// …but we declare Times New Roman in the .odt: it is metric-identical to
|
||||
// Liberation Serif, so LibreOffice (substitutes TNR→Liberation Serif) and Word
|
||||
// (has the real TNR) both render with the same metrics as the editor.
|
||||
const EXPORT_FONT = 'Times New Roman';
|
||||
const DEFAULT_LINE_HEIGHT = 1; // must match line-height multiplier default in ToolbarExpanded.svelte
|
||||
|
||||
// Editor page geometry (see editor.css / pageBreaks.ts): 96px top/bottom and
|
||||
// 80px side padding on a 794px (≈A4) page. Match them so the exported text
|
||||
// column width — and therefore line wrapping and page flow — is identical.
|
||||
const PAGE_MARGIN_V = '2.54cm'; // 96px @96dpi
|
||||
const PAGE_MARGIN_H = '2.12cm'; // 80px @96dpi
|
||||
|
||||
// Heading sizes/margins shown in the editor (editor.css). odf-kit's built-in
|
||||
// Heading_20_N styles use larger sizes (28/24/20pt), so we rewrite them on
|
||||
// export. Margins are the editor's em-based values resolved against each
|
||||
// heading's own font size (top 1.5em, bottom 0.5em), converted to cm.
|
||||
const HEADING_STYLE_OVERRIDES: { name: string; fontSize: string; marginTop: string; marginBottom: string }[] = [
|
||||
{ name: 'Heading_20_1', fontSize: '20pt', marginTop: '1.058cm', marginBottom: '0.353cm' },
|
||||
{ name: 'Heading_20_2', fontSize: '16pt', marginTop: '0.847cm', marginBottom: '0.282cm' },
|
||||
{ name: 'Heading_20_3', fontSize: '14pt', marginTop: '0.741cm', marginBottom: '0.247cm' },
|
||||
];
|
||||
|
||||
function hasCustomAttrs(attrs: TiptapNode['attrs']): boolean {
|
||||
if (!attrs) return false;
|
||||
@@ -103,8 +125,11 @@ function applyListItemAlignments(odtBytes: Uint8Array, aligns: (AlignValue | nul
|
||||
content = content.replace('</office:automatic-styles>', `${newStyles}\n</office:automatic-styles>`);
|
||||
|
||||
files['content.xml'] = strToU8(content);
|
||||
return rezipOdt(files);
|
||||
}
|
||||
|
||||
// Re-zip preserving ODF's mimetype-first uncompressed requirement.
|
||||
// Re-zip an unpacked ODF, preserving the mimetype-first uncompressed requirement.
|
||||
function rezipOdt(files: Record<string, Uint8Array>): Uint8Array {
|
||||
const mimetype = files['mimetype'];
|
||||
const out: Record<string, [Uint8Array, { level: 0 | 6 }]> = {};
|
||||
if (mimetype) out['mimetype'] = [mimetype, { level: 0 }];
|
||||
@@ -115,6 +140,40 @@ function applyListItemAlignments(odtBytes: Uint8Array, aligns: (AlignValue | nul
|
||||
return zipSync(out);
|
||||
}
|
||||
|
||||
// Rewrite styles.xml so the exported document matches the editor's preview:
|
||||
// • default font Liberation Serif → Times New Roman (metric-identical; renders
|
||||
// the same in the editor, LibreOffice, and Word — see EXPORT_FONT).
|
||||
// • Heading_20_1/2/3 sizes & margins → the editor's values (odf-kit's defaults
|
||||
// are larger). See HEADING_STYLE_OVERRIDES.
|
||||
function rewriteStylesXml(odtBytes: Uint8Array): Uint8Array {
|
||||
const files = unzipSync(odtBytes);
|
||||
const stylesBytes = files['styles.xml'];
|
||||
if (!stylesBytes) return odtBytes;
|
||||
|
||||
let styles = strFromU8(stylesBytes);
|
||||
|
||||
// The only occurrences of "Liberation Serif" in styles.xml are the default
|
||||
// font-face declaration and the Standard style's font-name attributes.
|
||||
styles = styles.split(ODFKIT_DEFAULT_FONT).join(EXPORT_FONT);
|
||||
|
||||
// Scope each rewrite to its own <style:style …>…</style:style> block so the
|
||||
// font-size/margin replacements never bleed across heading levels.
|
||||
for (const { name, fontSize, marginTop, marginBottom } of HEADING_STYLE_OVERRIDES) {
|
||||
const re = new RegExp(`<style:style style:name="${name}"[\\s\\S]*?</style:style>`);
|
||||
styles = styles.replace(re, (block) =>
|
||||
block
|
||||
.replace(/fo:font-size="[^"]*"/g, `fo:font-size="${fontSize}"`)
|
||||
.replace(/style:font-size-asian="[^"]*"/g, `style:font-size-asian="${fontSize}"`)
|
||||
.replace(/style:font-size-complex="[^"]*"/g, `style:font-size-complex="${fontSize}"`)
|
||||
.replace(/fo:margin-top="[^"]*"/g, `fo:margin-top="${marginTop}"`)
|
||||
.replace(/fo:margin-bottom="[^"]*"/g, `fo:margin-bottom="${marginBottom}"`),
|
||||
);
|
||||
}
|
||||
|
||||
files['styles.xml'] = strToU8(styles);
|
||||
return rezipOdt(files);
|
||||
}
|
||||
|
||||
// ODF requires fo:color in `#RRGGBB` form. TipTap stores whatever string went
|
||||
// in: hex from the color picker, but `rgb(r, g, b)` after any HTML round-trip
|
||||
// (paste, parseHTML in fontColor.ts). Anything that isn't valid hex is silently
|
||||
@@ -157,7 +216,12 @@ function applyRuns(p: ParagraphBuilder, content: TiptapNode[] = []) {
|
||||
if (marks.some(m => m.type === 'bold')) fmt.bold = true;
|
||||
if (marks.some(m => m.type === 'italic')) fmt.italic = true;
|
||||
if (marks.some(m => m.type === 'underline')) fmt.underline = true;
|
||||
if (tsm?.attrs?.fontFamily) fmt.fontFamily = String(tsm.attrs.fontFamily);
|
||||
if (tsm?.attrs?.fontFamily) {
|
||||
const ff = String(tsm.attrs.fontFamily);
|
||||
// Explicitly choosing the editor default should match the untagged
|
||||
// default, which resolves to EXPORT_FONT via the Standard style.
|
||||
fmt.fontFamily = ff === ODFKIT_DEFAULT_FONT ? EXPORT_FONT : ff;
|
||||
}
|
||||
if (tsm?.attrs?.fontSize) fmt.fontSize = String(tsm.attrs.fontSize);
|
||||
if (tsm?.attrs?.color) {
|
||||
const c = normalizeColor(String(tsm.attrs.color));
|
||||
@@ -172,6 +236,10 @@ export async function exportToOdt(editor: Editor): Promise<void> {
|
||||
const json = injectCustomTypes(raw);
|
||||
|
||||
const odt = await tiptapToOdt(json, {
|
||||
marginTop: PAGE_MARGIN_V,
|
||||
marginBottom: PAGE_MARGIN_V,
|
||||
marginLeft: PAGE_MARGIN_H,
|
||||
marginRight: PAGE_MARGIN_H,
|
||||
unknownNodeHandler(node: TiptapNode, doc: OdtDocument) {
|
||||
const opts: { lineHeight?: number | string; align?: AlignValue } = {};
|
||||
if (node.attrs?.lineHeight != null) {
|
||||
@@ -202,7 +270,8 @@ export async function exportToOdt(editor: Editor): Promise<void> {
|
||||
|
||||
const listAligns: (AlignValue | null)[] = [];
|
||||
collectListItemAligns(raw, listAligns);
|
||||
const finalBytes = applyListItemAlignments(odt as Uint8Array, listAligns);
|
||||
const aligned = applyListItemAlignments(odt as Uint8Array, listAligns);
|
||||
const finalBytes = rewriteStylesXml(aligned);
|
||||
|
||||
const blob = new Blob([finalBytes as Uint8Array<ArrayBuffer>], { type: 'application/vnd.oasis.opendocument.text' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
+11
-11
@@ -23,8 +23,12 @@
|
||||
padding: var(--page-margin-top) 80px var(--page-margin-bottom);
|
||||
outline: none;
|
||||
font-family: var(--font-serif);
|
||||
font-size: 1.05rem;
|
||||
line-height: 1;
|
||||
/* 12pt matches odf-kit's Standard style and the toolbar's declared default. */
|
||||
font-size: 12pt;
|
||||
/* ODF/LibreOffice "single" spacing = the font's natural line height
|
||||
(Liberation Serif ≈ 1.15× em), NOT 1.0× the font size like CSS unitless 1.
|
||||
Keep this in sync with LINE_HEIGHT_RATIO in lineHeight.ts. */
|
||||
line-height: 1.15;
|
||||
color: var(--color-page-text);
|
||||
|
||||
/* Multi-page background: page surfaces separated by editor background */
|
||||
@@ -102,23 +106,19 @@
|
||||
|
||||
.paper .tiptap p {
|
||||
margin-top: 0;
|
||||
margin-bottom: 0.75em;
|
||||
/* matches odf-kit Standard style fo:margin-bottom (0.212cm ≈ 6pt) */
|
||||
margin-bottom: 0.212cm;
|
||||
}
|
||||
|
||||
.paper .tiptap h1,
|
||||
.paper .tiptap h2,
|
||||
.paper .tiptap h3 {
|
||||
font-weight: 700;
|
||||
line-height: 1;
|
||||
line-height: 1.15;
|
||||
margin-top: 1.5em;
|
||||
margin-bottom: 0.5em;
|
||||
}
|
||||
|
||||
.paper .tiptap h1:first-child,
|
||||
.paper .tiptap h2:first-child,
|
||||
.paper .tiptap h3:first-child {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.paper .tiptap h1 {
|
||||
font-size: 20pt;
|
||||
@@ -136,14 +136,14 @@
|
||||
.paper .tiptap ol {
|
||||
padding-left: 1.5rem;
|
||||
margin-top: 0;
|
||||
margin-bottom: 0.75em;
|
||||
margin-bottom: 0.212cm;
|
||||
}
|
||||
|
||||
.paper .tiptap li {
|
||||
margin-top: 0;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
|
||||
.paper .tiptap li p {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
+35
-1
@@ -6,11 +6,45 @@
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
/* Liberation Serif — bundled so the editor renders the EXACT metrics of the
|
||||
font the exported .odt uses. odf-kit emits Liberation Serif as the document
|
||||
default, and LibreOffice ships the identical file; this keeps the on-screen
|
||||
preview pixel-faithful to the export. (SIL Open Font License — see
|
||||
src/assets/fonts/LICENSE-LiberationFonts.txt.) */
|
||||
@font-face {
|
||||
font-family: 'Liberation Serif';
|
||||
font-style: normal;
|
||||
font-weight: 400;
|
||||
font-display: swap;
|
||||
src: url('../assets/fonts/LiberationSerif-Regular.ttf') format('truetype');
|
||||
}
|
||||
@font-face {
|
||||
font-family: 'Liberation Serif';
|
||||
font-style: normal;
|
||||
font-weight: 700;
|
||||
font-display: swap;
|
||||
src: url('../assets/fonts/LiberationSerif-Bold.ttf') format('truetype');
|
||||
}
|
||||
@font-face {
|
||||
font-family: 'Liberation Serif';
|
||||
font-style: italic;
|
||||
font-weight: 400;
|
||||
font-display: swap;
|
||||
src: url('../assets/fonts/LiberationSerif-Italic.ttf') format('truetype');
|
||||
}
|
||||
@font-face {
|
||||
font-family: 'Liberation Serif';
|
||||
font-style: italic;
|
||||
font-weight: 700;
|
||||
font-display: swap;
|
||||
src: url('../assets/fonts/LiberationSerif-BoldItalic.ttf') format('truetype');
|
||||
}
|
||||
|
||||
:root {
|
||||
--radius: 6px;
|
||||
--shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
|
||||
--font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
--font-serif: Georgia, 'Times New Roman', serif;
|
||||
--font-serif: 'Liberation Serif', 'Times New Roman', Georgia, serif;
|
||||
}
|
||||
|
||||
[data-theme="light"] {
|
||||
|
||||
Reference in new issue
Block a user