55
66import { AppState , Platform , ToastAndroid } from 'react-native' ;
77import { ClipboardContent } from '../../types/clipboard' ;
8- import { SyncDirection , SyncResult } from '../../types/sync' ;
98import type { AppConfig } from '../../types' ;
109import { clipboardSyncState } from './SyncState' ;
1110import { configService } from '../ConfigService' ;
1211import { remoteClipboardMonitor } from './RemoteClipboardMonitor' ;
12+ import { uploadLocalClipboard } from './ClipboardSyncActions' ;
1313import { SyncManager } from './SyncManager' ;
1414import { historyService } from '../history/HistoryService' ;
1515import { calculateTextHash } from '../../utils/hash' ;
@@ -20,7 +20,6 @@ class ClipboardChangedHandler {
2020
2121 private lastRemoteProfileHash : string | null = null ;
2222 private lastLocalProfileHash : string | null = null ;
23- private isAutoSyncing = false ;
2423
2524 private constructor ( ) { }
2625
@@ -123,7 +122,7 @@ class ClipboardChangedHandler {
123122 const localMatchesRemote = remoteHash === this . lastLocalProfileHash ;
124123 const activeServer = await configService . getActiveServer ( ) ;
125124
126- if ( localMatchesRemote || ! activeServer || this . isAutoSyncing ) {
125+ if ( localMatchesRemote || ! activeServer ) {
127126 return ;
128127 }
129128
@@ -132,7 +131,6 @@ class ClipboardChangedHandler {
132131 return ;
133132 }
134133
135- this . isAutoSyncing = true ;
136134 try {
137135 const result = await this . copyToLocalClipboard ( content ) ;
138136 if ( result . success && Platform . OS === 'android' ) {
@@ -144,8 +142,6 @@ class ClipboardChangedHandler {
144142 }
145143 } catch ( error ) {
146144 console . error ( '[ClipboardChangedHandler] Auto-copy failed:' , error ) ;
147- } finally {
148- this . isAutoSyncing = false ;
149145 }
150146 }
151147
@@ -191,28 +187,21 @@ class ClipboardChangedHandler {
191187 if ( currentHash === this . lastLocalProfileHash ) return ;
192188 this . lastLocalProfileHash = currentHash ;
193189
194- if ( this . isAutoSyncing ) return ;
195- this . isAutoSyncing = true ;
196-
197- SyncManager . getInstance ( )
198- . sync ( SyncDirection . Upload , true )
199- . then ( ( result : SyncResult ) => {
200- if ( result . success && ! result . skipped && Platform . OS === 'android' ) {
201- const preview =
202- content . type === 'Text' && content . text
203- ? content . text . trim ( ) . replace ( / \s + / g, ' ' ) . slice ( 0 , 30 )
204- : content . fileName || content . type ;
205- SyncManager . getInstance ( ) . updateForegroundNotification ( `已上传: ${ preview } ` ) ;
206- if ( config ?. syncToastEnabled !== false ) {
207- ToastAndroid . show ( `已上传\n${ preview } ` , ToastAndroid . SHORT ) ;
208- }
209- remoteClipboardMonitor . refresh ( ) . catch ( ( ) => { } ) ;
190+ try {
191+ const uploaded = await uploadLocalClipboard ( content ) ;
192+ if ( uploaded && Platform . OS === 'android' ) {
193+ const preview = this . getContentPreview ( content ) ;
194+ SyncManager . getInstance ( ) . updateForegroundNotification ( `已上传: ${ preview } ` ) ;
195+ if ( config ?. syncToastEnabled !== false ) {
196+ ToastAndroid . show ( `已上传\n${ preview } ` , ToastAndroid . SHORT ) ;
210197 }
211- } )
212- . catch ( ( e : Error ) => console . error ( '[ClipboardChangedHandler] Auto-upload failed:' , e ) )
213- . finally ( ( ) => {
214- this . isAutoSyncing = false ;
215- } ) ;
198+ remoteClipboardMonitor . refresh ( ) . catch ( ( ) => { } ) ;
199+ }
200+ } catch ( e : unknown ) {
201+ if ( ! ( e instanceof DOMException && e . name === 'AbortError' ) ) {
202+ console . error ( '[ClipboardChangedHandler] Auto-upload failed:' , e ) ;
203+ }
204+ }
216205 }
217206
218207 async downloadRemoteFile (
0 commit comments