Markpad icon indicating copy to clipboard operation
Markpad copied to clipboard

Undo deletes pasted/dropped images permanently, and sometimes the wrong one

Open PathGao opened this issue 4 months ago • 1 comments

Describe the bug

Undoing an image insert deletes the copied file from disk. Two things about that are worth deciding on rather than patching, so raising it here before sending a PR.

1. The deletion is permanent

delete_file is fs::remove_file — a plain unlink. It does not go to the macOS Trash or the Windows Recycle Bin; those require the platform APIs to be called explicitly. So a file removed this way cannot be recovered by the user at all.

That matters because the file lives in the user's own notes folder, not in app storage:

~/Documents/notes/
    ├── report.md
    └── img/
        └── logo.png     ← copied here on drop, deleted on undo

2. Undo is the only way of removing an embed that touches the disk

How the embed is removed The file on disk
Select it and press Delete stays
Type over it stays
Delete the whole line stays
Close the tab without saving stays
Save, then delete the line later stays
Ctrl+Z permanently deleted

So it does not actually prevent orphans — five of the six paths leave one — while adding an irreversible operation to the one path users least expect to touch their filesystem. Undo reads as "go back one step in the text" to most people.

3. It can delete a file the document is still using

Because the handler has to infer intent from the buffer, two cases get it wrong:

Wrong tab. The editor keeps one component instance across tab switches, so the managed list mixes every tab's images while the handler only inspects the newest entry.

  1. Open two documents, both in edit mode
  2. Drop an image into tab A
  3. Switch to tab B, type something, press Ctrl+Z
  4. Tab A's image file is deleted; tab A still displays the embed

Edited caption. The check is content.includes(embed) against the whole embed string.

  1. Drop an image — the buffer gets ![alt](img/a.png)
  2. Change the caption to ![logo](img/a.png)
  3. Press Ctrl+Z for anything
  4. img/a.png is deleted while the document still points at it

Expected behavior

Depends on which direction you prefer — that is really the question:

  • A. Stop deleting on undo. Consistent with the other five paths and with how Obsidian and Typora treat copied attachments. Orphan cleanup becomes a separate explicit action ("find images no attachment references" → show a list → confirm → move to trash), which would cover all six paths instead of one.
  • B. Keep the behaviour, move to the trash instead of unlinking. Smallest change; makes a wrong guess recoverable. The inconsistency in the table above remains.
  • C. Keep the behaviour, fix the two mis-targeting cases. Entries carry their owning tab, and the match is on the link target rather than the whole embed string. Fixes the symptoms; the design questions stay open.

I had C ready as #357's follow-up and closed it, because it seemed wrong to harden the guessing without asking whether the guess should exist. Happy to implement whichever you'd rather have — A is the one I'd suggest, with the cleanup command as a follow-up.

Device

  • Reproduced on macOS; the logic is platform-independent, and the deletion is permanent on all three.
  • App version: current master (67a93ee)

PathGao avatar Aug 02 '26 18:08 PathGao

@PathGao option A looks like the best route, thanks for breaking this down

alecdotdev avatar Aug 05 '26 01:08 alecdotdev