-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
136 lines (115 loc) · 4.88 KB
/
main.js
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
const { Plugin } = require('obsidian');
class PaneZoomLevelsPlugin extends Plugin {
async onload() {
this.zoomLevels = new Map();
this.zoomStep = 0.1; // 10% zoom step
// Add command to set custom zoom level
this.addCommand({
id: 'set-pane-zoom',
name: 'Set Custom Zoom Level for Current Pane',
callback: async () => {
const leaf = this.app.workspace.activeLeaf;
if (!leaf) return;
const currentZoom = this.zoomLevels.get(leaf.id)?.scale || 1;
const input = await this.promptForZoom(currentZoom);
if (input !== null) {
this.setZoomForPane(leaf, { scale: input / 100 });
}
}
});
// Add command to zoom in
this.addCommand({
id: 'zoom-in',
name: 'Zoom In',
callback: () => this.incrementalZoom(true),
hotkeys: [{ modifiers: ["Alt"], key: "=" }]
});
// Add command to zoom out
this.addCommand({
id: 'zoom-out',
name: 'Zoom Out',
callback: () => this.incrementalZoom(false),
hotkeys: [{ modifiers: ["Alt"], key: "-" }]
});
// Register for layout change events
this.registerEvent(
this.app.workspace.on('layout-change', this.reapplyZoomLevels.bind(this))
);
// Register for file open events
this.registerEvent(
this.app.workspace.on('file-open', this.reapplyZoomLevels.bind(this))
);
}
async promptForZoom(currentZoom) {
const input = await new Promise(resolve => {
const modal = new this.app.Modal();
modal.contentEl.createEl('h2', { text: 'Set Zoom Level' });
const inputEl = modal.contentEl.createEl('input', {
type: 'number',
value: Math.round(currentZoom * 100),
placeholder: 'Enter zoom level (e.g., 150 for 150%)'
});
const buttonEl = modal.contentEl.createEl('button', { text: 'Set' });
buttonEl.onclick = () => {
modal.close();
resolve(inputEl.value);
};
modal.open();
});
if (!input) return null;
const numericInput = parseFloat(input);
return isNaN(numericInput) ? null : Math.max(10, numericInput); // Minimum 10% zoom
}
incrementalZoom(zoomIn) {
const leaf = this.app.workspace.activeLeaf;
if (!leaf) return;
const currentZoom = this.zoomLevels.get(leaf.id)?.scale || 1;
const newZoom = zoomIn ? currentZoom + this.zoomStep : currentZoom - this.zoomStep;
this.setZoomForPane(leaf, { scale: Math.max(0.1, newZoom) }); // Minimum 10% zoom
}
setZoomForPane(leaf, zoomLevel) {
const leafId = leaf.id;
const contentEl = leaf.view.contentEl;
// Find the actual content container
const contentContainer = contentEl.querySelector('.markdown-preview-view, .markdown-source-view');
if (!contentContainer) return;
// Remove transform from any existing zoom level
contentContainer.style.transform = '';
contentContainer.style.transformOrigin = '';
// Apply new zoom level
if (zoomLevel) {
contentContainer.style.transform = `scale(${zoomLevel.scale})`;
contentContainer.style.transformOrigin = 'top left';
// Adjust the container size to accommodate the scaled content
contentContainer.style.width = `${100 / zoomLevel.scale}%`;
contentContainer.style.height = `${100 / zoomLevel.scale}%`;
} else {
// Reset the container size
contentContainer.style.width = '';
contentContainer.style.height = '';
}
// Store the zoom level for this pane
this.zoomLevels.set(leafId, zoomLevel);
}
reapplyZoomLevels() {
this.app.workspace.iterateAllLeaves(leaf => {
const zoomLevel = this.zoomLevels.get(leaf.id);
if (zoomLevel) {
this.setZoomForPane(leaf, zoomLevel);
}
});
}
onunload() {
// Remove all zoom transforms when plugin is disabled
this.app.workspace.iterateAllLeaves(leaf => {
const contentContainer = leaf.view.contentEl.querySelector('.markdown-preview-view, .markdown-source-view');
if (contentContainer) {
contentContainer.style.transform = '';
contentContainer.style.transformOrigin = '';
contentContainer.style.width = '';
contentContainer.style.height = '';
}
});
}
}
module.exports = PaneZoomLevelsPlugin;