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

Method onLoadFile

src/code_view.ts:1013–1066  ·  view source on GitHub ↗
(file: TFile)

Source from the content-addressed store, hash-verified

1011 }
1012
1013 async onLoadFile(file: TFile): Promise<void> {
1014 await super.onLoadFile(file);
1015
1016 // Obsidian 原生支持的二进制文件类型列表
1017 const nativeBinaryExtensions = [
1018 // 图片
1019 'png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'bmp', 'ico', 'tiff', 'psd',
1020 // PDF
1021 'pdf',
1022 // 音频
1023 'mp3', 'wav', 'ogg', 'flac', 'aac', 'm4a', 'wma',
1024 // 视频
1025 'mp4', 'avi', 'mkv', 'mov', 'wmv', 'flv', 'webm', 'm4v',
1026 // 压缩文件
1027 'zip', 'rar', '7z', 'tar', 'gz', 'bz2', 'xz',
1028 // Office 文档
1029 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx',
1030 // 其他二进制文件
1031 'exe', 'dll', 'so', 'dylib', 'bin', 'dat'
1032 ];
1033
1034 // 检查当前文件是否是二进制文件
1035 const ext = file.extension.toLowerCase();
1036 if (nativeBinaryExtensions.includes(ext)) {
1037 console.debug(`Code Space: Detected binary file .${ext}, opening with native viewer`);
1038
1039 // 先销毁编辑器,防止它保存二进制内容
1040 if (this.editorView) {
1041 this.editorView.destroy();
1042 this.editorView = null as unknown as EditorView;
1043 }
1044
1045 // 使用 Obsidian 原生方式打开文件
1046 await this.app.workspace.openLinkText(file.path, '', true);
1047
1048 // 关闭当前的 Code Space 视图(因为已经用原生查看器打开了)
1049 this.leaf.detach();
1050 return;
1051 }
1052
1053 // Update language extension when file is loaded
1054 if (this.editorView) {
1055 console.debug("Code Space: File loaded:", file.name, "extension:", ext);
1056 const langExt = LANGUAGE_PACKAGES[ext] || [];
1057 console.debug("Code Space: Applying language extension:", langExt);
1058 this.editorView.dispatch({
1059 effects: this.languageCompartment.reconfigure(langExt)
1060 });
1061 }
1062
1063 // 文件加载完成,清除 dirty 状态
1064 this.isDirty = false;
1065 this.updateTitle();
1066 }
1067
1068 async onOpen(): Promise<void> {
1069 await Promise.resolve(); // Required for async function

Callers

nothing calls this directly

Calls 2

updateTitleMethod · 0.95
destroyMethod · 0.80

Tested by

no test coverage detected