Mercurial > obsidian-remember-file-state
annotate src/main.ts @ 25:6c168de105c0 1.0.5
Bump versions to 1.0.5, remove git stuff in script
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 18 Mar 2022 19:35:47 -0700 |
parents | ba74a7d3709c |
children | 3d0ac176118f |
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, |
5 OpenViewState, | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
6 Plugin, |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
7 TAbstractFile, |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
8 TFile, |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
9 View, |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
10 WorkspaceLeaf |
0 | 11 } from 'obsidian'; |
12 | |
13 import { | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
14 EditorView |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
15 } from '@codemirror/view'; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
16 |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
17 import { |
2 | 18 EditorState, |
0 | 19 EditorSelection |
20 } from '@codemirror/state'; | |
21 | |
22 import { | |
23 around | |
24 } from 'monkey-around'; | |
25 | |
26 import { | |
27 DEFAULT_SETTINGS, | |
28 RememberFileStatePluginSettings, | |
29 RememberFileStatePluginSettingTab | |
30 } from './settings'; | |
31 | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
32 declare var app: App; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
33 |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
34 // Interface for CM6 editor view |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
35 interface EditorWithCM6 extends Editor { |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
36 cm: EditorView |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
37 }; |
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 // View with unique ID |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
40 interface ViewWithID extends View { |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
41 __uniqueId: number |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
42 }; |
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 // Scroll info interface |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
45 interface ScrollInfo { |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
46 top: number, left: number |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
47 }; |
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 interface StateData { |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
50 selection: EditorSelection, |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
51 scrollInfo: ScrollInfo |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
52 }; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
53 |
0 | 54 // Interface for a file state. |
55 interface RememberedFileState { | |
56 path: string; | |
57 lastSavedTime: number; | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
58 stateData: StateData; |
0 | 59 } |
60 | |
61 // Interface for all currently remembered file states. | |
62 interface RememberFileStatePluginData { | |
11
6f7f35af6335
Better type information for the plugin data
Ludovic Chabant <ludovic@chabant.com>
parents:
8
diff
changeset
|
63 rememberedFiles: Record<string, RememberedFileState>; |
0 | 64 } |
65 | |
66 // Default empty list of remembered file states. | |
67 const DEFAULT_DATA: RememberFileStatePluginData = { | |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
68 rememberedFiles: {} |
0 | 69 }; |
70 | |
71 export default class RememberFileStatePlugin extends Plugin { | |
72 settings: RememberFileStatePluginSettings; | |
73 data: RememberFileStatePluginData; | |
74 | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
75 // Don't restore state on the next file being opened. |
0 | 76 private _suppressNextFileOpen: boolean = false; |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
77 // 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
|
78 private _nextUniqueViewId: number = 0; |
0 | 79 |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
80 // 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
|
81 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
|
82 // Functions to unregister any global callbacks on plugin unload. |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
83 private _globalUninstallers: Function[] = []; |
0 | 84 |
85 async onload() { | |
86 console.log("Loading RememberFileState plugin"); | |
87 | |
88 await this.loadSettings(); | |
89 | |
90 this.data = Object.assign({}, DEFAULT_DATA); | |
91 | |
92 this.registerEvent(this.app.workspace.on('file-open', this.onFileOpen)); | |
93 this.registerEvent(this.app.vault.on('rename', this.onFileRename)); | |
94 this.registerEvent(this.app.vault.on('delete', this.onFileDelete)); | |
95 | |
96 this.app.workspace.getLeavesOfType("markdown").forEach( | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
97 (leaf: WorkspaceLeaf) => { this.registerOnUnloadFile(leaf.view as MarkdownView); }); |
0 | 98 |
99 const _this = this; | |
100 var uninstall = around(this.app.workspace, { | |
101 openLinkText: function(next) { | |
102 return async function( | |
103 linktext: string, sourcePath: string, | |
104 newLeaf?: boolean, openViewState?: OpenViewState) { | |
105 // When opening a link, we don't want to restore the | |
106 // scroll position/selection/etc because there's a | |
107 // good chance we want to show the file back at the | |
108 // top, or we're going straight to a specific block. | |
109 _this._suppressNextFileOpen = true; | |
110 return await next.call( | |
111 this, linktext, sourcePath, newLeaf, openViewState); | |
112 }; | |
113 } | |
114 }); | |
115 this._globalUninstallers.push(uninstall); | |
116 | |
117 this.addSettingTab(new RememberFileStatePluginSettingTab(this.app, this)); | |
118 } | |
119 | |
120 onunload() { | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
121 // 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
|
122 var numViews: number = 0; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
123 this.app.workspace.getLeavesOfType("markdown").forEach( |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
124 (leaf: WorkspaceLeaf) => { |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
125 const filePath = (leaf.view as MarkdownView).file.path; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
126 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
|
127 if (viewId != undefined) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
128 var uninstaller = this._viewUninstallers[viewId]; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
129 if (uninstaller) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
130 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
|
131 uninstaller(leaf.view); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
132 ++numViews; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
133 } else { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
134 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
|
135 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
136 // 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
|
137 // is re-enabled later. |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
138 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
|
139 } else { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
140 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
|
141 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
142 }); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
143 console.debug(`Unregistered ${numViews} view callbacks`); |
0 | 144 this._viewUninstallers = {}; |
145 | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
146 // Run global unhooks. |
0 | 147 this._globalUninstallers.forEach((cb) => cb()); |
148 } | |
149 | |
150 async loadSettings() { | |
151 this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); | |
152 } | |
153 | |
154 async saveSettings() { | |
155 await this.saveData(this.settings); | |
156 } | |
157 | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
158 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
|
159 var filePath = view.file.path; |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
160 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
|
161 if (viewId in this._viewUninstallers) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
162 console.debug(`View ${viewId} is already registered`, filePath); |
0 | 163 return; |
164 } | |
165 | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
166 console.debug(`Registering callback on view ${viewId}`, filePath); |
0 | 167 const _this = this; |
168 var uninstall = around(view, { | |
169 onUnloadFile: function(next) { | |
170 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
|
171 _this.rememberFileState(unloaded, this); |
0 | 172 return await next.call(this, unloaded); |
173 }; | |
174 } | |
175 }); | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
176 this._viewUninstallers[viewId] = uninstall; |
0 | 177 |
178 view.register(() => { | |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
179 // 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
|
180 // 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
|
181 // do nothing if we don't find it. |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
182 // @ts-ignore |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
183 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
|
184 if (plugin) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
185 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
|
186 delete plugin._viewUninstallers[viewId]; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
187 uninstall(); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
188 } else { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
189 console.debug( |
16
a50ef39473b6
Fix plugin ID, bump version to 1.0.4
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
190 "Plugin obsidian-remember-file-state has been unloaded, ignoring unregister"); |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
191 } |
0 | 192 }); |
193 } | |
194 | |
195 private readonly onFileOpen = async ( | |
196 openedFile: TFile | |
197 ): Promise<void> => { | |
198 // If `openedFile` is null, it's because the last pane was closed | |
199 // and there is now an empty pane. | |
200 if (openedFile) { | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
201 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
|
202 if (activeView) { |
b1cb0474bb18
Fix possible crash when an opened file isn't a markdown file
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
203 this.registerOnUnloadFile(activeView); |
0 | 204 |
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
|
205 // 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
|
206 // - We are suppressing it explicitly (such as if the file was |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
207 // opened via clicking a hyperlink) |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
208 // - The file is already currently open in another pane |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
209 if (!this._suppressNextFileOpen && !this.isFileMultiplyOpen(openedFile)) { |
23
ba74a7d3709c
Add try/catch around restoring file state
Ludovic Chabant <ludovic@chabant.com>
parents:
22
diff
changeset
|
210 try { |
ba74a7d3709c
Add try/catch around restoring file state
Ludovic Chabant <ludovic@chabant.com>
parents:
22
diff
changeset
|
211 this.restoreFileState(openedFile, activeView); |
ba74a7d3709c
Add try/catch around restoring file state
Ludovic Chabant <ludovic@chabant.com>
parents:
22
diff
changeset
|
212 } catch (err) { |
ba74a7d3709c
Add try/catch around restoring file state
Ludovic Chabant <ludovic@chabant.com>
parents:
22
diff
changeset
|
213 console.error("Couldn't restore file state: ", err); |
ba74a7d3709c
Add try/catch around restoring file state
Ludovic Chabant <ludovic@chabant.com>
parents:
22
diff
changeset
|
214 } |
7
b1cb0474bb18
Fix possible crash when an opened file isn't a markdown file
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
215 } |
0 | 216 } |
7
b1cb0474bb18
Fix possible crash when an opened file isn't a markdown file
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
217 // else: the file isn't handled by a markdown editor. |
b1cb0474bb18
Fix possible crash when an opened file isn't a markdown file
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
218 |
b1cb0474bb18
Fix possible crash when an opened file isn't a markdown file
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
219 this._suppressNextFileOpen = false; |
0 | 220 } |
221 } | |
222 | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
223 private readonly rememberFileState = async (file: TFile, view: MarkdownView): Promise<void> => { |
0 | 224 const scrollInfo = view.editor.getScrollInfo(); |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
225 const cm6editor = view.editor as EditorWithCM6; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
226 const stateSelection: EditorSelection = cm6editor.cm.state.selection; |
13
7da0dec2dc8d
Simple bail out when legacy editors are enabled
Ludovic Chabant <ludovic@chabant.com>
parents:
12
diff
changeset
|
227 if (stateSelection == undefined) { |
7da0dec2dc8d
Simple bail out when legacy editors are enabled
Ludovic Chabant <ludovic@chabant.com>
parents:
12
diff
changeset
|
228 // Legacy editor is in use, let's ignore |
7da0dec2dc8d
Simple bail out when legacy editors are enabled
Ludovic Chabant <ludovic@chabant.com>
parents:
12
diff
changeset
|
229 return; |
7da0dec2dc8d
Simple bail out when legacy editors are enabled
Ludovic Chabant <ludovic@chabant.com>
parents:
12
diff
changeset
|
230 } |
7da0dec2dc8d
Simple bail out when legacy editors are enabled
Ludovic Chabant <ludovic@chabant.com>
parents:
12
diff
changeset
|
231 const stateSelectionJSON = stateSelection.toJSON(); |
0 | 232 const stateData = {'scrollInfo': scrollInfo, 'selection': stateSelectionJSON}; |
233 | |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
234 var existingFile = this.data.rememberedFiles[file.path]; |
0 | 235 if (existingFile) { |
236 existingFile.lastSavedTime = Date.now(); | |
237 existingFile.stateData = stateData; | |
238 } else { | |
239 let newFileState = { | |
240 path: file.path, | |
241 lastSavedTime: Date.now(), | |
242 stateData: stateData | |
243 }; | |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
244 this.data.rememberedFiles[file.path] = newFileState; |
0 | 245 |
246 // If we need to keep the number remembered files under a maximum, | |
247 // do it now. | |
248 this.forgetExcessFiles(); | |
249 } | |
250 console.debug("Remember file state for:", file.path); | |
251 } | |
252 | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
253 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
|
254 const existingFile = this.data.rememberedFiles[file.path]; |
0 | 255 if (existingFile) { |
256 console.debug("Restoring file state for:", file.path); | |
257 const stateData = existingFile.stateData; | |
258 view.editor.scrollTo(stateData.scrollInfo.left, stateData.scrollInfo.top); | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
259 const cm6editor = view.editor as EditorWithCM6; |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
260 var transaction = cm6editor.cm.state.update({ |
2 | 261 selection: EditorSelection.fromJSON(stateData.selection)}) |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
262 |
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
263 cm6editor.cm.dispatch(transaction); |
0 | 264 } |
265 } | |
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
|
266 |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
267 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
|
268 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
|
269 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
|
270 (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
|
271 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
|
272 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
|
273 ++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
|
274 } |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
275 }); |
f7e0926c2500
Don't restore state on a file that's already open in another pane.
Ludovic Chabant <ludovic@chabant.com>
parents:
21
diff
changeset
|
276 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
|
277 } |
0 | 278 |
6
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
279 private readonly forgetExcessFiles = function() { |
0 | 280 const keepMax = this.settings.rememberMaxFiles; |
281 if (keepMax <= 0) { | |
282 return; | |
283 } | |
284 | |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
285 // Sort newer files first, older files last. |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
286 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
|
287 filesData.sort((a, b) => { |
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
288 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
|
289 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
|
290 return 0; |
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
291 }); |
0 | 292 |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
293 // Remove older files past the limit. |
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
294 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
|
295 var fileData = filesData[i]; |
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
296 delete this.data.rememberedFiles[fileData.path]; |
0 | 297 } |
298 } | |
299 | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
300 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
|
301 if (view.__uniqueId == undefined) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
302 if (!autocreateId) { |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
303 return -1; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
304 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
305 view.__uniqueId = (this._nextUniqueViewId++); |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
306 return view.__uniqueId; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
307 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
308 return view.__uniqueId; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
309 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
310 |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
311 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
|
312 delete view["__uniqueId"]; |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
313 } |
114d7e6d2633
Fix various issues around keeping references to editor objects
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
314 |
0 | 315 private readonly onFileRename = async ( |
316 file: TAbstractFile, | |
317 oldPath: string, | |
318 ): Promise<void> => { | |
21
815b93d13e0f
Improve typescript compliance
Ludovic Chabant <ludovic@chabant.com>
parents:
16
diff
changeset
|
319 const existingFile: RememberedFileState = this.data.rememberedFiles[oldPath]; |
0 | 320 if (existingFile) { |
321 existingFile.path = file.path; | |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
322 delete this.data.rememberedFiles[oldPath]; |
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
323 this.data.rememberedFiles[file.path] = existingFile; |
0 | 324 } |
325 }; | |
326 | |
327 private readonly onFileDelete = async ( | |
328 file: TAbstractFile, | |
329 ): Promise<void> => { | |
8
ec6c48a07b03
Make the plugin data into a dictionary
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
330 delete this.data.rememberedFiles[file.path]; |
0 | 331 }; |
332 } | |
333 |