Mercurial > obsidian-remember-file-state
annotate 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 |
rev | line source |
---|---|
0 | 1 import { |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
2 App, |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
3 Editor, |
0 | 4 MarkdownView, |
27
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
5 Modal, |
0 | 6 OpenViewState, |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
7 Plugin, |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
8 TAbstractFile, |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
9 TFile, |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
10 View, |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
11 WorkspaceLeaf |
0 | 12 } from 'obsidian'; |
13 | |
14 import { | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
15 EditorView |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
16 } from '@codemirror/view'; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
17 |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
18 import { |
2 | 19 EditorState, |
0 | 20 EditorSelection |
21 } from '@codemirror/state'; | |
22 | |
23 import { | |
24 around | |
25 } from 'monkey-around'; | |
26 | |
27 import { | |
28 DEFAULT_SETTINGS, | |
29 RememberFileStatePluginSettings, | |
30 RememberFileStatePluginSettingTab | |
31 } from './settings'; | |
32 | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
33 declare var app: App; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
34 |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
35 // Interface for CM6 editor view |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
36 interface EditorWithCM6 extends Editor { |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
37 cm: EditorView |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
38 }; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
39 |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
40 // View with unique ID |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
41 interface ViewWithID extends View { |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
42 __uniqueId: number |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
43 }; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
44 |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
45 // Scroll info interface |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
46 interface ScrollInfo { |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
47 top: number, left: number |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
48 }; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
49 |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
50 interface StateData { |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
51 selection: EditorSelection, |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
52 scrollInfo: ScrollInfo |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
53 }; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
54 |
0 | 55 // Interface for a file state. |
56 interface RememberedFileState { | |
57 path: string; | |
58 lastSavedTime: number; | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
59 stateData: StateData; |
0 | 60 } |
61 | |
62 // Interface for all currently remembered file states. | |
63 interface RememberFileStatePluginData { | |
11
6f7f35af6335
Better type information for the plugin data
Ludovic Chabant <ludovic@chabant.com>
parents:
8
diff
changeset
|
64 rememberedFiles: Record<string, RememberedFileState>; |
0 | 65 } |
66 | |
67 // Default empty list of remembered file states. | |
68 const DEFAULT_DATA: RememberFileStatePluginData = { | |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
69 rememberedFiles: {} |
0 | 70 }; |
71 | |
27
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
72 // Simple warning message. |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
73 class WarningModal extends Modal { |
29
66cada11efb8
Fix typescript warnings
Ludovic Chabant <ludovic@chabant.com>
parents:
28
diff
changeset
|
74 title: string = ""; |
66cada11efb8
Fix typescript warnings
Ludovic Chabant <ludovic@chabant.com>
parents:
28
diff
changeset
|
75 message: string = ""; |
66cada11efb8
Fix typescript warnings
Ludovic Chabant <ludovic@chabant.com>
parents:
28
diff
changeset
|
76 |
27
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
77 constructor(app: App, title: string, message: string) { |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
78 super(app) |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
79 this.title = title; |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
80 this.message = message; |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
81 } |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
82 onOpen() { |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
83 this.contentEl.createEl('h2', {text: this.title}); |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
84 this.contentEl.createEl('p', {text: this.message}); |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
85 } |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
86 }; |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
87 |
0 | 88 export default class RememberFileStatePlugin extends Plugin { |
89 settings: RememberFileStatePluginSettings; | |
90 data: RememberFileStatePluginData; | |
91 | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
92 // Don't restore state on the next file being opened. |
0 | 93 private _suppressNextFileOpen: boolean = false; |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
94 // Next unique ID to identify views without keeping references to them. |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
95 private _nextUniqueViewId: number = 0; |
0 | 96 |
28
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
97 // Remember last open file in each view. |
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
98 private _lastOpenFiles: Record<string, string> = {}; |
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
99 |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
100 // Functions to unregister any monkey-patched view hooks on plugin unload. |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
101 private _viewUninstallers: Record<string, Function> = {}; |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
102 // Functions to unregister any global callbacks on plugin unload. |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
103 private _globalUninstallers: Function[] = []; |
0 | 104 |
105 async onload() { | |
35
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
106 console.log("RememberFileState: loading plugin"); |
0 | 107 |
108 await this.loadSettings(); | |
109 | |
110 this.data = Object.assign({}, DEFAULT_DATA); | |
111 | |
112 this.registerEvent(this.app.workspace.on('file-open', this.onFileOpen)); | |
113 this.registerEvent(this.app.vault.on('rename', this.onFileRename)); | |
114 this.registerEvent(this.app.vault.on('delete', this.onFileDelete)); | |
115 | |
116 this.app.workspace.getLeavesOfType("markdown").forEach( | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
117 (leaf: WorkspaceLeaf) => { this.registerOnUnloadFile(leaf.view as MarkdownView); }); |
0 | 118 |
119 const _this = this; | |
120 var uninstall = around(this.app.workspace, { | |
121 openLinkText: function(next) { | |
122 return async function( | |
123 linktext: string, sourcePath: string, | |
124 newLeaf?: boolean, openViewState?: OpenViewState) { | |
125 // When opening a link, we don't want to restore the | |
126 // scroll position/selection/etc because there's a | |
127 // good chance we want to show the file back at the | |
128 // top, or we're going straight to a specific block. | |
129 _this._suppressNextFileOpen = true; | |
130 return await next.call( | |
131 this, linktext, sourcePath, newLeaf, openViewState); | |
132 }; | |
133 } | |
134 }); | |
135 this._globalUninstallers.push(uninstall); | |
136 | |
137 this.addSettingTab(new RememberFileStatePluginSettingTab(this.app, this)); | |
27
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
138 |
29
66cada11efb8
Fix typescript warnings
Ludovic Chabant <ludovic@chabant.com>
parents:
28
diff
changeset
|
139 if ((this.app.vault as any).getConfig('legacyEditor') !== false) { |
27
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
140 new WarningModal( |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
141 this.app, |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
142 "Legacy Editor Not Supported", |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
143 "The 'Remember File State' plugin works only with the new editor. Please turn off 'Legacy Editor' in the options." |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
144 ).open(); |
3d0ac176118f
Show warning message if using the plugin with the legacy editor
Ludovic Chabant <ludovic@chabant.com>
parents:
23
diff
changeset
|
145 } |
0 | 146 } |
147 | |
148 onunload() { | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
149 // Run view uninstallers on all current views. |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
150 var numViews: number = 0; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
151 this.app.workspace.getLeavesOfType("markdown").forEach( |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
152 (leaf: WorkspaceLeaf) => { |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
153 const filePath = (leaf.view as MarkdownView).file.path; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
154 const viewId = this.getUniqueViewId(leaf.view as ViewWithID); |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
155 if (viewId != undefined) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
156 var uninstaller = this._viewUninstallers[viewId]; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
157 if (uninstaller) { |
35
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
158 console.debug(`RememberedFileState: uninstalling hooks for view ${viewId}`, filePath); |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
159 uninstaller(leaf.view); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
160 ++numViews; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
161 } else { |
35
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
162 console.debug("RememberedFileState: found markdown view without an uninstaller!", filePath); |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
163 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
164 // Clear the ID so we don't get confused if the plugin |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
165 // is re-enabled later. |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
166 this.clearUniqueViewId(leaf.view as ViewWithID); |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
167 } else { |
35
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
168 console.debug("RememberedFileState: found markdown view without an ID!", filePath); |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
169 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
170 }); |
35
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
171 console.debug(`RememberedFileState: unregistered ${numViews} view callbacks`); |
0 | 172 this._viewUninstallers = {}; |
28
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
173 this._lastOpenFiles = {}; |
0 | 174 |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
175 // Run global unhooks. |
0 | 176 this._globalUninstallers.forEach((cb) => cb()); |
177 } | |
178 | |
179 async loadSettings() { | |
180 this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); | |
181 } | |
182 | |
183 async saveSettings() { | |
184 await this.saveData(this.settings); | |
185 } | |
186 | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
187 private readonly registerOnUnloadFile = function(view: MarkdownView) { |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
188 var filePath = view.file.path; |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
189 var viewId = this.getUniqueViewId(view as unknown as ViewWithID, true); |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
190 if (viewId in this._viewUninstallers) { |
0 | 191 return; |
192 } | |
193 | |
35
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
194 console.debug(`RememberedFileState: registering callback on view ${viewId}`, filePath); |
0 | 195 const _this = this; |
196 var uninstall = around(view, { | |
197 onUnloadFile: function(next) { | |
198 return async function (unloaded: TFile) { | |
12
42396b88c64d
Don't unnecessarily capture the view reference in injected callbacks
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
199 _this.rememberFileState(unloaded, this); |
0 | 200 return await next.call(this, unloaded); |
201 }; | |
202 } | |
203 }); | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
204 this._viewUninstallers[viewId] = uninstall; |
0 | 205 |
206 view.register(() => { | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
207 // Don't hold a reference to this plugin here because this callback |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
208 // will outlive it if it gets deactivated. So let's find it, and |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
209 // do nothing if we don't find it. |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
210 // @ts-ignore |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
211 var plugin: RememberFileStatePlugin = app.plugins.getPlugin("obsidian-remember-file-state"); |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
212 if (plugin) { |
35
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
213 console.debug(`RememberedFileState: unregistering view ${viewId} callback`, filePath); |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
214 delete plugin._viewUninstallers[viewId]; |
28
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
215 delete plugin._lastOpenFiles[viewId]; |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
216 uninstall(); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
217 } else { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
218 console.debug( |
35
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
219 "RememberedFileState: plugin was unloaded, ignoring unregister"); |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
220 } |
0 | 221 }); |
222 } | |
223 | |
224 private readonly onFileOpen = async ( | |
225 openedFile: TFile | |
226 ): Promise<void> => { | |
227 // If `openedFile` is null, it's because the last pane was closed | |
228 // and there is now an empty pane. | |
229 if (openedFile) { | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
230 var activeView: MarkdownView = this.app.workspace.getActiveViewOfType(MarkdownView); |
7
b1cb0474bb18
Fix possible crash when an opened file isn't a markdown file
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
231 if (activeView) { |
b1cb0474bb18
Fix possible crash when an opened file isn't a markdown file
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
232 this.registerOnUnloadFile(activeView); |
0 | 233 |
28
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
234 var isRealFileOpen = true; |
29
66cada11efb8
Fix typescript warnings
Ludovic Chabant <ludovic@chabant.com>
parents:
28
diff
changeset
|
235 const viewId = this.getUniqueViewId(activeView as unknown as ViewWithID); |
28
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
236 if (viewId != undefined) { |
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
237 const lastOpenFileInView = this._lastOpenFiles[viewId]; |
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
238 isRealFileOpen = (lastOpenFileInView != openedFile.path); |
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
239 this._lastOpenFiles[viewId] = openedFile.path; |
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
240 } |
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
241 |
22
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
242 // Don't restore the file state if: |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
243 // - We are suppressing it explicitly (such as if the file was |
28
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
244 // opened via clicking a hyperlink) |
22
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
245 // - The file is already currently open in another pane |
28
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
246 // - The file was already opened in this pane, and we're just |
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
247 // returning to it. |
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
248 if (!this._suppressNextFileOpen && |
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
249 !this.isFileMultiplyOpen(openedFile) && |
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
250 isRealFileOpen |
fbaf7c7126be
Don't restore file state if we're just switching to another pane
Ludovic Chabant <ludovic@chabant.com>
parents:
27
diff
changeset
|
251 ) { |
23
ba74a7d3709c
Add try/catch around restoring file state
Ludovic Chabant <ludovic@chabant.com>
parents:
22
diff
changeset
|
252 try { |
ba74a7d3709c
Add try/catch around restoring file state
Ludovic Chabant <ludovic@chabant.com>
parents:
22
diff
changeset
|
253 this.restoreFileState(openedFile, activeView); |
ba74a7d3709c
Add try/catch around restoring file state
Ludovic Chabant <ludovic@chabant.com>
parents:
22
diff
changeset
|
254 } catch (err) { |
35
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
255 console.error("RememberedFileState: couldn't restore file state: ", err); |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
256 } |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
257 } |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
258 else { |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
259 console.debug("RememberedFileState: not restoring file state because:"); |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
260 if (this._suppressNextFileOpen) { |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
261 console.debug("...we were told to not do it."); |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
262 } else if (this.isFileMultiplyOpen(openedFile)) { |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
263 console.debug("...it's open in multiple panes."); |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
264 } else if (!isRealFileOpen) { |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
265 console.debug("...that file was already open in this pane."); |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
266 } else { |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
267 console.debug("...unknown reason."); |
23
ba74a7d3709c
Add try/catch around restoring file state
Ludovic Chabant <ludovic@chabant.com>
parents:
22
diff
changeset
|
268 } |
7
b1cb0474bb18
Fix possible crash when an opened file isn't a markdown file
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
269 } |
0 | 270 } |
7
b1cb0474bb18
Fix possible crash when an opened file isn't a markdown file
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
271 // else: the file isn't handled by a markdown editor. |
35
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
272 else { |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
273 console.debug("RememberedFileState: not restoring anything, it's not a markdown view"); |
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
274 } |
7
b1cb0474bb18
Fix possible crash when an opened file isn't a markdown file
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
275 |
b1cb0474bb18
Fix possible crash when an opened file isn't a markdown file
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
276 this._suppressNextFileOpen = false; |
0 | 277 } |
278 } | |
279 | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
280 private readonly rememberFileState = async (file: TFile, view: MarkdownView): Promise<void> => { |
37
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
281 // Save scrolling position (Obsidian API only gives vertical position). |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
282 const scrollInfo = {top: view.currentMode.getScroll(), left: 0}; |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
283 |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
284 // Save current selection. |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
285 // If state selection is undefined, we have a legacy editor. Just ignore that part. |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
286 const cm6editor = view.editor as EditorWithCM6; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
287 const stateSelection: EditorSelection = cm6editor.cm.state.selection; |
37
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
288 const stateSelectionJSON = (stateSelection !== undefined) ? stateSelection.toJSON() : ""; |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
289 |
0 | 290 const stateData = {'scrollInfo': scrollInfo, 'selection': stateSelectionJSON}; |
291 | |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
292 var existingFile = this.data.rememberedFiles[file.path]; |
0 | 293 if (existingFile) { |
294 existingFile.lastSavedTime = Date.now(); | |
295 existingFile.stateData = stateData; | |
296 } else { | |
297 let newFileState = { | |
298 path: file.path, | |
299 lastSavedTime: Date.now(), | |
300 stateData: stateData | |
301 }; | |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
302 this.data.rememberedFiles[file.path] = newFileState; |
0 | 303 |
36 | 304 // If we need to keep the number of remembered files under a maximum, |
0 | 305 // do it now. |
306 this.forgetExcessFiles(); | |
307 } | |
35
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
308 console.debug("RememberedFileState: remembered state for:", file.path, stateData); |
0 | 309 } |
310 | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
311 private readonly restoreFileState = function(file: TFile, view: MarkdownView) { |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
312 const existingFile = this.data.rememberedFiles[file.path]; |
0 | 313 if (existingFile) { |
35
42ff65e35f4f
More standardized logging.
Ludovic Chabant <ludovic@chabant.com>
parents:
29
diff
changeset
|
314 console.debug("RememberedFileState: restoring state for:", file.path, existingFile.stateData); |
0 | 315 const stateData = existingFile.stateData; |
37
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
316 |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
317 // Restore scrolling position (Obsidian API only allows setting vertical position). |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
318 view.currentMode.applyScroll(stateData.scrollInfo.top); |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
319 |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
320 // Restore last known selection, if any. |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
321 if (stateData.selection != "") { |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
322 const cm6editor = view.editor as EditorWithCM6; |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
323 var transaction = cm6editor.cm.state.update({ |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
324 selection: EditorSelection.fromJSON(stateData.selection)}) |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
325 |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
326 cm6editor.cm.dispatch(transaction); |
8be02002ed66
Use Obsidian's view APIs for scrolling state
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
327 } |
0 | 328 } |
329 } | |
22
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
330 |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
331 private readonly isFileMultiplyOpen = function(file: TFile) { |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
332 var numFound: number = 0; |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
333 this.app.workspace.getLeavesOfType("markdown").forEach( |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
334 (leaf: WorkspaceLeaf) => { |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
335 const filePath = (leaf.view as MarkdownView).file.path; |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
336 if (filePath == file.path) { |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
337 ++numFound; |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
338 } |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
339 }); |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
340 return numFound >= 2; |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
341 } |
0 | 342 |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
343 private readonly forgetExcessFiles = function() { |
0 | 344 const keepMax = this.settings.rememberMaxFiles; |
345 if (keepMax <= 0) { | |
346 return; | |
347 } | |
348 | |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
349 // Sort newer files first, older files last. |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
350 var filesData: RememberedFileState[] = Object.values(this.data.rememberedFiles); |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
351 filesData.sort((a, b) => { |
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
352 if (a.lastSavedTime > b.lastSavedTime) return -1; // a before b |
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
353 if (a.lastSavedTime < b.lastSavedTime) return 1; // b before a |
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
354 return 0; |
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
355 }); |
0 | 356 |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
357 // Remove older files past the limit. |
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
358 for (var i = keepMax; i < filesData.length; ++i) { |
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
359 var fileData = filesData[i]; |
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
360 delete this.data.rememberedFiles[fileData.path]; |
0 | 361 } |
362 } | |
363 | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
364 private readonly getUniqueViewId = function(view: ViewWithID, autocreateId: boolean = false) { |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
365 if (view.__uniqueId == undefined) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
366 if (!autocreateId) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
367 return -1; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
368 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
369 view.__uniqueId = (this._nextUniqueViewId++); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
370 return view.__uniqueId; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
371 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
372 return view.__uniqueId; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
373 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
374 |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
375 private readonly clearUniqueViewId = function(view: ViewWithID) { |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
376 delete view["__uniqueId"]; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
377 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
378 |
0 | 379 private readonly onFileRename = async ( |
380 file: TAbstractFile, | |
381 oldPath: string, | |
382 ): Promise<void> => { | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
383 const existingFile: RememberedFileState = this.data.rememberedFiles[oldPath]; |
0 | 384 if (existingFile) { |
385 existingFile.path = file.path; | |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
386 delete this.data.rememberedFiles[oldPath]; |
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
387 this.data.rememberedFiles[file.path] = existingFile; |
0 | 388 } |
389 }; | |
390 | |
391 private readonly onFileDelete = async ( | |
392 file: TAbstractFile, | |
393 ): Promise<void> => { | |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
394 delete this.data.rememberedFiles[file.path]; |
0 | 395 }; |
396 } | |
397 |