1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-01-24 21:42:51 -05:00

clipboard: add copyEntry (by id) handler

This commit is contained in:
bbedward
2025-12-11 12:00:47 -05:00
parent 56ff9368be
commit 7c6f0432c8
3 changed files with 60 additions and 25 deletions

View File

@@ -71,11 +71,13 @@ var clipHistoryCmd = &cobra.Command{
var clipGetCmd = &cobra.Command{
Use: "get <id>",
Short: "Get clipboard entry by ID",
Long: "Get full clipboard entry data by ID (requires server)",
Long: "Get full clipboard entry data by ID (requires server). Use --copy to copy it to clipboard.",
Args: cobra.ExactArgs(1),
Run: runClipGet,
}
var clipGetCopy bool
var clipDeleteCmd = &cobra.Command{
Use: "delete <id>",
Short: "Delete clipboard entry",
@@ -153,6 +155,7 @@ func init() {
clipWatchCmd.Flags().BoolVar(&clipJSONOutput, "json", false, "Output as JSON")
clipHistoryCmd.Flags().BoolVar(&clipJSONOutput, "json", false, "Output as JSON")
clipGetCmd.Flags().BoolVar(&clipJSONOutput, "json", false, "Output as JSON")
clipGetCmd.Flags().BoolVarP(&clipGetCopy, "copy", "c", false, "Copy entry to clipboard")
clipSearchCmd.Flags().IntVarP(&clipSearchLimit, "limit", "l", 50, "Max results")
clipSearchCmd.Flags().IntVarP(&clipSearchOffset, "offset", "o", 0, "Result offset")
@@ -349,6 +352,28 @@ func runClipGet(cmd *cobra.Command, args []string) {
log.Fatalf("Invalid ID: %v", err)
}
if clipGetCopy {
req := map[string]any{
"id": 1,
"method": "clipboard.copyEntry",
"params": map[string]any{
"id": id,
},
}
resp, err := sendServerRequest(req)
if err != nil {
log.Fatalf("Failed to copy clipboard entry: %v", err)
}
if resp.Error != "" {
log.Fatalf("Error: %s", resp.Error)
}
fmt.Printf("Copied entry %d to clipboard\n", id)
return
}
req := map[string]any{
"id": 1,
"method": "clipboard.getEntry",
@@ -375,18 +400,18 @@ func runClipGet(cmd *cobra.Command, args []string) {
log.Fatal("Invalid response format")
}
if clipJSONOutput {
switch {
case clipJSONOutput:
output, _ := json.MarshalIndent(entry, "", " ")
fmt.Println(string(output))
return
}
default:
if data, ok := entry["data"].(string); ok {
fmt.Print(data)
} else {
output, _ := json.MarshalIndent(entry, "", " ")
fmt.Println(string(output))
}
}
}
func runClipDelete(cmd *cobra.Command, args []string) {

View File

@@ -23,6 +23,8 @@ func HandleRequest(conn net.Conn, req models.Request, m *Manager) {
handleClearHistory(conn, req, m)
case "clipboard.copy":
handleCopy(conn, req, m)
case "clipboard.copyEntry":
handleCopyEntry(conn, req, m)
case "clipboard.paste":
handlePaste(conn, req, m)
case "clipboard.subscribe":
@@ -103,6 +105,27 @@ func handleCopy(conn net.Conn, req models.Request, m *Manager) {
models.Respond(conn, req.ID, models.SuccessResult{Success: true, Message: "copied to clipboard"})
}
func handleCopyEntry(conn net.Conn, req models.Request, m *Manager) {
id, err := params.Int(req.Params, "id")
if err != nil {
models.RespondError(conn, req.ID, err.Error())
return
}
entry, err := m.GetEntry(uint64(id))
if err != nil {
models.RespondError(conn, req.ID, err.Error())
return
}
if err := m.SetClipboard(entry.Data, entry.MimeType); err != nil {
models.RespondError(conn, req.ID, err.Error())
return
}
models.Respond(conn, req.ID, models.SuccessResult{Success: true, Message: "copied to clipboard"})
}
func handlePaste(conn net.Conn, req models.Request, m *Manager) {
text, err := m.PasteText()
if err != nil {

View File

@@ -95,27 +95,14 @@ DankModal {
}
function copyEntry(entry) {
DMSService.sendRequest("clipboard.getEntry", {
DMSService.sendRequest("clipboard.copyEntry", {
"id": entry.id
}, function (response) {
if (response.error) {
ToastService.showError(I18n.tr("Failed to copy entry"));
return;
}
const fullEntry = response.result;
if (fullEntry.isImage) {
ToastService.showInfo(I18n.tr("Image copied to clipboard"));
} else {
DMSService.sendRequest("clipboard.copy", {
"text": fullEntry.data
}, function (copyResponse) {
if (copyResponse.error) {
ToastService.showError(I18n.tr("Failed to copy"));
return;
}
ToastService.showInfo(I18n.tr("Copied to clipboard"));
});
}
ToastService.showInfo(entry.isImage ? I18n.tr("Image copied to clipboard") : I18n.tr("Copied to clipboard"));
hide();
});
}