diff --git a/src/App.svelte b/src/App.svelte index c508712..7d43642 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -590,14 +590,25 @@ if (!editor) return; try { const name = sourceName?.toLowerCase() ?? ''; - const isDocx = name.endsWith('.docx') || name.endsWith('.dotx'); + let isDocx = name.endsWith('.docx') || name.endsWith('.dotx'); // A template is loaded for its content but never bound as the handle, so the // first Save prompts for a new document instead of overwriting the template. const isTemplate = name.endsWith('.ott') || name.endsWith('.dotx'); // Pre-decode any images in a format the browser can't render (TIFF, …) to PNG. // Lazy: the decoder loads only when such an image is present, else this is a no-op. const converted = await convertUnsupportedImages(bytes); - const result = isDocx ? importDocx(bytes, converted) : importOdt(bytes, converted); + // An extension can be wrong (a renamed or mis-saved file) and both word processors + // go by content, so the other format is tried before the file is called broken — + // a file that is neither reports the error for the extension it carries. + let result; + try { + result = isDocx ? importDocx(bytes, converted) : importOdt(bytes, converted); + } catch (err) { + try { + result = isDocx ? importOdt(bytes, converted) : importDocx(bytes, converted); + isDocx = !isDocx; + } catch { throw err; } + } const hasContent = editor.state.doc.textContent.length > 0 || editor.state.doc.childCount > 1; if (hasContent && !confirm(t().dialogs.confirmReplace)) { diff --git a/src/lib/import/CLAUDE.md b/src/lib/import/CLAUDE.md index 8514396..5618c47 100644 --- a/src/lib/import/CLAUDE.md +++ b/src/lib/import/CLAUDE.md @@ -5,7 +5,7 @@ ## ODF import (`odt.ts`, `styleResolver.ts`) -The **Open .odt** button (`App.svelte`) parses an uploaded file with `importOdt(bytes)` and replaces the document via `setContent` (after a confirm when the current doc is non-empty), also adopting the file's page margins/orientation. It parses `content.xml`/`styles.xml` directly (fflate + `DOMParser`) rather than odf-kit's reader, which flattens cell blocks and drops row heights / list number formats. +The **Open .odt** button (`App.svelte`) parses an uploaded file with `importOdt(bytes)` and replaces the document via `setContent` (after a confirm when the current doc is non-empty), also adopting the file's page margins/orientation. It parses `content.xml`/`styles.xml` directly (fflate + `DOMParser`) rather than odf-kit's reader, which flattens cell blocks and drops row heights / list number formats. Which importer runs is chosen by extension, but a **failed read is retried as the other format** — a renamed or mis-saved file is what both word processors go by content for; a file that is neither reports the error for the extension it carries. - **`StyleResolver`** flattens ODF style indirection: named + automatic styles from both XML files, `style:parent-style-name` chains rooted in `style:default-style`, font-face declarations (`style:font-name` → family), `rel-column-width`/`column-width`, `min-row-height`, list styles, and page geometry. `fo:font-family` and `style:font-name` shadow each other across chain levels (`layerTextProps`). A **percentage `fo:font-size`** (`"130%"` — how LibreOffice defines its heading styles) is resolved against the inherited size while walking the chain (`resolvePercentSize`); without it the file's size is dropped and our default silently replaces it. - **LibreOffice's paragraph properties are not all inherited one by one.** Its two vertical