Mercurial > obsidian-remember-file-state
comparison src/main.ts @ 44:fae202d7b3de
Fix Typescript warnings and errors
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 20 Sep 2023 23:07:48 -0700 |
parents | 7e981d54a055 |
children | 1fe2cd2c603f |
comparison
equal
deleted
inserted
replaced
43:7e981d54a055 | 44:fae202d7b3de |
---|---|
198 // On startup, assign unique IDs to views and register the | 198 // On startup, assign unique IDs to views and register the |
199 // unload callback to remember their state. | 199 // unload callback to remember their state. |
200 this.registerOnUnloadFile(view); | 200 this.registerOnUnloadFile(view); |
201 | 201 |
202 // Also remember which file is opened in which view. | 202 // Also remember which file is opened in which view. |
203 const viewId = this.getUniqueViewId(view as ViewWithID); | 203 const viewId = this.getUniqueViewId(view as unknown as ViewWithID); |
204 if (viewId != undefined) { | 204 if (viewId != undefined) { |
205 this._lastOpenFiles[viewId] = view.file.path; | 205 this._lastOpenFiles[viewId] = view.file.path; |
206 } | 206 } |
207 | 207 |
208 // Restore state for each opened pane on startup. | 208 // Restore state for each opened pane on startup. |
259 // and there is now an empty pane. | 259 // and there is now an empty pane. |
260 if (!openedFile) { | 260 if (!openedFile) { |
261 return; | 261 return; |
262 } | 262 } |
263 | 263 |
264 var shouldSuppressThis: bool = this._suppressNextFileOpen; | 264 var shouldSuppressThis: boolean = this._suppressNextFileOpen; |
265 this._suppressNextFileOpen = false; | 265 this._suppressNextFileOpen = false; |
266 if (shouldSuppressThis) { | 266 if (shouldSuppressThis) { |
267 console.debug("RememberFileState: not restoring file state because of explicit suppression"); | 267 console.debug("RememberFileState: not restoring file state because of explicit suppression"); |
268 return; | 268 return; |
269 } | 269 } |
340 | 340 |
341 private readonly getState = function(view: MarkdownView) { | 341 private readonly getState = function(view: MarkdownView) { |
342 // Save scrolling position (Obsidian API only gives vertical position). | 342 // Save scrolling position (Obsidian API only gives vertical position). |
343 const scrollInfo = {top: view.currentMode.getScroll(), left: 0}; | 343 const scrollInfo = {top: view.currentMode.getScroll(), left: 0}; |
344 | 344 |
345 // Save current selection. | 345 // Save current selection. CodeMirror returns a JSON object (not a |
346 // JSON string!) when we call toJSON. | |
346 // If state selection is undefined, we have a legacy editor. Just ignore that part. | 347 // If state selection is undefined, we have a legacy editor. Just ignore that part. |
347 const cm6editor = view.editor as EditorWithCM6; | 348 const cm6editor = view.editor as EditorWithCM6; |
348 const stateSelection: EditorSelection = cm6editor.cm.state.selection; | 349 const stateSelection: EditorSelection = cm6editor.cm.state.selection; |
349 const stateSelectionJSON = (stateSelection !== undefined) ? stateSelection.toJSON() : ""; | 350 const stateSelectionJSON = (stateSelection !== undefined) ? stateSelection.toJSON() : undefined; |
350 | 351 |
351 const stateData = {'scrollInfo': scrollInfo, 'selection': stateSelectionJSON}; | 352 const stateData = {'scrollInfo': scrollInfo, 'selection': stateSelectionJSON}; |
352 | 353 |
353 return stateData; | 354 return stateData; |
354 } | 355 } |
356 private readonly restoreState = function(stateData: StateData, view: MarkdownView) { | 357 private readonly restoreState = function(stateData: StateData, view: MarkdownView) { |
357 // Restore scrolling position (Obsidian API only allows setting vertical position). | 358 // Restore scrolling position (Obsidian API only allows setting vertical position). |
358 view.currentMode.applyScroll(stateData.scrollInfo.top); | 359 view.currentMode.applyScroll(stateData.scrollInfo.top); |
359 | 360 |
360 // Restore last known selection, if any. | 361 // Restore last known selection, if any. |
361 if (stateData.selection != "") { | 362 if (stateData.selection !== undefined) { |
362 const cm6editor = view.editor as EditorWithCM6; | 363 const cm6editor = view.editor as EditorWithCM6; |
363 var transaction = cm6editor.cm.state.update({ | 364 var transaction = cm6editor.cm.state.update({ |
364 selection: EditorSelection.fromJSON(stateData.selection)}) | 365 selection: EditorSelection.fromJSON(stateData.selection)}) |
365 | 366 |
366 cm6editor.cm.dispatch(transaction); | 367 cm6editor.cm.dispatch(transaction); |
442 | 443 |
443 private readonly onAppQuit = async (tasks: Tasks): Promise<void> => { | 444 private readonly onAppQuit = async (tasks: Tasks): Promise<void> => { |
444 const _this = this; | 445 const _this = this; |
445 tasks.addPromise( | 446 tasks.addPromise( |
446 _this.rememberAllOpenedFileStates() | 447 _this.rememberAllOpenedFileStates() |
447 .then(_this.writeStateDatabase(STATE_DB_PATH))); | 448 .then(() => _this.writeStateDatabase(STATE_DB_PATH))); |
448 } | 449 } |
449 | 450 |
450 private readonly rememberAllOpenedFileStates = async(): Promise<void> => { | 451 private readonly rememberAllOpenedFileStates = async(): Promise<void> => { |
451 this.app.workspace.getLeavesOfType("markdown").forEach( | 452 this.app.workspace.getLeavesOfType("markdown").forEach( |
452 (leaf: WorkspaceLeaf) => { | 453 (leaf: WorkspaceLeaf) => { |