1
1
import clipboardEvent from "clipboard-event" ;
2
2
import * as electron from "electron" ;
3
3
import { storage } from "./storage" ;
4
- import type { ClipItem , ClipItemMeta , ClipItemType } from "./types/types" ;
5
- import { hash } from "./util/hash" ;
4
+ import type { ClipItem , ClipItemImage , ClipItemType } from "./types/types" ;
5
+ import { getNextClipItemId } from "./util/clipItemId" ;
6
+ import { createHash } from "./util/createHash" ;
6
7
import { toMarkdownImageLink } from "./util/transformations" ;
7
8
8
9
// Clipboards containing any of these formats should not be saved
@@ -25,59 +26,48 @@ function read(): ClipItem | null {
25
26
return null ;
26
27
}
27
28
29
+ const created = Date . now ( ) ;
30
+ const id = getNextClipItemId ( created ) ;
28
31
const text = electron . clipboard . readText ( ) || undefined ;
29
32
const rtf = electron . clipboard . readRTF ( ) || undefined ;
30
33
const html = electron . clipboard . readHTML ( ) || undefined ;
31
- const bookmark = ( ( ) => {
32
- const bookmark = electron . clipboard . readBookmark ( ) ;
33
- return bookmark . title || bookmark . url ? bookmark : undefined ;
34
- } ) ( ) ;
35
- const image = ( ( ) => {
36
- const nativeImage = electron . clipboard . readImage ( ) ;
37
- return nativeImage . isEmpty ( ) ? undefined : nativeImage . toDataURL ( ) ;
38
- } ) ( ) ;
39
- const type : ClipItemType = image ? "image" : "text" ;
40
- let id : string | undefined ;
41
- let meta : ClipItemMeta | undefined ;
42
-
43
- if ( image ) {
44
- id = `image_${ hash ( image ) } ` ;
45
-
46
- if ( html ) {
47
- meta = {
48
- src : / < i m g .* ?s r c = (?: " ( .+ ?) " | ' ( .+ ?) ' ) .* ?> / g. exec ( html ) ?. [ 1 ] ,
49
- alt : / < i m g .* ?a l t = (?: " ( .+ ?) " | ' ( .+ ?) ' ) .* ?> / g. exec ( html ) ?. [ 1 ] ,
50
- } ;
51
- }
52
- } else {
53
- const value = text ?? rtf ?? html ;
54
- if ( value ) {
55
- id = `text_${ hash ( value ) } ` ;
56
- }
57
- }
34
+ const image = redImage ( html ) ;
35
+ const bookmark = readBookmark ( ) ;
36
+ const type : ClipItemType = image != null ? "image" : "text" ;
37
+ const hash = createHash ( image ?. data ?? text ?? rtf ?? html ?? "" ) ;
58
38
59
- const item = {
39
+ return {
40
+ id,
41
+ created,
42
+ hash,
60
43
type,
44
+ name : undefined ,
45
+ list : undefined ,
61
46
text,
62
47
rtf,
63
48
html,
64
49
bookmark,
65
- meta,
66
50
image,
67
51
} ;
52
+ }
68
53
69
- if ( id == null ) {
70
- console . error ( "Missing id" , formats , item ) ;
71
- return null ;
54
+ function redImage ( html : string | undefined ) : ClipItemImage | undefined {
55
+ const nativeImage = electron . clipboard . readImage ( ) ;
56
+ if ( nativeImage . isEmpty ( ) ) {
57
+ return undefined ;
72
58
}
73
-
74
59
return {
75
- id ,
76
- created : Date . now ( ) ,
77
- ... item ,
60
+ src : html != null ? / < i m g . * ? s r c = (?: " ( . + ? ) " | ' ( . + ? ) ' ) . * ? > / g . exec ( html ) ?. [ 1 ] : undefined ,
61
+ alt : html != null ? / < i m g . * ? a l t = (?: " ( . + ? ) " | ' ( . + ? ) ' ) . * ? > / g . exec ( html ) ?. [ 1 ] : undefined ,
62
+ data : nativeImage . toDataURL ( ) ,
78
63
} ;
79
64
}
80
65
66
+ function readBookmark ( ) : electron . ReadBookmark | undefined {
67
+ const bookmark = electron . clipboard . readBookmark ( ) ;
68
+ return bookmark . title || bookmark . url ? bookmark : undefined ;
69
+ }
70
+
81
71
function write ( items : ClipItem [ ] ) {
82
72
if ( items . length === 1 ) {
83
73
writeItem ( items [ 0 ] ) ;
@@ -91,12 +81,12 @@ function writeItems(items: ClipItem[]) {
91
81
for ( const item of items ) {
92
82
switch ( item . type ) {
93
83
case "text" :
94
- texts . push ( item . text ?? "[TEXT] ") ;
84
+ texts . push ( item . text ?? item . rtf ?? item . html ?? " ") ;
95
85
break ;
96
86
case "image" : {
97
- const name = item . name ?? item . meta ?. alt ?? "[ IMAGE] " ;
98
- if ( item . meta ?. src ) {
99
- texts . push ( toMarkdownImageLink ( name , item . meta . src ) ) ;
87
+ const name = item . name ?? item . image ?. alt ?? "IMAGE" ;
88
+ if ( item . image ?. src ) {
89
+ texts . push ( toMarkdownImageLink ( name , item . image . src ) ) ;
100
90
} else {
101
91
texts . push ( name ) ;
102
92
}
@@ -114,11 +104,11 @@ function writeItem(item: ClipItem) {
114
104
rtf,
115
105
html,
116
106
bookmark : bookmark ?. title ,
117
- image : image != null ? electron . nativeImage . createFromDataURL ( image ) : undefined ,
107
+ image : image != null ? electron . nativeImage . createFromDataURL ( image . data ) : undefined ,
118
108
} ) ;
119
109
}
120
110
121
- function onChange ( callback : ( item : ClipItem ) => void ) {
111
+ function onChange ( listener : ( item : ClipItem ) => void ) {
122
112
clipboardEvent . startListening ( ) ;
123
113
124
114
clipboardEvent . on ( "change" , ( ) => {
@@ -129,13 +119,12 @@ function onChange(callback: (item: ClipItem) => void) {
129
119
const item = read ( ) ;
130
120
131
121
if ( item != null ) {
132
- callback ( item ) ;
122
+ listener ( item ) ;
133
123
}
134
124
} ) ;
135
125
}
136
126
137
127
export const clipboard = {
138
- read,
139
128
write,
140
129
onChange,
141
130
} ;
0 commit comments