Skip to content

Fix DOCX image import and render images inside table cells#120

Merged
hackerwins merged 2 commits into
mainfrom
fix-docx-image-import-and-table-render
Apr 12, 2026
Merged

Fix DOCX image import and render images inside table cells#120
hackerwins merged 2 commits into
mainfrom
fix-docx-image-import-and-table-render

Conversation

@hackerwins

@hackerwins hackerwins commented Apr 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

Importing a .docx with embedded images hit two separate bugs that together made image-bearing files unusable in the docs editor. ~/Downloads/form2.docx reproduced both on the local dev server — three PNGs nested inside the main "신청 프로젝트 정보" table were invisible after import.

  • HTTP 400 from /images on every embedded image. DocxImporter.uploadImages() handed JSZip's untyped Blob (type '') straight to the uploader. FormData then sent the multipart part as application/octet-stream, which ImageService.upload() rejects against its png/jpeg/gif/webp allowlist. The importer now repackages the bytes with a MIME derived from the .rels target extension before calling the uploader.
  • Images inside table cells never rendered. renderTableContent() unconditionally called fillText(run.text) on every run — including image runs whose run.text is the Object Replacement Character (\uFFFC). The body path in doc-canvas handled this via drawImage; the table path was simply missing the mirror branch. Extracted the shared imageCache / getOrLoadImage into packages/docs/src/view/image-cache.ts so both renderers share a single load cache, and taught renderTableContent to bottom-align image runs and call drawImage, scheduling a repaint via requestRender once the load resolves.

Test plan

  • pnpm --filter @wafflebase/docs test — 491 → 493 (two new regression cases)
  • pnpm verify:fast
  • Manual end-to-end against form2.docx on local dev server:
    • 3 PNGs upload with type="image/png" → HTTP 201
    • Yorkie architecture diagram, CodePair README screenshot, and Wafflebase demo all render inside the correct cell at the correct size/position.

Regression tests

  • packages/docs/test/import/docx-importer.test.ts — asserts the uploader receives a Blob with type === 'image/png' and the correct filename.
  • packages/docs/test/view/table-renderer.test.ts — asserts an image run is never drawn via fillText(ORC) and that drawImage is never called while the image is still loading (entry point for the cache).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Improved DOCX image import so uploaded images carry proper image MIME types.
  • Bug Fixes

    • Table-cell images now render instead of placeholder glyphs.
    • Image caching unified to prevent duplicate loads and inconsistent renderer behavior.
  • Documentation

    • Added lessons and a TODO guide capturing findings and verification steps for DOCX image import and rendering.
  • Tests

    • Added regression tests covering importer MIME handling and table-image rendering behavior.

Importing a .docx with embedded images failed with HTTP 400 from
the backend /images endpoint, and even when the upload is bypassed
the editor silently dropped any image that lives inside a table
cell. form2.docx hit both bugs: three PNGs in a deeply nested row
of the main project table were invisible after import.

- DocxImporter.uploadImages() handed JSZip's untyped Blob (type '')
  straight to the uploader. FormData then sent the multipart part
  as application/octet-stream, which ImageService.upload() rejects
  against its png/jpeg/gif/webp allowlist. The importer now
  repackages the bytes with a MIME derived from the .rels target
  extension before calling the uploader.

- renderTableContent() in the table renderer had no image branch
  and was calling fillText(run.text) on every run, even image
  inlines whose run.text is the Object Replacement Character. The
  body path in doc-canvas handled this correctly; the table path
  was simply missing the mirror drawImage branch. Extract the
  shared imageCache / getOrLoadImage into packages/docs/src/view/
  image-cache.ts so both renderers use a single load cache, and
  teach renderTableContent to bottom-align and drawImage image
  runs, scheduling a repaint via requestRender once the load
  resolves.

Adds regression tests on both paths: the DOCX importer test now
asserts the uploader receives a Blob with a concrete image/* type,
and the table-renderer test asserts an image run is never painted
via fillText(ORC) and that the drawImage entry point is reached.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Apr 11, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 43a39f4f-9cab-4f22-99c7-bba1a9773d39

📥 Commits

Reviewing files that changed from the base of the PR and between d9eb0eb and 2933f37.

📒 Files selected for processing (2)
  • docs/tasks/archive/2026/04/20260412-docx-image-import-render-todo.md
  • packages/docs/test/view/table-renderer.test.ts

📝 Walkthrough

Walkthrough

Updated DOCX image import and Canvas rendering: importer now derives and sets image MIME types for uploads; image caching/loading moved to a shared module; table renderer draws inline images (no ORC glyphs); docs and tests added to record lessons and verify fixes.

Changes

Cohort / File(s) Summary
Documentation - Index & Archive
docs/tasks/README.md, docs/tasks/archive/README.md
Incremented archived task count to 122 and added new 2026-04-12 docx image import/render task entry.
Documentation - Task Files
docs/tasks/archive/2026/04/20260412-docx-image-import-render-lessons.md, docs/tasks/archive/2026/04/20260412-docx-image-import-render-todo.md
Added lessons and TODO records describing MIME repackaging, rendering mismatches, cache extraction, fixes applied, tests, and manual verification steps.
Importer: MIME handling
packages/docs/src/import/docx-importer.ts
Added EXT_TO_IMAGE_MIME mapping; derive extension from .rels targets and repackage JSZip blob bytes into new Blob with appropriate image/* MIME for upload when recognized.
Shared Image Cache
packages/docs/src/view/image-cache.ts
New exported getOrLoadImage(src, onLoad) that caches HTMLImageElements, coordinates simultaneous load requests via per-src pending callback sets, and invokes listeners on load/error.
Canvas integration
packages/docs/src/view/doc-canvas.ts
Removed local image-cache implementation; import getOrLoadImage from shared module and pass optional requestRender callback into renderTableContent to support async rerendering.
Table renderer
packages/docs/src/view/table-renderer.ts
renderTableContent(..., requestRender?) updated to handle inline image runs: compute position/size, use getOrLoadImage, draw with ctx.drawImage (skip ORC text), and trigger requestRender after async loads.
Tests
packages/docs/test/import/docx-importer.test.ts, packages/docs/test/view/table-renderer.test.ts
Added importer test asserting repackaged blob has correct image/* MIME and filename; added jsdom test suite, mocked drawImage, and assertions that image runs avoid placeholder glyphs and do not synchronously call drawImage or requestRender.

Sequence Diagram

sequenceDiagram
    participant Importer as DOCX Importer
    participant Uploader as File Uploader
    participant Renderer as Table Renderer
    participant Cache as Image Cache
    participant Canvas as Canvas Context

    Importer->>Importer: extract image bytes from JSZip (Blob.type empty)
    Importer->>Importer: derive ext from .rels target (e.g., .png)
    Importer->>Importer: repackage bytes into Blob type:image/png
    Importer->>Uploader: upload repackaged image Blob

    Renderer->>Cache: getOrLoadImage(src, onLoad)
    alt cached & loaded
        Cache-->>Renderer: return HTMLImageElement
        Renderer->>Canvas: drawImage(img, x,y,w,h)
    else not cached or pending
        Cache->>Cache: create Image, start load, store pending onLoad
        Cache-->>Renderer: return null
        Note over Cache: when image loads, invoke pending callbacks
        Cache-->>Renderer: onLoad callback triggers
        Renderer->>Canvas: drawImage(loadedImg, x,y,w,h)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 I hopped through blobs and .rels with care,

Repackaged pixels from the zip-laid lair.
I cached each sprite so renders won't fight,
Tables now show pictures instead of a boxy fright.
Hop, hop — the canvas smiles in light! 🎨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the two main fixes: correcting DOCX image import with proper MIME types and enabling image rendering in table cells.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-docx-image-import-and-table-render

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

github-actions Bot commented Apr 11, 2026

Copy link
Copy Markdown
Contributor

Verification: verify:self

Result: ✅ PASS in 113.9s

Lane Status Duration
sheets:build ✅ pass 12.9s
docs:build ✅ pass 7.5s
verify:fast ✅ pass 56.9s
frontend:build ✅ pass 15.6s
verify:frontend:chunks ✅ pass 0.3s
backend:build ✅ pass 4.6s
cli:build ✅ pass 1.7s
verify:entropy ✅ pass 14.4s

Verification: verify:integration

Result: ✅ PASS

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@docs/tasks/archive/2026/04/20260412-docx-image-import-render-todo.md`:
- Around line 11-13: The fenced code block containing the text "Unsupported file
type: application/octet-stream" is missing a language specifier; update that
backtick fence to include a language (e.g., change ``` to ```text) so the block
becomes a labeled code fence and resolves MD040, leaving the inner content
unchanged.

In `@packages/docs/test/view/table-renderer.test.ts`:
- Around line 255-277: The test titled "invokes requestRender once while the
image is still loading" checks drawImage but never asserts on requestRender;
update the test (inside the same it block using makeRecordingCtx(),
makeImageCellTable() and renderTableContent(..., requestRender)) to assert that
requestRender was called once (e.g.,
expect(requestRender).toHaveBeenCalledTimes(1)) so the regression path is
actually covered; keep the existing drawImage assertion as-is.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 28814b46-da3d-4d61-9a4c-29ee966096cf

📥 Commits

Reviewing files that changed from the base of the PR and between 24e0b84 and d9eb0eb.

📒 Files selected for processing (10)
  • docs/tasks/README.md
  • docs/tasks/archive/2026/04/20260412-docx-image-import-render-lessons.md
  • docs/tasks/archive/2026/04/20260412-docx-image-import-render-todo.md
  • docs/tasks/archive/README.md
  • packages/docs/src/import/docx-importer.ts
  • packages/docs/src/view/doc-canvas.ts
  • packages/docs/src/view/image-cache.ts
  • packages/docs/src/view/table-renderer.ts
  • packages/docs/test/import/docx-importer.test.ts
  • packages/docs/test/view/table-renderer.test.ts

Comment thread docs/tasks/archive/2026/04/20260412-docx-image-import-render-todo.md Outdated
Comment thread packages/docs/test/view/table-renderer.test.ts Outdated
@codecov

codecov Bot commented Apr 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 61.42857% with 27 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
packages/docs/src/view/image-cache.ts 56.09% 18 Missing ⚠️
packages/docs/src/view/table-renderer.ts 68.75% 5 Missing ⚠️
packages/docs/src/import/docx-importer.ts 81.81% 1 Missing and 1 partial ⚠️
packages/docs/src/view/doc-canvas.ts 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

Two minor follow-ups on the DOCX image import / table-cell render
fix:

- Rename the 'invokes requestRender once' test to actually match
  what it verifies and add the missing negative assertion on
  requestRender. Under jsdom the image load is asynchronous, so
  during a single renderTableContent call neither drawImage nor
  requestRender should fire — the test now asserts both.

- Add a `text` language tag to the fenced error-message block in
  the archived task doc so markdownlint MD040 passes.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@hackerwins
hackerwins merged commit 83705ac into main Apr 12, 2026
1 check passed
@hackerwins
hackerwins deleted the fix-docx-image-import-and-table-render branch April 12, 2026 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant