lishuai199315 downloadsCollapse folders outside the active note's path, then position and highlight the active file with anchor-first scrolling.
Folder Focus Collapse keeps the file explorer focused on the note you are working on. When a note is activated, folders outside its path collapse, the active row is brought into view at a stable position, and the row is highlighted so it can be found at a glance. It is a derivative of Auto Folder Collapse: the accordion and inactivity behaviour of the original plugin are preserved, while activation-driven focusing, anchoring, scrolling, highlighting, and bilingual settings are new work introduced by this plugin.
Three independent triggers decide when the tree collapses around the active note:
The scope of a collapse is controlled by the keep mode:
| Keep mode | Folders left open |
|---|---|
| Active file path only | Only the ancestor chain of the active note. |
| Path + sibling folders | The ancestor chain plus the direct subfolders of the note's parent folder. |
| Path + current folder subtree | The entire subtree of the note's parent folder. |
For Projects/Research/Papers/note.md, the three modes keep respectively Projects/Research/Papers, that chain plus the direct children of Papers, or the whole Papers subtree. None of the modes opens folders on its own; they only decide what survives a collapse. A note at the vault root keeps the whole tree in subtree mode.
The effect is a tree that shows only the current working context. Unrelated folders stop competing for space and attention, so there is no repeated scrolling or scanning through layers to find the note; keeping the ancestor chain open preserves where the note sits in the vault.
After collapsing, the plugin positions the active row instead of merely making it visible. The container is divided into a comfort band from 10% to 78% of its height; the first top-level folder of the active path is treated as an anchor and kept at its pre-collapse screen position whenever the active row still fits in the band. When the ancestor chain is too long for that, the plugin concedes the minimum amount of scroll, and only then falls back to centering the row at a configurable ratio (40% by default, adjustable from 30% to 55%).
Four mutually exclusive scroll modes are available:
| Scroll mode | Behaviour |
|---|---|
| Anchor first (default) | Keeps the top-level folder fixed and centres only when the chain no longer fits. |
| Always center | Places the active row at the configured height on every activation. |
| Only keep visible | Scrolls only when the row is completely out of view, using native scrollIntoView. |
| No scrolling | Collapses only and never writes the scroll position. |
The write happens in the next animation frame, with up to two retries while the row is still mounting. A 250 ms correction window fixes native auto-reveal if it arrives late and pushes the row out of the comfort band, and never fights a result that is already comfortable. Each file-explorer pane is positioned independently, so split view works per pane.
Anchor-first scrolling is not only about bringing the row into view. Its main effect is keeping the tree above the target row visually stable: when the collapse changes the tree height, the top-level folder does not jump up or down, so the sidebar does not visibly shift. Spatial memory stays intact, users do not have to re-establish where things are, and attention is not pulled away by a large scroll. When anchoring cannot cover a long ancestor chain, the centre ratio still lands the row in the natural upper-middle viewing area.
Three highlight styles make the active row easy to spot without relying on scroll position alone:
Styles are scoped to the file explorer and are added or removed as the setting changes or the plugin unloads.
The highlight is not decoration. Its effect is to make the active row the one immediately identifiable element in the list: even when the row sits above the visual centre, the accent fill lets it stand out without scanning row by row. Together with stable positioning, this means users confirm the active note by appearance rather than by scrolling or searching.
The settings tab can follow Obsidian's language automatically or be forced to Chinese or English. Switching the language re-renders every section, setting name, description, and notice. Other settings update in place: for example, changing the scroll mode greys out or restores the centre-position slider without re-rendering the page, so the scroll position and keyboard focus are preserved. A reset button restores every setting to its default.
view.fileItems API is preferred; a DOM fallback handles builds where the API is missing.revealActiveFile and revealStrategy) are migrated automatically to the unified revealMode.The collapse is scheduled in a microtask before the next paint, so the explorer never shows the old tree. When the internal folder API is available, setCollapsed is used and the is-collapsed class is applied immediately as a fast path; the expanded ancestors are mounted in the same frame. A 300 ms accordion-suppression window prevents these programmatic changes from being interpreted as manual gestures.
This is an implementation optimization rather than a user-facing feature. The effect is that a switch completes in one visual step: no old tree, no intermediate height, and no flash of an expanded-then-collapsed chain.
Collapsing a folder also collapses all of its descendant folders.
Expanding a folder collapses every other folder.
After a configurable idle period (5 minutes by default), all expanded folders collapse. The duration is adjustable from 1 to 60 minutes.
The original keys autoCollapseEnabled, inactivityMinutes, and exclusiveAccordionEnabled are retained, so existing values carry over.
| Key | Default | Description |
|---|---|---|
uiLanguage |
auto |
Settings language: follow Obsidian, Chinese, or English. |
collapseOnEditorClick |
on | Collapse when clicking into the editor. |
collapseOnActiveLeafChange |
on | Collapse when the active tab changes. |
collapseOnFileOpen |
on | Collapse when a file opens through other routes. |
keepMode |
path |
Which folders survive a collapse. |
revealMode |
anchor |
Anchor first / always center / only keep visible / no scrolling. |
revealCenterRatio |
40 | Height ratio used when centring (30–55). |
activeFileHighlight |
accent |
Accent fill / left bar / theme default. |
exclusiveAccordionEnabled |
on | Collapse other folders when one is expanded. |
autoCollapseEnabled |
on | Collapse folders after inactivity. |
inactivityMinutes |
5 | Idle minutes before collapsing (1–60). |
debugLogging |
off | Record the switch debug log in memory. |
debugBypass |
off | Observe native behaviour without changing the tree. |
folder-focus-collapse folder into <vault>/.obsidian/plugins/.MIT. This project is derived from Auto Folder Collapse by Dario Casciato; see LICENSE for the license text and NOTICE for the attribution notice.
Folder Focus Collapse 让文件树始终围绕你正在编辑的那篇笔记。笔记被激活时,路径之外的目录自动收拢,当前文件行被带到视口中一个稳定的位置,并以醒目的高亮标出。本插件基于 Auto Folder Collapse 二次开发:原插件的手风琴与闲置折叠行为完整保留,激活聚焦、锚点定位、滚动、高亮与中英文设置均为本插件新增。
三种互相独立的触发方式决定何时围绕当前笔记收拢目录:
收拢范围由「保持展开的目录」决定:
| 保留模式 | 保持展开的目录 |
|---|---|
| 仅当前文件路径 | 只保留当前笔记的祖先目录链。 |
| 路径 + 同级目录 | 祖先目录链,以及笔记所在父目录的直接子文件夹。 |
| 路径 + 当前目录整层 | 笔记所在父目录的整棵子树。 |
以 工作管理/技术沉淀/深度学习框架/笔记.md 为例,三种模式分别保留 工作管理/技术沉淀/深度学习框架、该链条加上 深度学习框架 的直接子文件夹,或 深度学习框架 的整棵子树。三种模式都不会主动展开目录,只决定收拢后哪些目录保持原样。笔记位于库根目录时,整层模式保留整棵树。
这一设计的效果是让文件树只呈现当前工作上下文:无关目录不再占据左侧空间与注意力,用户不需要反复滚动或在多层目录中寻找笔记;同时祖先链保持展开,笔记在库中的位置关系不会丢失。
收拢后不是简单地「让当前文件可见」,而是把它放到一个稳定位置。文件树容器按高度划分出 10% 至 78% 的舒适带;当前路径的首段顶层目录作为锚点,尽量保持在收拢前的屏幕高度。祖先链过长、锚点无法兼顾时,先做最小让步,仍不满足才回退到按比例居中(默认 40%,可在 30% 至 55% 之间调整)。
滚动方式共四档,彼此互斥:
| 滚动方式 | 行为 |
|---|---|
| 顶部锚定优先(默认) | 锁住顶层目录的位置,链条放不下时才居中。 |
| 始终居中 | 每次激活都把当前文件放到设定的高度。 |
| 仅保证可见 | 只在当前文件完全不可见时贴近边缘,使用原生 scrollIntoView。 |
| 不滚动(只收拢) | 只执行收拢,完全不改变滚动位置。 |
滚动写入发生在下一帧;文件行尚未挂载时最多重试两帧。写入后 250 毫秒内监听原生滚动:如果原生「自动显示当前文件」晚到并把文件行推出舒适带,只纠正一次;如果结果已经舒适,则不再与其争夺。分屏中的每个文件树面板独立定位。
锚点优先的滚动定位不只是把文件行带进视口。它的核心效果是保持目标文件行上方那段文件树在屏幕上的位置稳定:收拢改变树高时,顶层目录不会突然上跳或下移,整个左侧栏不产生明显跳动。用户的空间记忆因此保持连续,不需要重新建立「文件在哪里」的位置感,注意力也不会被大幅滚动分散。祖先链过长、锚点无法兼顾时,居中比例仍让文件落在视线自然区域的中部偏上。
三档高亮让当前文件在不依赖滚动位置的情况下也能被一眼找到:
样式仅作用于文件树内的当前文件行,随设置变更或插件卸载成对增删。
高亮不是装饰。它的效果是让当前文件行成为列表中唯一可以立即识别的元素:即使文件落在偏上位置、不在视线正中心,强调色底也能让它在整列文件中脱颖而出,无需逐行扫视。与稳定的定位配合,用户依靠外观即可确认当前笔记,而不必再通过滚动或寻找来确认。
设置页可以跟随 Obsidian 语言,也可以固定为中文或英文。切换语言会重排全部分区标题、设置项、释义与提示文字。其余设置原地更新:例如切换滚动方式时,「居中位置」滑杆只会置灰或恢复,不重建整个设置页,因此滚动位置与键盘焦点都能保留。设置页底部提供恢复默认设置。
view.fileItems 接口,接口缺失时回退到 DOM 方式。revealActiveFile 与 revealStrategy)会自动迁移为统一的 revealMode。收拢在微任务内完成,赶在下一帧绘制之前生效,文件树不会先显示旧形态再跳变。走内部接口时调用 setCollapsed 并立即补上 is-collapsed 类作为快路径,展开的祖先目录在同一帧内挂载完成。整个过程设置 300 毫秒的手风琴抑制窗口,程序化的展开与折叠不会被误判为手动操作。
这属于算法与实现层面的优化,不是一项独立功能。它的效果是让一次切换在视觉上一步到位:不出现旧树形态、不出现中间树高,也不会闪过先展开后折叠的链条。
折叠一个目录时,其内部所有已展开的子目录一并折叠。
手动展开一个目录时,折叠其它目录。
一段时间没有操作后折叠全部已展开的目录,默认 5 分钟,可在 1 至 60 分钟之间调整。
沿用原插件的 autoCollapseEnabled、inactivityMinutes、exclusiveAccordionEnabled 三个键,原有取值直接生效。
| 键 | 默认 | 说明 |
|---|---|---|
uiLanguage |
auto |
设置页语言:跟随 Obsidian、中文或英文。 |
collapseOnEditorClick |
开 | 点击正文时收拢。 |
collapseOnActiveLeafChange |
开 | 切换标签页时收拢。 |
collapseOnFileOpen |
开 | 通过其它方式打开文件时收拢。 |
keepMode |
path |
收拢时保留哪些目录。 |
revealMode |
anchor |
顶部锚定优先 / 始终居中 / 仅保证可见 / 不滚动。 |
revealCenterRatio |
40 | 居中时的行高比例(30–55)。 |
activeFileHighlight |
accent |
强调色底 / 左侧强调条 / 保持主题。 |
exclusiveAccordionEnabled |
开 | 展开一个目录时折叠其它目录。 |
autoCollapseEnabled |
开 | 闲置后自动折叠。 |
inactivityMinutes |
5 | 闲置时长(1–60 分钟)。 |
debugLogging |
关 | 在内存中记录切换调试日志。 |
debugBypass |
关 | 只观测原生行为,不改动文件树。 |
folder-focus-collapse 目录复制到 <库>/.obsidian/plugins/。MIT。本项目派生自 Dario Casciato 的 Auto Folder Collapse,许可证正文见 LICENSE,完整署名见 NOTICE。