comparison src/main.ts @ 35:42ff65e35f4f

More standardized logging.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 14 Aug 2023 10:42:04 -0700
parents 66cada11efb8
children 16d304d586b1
comparison
equal deleted inserted replaced
34:1c8e46c3e941 35:42ff65e35f4f
101 private _viewUninstallers: Record<string, Function> = {}; 101 private _viewUninstallers: Record<string, Function> = {};
102 // Functions to unregister any global callbacks on plugin unload. 102 // Functions to unregister any global callbacks on plugin unload.
103 private _globalUninstallers: Function[] = []; 103 private _globalUninstallers: Function[] = [];
104 104
105 async onload() { 105 async onload() {
106 console.log("Loading RememberFileState plugin"); 106 console.log("RememberFileState: loading plugin");
107 107
108 await this.loadSettings(); 108 await this.loadSettings();
109 109
110 this.data = Object.assign({}, DEFAULT_DATA); 110 this.data = Object.assign({}, DEFAULT_DATA);
111 111
153 const filePath = (leaf.view as MarkdownView).file.path; 153 const filePath = (leaf.view as MarkdownView).file.path;
154 const viewId = this.getUniqueViewId(leaf.view as ViewWithID); 154 const viewId = this.getUniqueViewId(leaf.view as ViewWithID);
155 if (viewId != undefined) { 155 if (viewId != undefined) {
156 var uninstaller = this._viewUninstallers[viewId]; 156 var uninstaller = this._viewUninstallers[viewId];
157 if (uninstaller) { 157 if (uninstaller) {
158 console.debug(`Uninstalling hooks for view ${viewId}`, filePath); 158 console.debug(`RememberedFileState: uninstalling hooks for view ${viewId}`, filePath);
159 uninstaller(leaf.view); 159 uninstaller(leaf.view);
160 ++numViews; 160 ++numViews;
161 } else { 161 } else {
162 console.debug("Found markdown view without an uninstaller!", filePath); 162 console.debug("RememberedFileState: found markdown view without an uninstaller!", filePath);
163 } 163 }
164 // Clear the ID so we don't get confused if the plugin 164 // Clear the ID so we don't get confused if the plugin
165 // is re-enabled later. 165 // is re-enabled later.
166 this.clearUniqueViewId(leaf.view as ViewWithID); 166 this.clearUniqueViewId(leaf.view as ViewWithID);
167 } else { 167 } else {
168 console.debug("Found markdown view without an ID!", filePath); 168 console.debug("RememberedFileState: found markdown view without an ID!", filePath);
169 } 169 }
170 }); 170 });
171 console.debug(`Unregistered ${numViews} view callbacks`); 171 console.debug(`RememberedFileState: unregistered ${numViews} view callbacks`);
172 this._viewUninstallers = {}; 172 this._viewUninstallers = {};
173 this._lastOpenFiles = {}; 173 this._lastOpenFiles = {};
174 174
175 // Run global unhooks. 175 // Run global unhooks.
176 this._globalUninstallers.forEach((cb) => cb()); 176 this._globalUninstallers.forEach((cb) => cb());
189 var viewId = this.getUniqueViewId(view as unknown as ViewWithID, true); 189 var viewId = this.getUniqueViewId(view as unknown as ViewWithID, true);
190 if (viewId in this._viewUninstallers) { 190 if (viewId in this._viewUninstallers) {
191 return; 191 return;
192 } 192 }
193 193
194 console.debug(`Registering callback on view ${viewId}`, filePath); 194 console.debug(`RememberedFileState: registering callback on view ${viewId}`, filePath);
195 const _this = this; 195 const _this = this;
196 var uninstall = around(view, { 196 var uninstall = around(view, {
197 onUnloadFile: function(next) { 197 onUnloadFile: function(next) {
198 return async function (unloaded: TFile) { 198 return async function (unloaded: TFile) {
199 _this.rememberFileState(unloaded, this); 199 _this.rememberFileState(unloaded, this);
208 // will outlive it if it gets deactivated. So let's find it, and 208 // will outlive it if it gets deactivated. So let's find it, and
209 // do nothing if we don't find it. 209 // do nothing if we don't find it.
210 // @ts-ignore 210 // @ts-ignore
211 var plugin: RememberFileStatePlugin = app.plugins.getPlugin("obsidian-remember-file-state"); 211 var plugin: RememberFileStatePlugin = app.plugins.getPlugin("obsidian-remember-file-state");
212 if (plugin) { 212 if (plugin) {
213 console.debug(`Unregistering view ${viewId} callback`, filePath); 213 console.debug(`RememberedFileState: unregistering view ${viewId} callback`, filePath);
214 delete plugin._viewUninstallers[viewId]; 214 delete plugin._viewUninstallers[viewId];
215 delete plugin._lastOpenFiles[viewId]; 215 delete plugin._lastOpenFiles[viewId];
216 uninstall(); 216 uninstall();
217 } else { 217 } else {
218 console.debug( 218 console.debug(
219 "Plugin obsidian-remember-file-state has been unloaded, ignoring unregister"); 219 "RememberedFileState: plugin was unloaded, ignoring unregister");
220 } 220 }
221 }); 221 });
222 } 222 }
223 223
224 private readonly onFileOpen = async ( 224 private readonly onFileOpen = async (
250 isRealFileOpen 250 isRealFileOpen
251 ) { 251 ) {
252 try { 252 try {
253 this.restoreFileState(openedFile, activeView); 253 this.restoreFileState(openedFile, activeView);
254 } catch (err) { 254 } catch (err) {
255 console.error("Couldn't restore file state: ", err); 255 console.error("RememberedFileState: couldn't restore file state: ", err);
256 } 256 }
257 } 257 }
258 else {
259 console.debug("RememberedFileState: not restoring file state because:");
260 if (this._suppressNextFileOpen) {
261 console.debug("...we were told to not do it.");
262 } else if (this.isFileMultiplyOpen(openedFile)) {
263 console.debug("...it's open in multiple panes.");
264 } else if (!isRealFileOpen) {
265 console.debug("...that file was already open in this pane.");
266 } else {
267 console.debug("...unknown reason.");
268 }
269 }
258 } 270 }
259 // else: the file isn't handled by a markdown editor. 271 // else: the file isn't handled by a markdown editor.
272 else {
273 console.debug("RememberedFileState: not restoring anything, it's not a markdown view");
274 }
260 275
261 this._suppressNextFileOpen = false; 276 this._suppressNextFileOpen = false;
262 } 277 }
263 } 278 }
264 279
287 302
288 // If we need to keep the number remembered files under a maximum, 303 // If we need to keep the number remembered files under a maximum,
289 // do it now. 304 // do it now.
290 this.forgetExcessFiles(); 305 this.forgetExcessFiles();
291 } 306 }
292 console.debug("Remember file state for:", file.path); 307 console.debug("RememberedFileState: remembered state for:", file.path, stateData);
293 } 308 }
294 309
295 private readonly restoreFileState = function(file: TFile, view: MarkdownView) { 310 private readonly restoreFileState = function(file: TFile, view: MarkdownView) {
296 const existingFile = this.data.rememberedFiles[file.path]; 311 const existingFile = this.data.rememberedFiles[file.path];
297 if (existingFile) { 312 if (existingFile) {
298 console.debug("Restoring file state for:", file.path); 313 console.debug("RememberedFileState: restoring state for:", file.path, existingFile.stateData);
299 const stateData = existingFile.stateData; 314 const stateData = existingFile.stateData;
300 view.editor.scrollTo(stateData.scrollInfo.left, stateData.scrollInfo.top); 315 view.editor.scrollTo(stateData.scrollInfo.left, stateData.scrollInfo.top);
301 const cm6editor = view.editor as EditorWithCM6; 316 const cm6editor = view.editor as EditorWithCM6;
302 var transaction = cm6editor.cm.state.update({ 317 var transaction = cm6editor.cm.state.update({
303 selection: EditorSelection.fromJSON(stateData.selection)}) 318 selection: EditorSelection.fromJSON(stateData.selection)})