MCPcopy Create free account
hub / github.com/UNLINEARITY/Obsidian-CodeSpace / setupZoomHandler

Method setupZoomHandler

src/code_view.ts:1266–1313  ·  view source on GitHub ↗
(root: HTMLElement)

Source from the content-addressed store, hash-verified

1264
1265 // 添加 Ctrl+滚轮缩放功能
1266 private setupZoomHandler(root: HTMLElement) {
1267 this.registerDomEvent(root, "wheel", (event: WheelEvent) => {
1268 // 检测是否按下了 Ctrl 或 Cmd 键
1269 if (event.ctrlKey || event.metaKey) {
1270 event.preventDefault();
1271
1272 // 计算缩放方向
1273 const delta = event.deltaY > 0 ? -1 : 1;
1274
1275 // 调整字体大小(每次变化 1px,范围 9-36px)
1276 const newSize = Math.max(9, Math.min(36, this.fontSize + delta));
1277
1278 if (newSize !== this.fontSize) {
1279 this.fontSize = newSize;
1280
1281 // 使用 Compartment 重新配置字体大小扩展
1282 this.editorView.dispatch({
1283 effects: this.fontSizeCompartment.reconfigure(this.getFontSizeExtension())
1284 });
1285
1286 console.debug(`Code Space: Font size changed to ${this.fontSize}px`);
1287 }
1288 }
1289 }, { passive: false });
1290
1291 this.registerEvent(this.app.workspace.on("css-change", () => {
1292 this.editorView.dispatch({
1293 effects: this.themeCompartment.reconfigure(this.getThemeExtension())
1294 });
1295 }));
1296
1297 // 监听文件修改事件(外部编辑)
1298 this.registerEvent(this.app.vault.on("modify", (file: TFile) => {
1299 // 检查修改的文件是否是当前打开的文件
1300 if (this.file && file.path === this.file.path) {
1301 // 如果有未保存的修改,不要重新加载(保护用户的编辑)
1302 if (this.isDirty) {
1303 console.debug("Code Space: File modified externally but has unsaved changes");
1304 new Notice(t('NOTICE_MODIFIED_EXTERNALLY'), 5000);
1305 return;
1306 }
1307
1308 // 没有未保存的修改,直接刷新
1309 console.debug("Code Space: File modified externally, reloading...");
1310 void this.loadFileContent();
1311 }
1312 }));
1313 }
1314
1315 async loadFileContent() {
1316 if (!this.file) return;

Callers 1

initCodeMirrorMethod · 0.95

Calls 4

getFontSizeExtensionMethod · 0.95
getThemeExtensionMethod · 0.95
loadFileContentMethod · 0.95
tFunction · 0.90

Tested by

no test coverage detected