@@ -9,9 +9,7 @@ import { StrUtil } from '../../../utils/StrUtil'
9
9
10
10
const systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard()
11
11
12
- export interface OnPasteboardUpdateListener {
13
- onUpdate(entry: PasteboardEntry): void
14
- }
12
+ export type OnPasteboardUpdateListener = (entry: PasteboardEntry) => void
15
13
16
14
export class PasteboardController {
17
15
private psbDao: PasteboardDao | null = null
@@ -29,12 +27,22 @@ export class PasteboardController {
29
27
this.onUpdateListeners.remove(listener)
30
28
}
31
29
30
+ private itemCount: number = 0
31
+
32
+ getItemCount(): number {
33
+ return this.itemCount
34
+ }
35
+
36
+ private async updateItemCount() {
37
+ this.itemCount = await this.psbDao!.itemCount()
38
+ }
39
+
32
40
public lastEntry: PasteboardEntry | null = null
33
41
34
42
private updateLastEntry(entry: PasteboardEntry): void {
35
43
this.lastEntry = entry
36
44
this.onUpdateListeners.forEach((listener) => {
37
- listener?.onUpdate (entry)
45
+ listener?.(entry)
38
46
})
39
47
}
40
48
@@ -43,20 +51,86 @@ export class PasteboardController {
43
51
PasteboardDatabase.init(context)
44
52
this.psbDao = new PasteboardDao()
45
53
systemPasteboard.on('update', this.onSystemPasteboardUpdate)
54
+ const launch = async () => {
55
+ await this.updateItemCount()
56
+ }
57
+ launch().catch()
46
58
}
47
59
48
60
public onDestroy(): void {
49
61
console.debug('onDestroy')
50
62
systemPasteboard.off('update', this.onSystemPasteboardUpdate)
51
63
}
52
64
65
+ async get(id: number): Promise<PasteboardEntry> {
66
+ return await this.psbDao!.getById(id)
67
+ }
68
+
69
+ async haveUnpinned(): Promise<boolean> {
70
+ return await this.psbDao!.haveUnpinned()
71
+ }
72
+
73
+ async allEntries(): Promise<Array<PasteboardEntry>> {
74
+ return await this.psbDao!.allEntries()
75
+ }
76
+
77
+ async pin(id: number) {
78
+ await this.psbDao!.updatePinStatus(id, true)
79
+ }
80
+
81
+ async unpin(id: number) {
82
+ await this.psbDao!.updatePinStatus(id, false)
83
+ }
84
+
85
+ async updateText(id: number, text: string) {
86
+ if (this.lastEntry) {
87
+ if (id === this.lastEntry.id) {
88
+ const copy = this.lastEntry
89
+ copy.text = text
90
+ this.updateLastEntry(copy)
91
+ }
92
+ }
93
+ await this.psbDao!.updateText(id, text)
94
+ }
95
+
96
+ async delete(id: number) {
97
+ await this.psbDao!.markAsDeleted(id)
98
+ await this.updateItemCount()
99
+ }
100
+
101
+ async deleteAll(skipPinned: boolean = true): Promise<Array<number>> {
102
+ let ids: number[]
103
+ if (skipPinned) {
104
+ ids = await this.psbDao!.findUnpinnedIds()
105
+ } else {
106
+ ids = await this.psbDao!.findAllIds()
107
+ }
108
+ this.psbDao?.markAsDeleted(...ids)
109
+ await this.updateItemCount()
110
+ return ids
111
+ }
112
+
113
+ async undoDelete(...ids: number[]) {
114
+ await this.psbDao!.undoDelete(...ids)
115
+ await this.updateItemCount()
116
+ }
117
+
118
+ async realDelete() {
119
+ await this.psbDao!.realDelete()
120
+ }
121
+
122
+ async nukeTables() {
123
+ await this.psbDao!.clearAllTables()
124
+ await this.updateItemCount()
125
+ }
126
+
53
127
@sql.Transactional()
54
128
private async insertEntry(entry: PasteboardEntry): Promise<PasteboardEntry> {
55
129
const rowId = await this.psbDao!.insert(entry)
56
130
if (!rowId) {
57
131
return entry
58
132
}
59
- const inserted = await this.psbDao!.get (rowId)
133
+ const inserted = await this.psbDao!.getByRowId (rowId)
60
134
return inserted ? inserted : entry
61
135
}
62
136
@@ -73,11 +147,15 @@ export class PasteboardController {
73
147
try {
74
148
const existed = await pasteboardController.psbDao!.find(entry.text, entry.sensitive)
75
149
if (existed) {
150
+ const copy = existed
151
+ copy.timestamp = entry.timestamp
152
+ pasteboardController.updateLastEntry(copy)
76
153
await pasteboardController.psbDao!.updateTime(existed.id, entry.timestamp)
77
154
} else {
78
155
const insertedEntry = await pasteboardController.insertEntry(entry)
79
156
console.debug('get entry text: ' + insertedEntry.text)
80
157
pasteboardController.updateLastEntry(insertedEntry)
158
+ pasteboardController.updateItemCount()
81
159
}
82
160
} catch (e) {
83
161
const error = e as Error
0 commit comments