comparison src/main.ts @ 37:8be02002ed66

Use Obsidian's view APIs for scrolling state Obsidian does some asynchronous formatting upon opening a file which can affect the vertical positioning of things, especially when there are tables for instance. Setting the scrolling position via the CM6 APIs gets invalidated by this, so we're using the Obsidian view API instead even if it only allows getting/setting vertical scrolling (not horizontal).
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 14 Aug 2023 10:44:55 -0700
parents 16d304d586b1
children 96e86650043b
comparison
equal deleted inserted replaced
36:16d304d586b1 37:8be02002ed66
276 this._suppressNextFileOpen = false; 276 this._suppressNextFileOpen = false;
277 } 277 }
278 } 278 }
279 279
280 private readonly rememberFileState = async (file: TFile, view: MarkdownView): Promise<void> => { 280 private readonly rememberFileState = async (file: TFile, view: MarkdownView): Promise<void> => {
281 const scrollInfo = view.editor.getScrollInfo(); 281 // Save scrolling position (Obsidian API only gives vertical position).
282 const scrollInfo = {top: view.currentMode.getScroll(), left: 0};
283
284 // Save current selection.
285 // If state selection is undefined, we have a legacy editor. Just ignore that part.
282 const cm6editor = view.editor as EditorWithCM6; 286 const cm6editor = view.editor as EditorWithCM6;
283 const stateSelection: EditorSelection = cm6editor.cm.state.selection; 287 const stateSelection: EditorSelection = cm6editor.cm.state.selection;
284 if (stateSelection == undefined) { 288 const stateSelectionJSON = (stateSelection !== undefined) ? stateSelection.toJSON() : "";
285 // Legacy editor is in use, let's ignore 289
286 return;
287 }
288 const stateSelectionJSON = stateSelection.toJSON();
289 const stateData = {'scrollInfo': scrollInfo, 'selection': stateSelectionJSON}; 290 const stateData = {'scrollInfo': scrollInfo, 'selection': stateSelectionJSON};
290 291
291 var existingFile = this.data.rememberedFiles[file.path]; 292 var existingFile = this.data.rememberedFiles[file.path];
292 if (existingFile) { 293 if (existingFile) {
293 existingFile.lastSavedTime = Date.now(); 294 existingFile.lastSavedTime = Date.now();
310 private readonly restoreFileState = function(file: TFile, view: MarkdownView) { 311 private readonly restoreFileState = function(file: TFile, view: MarkdownView) {
311 const existingFile = this.data.rememberedFiles[file.path]; 312 const existingFile = this.data.rememberedFiles[file.path];
312 if (existingFile) { 313 if (existingFile) {
313 console.debug("RememberedFileState: restoring state for:", file.path, existingFile.stateData); 314 console.debug("RememberedFileState: restoring state for:", file.path, existingFile.stateData);
314 const stateData = existingFile.stateData; 315 const stateData = existingFile.stateData;
315 view.editor.scrollTo(stateData.scrollInfo.left, stateData.scrollInfo.top); 316
316 const cm6editor = view.editor as EditorWithCM6; 317 // Restore scrolling position (Obsidian API only allows setting vertical position).
317 var transaction = cm6editor.cm.state.update({ 318 view.currentMode.applyScroll(stateData.scrollInfo.top);
318 selection: EditorSelection.fromJSON(stateData.selection)}) 319
319 320 // Restore last known selection, if any.
320 cm6editor.cm.dispatch(transaction); 321 if (stateData.selection != "") {
322 const cm6editor = view.editor as EditorWithCM6;
323 var transaction = cm6editor.cm.state.update({
324 selection: EditorSelection.fromJSON(stateData.selection)})
325
326 cm6editor.cm.dispatch(transaction);
327 }
321 } 328 }
322 } 329 }
323 330
324 private readonly isFileMultiplyOpen = function(file: TFile) { 331 private readonly isFileMultiplyOpen = function(file: TFile) {
325 var numFound: number = 0; 332 var numFound: number = 0;