Skip to content

refactor(editor): extract pure apply_pending_edits dispatcher (R1) - #156

Merged
nelsonduarte merged 3 commits into
mainfrom
refactor/editor-apply-edits
Aug 8, 2026
Merged

refactor(editor): extract pure apply_pending_edits dispatcher (R1)#156
nelsonduarte merged 3 commits into
mainfrom
refactor/editor-apply-edits

Conversation

@nelsonduarte

Copy link
Copy Markdown
Owner

Sumario

Refactor puro (R1 do plano de modularidade do editor). Sem alteracao de comportamento.

O que muda

Extrai o corpo do loop for e in self._pending de TabEditar._run (app/editor/tab.py) para uma funcao pura em app/editor/apply_edits.py:

apply_pending_edits(doc, pending, *, warn_fn) -> ApplyResult
  • Sem Qt, sem dependencia de ciclo de vida da UI.
  • Cobre os 9 ramos de edicao: redact, text, image, signature, highlight, note, draw, delete_annot, text_edit.
  • Os 9 ramos sao byte-equivalentes ao codigo anterior.

TabEditar._run passa a ser so orquestracao de UI: open/password/encriptacao, escrita atomica, save, reload e dialogos. O subset_fonts continua a ser chamado no mesmo momento e sob o mesmo gate.

Beneficio

Melhora a testabilidade: a logica de dominio (aplicacao de edicoes ao documento) fica testavel sem GUI.

Testes

  • +17 testes headless novos: 14 do dispatcher + 3 de cobertura dos ramos image/highlight.
  • Suite completa: 575 passed / 3 skipped / 0 failed.
  • Aprovado por revisao adversarial.

Notas

  • ZERO alteracao de comportamento.
  • Depende do R2 (ja em main).

nelsonduarte and others added 2 commits August 8, 2026 10:00
…bEditar._run

Move the edit-application loop (redact / text / image / signature /
highlight / note / draw / delete_annot / text_edit branches, plus the
post-loop subset_fonts) out of TabEditar._run into a new pure module
app/editor/apply_edits.py. apply_pending_edits(doc, pending, *, warn_fn)
takes an already-open (and authenticated) fitz.Document, mutates it in
place and does no file I/O and no Qt work, so it is unit-testable
headless. It returns an ApplyResult carrying the text_fit_warnings and
the embedded_font flag.

TabEditar._run keeps only UI orchestration: opening the document,
password/encryption prompts, the non-latin heads-up, the defensive
try/except, the atomic tempfile+os.replace write, encrypted/plain save,
reload and the toast/QMessageBox dialogs. Behaviour is identical; the
order of operations (apply edits -> subset_fonts -> atomic write -> save
-> reload) is unchanged.

Add tests/test_apply_edits.py exercising the dispatcher without a GUI
(text_edit, redact, text, draw, note, delete_annot, the _existing gate
and the warn_fn / ApplyResult warning propagation). Update the three
source-scraping regression tests that pinned the moved loop to tab.py so
they read the new apply_edits.py module.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add direct headless tests for the two previously-uncovered branches of
apply_pending_edits:

* image/signature: parametrised over both type strings (they share one
  branch); writes a real PNG via fitz.Pixmap, applies the edit and asserts
  exactly one raster is embedded after a bytes round-trip.
* highlight: asserts a single Highlight annotation is added over the text
  rect and stamped with the requested stroke colour.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Comment thread tests/test_pdfapps.py Fixed
Wrap the source-inspection open().read() calls in context managers so
the file handle is always closed. Fixes CodeQL alert #303 (File is not
always closed) at tests/test_pdfapps.py and the same pattern in nine
sibling audit-regression tests. No test logic or assertions changed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 8, 2026

Copy link
Copy Markdown

Deploying pdfapps with  Cloudflare Pages  Cloudflare Pages

Latest commit: a1678e8
Status: ✅  Deploy successful!
Preview URL: https://ca054e52.pdfapps.pages.dev
Branch Preview URL: https://refactor-editor-apply-edits.pdfapps.pages.dev

View logs

@nelsonduarte
nelsonduarte merged commit c4854b6 into main Aug 8, 2026
4 checks passed
@nelsonduarte
nelsonduarte deleted the refactor/editor-apply-edits branch August 8, 2026 09:35
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.

2 participants