mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-01-29 07:52:50 -05:00
clipboard: add copyEntry (by id) handler
This commit is contained in:
@@ -71,11 +71,13 @@ var clipHistoryCmd = &cobra.Command{
|
|||||||
var clipGetCmd = &cobra.Command{
|
var clipGetCmd = &cobra.Command{
|
||||||
Use: "get <id>",
|
Use: "get <id>",
|
||||||
Short: "Get clipboard entry by 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),
|
Args: cobra.ExactArgs(1),
|
||||||
Run: runClipGet,
|
Run: runClipGet,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var clipGetCopy bool
|
||||||
|
|
||||||
var clipDeleteCmd = &cobra.Command{
|
var clipDeleteCmd = &cobra.Command{
|
||||||
Use: "delete <id>",
|
Use: "delete <id>",
|
||||||
Short: "Delete clipboard entry",
|
Short: "Delete clipboard entry",
|
||||||
@@ -153,6 +155,7 @@ func init() {
|
|||||||
clipWatchCmd.Flags().BoolVar(&clipJSONOutput, "json", false, "Output as JSON")
|
clipWatchCmd.Flags().BoolVar(&clipJSONOutput, "json", false, "Output as JSON")
|
||||||
clipHistoryCmd.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().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(&clipSearchLimit, "limit", "l", 50, "Max results")
|
||||||
clipSearchCmd.Flags().IntVarP(&clipSearchOffset, "offset", "o", 0, "Result offset")
|
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)
|
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{
|
req := map[string]any{
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"method": "clipboard.getEntry",
|
"method": "clipboard.getEntry",
|
||||||
@@ -375,17 +400,17 @@ func runClipGet(cmd *cobra.Command, args []string) {
|
|||||||
log.Fatal("Invalid response format")
|
log.Fatal("Invalid response format")
|
||||||
}
|
}
|
||||||
|
|
||||||
if clipJSONOutput {
|
switch {
|
||||||
output, _ := json.MarshalIndent(entry, "", " ")
|
case clipJSONOutput:
|
||||||
fmt.Println(string(output))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if data, ok := entry["data"].(string); ok {
|
|
||||||
fmt.Print(data)
|
|
||||||
} else {
|
|
||||||
output, _ := json.MarshalIndent(entry, "", " ")
|
output, _ := json.MarshalIndent(entry, "", " ")
|
||||||
fmt.Println(string(output))
|
fmt.Println(string(output))
|
||||||
|
default:
|
||||||
|
if data, ok := entry["data"].(string); ok {
|
||||||
|
fmt.Print(data)
|
||||||
|
} else {
|
||||||
|
output, _ := json.MarshalIndent(entry, "", " ")
|
||||||
|
fmt.Println(string(output))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ func HandleRequest(conn net.Conn, req models.Request, m *Manager) {
|
|||||||
handleClearHistory(conn, req, m)
|
handleClearHistory(conn, req, m)
|
||||||
case "clipboard.copy":
|
case "clipboard.copy":
|
||||||
handleCopy(conn, req, m)
|
handleCopy(conn, req, m)
|
||||||
|
case "clipboard.copyEntry":
|
||||||
|
handleCopyEntry(conn, req, m)
|
||||||
case "clipboard.paste":
|
case "clipboard.paste":
|
||||||
handlePaste(conn, req, m)
|
handlePaste(conn, req, m)
|
||||||
case "clipboard.subscribe":
|
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"})
|
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) {
|
func handlePaste(conn net.Conn, req models.Request, m *Manager) {
|
||||||
text, err := m.PasteText()
|
text, err := m.PasteText()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -95,27 +95,14 @@ DankModal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function copyEntry(entry) {
|
function copyEntry(entry) {
|
||||||
DMSService.sendRequest("clipboard.getEntry", {
|
DMSService.sendRequest("clipboard.copyEntry", {
|
||||||
"id": entry.id
|
"id": entry.id
|
||||||
}, function (response) {
|
}, function (response) {
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
ToastService.showError(I18n.tr("Failed to copy entry"));
|
ToastService.showError(I18n.tr("Failed to copy entry"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const fullEntry = response.result;
|
ToastService.showInfo(entry.isImage ? I18n.tr("Image copied to clipboard") : I18n.tr("Copied to clipboard"));
|
||||||
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"));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
hide();
|
hide();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user