diff --git a/shell/plugins/clipboard/Clipboard.qml b/shell/plugins/clipboard/Clipboard.qml index 6c3e80ab..bd090afe 100644 --- a/shell/plugins/clipboard/Clipboard.qml +++ b/shell/plugins/clipboard/Clipboard.qml @@ -84,6 +84,23 @@ Item { root.addClipboardEntry(ClipboardHistory.parseEntryJson(line)) } + function removeDisplayIndex(index) { + if (index < 0 || index >= displayModel.count) return + + var row = displayModel.get(index) + root.history = ClipboardHistory.removeEntryAt(root.history, row.index) + root.saveHistory() + + if (displayModel.count <= 1) { + root.selectedIndex = 0 + root.cursorActive = false + } else if (root.selectedIndex >= displayModel.count - 1) { + root.selectedIndex = displayModel.count - 2 + } + + root.rebuildDisplay() + } + function rebuildDisplay() { var rows = ClipboardHistory.displayRows(root.history, root.filterText, 50) @@ -231,6 +248,9 @@ Item { } else if (event.key === Qt.Key_Backspace) { if (root.filterText.length > 0) root.setFilter(root.filterText.slice(0, -1)) event.accepted = true + } else if (event.key === Qt.Key_Delete) { + root.removeDisplayIndex(root.selectedIndex) + event.accepted = true } else if (event.key === Qt.Key_Up) { root.select(-1) event.accepted = true diff --git a/shell/plugins/clipboard/ClipboardHistory.js b/shell/plugins/clipboard/ClipboardHistory.js index 8e5587bb..667c4b5c 100644 --- a/shell/plugins/clipboard/ClipboardHistory.js +++ b/shell/plugins/clipboard/ClipboardHistory.js @@ -66,6 +66,16 @@ function addEntry(history, entry, limit) { return next } +function removeEntryAt(history, index) { + var values = Array.isArray(history) ? history : [] + var target = Number(index) + if (isNaN(target) || target < 0 || target >= values.length) return values.slice() + + var next = values.slice() + next.splice(target, 1) + return next +} + function parseEntryJson(line) { var raw = String(line || "").trim() if (!raw) return null @@ -121,6 +131,7 @@ if (typeof module !== "undefined") { entryKey: entryKey, parseHistory: parseHistory, addEntry: addEntry, + removeEntryAt: removeEntryAt, parseEntryJson: parseEntryJson, searchableText: searchableText, previewText: previewText, diff --git a/test/shell.d/clipboard-test.sh b/test/shell.d/clipboard-test.sh index cf42a2e5..c4426742 100644 --- a/test/shell.d/clipboard-test.sh +++ b/test/shell.d/clipboard-test.sh @@ -47,6 +47,17 @@ assertDeepEqual( 'clipboard addEntry moves duplicate text to front' ) +assertDeepEqual( + clipboard.removeEntryAt(history, 1), + [ + { type: 'text', text: 'old' }, + { type: 'image', path: '/tmp/a.png', mime: 'image/png' } + ], + 'clipboard removeEntryAt removes the requested entry' +) + +assertDeepEqual(clipboard.removeEntryAt(history, 10), history, 'clipboard removeEntryAt ignores invalid indexes') + assertDeepEqual( clipboard.displayRows(history, 'image', 50).map(row => ({ type: row.entryType, preview: row.previewText, mime: row.mime })), [{ type: 'image', preview: 'Image', mime: 'image/png' }],