forked from golang-design/clipboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacooclip.go
52 lines (45 loc) · 1.46 KB
/
cacooclip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2021 The golang.design Initiative Authors, 2023 Gabriele V.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
//
// Original code written by Changkun Ou <changkun.de>
// Modified to handle Cacoo web MIME type by Gabriele V. <[email protected]>
package cacooclip
import (
"errors"
"fmt"
"github.com/vibridi/cacooclip/internal/chromium"
)
const (
CacooWebMIMEType = "cacoo/shape"
)
var (
errUnavailable = errors.New("unavailable or no data from clipboard")
errWriteFailure = errors.New("unavailable or no data was written to the clipboard")
errNoCacooMime = errors.New("no content with Cacoo MIME type in clipboard")
)
// Read reads Cacoo shape data from the clipboard.
// Not thread safe. Concurrent reads on some OS'es including Darwin may panic.
func Read() (string, error) {
b, err := read()
if err != nil {
return "", fmt.Errorf("read clipboard err: %w", err)
}
wcmap, err := chromium.DecodeWebCustomMIMEData(b)
if err != nil {
return "", fmt.Errorf("decode err: %w", err)
}
content, ok := wcmap[CacooWebMIMEType]
if !ok {
return "", errNoCacooMime
}
return content, nil
}
// Write writes Cacoo shape data to the clipboard so that you can CTRL+V directly onto the Cacoo canvas.
func Write(str string) error {
enc, err := chromium.EncodeWebCustomMIMEData(map[string]string{CacooWebMIMEType: str})
if err != nil {
return fmt.Errorf("encode err: %w", err)
}
return write(enc)
}