Mercurial > obsidian-remember-file-state
annotate src/main.ts @ 6:114d7e6d2633
Fix various issues around keeping references to editor objects
- Don't keep references to view. Instead, generate unique view IDs and
use that to find the correct uninstaller when needed.
- Don't keep a reference to the plugin itself in registered callbacks
on views because that could keep the plugin alive after it has been
unloaded.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 14 Feb 2022 12:35:45 -0800 |
parents | f3297d90329d |
children | b1cb0474bb18 |
rev | line source |
---|---|
0 | 1 import { |
2 Editor, | |
3 MarkdownView, | |
4 OpenViewState, | |
5 Plugin, | |
6 View | |
7 } from 'obsidian'; | |
8 | |
9 import { | |
2 | 10 EditorState, |
0 | 11 EditorSelection |
12 } from '@codemirror/state'; | |
13 | |
14 import { | |
15 around | |
16 } from 'monkey-around'; | |
17 | |
18 import { | |
19 DEFAULT_SETTINGS, | |
20 RememberFileStatePluginSettings, | |
21 RememberFileStatePluginSettingTab | |
22 } from './settings'; | |
23 | |
24 // Interface for a file state. | |
25 interface RememberedFileState { | |
26 path: string; | |
27 lastSavedTime: number; | |
28 stateData: Object; | |
29 } | |
30 | |
31 // Interface for all currently remembered file states. | |
32 interface RememberFileStatePluginData { | |
33 rememberedFiles: RememberedFileState[]; | |
34 } | |
35 | |
36 // Default empty list of remembered file states. | |
37 const DEFAULT_DATA: RememberFileStatePluginData = { | |
38 rememberedFiles: [] | |
39 }; | |
40 | |
41 export default class RememberFileStatePlugin extends Plugin { | |
42 settings: RememberFileStatePluginSettings; | |
43 data: RememberFileStatePluginData; | |
44 | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
45 // Don't restore state on the next file being opened. |
0 | 46 private _suppressNextFileOpen: boolean = false; |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
47 // 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
|
48 private _nextUniqueViewId: number = 0; |
0 | 49 |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
50 // Functions to unregister any monkey-patched view hooks on plugin unload. |
0 | 51 private _viewUninstallers = {}; |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
52 // Functions to unregister any global callbacks on plugin unload. |
0 | 53 private _globalUninstallers = []; |
54 | |
55 async onload() { | |
56 console.log("Loading RememberFileState plugin"); | |
57 | |
58 await this.loadSettings(); | |
59 | |
60 this.data = Object.assign({}, DEFAULT_DATA); | |
61 | |
62 this.registerEvent(this.app.workspace.on('file-open', this.onFileOpen)); | |
63 this.registerEvent(this.app.vault.on('rename', this.onFileRename)); | |
64 this.registerEvent(this.app.vault.on('delete', this.onFileDelete)); | |
65 | |
66 this.app.workspace.getLeavesOfType("markdown").forEach( | |
67 (leaf) => { this.registerOnUnloadFile(leaf.view); }); | |
68 | |
69 const _this = this; | |
70 var uninstall = around(this.app.workspace, { | |
71 openLinkText: function(next) { | |
72 return async function( | |
73 linktext: string, sourcePath: string, | |
74 newLeaf?: boolean, openViewState?: OpenViewState) { | |
75 // When opening a link, we don't want to restore the | |
76 // scroll position/selection/etc because there's a | |
77 // good chance we want to show the file back at the | |
78 // top, or we're going straight to a specific block. | |
79 _this._suppressNextFileOpen = true; | |
80 return await next.call( | |
81 this, linktext, sourcePath, newLeaf, openViewState); | |
82 }; | |
83 } | |
84 }); | |
85 this._globalUninstallers.push(uninstall); | |
86 | |
87 this.addSettingTab(new RememberFileStatePluginSettingTab(this.app, this)); | |
88 } | |
89 | |
90 onunload() { | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
91 // 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
|
92 var numViews: number = 0; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
93 this.app.workspace.getLeavesOfType("markdown").forEach( |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
94 (leaf) => { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
95 const filePath = leaf.view.file.path; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
96 const viewId = this.getUniqueViewId(leaf.view); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
97 if (viewId != undefined) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
98 var uninstaller = this._viewUninstallers[viewId]; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
99 if (uninstaller) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
100 console.debug(`Uninstalling hooks for view ${viewId}`, filePath); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
101 uninstaller(leaf.view); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
102 ++numViews; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
103 } else { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
104 console.debug("Found markdown view without an uninstaller!", filePath); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
105 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
106 // 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
|
107 // is re-enabled later. |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
108 this.clearUniqueViewId(leaf.view); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
109 } else { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
110 console.debug("Found markdown view without an ID!", filePath); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
111 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
112 }); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
113 console.debug(`Unregistered ${numViews} view callbacks`); |
0 | 114 this._viewUninstallers = {}; |
115 | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
116 // Run global unhooks. |
0 | 117 this._globalUninstallers.forEach((cb) => cb()); |
118 } | |
119 | |
120 async loadSettings() { | |
121 this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); | |
122 } | |
123 | |
124 async saveSettings() { | |
125 await this.saveData(this.settings); | |
126 } | |
127 | |
128 private readonly registerOnUnloadFile = function(view) { | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
129 var filePath = view.file.path; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
130 var viewId = this.getUniqueViewId(view, true); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
131 if (viewId in this._viewUninstallers) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
132 console.debug(`View ${viewId} is already registered`, filePath); |
0 | 133 return; |
134 } | |
135 | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
136 console.debug(`Registering callback on view ${viewId}`, filePath); |
0 | 137 const _this = this; |
138 var uninstall = around(view, { | |
139 onUnloadFile: function(next) { | |
140 return async function (unloaded: TFile) { | |
141 _this.rememberFileState(unloaded, view); | |
142 return await next.call(this, unloaded); | |
143 }; | |
144 } | |
145 }); | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
146 this._viewUninstallers[viewId] = uninstall; |
0 | 147 |
148 view.register(() => { | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
149 // 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
|
150 // 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
|
151 // do nothing if we don't find it. |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
152 var plugin = app.plugins.getPlugin("remember-file-state"); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
153 if (plugin) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
154 console.debug(`Unregistering view ${viewId} callback`, filePath); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
155 delete plugin._viewUninstallers[viewId]; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
156 uninstall(); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
157 } else { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
158 console.debug( |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
159 "Plugin remember-file-state has been unloaded, ignoring unregister"); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
160 } |
0 | 161 }); |
162 } | |
163 | |
164 private readonly onFileOpen = async ( | |
165 openedFile: TFile | |
166 ): Promise<void> => { | |
167 // If `openedFile` is null, it's because the last pane was closed | |
168 // and there is now an empty pane. | |
169 if (openedFile) { | |
2 | 170 var activeView = this.app.workspace.getActiveViewOfType(MarkdownView); |
0 | 171 this.registerOnUnloadFile(activeView); |
172 | |
173 if (!this._suppressNextFileOpen) { | |
2 | 174 this.restoreFileState(openedFile, activeView); |
0 | 175 } else { |
176 this._suppressNextFileOpen = false; | |
177 } | |
178 } | |
179 } | |
180 | |
181 private readonly rememberFileState = async (file: TFile, view: View): Promise<void> => { | |
182 const scrollInfo = view.editor.getScrollInfo(); | |
183 const stateSelectionJSON = view.editor.cm.state.selection.toJSON(); | |
184 const stateData = {'scrollInfo': scrollInfo, 'selection': stateSelectionJSON}; | |
185 | |
186 var existingFile = this.data.rememberedFiles.find( | |
187 curFile => curFile.path == file.path | |
188 ); | |
189 | |
190 if (existingFile) { | |
191 existingFile.lastSavedTime = Date.now(); | |
192 existingFile.stateData = stateData; | |
193 } else { | |
194 let newFileState = { | |
195 path: file.path, | |
196 lastSavedTime: Date.now(), | |
197 stateData: stateData | |
198 }; | |
199 this.data.rememberedFiles.push(newFileState); | |
200 | |
201 // If we need to keep the number remembered files under a maximum, | |
202 // do it now. | |
203 this.forgetExcessFiles(); | |
204 } | |
205 console.debug("Remember file state for:", file.path); | |
206 } | |
207 | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
208 private readonly restoreFileState = function(file: TFile, view: View) { |
0 | 209 const existingFile = this.data.rememberedFiles.find( |
210 (curFile) => curFile.path === file.path | |
211 ); | |
212 if (existingFile) { | |
213 console.debug("Restoring file state for:", file.path); | |
214 const stateData = existingFile.stateData; | |
215 view.editor.scrollTo(stateData.scrollInfo.left, stateData.scrollInfo.top); | |
2 | 216 var transaction = view.editor.cm.state.update({ |
217 selection: EditorSelection.fromJSON(stateData.selection)}) | |
218 view.editor.cm.dispatch(transaction); | |
0 | 219 } |
220 } | |
221 | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
222 private readonly forgetExcessFiles = function() { |
0 | 223 const keepMax = this.settings.rememberMaxFiles; |
224 if (keepMax <= 0) { | |
225 return; | |
226 } | |
227 | |
228 this.data.rememberedFiles.sort((a, b) => a.lastSavedTime < b.lastSavedTime); | |
229 | |
230 if (this.data.rememberedFiles.length > keepMax) { | |
231 this.data.rememberedFiles.splice(keepMax); | |
232 } | |
233 } | |
234 | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
235 private readonly getUniqueViewId = function(view: View, autocreateId: boolean = false) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
236 if (view.__uniqueId == undefined) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
237 if (!autocreateId) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
238 return -1; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
239 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
240 view.__uniqueId = (this._nextUniqueViewId++); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
241 return view.__uniqueId; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
242 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
243 return view.__uniqueId; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
244 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
245 |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
246 private readonly clearUniqueViewId = function(view: View) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
247 delete view["__uniqueId"]; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
248 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
249 |
0 | 250 private readonly onFileRename = async ( |
251 file: TAbstractFile, | |
252 oldPath: string, | |
253 ): Promise<void> => { | |
254 const existingFile = this.data.rememberedFiles.find( | |
255 (curFile) => curFile.path === oldPath | |
256 ); | |
257 if (existingFile) { | |
258 existingFile.path = file.path; | |
259 } | |
260 }; | |
261 | |
262 private readonly onFileDelete = async ( | |
263 file: TAbstractFile, | |
264 ): Promise<void> => { | |
265 this.data.rememberedFiles = this.data.rememberedFiles.filter( | |
266 (curFile) => curFile.path !== file.path | |
267 ); | |
268 }; | |
269 } | |
270 |