-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
415 lines (342 loc) · 13.6 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import { Plugin, TFile, View, Notice, Platform, PluginSettingTab, Setting, App } from 'obsidian';
interface CameraPosition {
tx: number;
ty: number;
tZoom: number;
}
interface CanvasViewportSettings {
debugMode: boolean;
useGlobalViewport: boolean;
}
const DEFAULT_SETTINGS: CanvasViewportSettings = {
debugMode: false,
useGlobalViewport: false,
};
interface CanvasView extends View {
canvas: {
tx: number;
ty: number;
tZoom: number;
viewportChanged: boolean;
requestFrame: () => void;
};
file: TFile;
}
export default class CanvasViewportPlugin extends Plugin {
private openCanvasFiles: string[] = [];
private currentDevice: string = '';
settings: CanvasViewportSettings;
// Custom logging function
private log(...args: any[]) {
if (this.settings.debugMode) {
console.log(...args);
}
}
private logGroup(name: string) {
if (this.settings.debugMode) {
console.group(name);
}
}
private logGroupEnd() {
if (this.settings.debugMode) {
console.groupEnd();
}
}
async onload() {
await this.loadSettings();
this.addSettingTab(new CanvasViewportSettingTab(this.app, this));
this.currentDevice = this.getDeviceIdentifier();
this.addCommand({
id: 'save-canvas-viewport',
name: 'Save current viewport',
checkCallback: (checking) => {
const canvasLeaves = this.app.workspace.getLeavesOfType('canvas');
if (canvasLeaves.length === 0) {
if (!checking) {
new Notice('Please open a canvas first');
}
return false;
}
if (!checking) {
const canvasView = canvasLeaves[0].view as CanvasView;
this.saveCurrentPosition(canvasView);
}
return true;
}
});
this.addCommand({
id: 'restore-canvas-viewport',
name: 'Restore saved viewport',
checkCallback: (checking) => {
const canvasLeaves = this.app.workspace.getLeavesOfType('canvas');
if (canvasLeaves.length === 0) {
if (!checking) {
new Notice('Please open a canvas first');
}
return false;
}
if (!checking) {
const canvasView = canvasLeaves[0].view as CanvasView;
this.restoreViewport(canvasView.file);
}
return true;
}
});
this.addCommand({
id: 'delete-canvas-viewport',
name: 'Delete saved viewport',
checkCallback: (checking) => {
const canvasLeaves = this.app.workspace.getLeavesOfType('canvas');
if (canvasLeaves.length === 0) {
if (!checking) {
new Notice('Please open a canvas first');
}
return false;
}
if (!checking) {
const canvasView = canvasLeaves[0].view as CanvasView;
this.deleteSavedPosition(canvasView).then(deleted => {
if (deleted) {
this.log(`viewport deleted for device: ${this.getViewportKey()}`);
new Notice(`Canvas viewport deleted for ${this.getViewportKey()}`);
} else {
this.log(`No viewport found to delete for device: ${this.getViewportKey()}`);
new Notice('No saved viewport found');
}
});
}
return true;
}
});
// The file-open event is triggered not just when opening files, but also during
// Canvas operations like copy/paste or deleting elements. These operations appear
// to cause a reload which fires this event. To prevent unwanted viewport
// restoration during these operations, we maintain a list of already-open canvas
// files and only restore the viewport when a canvas is truly being opened for
// the first time.
this.registerEvent(
this.app.workspace.on('file-open', async (file: TFile) => {
this.logGroup('Canvas Viewport Plugin - File Open Event');
const wasAlreadyOpen = file?.extension === 'canvas' && this.openCanvasFiles.includes(file.path);
this.log('File path:', file?.path);
this.log('File type:', file?.extension);
this.log('Was already open:', wasAlreadyOpen);
const canvasLeaves = this.app.workspace.getLeavesOfType('canvas');
this.openCanvasFiles = canvasLeaves
.map(leaf => (leaf.view as CanvasView).file.path);
this.log('Currently open canvas files:', this.openCanvasFiles);
if (!file || file.extension !== 'canvas' || wasAlreadyOpen) {
this.log('Skipping viewport restoration');
this.logGroupEnd();
return;
}
this.log('Proceeding with viewport restoration');
await this.restoreViewport(file);
this.logGroupEnd();
})
);
}
private getViewportKey(): string {
return this.settings.useGlobalViewport ? 'global' : this.currentDevice;
}
private getDeviceIdentifier(): string {
this.logGroup('Canvas Viewport Plugin - Device Detection');
let deviceType = "Unknown";
if (Platform.isMacOS) {
deviceType = "MacOS";
} else if (Platform.isWin) {
deviceType = "Windows";
} else if (Platform.isLinux) {
deviceType = "Linux";
}
if (Platform.isMobile) {
if (Platform.isPhone) {
deviceType += "_Phone";
} else if (Platform.isTablet) {
deviceType += "_Tablet";
}
if (Platform.isIosApp) {
deviceType = "iOS_" + deviceType;
} else if (Platform.isAndroidApp) {
deviceType = "Android_" + deviceType;
}
} else if (Platform.isDesktopApp) {
deviceType += "_Desktop";
}
const resolution = `${window.screen.width}x${window.screen.height}`;
const pixelRatio = window.devicePixelRatio;
const deviceId = `${deviceType}_${resolution}@${pixelRatio}x`;
this.log('Final device identifier:', deviceId);
this.logGroupEnd();
return deviceId;
}
private async restoreViewport(file: TFile) {
this.logGroup('Canvas Viewport Plugin - Restore Viewport');
const position = await this.loadSavedPosition(file);
if (!position) {
this.log('No saved viewport found');
new Notice('No saved viewport found');
this.logGroupEnd();
return;
}
this.log('Loaded position:', position);
// Add a small delay to ensure canvas is initialized
await new Promise(resolve => setTimeout(resolve, 100));
const canvasLeaves = this.app.workspace.getLeavesOfType('canvas');
if (canvasLeaves.length === 0) {
this.log('No canvas leaves found');
this.logGroupEnd();
return;
}
const canvasView = canvasLeaves[0].view as CanvasView;
const canvas = canvasView.canvas;
// Ensure the canvas view is properly initialized
if (!canvas || typeof canvas.tZoom === 'undefined') {
this.log('Canvas not yet initialized');
this.logGroupEnd();
return;
}
const currentZoom = canvas.tZoom;
const zoomDelta = position.tZoom - currentZoom;
this.log('Current zoom:', currentZoom);
this.log('Target zoom:', position.tZoom);
this.log('Zoom delta:', zoomDelta);
this.log('Current Position:', canvas.tx, canvas.ty);
this.log('Target Position', position);
try {
// Queue the viewport changes in the next animation frame
requestAnimationFrame(() => {
(canvas as any).zoomBy(zoomDelta);
(canvas as any).panTo(position.tx, position.ty);
(canvas as any).markViewportChanged();
canvas.requestFrame();
new Notice('Canvas viewport restored');
this.log('Viewport changes applied successfully');
});
} catch (error) {
console.error('Failed to restore viewport:', error);
new Notice('Failed to restore viewport');
}
this.logGroupEnd();
}
private async saveCurrentPosition(view: CanvasView) {
this.logGroup('Canvas Viewport Plugin - Save Position');
if (!view?.file || !view?.canvas) {
this.log('Invalid view or canvas');
this.logGroupEnd();
return;
}
try {
const content = await this.app.vault.read(view.file);
const canvasData = JSON.parse(content);
if (!canvasData.viewports) {
this.log('Initializing viewports object');
canvasData.viewports = {};
}
const position = {
tx: view.canvas.tx,
ty: view.canvas.ty,
tZoom: view.canvas.tZoom
};
const viewportKey = this.getViewportKey();
canvasData.viewports[viewportKey] = position;
this.log('Saving position for:', viewportKey);
this.log('Position:', position);
await this.app.vault.modify(view.file, JSON.stringify(canvasData, null, 2));
this.log('Position saved successfully');
new Notice('Canvas viewport saved');
} catch (error) {
console.error('Failed to save canvas viewport:', error);
new Notice('Failed to save viewport');
}
this.logGroupEnd();
}
private async deleteSavedPosition(view: CanvasView): Promise<boolean> {
this.logGroup('Canvas Viewport Plugin - Delete Position');
if (!view?.file) {
this.log('Invalid view');
this.logGroupEnd();
return false;
}
try {
const content = await this.app.vault.read(view.file);
const canvasData = JSON.parse(content);
const viewportKey = this.getViewportKey();
if (!canvasData.viewports?.[viewportKey]) {
this.log('No viewport found for:', viewportKey);
this.logGroupEnd();
return false;
}
this.log('Deleting viewport for:', viewportKey);
delete canvasData.viewports[viewportKey];
if (Object.keys(canvasData.viewports).length === 0) {
this.log('Removing empty viewports object');
delete canvasData.viewports;
}
await this.app.vault.modify(view.file, JSON.stringify(canvasData, null, 2));
this.log('Position deleted successfully');
this.logGroupEnd();
return true;
} catch (error) {
console.error('Failed to delete canvas viewport:', error);
this.logGroupEnd();
return false;
}
}
private async loadSavedPosition(file: TFile): Promise<CameraPosition | null> {
this.logGroup('Canvas Viewport Plugin - Load Position');
try {
const content = await this.app.vault.read(file);
const canvasData = JSON.parse(content);
const viewportKey = this.getViewportKey();
const position = canvasData.viewports?.[viewportKey] || null;
this.log('Loading position for:', viewportKey);
this.log('Found position:', position);
this.logGroupEnd();
return position;
} catch (error) {
console.error('Failed to load canvas viewport:', error);
this.logGroupEnd();
return null;
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
onunload() {
this.log('Canvas Viewport Plugin - Unloading');
this.openCanvasFiles = [];
}
}
class CanvasViewportSettingTab extends PluginSettingTab {
plugin: CanvasViewportPlugin;
constructor(app: App, plugin: CanvasViewportPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName('Debug Mode')
.setDesc('Enable debug logging in the console')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.debugMode)
.onChange(async (value) => {
this.plugin.settings.debugMode = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Global Viewport')
.setDesc('Use the same viewport across all devices')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.useGlobalViewport)
.onChange(async (value) => {
this.plugin.settings.useGlobalViewport = value;
await this.plugin.saveSettings();
}));
}
}