Skip to content

Edge-Drop is a clipboard shelf on the edge of your screen. Hover to open it. Drag files and images into any app, click to copy text and links. Free and open-source for Windows.

fix(clipboard): restore Clear-all action and make deletion ownership matching sequence-aware

Deepender25bd3e56220b59
diff --git a/electron/clipboard/formats.ts b/electron/clipboard/formats.ts
index 5a99674..fdb1671 100644
--- a/electron/clipboard/formats.ts
+++ b/electron/clipboard/formats.ts
@@ -541,3 +541,33 @@ export function clipboardSignature(): string {
export function isUrlText(s: string): boolean {
return URL_RE.test(s)
}
+
+/**
+ * Pure matcher: does a clipboard signature (as produced by
+ * `clipboardSignature()`) represent exactly this item's content?
+ *
+ * CRITICAL: signatures carry a Win32 `seq:<n>:` prefix whenever
+ * GetClipboardSequenceNumber() > 0 (i.e., virtually always on Windows). The
+ * comparison MUST strip that prefix first — comparing the raw string against
+ * a bare `text:<content>` made every text/link ownership check silently fail,
+ * so deleting an item whose content still sat on the system clipboard never
+ * cleared it (and the content could then be re-captured later, appearing to
+ * "come back").
+ *
+ * Image matching keeps the dimension-prefix heuristic (avoids a full pixel
+ * read); over-clearing when two same-dimension images are involved is the
+ * documented, acceptable trade-off.
+ */
+export function signatureMatchesItem(sig: string, data: ItemData): boolean {
+ const bare = sig.replace(/^seq:\d+:/, '')
+ switch (data.kind) {
+ case 'text':
+ return bare === `text:${data.text}`
+ case 'files':
+ return bare === `files:${data.paths.join('\n')}`
+ case 'image':
+ return bare.startsWith(`image:${data.width}x${data.height}:`)
+ case 'image-collection':
+ return bare.startsWith('image:')
+ }
+}
diff --git a/electron/main/ipc.ts b/electron/main/ipc.ts
index feaa4ad..1989481 100644
--- a/electron/main/ipc.ts
+++ b/electron/main/ipc.ts
@@ -17,7 +17,7 @@ import { registerGlobalHotkey } from './index'
import { getOnboardingWindow } from './onboardingWindow'
import { rebuildTrayMenu } from './tray'
import { startDragOut, resolveDragData, prestageDrag, stageDragFile } from './drag'
-import { clipboardSignature, formatTabularDataForClipboard } from '../clipboard/formats'
+import { clipboardSignature, formatTabularDataForClipboard, signatureMatchesItem } from '../clipboard/formats'
import type { ItemData, MergeResult } from '../../shared/types'
import { quitAndInstallUpdate, checkForUpdatesManual, startUpdateDownload, syncAutoUpdaterState } from './updater'
import { createId } from '../store/ids'
@@ -30,24 +30,17 @@ export { isStoreBuild }
/**
* Returns true if the current system clipboard content matches the given item data.
*
- * Used before delete to decide whether to clear the system clipboard. Clearing
- * is only done when the deleted item IS the thing currently on the clipboard;
- * deleting an old history entry that the user has since replaced must never
- * wipe their current clipboard contents.
+ * Delegates to the pure, unit-tested matcher in formats.ts, which strips the
+ * Win32 sequence-number prefix before comparing — without that strip, text
+ * ownership checks could never match on real Windows sessions.
+ *
+ * Used before delete/clear to decide whether to clear the system clipboard.
+ * Clearing is only done when the deleted item IS the thing currently on the
+ * clipboard; deleting an old history entry that the user has since replaced
+ * must never wipe their current clipboard contents.
*/
function clipboardMatchesItem(data: ItemData): boolean {
- const sig = clipboardSignature()
- if (data.kind === 'text') return sig === `text:${data.text}`
- if (data.kind === 'files') return sig === `files:${data.paths.join('\n')}`
- if (data.kind === 'image') {
- // sig format: "image:<W>x<H>:<hash>" — check the dimension prefix to avoid a full pixel read.
- // If another image with the same dimensions is on the clipboard, we over-clear, which is
- // acceptable (user loses clipboard content they were about to paste from a deleted item anyway).
- return sig.startsWith(`image:${data.width}x${data.height}:`)
- }
- // image-collection: clear if any image is on the clipboard (conservative but safe)
- if (data.kind === 'image-collection') return sig.startsWith('image:')
- return false
+ return signatureMatchesItem(clipboardSignature(), data)
}
/** Fire a transient toast to the renderer (best-effort; renderer may be closed). */
@@ -250,22 +243,14 @@ export function registerIpc(): void {
const item = getStore().get(id)
// Resolve clipboard ownership BEFORE mutating the store: if this exact
// content still sits on the system clipboard it must be cleared first so
- // the staged-temp cleanup that runs inside delete() can never yank a
- // file out from under a live OS clipboard reference.
+ // (a) the staged-temp cleanup inside delete() can never yank a file from
+ // under a live OS clipboard reference, and (b) resyncSignature() below
+ // locks onto an EMPTY clipboard, making an immediate re-copy of the same
+ // content a detectable change instead of an invisible no-op.
if (item && clipboardMatchesItem(item.data)) {
clipboard.clear()
}
getStore().delete(id)
- // If the deleted item is still on the system clipboard, clear the clipboard.
- // This is the fix for the copy→delete→copy cycle bug:
- // Without this, resyncSignature() would lock lastSig to the current
- // clipboard state. When the user immediately re-copies the same image the
- // clipboard never changes, so the watcher never fires and the item stays
- // invisible. Clearing makes the clipboard transition to 'empty', so the
- // next re-copy IS a detectable change.
- if (item && clipboardMatchesItem(item.data)) {
- clipboard.clear()
- }
getWatcher().resyncSignature()
pushState.items()
return getStore().toDto()
@@ -274,28 +259,22 @@ export function registerIpc(): void {
handle('item:delete-batch', (ids) => {
if (!ids || ids.length === 0) return getStore().toDto()
const items = ids.map((id) => getStore().get(id)).filter(Boolean)
- // Clear matching clipboard content before the batch removal triggers
- // staged-temp cleanup (same ownership rule as single delete).
+ // Single ownership pass before removal (same rule as single delete).
if (items.some((item) => item && clipboardMatchesItem(item.data))) {
clipboard.clear()
}
getStore().deleteBatch(ids)
- if (items.some((item) => item && clipboardMatchesItem(item.data))) {
- clipboard.clear()
- }
getWatcher().resyncSignature()
pushState.items()
return getStore().toDto()
})
handle('item:clear', () => {
- // Clear the system clipboard unconditionally: the user wiped their history,
- // so whatever is on the clipboard should not zombie-reappear, and clearing
- // ensures any subsequent re-copy of the same content is detectable.
- // Clear the system clipboard unconditionally and BEFORE the store wipe:
- // the user wiped their history, so whatever is on the clipboard should
- // not zombie-reappear, and every removed item's staged temp files become
- // safe to reap inside clearUnpinned().
+ // Wipe the system clipboard BEFORE the store: nothing the user just
+ // cleared may zombie-reappear, and every removed item's staged temp
+ // files become safe to reap inside clearUnpinned().
+ clipboard.clear()
+ getStore().clearUnpinned()
getWatcher().resyncSignature()
pushState.items()
return getStore().toDto()
diff --git a/tests/signatureMatch.test.ts b/tests/signatureMatch.test.ts
new file mode 100644
index 0000000..6a1c8e8
--- /dev/null
+++ b/tests/signatureMatch.test.ts
@@ -0,0 +1,55 @@
+import { describe, expect, it, vi } from 'vitest'
+
+// Minimal electron mock: signatureMatchesItem is pure string logic and never
+// touches the clipboard itself; formats.ts only needs the module to resolve.
+vi.mock('electron', () => ({
+ clipboard: {
+ availableFormats: vi.fn(() => []),
+ readText: vi.fn(() => ''),
+ readHTML: vi.fn(() => ''),
+ readImage: vi.fn(() => ({ isEmpty: () => true })),
+ readBuffer: vi.fn(() => Buffer.alloc(0))
+ }
+}))
+
+import { signatureMatchesItem } from '../electron/clipboard/formats'
+import type { ItemData } from '../shared/types'
+
+describe('signatureMatchesItem — ownership matching with seq prefixes', () => {
+ it('matches TEXT content even when the Win32 sequence prefix is present', () => {
+ // THE REGRESSION: GetClipboardSequenceNumber() is virtually always > 0 on
+ // Windows, so real signatures carry `seq:<n>:`. The old raw comparison
+ // could therefore NEVER match text — deleted items whose content sat on
+ // the clipboard were never cleared and could reappear later.
+ const data: ItemData = { kind: 'text', text: 'secret token 123', isUrl: false }
+ expect(signatureMatchesItem('seq:4821:text:secret token 123', data)).toBe(true)
+ expect(signatureMatchesItem('text:secret token 123', data)).toBe(true) // no-prefix fallback
+ expect(signatureMatchesItem('seq:4821:text:different', data)).toBe(false)
+ expect(signatureMatchesItem('seq:4821:empty', data)).toBe(false)
+ })
+
+ it('matches URL text items the same as plain text', () => {
+ const data: ItemData = { kind: 'text', text: 'https://example.com/a', isUrl: true }
+ expect(signatureMatchesItem('seq:9:text:https://example.com/a', data)).toBe(true)
+ })
+
+ it('matches FILES by exact path list, ignoring the sequence number', () => {
+ const data: ItemData = { kind: 'files', paths: ['C:\\a.pdf', 'C:\\b.txt'] }
+ expect(signatureMatchesItem('seq:7:files:C:\\a.pdf\nC:\\b.txt', data)).toBe(true)
+ expect(signatureMatchesItem('seq:7:files:C:\\a.pdf', data)).toBe(false)
+ expect(signatureMatchesItem('seq:8:files:C:\\a.pdf\nC:\\b.txt', data)).toBe(true) // new seq, same content
+ })
+
+ it('matches IMAGE by dimension prefix (documented heuristic, no pixel read)', () => {
+ const data: ItemData = { kind: 'image', imageId: 'x', width: 1920, height: 1080, bytes: 10 }
+ expect(signatureMatchesItem('seq:3:image:1920x1080:abc123', data)).toBe(true)
+ expect(signatureMatchesItem('image:1920x1080:abc123', data)).toBe(true)
+ expect(signatureMatchesItem('seq:3:image:800x600:abc123', data)).toBe(false)
+ })
+
+ it('treats any image on the clipboard as owned by an image-collection (conservative)', () => {
+ const data: ItemData = { kind: 'image-collection', images: [] }
+ expect(signatureMatchesItem('seq:3:image:100x100:zzz', data)).toBe(true)
+ expect(signatureMatchesItem('seq:3:text:hello', data)).toBe(false)
+ })
+})