1- import { Plugin , addIcon , TFile , debounce } from "obsidian" ;
1+ import { Plugin , addIcon , TFile , debounce } from "obsidian" ;
22import { OnlyEverFileManager as Manager } from "./src/FileCollection/OnlyEverFileManager" ;
33import { OnlyEverSettingsTab } from "./src/OnlyEverSettingsTab" ;
4- import { OnlyEverSettings } from "./src/interfaces" ;
5-
4+ import { OnlyEverSettings , WasEditedMap } from "./src/interfaces" ;
65
76const DEFAULT_SETTINGS : OnlyEverSettings = {
87 apiToken : "" ,
@@ -15,9 +14,11 @@ const DEFAULT_SETTINGS: OnlyEverSettings = {
1514export default class OnlyEverPlugin extends Plugin {
1615 settings : OnlyEverSettings ;
1716 oeFileManager : Manager ;
18- activeTab : null | TFile = null ;
19- previousTab : null | TFile = null ;
20- wasEdited : any = { }
17+
18+ previousTab : TFile ;
19+ activeTab : TFile ;
20+ wasEdited : WasEditedMap = { }
21+ timeout = 2500 ;
2122
2223 async onload ( ) {
2324 this . loadIcons ( ) ;
@@ -30,7 +31,7 @@ export default class OnlyEverPlugin extends Plugin {
3031
3132 this . scheduledSync ( ) ;
3233 this . addSettingTab ( new OnlyEverSettingsTab ( this . app , this ) ) ;
33- this . previousTab = this . app . workspace . getActiveFile ( )
34+ this . setPreviousAndActiveTab ( ) ;
3435 }
3536
3637 async loadSettings ( ) {
@@ -46,10 +47,6 @@ export default class OnlyEverPlugin extends Plugin {
4647 this . scanVault ( ) ;
4748 }
4849
49- getSettingsValue ( ) {
50- return this . settings . apiToken ;
51- }
52-
5350 private loadHotKeys ( ) {
5451 this . addCommand ( {
5552 id : "add-obsidian-sync-true-in-frontmatter" ,
@@ -104,74 +101,108 @@ export default class OnlyEverPlugin extends Plugin {
104101 }
105102
106103 /**
107- * Note by @PG-Momik
108- * Obsidian's default 'modify' event is 'throttled' not 'debounced'(As per my observation )
109- * The event delay is 2 to 3 seconds.
110- * So we set debounce interval to time greater than throttle interval.
104+ * Note by: @PG-Momik
105+ * Obsidian's default 'modify' event is 'throttled' not 'debounced' (I Think )
106+ * The event delay is 2 seconds.
107+ * So we set the debounce interval to time greater than the throttle interval.
111108 */
112- debouncedSync = debounce ( ( ) => {
113- this . oeFileManager . fileProcessor . processSingleFile ( this . settings , this . activeTab )
114- } , 3000 , true )
109+ debouncedSync = debounce ( async ( file : TFile ) => {
110+ console . log ( 'debounce sync vitra' ) ;
111+ if ( this . oeFileManager . isSupportedFile ( file ) ) {
112+ console . log ( 'i will sync now' ) ;
113+ await this . oeFileManager . fileProcessor . processSingleFile ( this . settings , file ) ;
114+ this . timeout = 2500 ;
115+ }
116+ } , this . timeout , true )
117+
118+ debouncedSave = debounce ( ( file : TFile ) => {
119+ this . timeout = 0
120+ this . wasEdited [ file . path ] = false ;
121+ this . debouncedSync ( file ) ;
122+ } , 500 , true ) ;
115123
116- /*
117- * Registers event and functionality on event
124+ /**
125+ * Register event and functionality on event
118126 */
127+ private registerAllEvents ( ) : void {
128+
129+ /**
119130 private registerAllEvents() {
120131 /*
121132 * Registers and handles initial Obsidian open event
122133 */
123134 this . registerEvent (
124- // @ts -ignore
125- this . app . workspace . on ( "layout-ready" , ( ) => {
126- this . oeFileManager . fileProcessor . processMarkedFiles ( this . settings ) . then ( ) ;
127- } )
135+ // @ts -ignore ( IDE SHOWING ERROR DESPITE THIS CODE WORKING. )
136+ this . app . workspace . on ( "layout-ready" ,
137+ ( ) => {
138+ this . oeFileManager . fileProcessor . processMarkedFiles ( this . settings ) . then ( ) ;
139+ }
140+ )
128141 ) ;
129142
130143 /*
131144 * Registers and handles active note edit event
132145 */
133146 this . registerEvent (
134- this . app . vault . on ( "modify" , ( ) => {
135- this . debouncedSync . cancel ( )
136- this . debouncedSync ( ) ;
137- } )
147+ this . app . vault . on ( "modify" ,
148+ async ( file ) => {
149+ if ( file instanceof TFile ) {
150+ this . debouncedSync ( file as TFile ) ;
151+ }
152+ }
153+ )
138154 ) ;
139155
140- /*
141- * Registers and handles vault's note rename event
156+ /**
157+ * Registers and handles vault's note rename event.
142158 */
143159 this . registerEvent (
144- this . app . vault . on ( "rename" , ( ) => {
145- this . oeFileManager . onActiveFileSaveAction ( this . settings ) . then ( ) ;
146- } )
160+ this . app . vault . on ( "rename" ,
161+ ( ) => {
162+ this . oeFileManager . onActiveFileSaveAction ( this . settings ) . then ( ) ;
163+ }
164+ )
147165 ) ;
148166
149- /*
150- * Registers and handles note save event
167+ /**
168+ * Registers and handles note save event.
151169 */
152170 const saveCommandDefinition = ( this . app as any ) . commands ?. commands ?. [ "editor:save-file" ] ;
153171 const save = saveCommandDefinition ?. callback ;
154172
155173 if ( typeof save === "function" ) {
156174 saveCommandDefinition . callback = async ( ) => {
157- this . debouncedSync . cancel ( ) ;
158- this . oeFileManager . onActiveFileSaveAction ( this . settings ) . then ( ) ;
175+ this . debouncedSave ( this . activeTab ) ;
159176 } ;
160177 }
161178
179+ /**
180+ * Registers and handles tab switch event.
181+ */
162182 this . registerEvent (
163- this . app . workspace . on ( 'active-leaf-change' , ( ) => {
164- if ( this . previousTab ) {
165- const prev = this . previousTab ;
166- if ( this . wasEdited [ prev . name ] ) {
167- this . debouncedSync . cancel ( )
168- this . oeFileManager . fileProcessor . processSingleFile ( this . settings , this . previousTab )
169-
170- this . previousTab = this . app . workspace . getActiveFile ( )
171- this . wasEdited [ prev . name ] = false
183+ this . app . workspace . on ( 'active-leaf-change' ,
184+ async ( ) => {
185+ if ( this . oeFileManager . isActualTabChanged ( this . previousTab ) ) {
186+ if ( this . oeFileManager . fileWasEdited ( this . wasEdited , this . previousTab ) && this . oeFileManager . isSupportedFile ( this . previousTab ) ) {
187+ this . debouncedSync ( this . previousTab ) ;
188+ }
189+
190+ this . setPreviousAndActiveTab ( ) ;
191+ this . wasEdited [ this . activeTab . path ] = false ;
172192 }
173193 }
174- } )
194+ )
175195 ) ;
176196 }
197+
198+ /**
199+ * Set previousTab and activeTab to current open file.
200+ */
201+ private setPreviousAndActiveTab ( ) : void {
202+ const openFileOnAppOpen = this . app . workspace . getActiveFile ( )
203+
204+ if ( openFileOnAppOpen ) {
205+ this . previousTab = this . activeTab = openFileOnAppOpen
206+ }
207+ }
177208}
0 commit comments