-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclipboard.go
50 lines (43 loc) · 1.58 KB
/
clipboard.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
package washeet
import (
"strings"
)
func (sheet *Sheet) setupClipboardTextArea() {
sheet.clipboardTextArea = sheet.document.Call("createElement", "textarea")
sheet.clipboardTextArea.Call("setAttribute", "style", "position:absolute;z-index:-2;")
sheet.container.Call("appendChild", sheet.clipboardTextArea)
}
func (sheet *Sheet) copySelectionToClipboard() {
sheet.copySelectionToClipboardAsync()
}
func (sheet *Sheet) copySelectionToClipboardAsync() {
sheet.navigator.Get("clipboard").Call("writeText", sheet.getMarkedTextTSV())
}
func (sheet *Sheet) copySelectionToClipboardSync() {
//fmt.Println("copySelectionToClipboard()")
x, y := sheet.window.Get("scrollX").Int(), sheet.window.Get("scrollY").Int()
sheet.clipboardTextArea.Set("value", sheet.getMarkedTextTSV())
sheet.clipboardTextArea.Call("select")
sheet.document.Call("execCommand", "copy")
sheet.canvasStore.foregroundCanvasElement.Call("focus")
// Restore the view as canvas.focus() will make the
// document scroll to begining of the canvas
sheet.window.Call("scrollTo", x, y)
}
func (sheet *Sheet) getMarkedTextTSV() string {
//fmt.Println("Copy : ", sheet.mark)
c1, r1, c2, r2 := sheet.mark.c1, sheet.mark.r1, sheet.mark.c2, sheet.mark.r2
found := sheet.dataSource.TrimToNonEmptyRange(&c1, &r1, &c2, &r2)
if !found {
return ""
}
lines := make([]string, r2-r1+1)
words := make([]string, c2-c1+1)
for nRow := r1; nRow <= r2; nRow++ {
for nCol := c1; nCol <= c2; nCol++ {
words[nCol-c1] = sheet.dataSource.GetDisplayString(nCol, nRow)
}
lines[nRow-r1] = strings.Join(words, "\t")
}
return strings.Join(lines, "\n")
}