@@ -9,9 +9,7 @@ import { StrUtil } from '../../../utils/StrUtil'
99
1010const systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard()
1111
12- export interface OnPasteboardUpdateListener {
13- onUpdate(entry: PasteboardEntry): void
14- }
12+ export type OnPasteboardUpdateListener = (entry: PasteboardEntry) => void
1513
1614export class PasteboardController {
1715 private psbDao: PasteboardDao | null = null
@@ -29,12 +27,22 @@ export class PasteboardController {
2927 this.onUpdateListeners.remove(listener)
3028 }
3129
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+
3240 public lastEntry: PasteboardEntry | null = null
3341
3442 private updateLastEntry(entry: PasteboardEntry): void {
3543 this.lastEntry = entry
3644 this.onUpdateListeners.forEach((listener) => {
37- listener?.onUpdate (entry)
45+ listener?.(entry)
3846 })
3947 }
4048
@@ -43,20 +51,86 @@ export class PasteboardController {
4351 PasteboardDatabase.init(context)
4452 this.psbDao = new PasteboardDao()
4553 systemPasteboard.on('update', this.onSystemPasteboardUpdate)
54+ const launch = async () => {
55+ await this.updateItemCount()
56+ }
57+ launch().catch()
4658 }
4759
4860 public onDestroy(): void {
4961 console.debug('onDestroy')
5062 systemPasteboard.off('update', this.onSystemPasteboardUpdate)
5163 }
5264
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+
53127 @sql.Transactional()
54128 private async insertEntry(entry: PasteboardEntry): Promise<PasteboardEntry> {
55129 const rowId = await this.psbDao!.insert(entry)
56130 if (!rowId) {
57131 return entry
58132 }
59- const inserted = await this.psbDao!.get (rowId)
133+ const inserted = await this.psbDao!.getByRowId (rowId)
60134 return inserted ? inserted : entry
61135 }
62136
@@ -73,11 +147,15 @@ export class PasteboardController {
73147 try {
74148 const existed = await pasteboardController.psbDao!.find(entry.text, entry.sensitive)
75149 if (existed) {
150+ const copy = existed
151+ copy.timestamp = entry.timestamp
152+ pasteboardController.updateLastEntry(copy)
76153 await pasteboardController.psbDao!.updateTime(existed.id, entry.timestamp)
77154 } else {
78155 const insertedEntry = await pasteboardController.insertEntry(entry)
79156 console.debug('get entry text: ' + insertedEntry.text)
80157 pasteboardController.updateLastEntry(insertedEntry)
158+ pasteboardController.updateItemCount()
81159 }
82160 } catch (e) {
83161 const error = e as Error
0 commit comments