* Switches the active file in the editor. * @param {string} id - The ID of the file to switch to.
(id)
| 2854 | * @param {string} id - The ID of the file to switch to. |
| 2855 | */ |
| 2856 | function switchFile(id) { |
| 2857 | const { id: activeFileId } = manager.activeFile || {}; |
| 2858 | if (activeFileId === id) return; |
| 2859 | |
| 2860 | const file = manager.getFile(id); |
| 2861 | if (!file) return; |
| 2862 | |
| 2863 | manager.activeFile?.tab.classList.remove("active"); |
| 2864 | |
| 2865 | // Hide previous content if it was non-editor |
| 2866 | if (manager.activeFile?.type !== "editor" && manager.activeFile?.content) { |
| 2867 | manager.activeFile.content.style.display = "none"; |
| 2868 | } |
| 2869 | |
| 2870 | // Persist the previous editor's state before switching away |
| 2871 | const prev = manager.activeFile; |
| 2872 | if (prev?.type === "editor") { |
| 2873 | prev.session = getRawEditorState(editor.state); |
| 2874 | prev.lastScrollTop = editor.scrollDOM?.scrollTop || 0; |
| 2875 | prev.lastScrollLeft = editor.scrollDOM?.scrollLeft || 0; |
| 2876 | window.setTimeout(() => { |
| 2877 | prev.flushCacheWrite?.().catch((error) => { |
| 2878 | warnRecoverable( |
| 2879 | `Failed to flush cache for ${prev.filename || prev.uri}`, |
| 2880 | error, |
| 2881 | `cache-flush-${prev.id}`, |
| 2882 | ); |
| 2883 | }); |
| 2884 | }, 1000); |
| 2885 | } |
| 2886 | |
| 2887 | manager.activeFile = file; |
| 2888 | file.tab.classList.add("active"); |
| 2889 | file.tab.scrollIntoView(); |
| 2890 | $header.text = file.filename; |
| 2891 | $header.subText = file.headerSubtitle || ""; |
| 2892 | |
| 2893 | if (file.type === "editor") { |
| 2894 | touchSelectionController?.setEnabled(true); |
| 2895 | if (!file.loaded && !file.loading) { |
| 2896 | showLoadingEditor(file); |
| 2897 | } else { |
| 2898 | // Apply active file content and language to CodeMirror |
| 2899 | applyFileToEditor(file); |
| 2900 | } |
| 2901 | $container.style.display = "block"; |
| 2902 | |
| 2903 | $hScrollbar.hideImmediately(); |
| 2904 | $vScrollbar.hideImmediately(); |
| 2905 | |
| 2906 | setVScrollValue(); |
| 2907 | if (!appSettings.value.textWrap) { |
| 2908 | setHScrollValue(); |
| 2909 | } |
| 2910 | } else { |
| 2911 | touchSelectionController?.setEnabled(false); |
| 2912 | $container.style.display = "none"; |
| 2913 | if (file.content) { |
no test coverage detected