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,8 +14,11 @@ const DEFAULT_SETTINGS: OnlyEverSettings = {
1514export default class OnlyEverPlugin extends Plugin {
1615 settings : OnlyEverSettings ;
1716 oeFileManager : Manager ;
18- previousTab : null | TFile = null ;
19- wasEdited : any = { }
17+
18+ previousTab : TFile ;
19+ activeTab : TFile ;
20+ wasEdited : WasEditedMap = { }
21+ timeout = 2500 ;
2022
2123 async onload ( ) {
2224 this . loadIcons ( ) ;
@@ -27,11 +29,9 @@ export default class OnlyEverPlugin extends Plugin {
2729 this . scanVault ( ) ;
2830 this . registerAllEvents ( ) ;
2931
30- await this . oeFileManager . fileProcessor . processMarkedFiles ( this . settings ) ;
31-
3232 this . scheduledSync ( ) ;
3333 this . addSettingTab ( new OnlyEverSettingsTab ( this . app , this ) ) ;
34- this . previousTab = this . app . workspace . getActiveFile ( )
34+ this . setPreviousAndActiveTab ( ) ;
3535 }
3636
3737 async loadSettings ( ) {
@@ -47,10 +47,6 @@ export default class OnlyEverPlugin extends Plugin {
4747 this . scanVault ( ) ;
4848 }
4949
50- getSettingsValue ( ) {
51- return this . settings . apiToken ;
52- }
53-
5450 private loadHotKeys ( ) {
5551 this . addCommand ( {
5652 id : "add-obsidian-sync-true-in-frontmatter" ,
@@ -104,69 +100,108 @@ export default class OnlyEverPlugin extends Plugin {
104100 } , syncIntervalMs ) ;
105101 }
106102
107- /*
108- * Registers event and functionality on event
103+ /**
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.
109108 */
110- private registerAllEvents ( ) {
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 )
111117
112- const debouncedSync = debounce ( ( ) => {
113- this . oeFileManager . fileProcessor . processSingleFile ( this . settings , this . previousTab )
114- } , 3200 , true )
118+ debouncedSave = debounce ( ( file : TFile ) => {
119+ this . timeout = 0
120+ this . wasEdited [ file . path ] = false ;
121+ this . debouncedSync ( file ) ;
122+ } , 500 , true ) ;
115123
116- /*
124+ /**
125+ * Register event and functionality on event
126+ */
127+ private registerAllEvents ( ) : void {
128+
129+ /**
117130 * Registers and handles initial Obsidian open event
118131 */
119132 this . registerEvent (
120133 // @ts -ignore
121- this . app . workspace . on ( "layout-ready" , ( ) => {
122- this . oeFileManager . fileProcessor . processMarkedFiles ( this . settings ) . then ( ) ;
123- } )
134+ // IDE SHOWING ERROR DESPITE THIS CODE WORKING.
135+ this . app . workspace . on ( "layout-ready" ,
136+ ( ) => {
137+ this . oeFileManager . fileProcessor . processMarkedFiles ( this . settings ) . then ( ) ;
138+ }
139+ )
124140 ) ;
125141
126142 /*
127143 * Registers and handles active note edit event
128144 */
129145 this . registerEvent (
130- this . app . vault . on ( "modify" , ( ) => {
131- debouncedSync . cancel ( )
132- debouncedSync ( ) ;
133- } )
146+ this . app . vault . on ( "modify" ,
147+ async ( file ) => {
148+ if ( file instanceof TFile ) {
149+ this . debouncedSync ( file as TFile ) ;
150+ }
151+ }
152+ )
134153 ) ;
135154
136- /*
137- * Registers and handles vault's note rename event
155+ /**
156+ * Registers and handles vault's note rename event.
138157 */
139158 this . registerEvent (
140- this . app . vault . on ( "rename" , ( ) => {
141- this . oeFileManager . onActiveFileSaveAction ( this . settings ) . then ( ) ;
142- } )
159+ this . app . vault . on ( "rename" ,
160+ ( ) => {
161+ this . oeFileManager . onActiveFileSaveAction ( this . settings ) . then ( ) ;
162+ }
163+ )
143164 ) ;
144165
145- /*
146- * Registers and handles note save event
166+ /**
167+ * Registers and handles note save event.
147168 */
148169 const saveCommandDefinition = ( this . app as any ) . commands ?. commands ?. [ "editor:save-file" ] ;
149170 const save = saveCommandDefinition ?. callback ;
150171
151172 if ( typeof save === "function" ) {
152173 saveCommandDefinition . callback = async ( ) => {
153- this . oeFileManager . onActiveFileSaveAction ( this . settings ) . then ( ) ;
174+ this . debouncedSave ( this . activeTab ) ;
154175 } ;
155176 }
156177
178+ /**
179+ * Registers and handles tab switch event.
180+ */
157181 this . registerEvent (
158- this . app . workspace . on ( 'active-leaf-change' , ( ) => {
159- if ( this . previousTab ) {
160- const prev = this . previousTab ;
161- if ( this . wasEdited [ prev . name ] ) {
162- debouncedSync . cancel ( )
163- this . oeFileManager . fileProcessor . processSingleFile ( this . settings , this . previousTab )
164-
165- this . previousTab = this . app . workspace . getActiveFile ( )
166- this . wasEdited [ prev . name ] = false
182+ this . app . workspace . on ( 'active-leaf-change' ,
183+ async ( ) => {
184+ if ( this . oeFileManager . isActualTabChanged ( this . previousTab ) ) {
185+ if ( this . oeFileManager . fileWasEdited ( this . wasEdited , this . previousTab ) && this . oeFileManager . isSupportedFile ( this . previousTab ) ) {
186+ this . debouncedSync ( this . previousTab ) ;
187+ }
188+
189+ this . setPreviousAndActiveTab ( ) ;
190+ this . wasEdited [ this . activeTab . path ] = false ;
167191 }
168192 }
169- } )
193+ )
170194 ) ;
171195 }
196+
197+ /**
198+ * Set previousTab and activeTab to current open file.
199+ */
200+ private setPreviousAndActiveTab ( ) : void {
201+ const openFileOnAppOpen = this . app . workspace . getActiveFile ( )
202+
203+ if ( openFileOnAppOpen ) {
204+ this . previousTab = this . activeTab = openFileOnAppOpen
205+ }
206+ }
172207}
0 commit comments