Skip to content

Commit 0579013

Browse files
committed
Feat(sidebar_enhance): Add setting HIDDEN_FOLDER_PATTERNS to allow customization of hiding folders (#1121)
1 parent 0dd0c5e commit 0579013

File tree

7 files changed

+55
-16
lines changed

7 files changed

+55
-16
lines changed

plugin/global/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,7 @@
10731073
"$label.CTRL_WHEEL_TO_SCROLL_SIDEBAR": "Ctrl+Wheel: Scroll Sidebar Horizontally",
10741074
"$label.KEEP_OUTLINE_FOLD_STATE": "Keep Outline Fold State",
10751075
"$label.SORTABLE_OUTLINE": "Sortable Outline",
1076+
"$label.HIDDEN_FOLDER_PATTERNS": "Hide Specific Folders (Regex)",
10761077
"$label.CUSTOMIZE_SIDEBAR_ICONS": "Customize Sidebar File Icons",
10771078
"$label.SIDEBAR_ICONS.enable": "Enable",
10781079
"$label.SIDEBAR_ICONS.extensions": "Extensions",

plugin/global/locales/zh-CN.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,7 @@
10731073
"$label.CTRL_WHEEL_TO_SCROLL_SIDEBAR": "Ctrl+Wheel:横向滚动侧边栏",
10741074
"$label.KEEP_OUTLINE_FOLD_STATE": "记忆大纲折叠状态",
10751075
"$label.SORTABLE_OUTLINE": "大纲拖拽排序",
1076+
"$label.HIDDEN_FOLDER_PATTERNS": "文件树隐藏指定文件夹(正则)",
10761077
"$label.CUSTOMIZE_SIDEBAR_ICONS": "定制侧边栏文件图标",
10771078
"$label.SIDEBAR_ICONS.enable": "启用",
10781079
"$label.SIDEBAR_ICONS.extensions": "扩展名",

plugin/global/locales/zh-TW.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,7 @@
10731073
"$label.CTRL_WHEEL_TO_SCROLL_SIDEBAR": "Ctrl+Wheel:橫向滾動側邊欄",
10741074
"$label.KEEP_OUTLINE_FOLD_STATE": "记忆大綱折疊狀態",
10751075
"$label.SORTABLE_OUTLINE": "大綱拖拽排序",
1076+
"$label.HIDDEN_FOLDER_PATTERNS": "檔案樹隱藏指定文件夾(正規)",
10761077
"$label.CUSTOMIZE_SIDEBAR_ICONS": "定制側邊欄檔案圖示",
10771078
"$label.SIDEBAR_ICONS.enable": "啟用",
10781079
"$label.SIDEBAR_ICONS.extensions": "副檔名",

plugin/global/settings/settings.default.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,10 @@ KEEP_OUTLINE_FOLD_STATE = true
17281728
# false: 不保持
17291729
SORTABLE_OUTLINE = true
17301730

1731+
# ------------------- 隐藏指定文件夹 -------------------
1732+
# 在侧边栏列表隐藏指定文件夹(正则)
1733+
HIDDEN_FOLDER_PATTERNS = []
1734+
17311735
# ------------------- 显示其他扩展名文件 -------------------
17321736
# 支持在侧边栏显示侧边栏列表显示其他扩展名文件
17331737
# true: 支持

plugin/preferences/rules.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ module.exports = {
6969
HIGHLIGHT_PATTERN: [required, regex],
7070
HIGHLIGHT_LINE_COLOR: required,
7171
},
72+
sidebar_enhance: {
73+
HIDDEN_FOLDER_PATTERNS: [required, regex],
74+
},
7275
text_stylize: {
7376
"DEFAULT_COLORS.FOREGROUND": [required, hexColor],
7477
"DEFAULT_COLORS.BACKGROUND": [required, hexColor],

plugin/preferences/schemas.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,9 @@ const conf_sidebar_enhance = [
11791179
Switch("KEEP_OUTLINE_FOLD_STATE", { tooltip: "canCollapseOutlinePanel" }),
11801180
Switch("SORTABLE_OUTLINE"),
11811181
),
1182+
UntitledBox(
1183+
Array_Inline("HIDDEN_FOLDER_PATTERNS"),
1184+
),
11821185
UntitledBox(
11831186
Switch("DISPLAY_NON_MARKDOWN_FILES"),
11841187
Array_Inline("OPEN_BY_TYPORA_EXT", { dependencies: { DISPLAY_NON_MARKDOWN_FILES: true } }),

plugin/sidebar_enhance.js

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class SidebarEnhancePlugin extends BasePlugin {
22
process = () => {
3-
if (this.config.DISPLAY_NON_MARKDOWN_FILES && File.SupportedFiles) {
3+
const displayNonMarkdownFiles = File.SupportedFiles && this.config.DISPLAY_NON_MARKDOWN_FILES
4+
if (displayNonMarkdownFiles) {
45
this._displayNonMarkdownFiles()
5-
if (this.config.CUSTOMIZE_SIDEBAR_ICONS) {
6-
this._customizeSidebarIcons()
7-
}
6+
}
7+
if (this.config.HIDDEN_FOLDER_PATTERNS.length || (displayNonMarkdownFiles && this.config.CUSTOMIZE_SIDEBAR_ICONS)) {
8+
this._rerenderOutlineNode()
89
}
910
if (this.config.KEEP_OUTLINE_FOLD_STATE && File.option.canCollapseOutlinePanel) {
1011
this._keepOutlineFoldState()
@@ -41,20 +42,45 @@ class SidebarEnhancePlugin extends BasePlugin {
4142
})
4243
}
4344

44-
_customizeSidebarIcons = () => {
45-
const ICONS = new Map(
46-
this.config.SIDEBAR_ICONS
47-
.filter(item => item.enable && item.extensions.length && item.extensions.every(ext => !!ext))
48-
.flatMap(item => item.extensions.map(ext => [`.${ext.trim()}`, item.icon.trim()]))
49-
)
50-
this.utils.decorate(() => File?.editor?.library?.fileTree, "renderNode", null, ($node, info) => {
51-
if (!info.isFile) return
52-
const ext = this.utils.Package.Path.extname(info.name)
53-
const icon = ICONS.get(ext)
54-
if (icon) {
55-
$node.find(".file-node-icon").removeClass("fa fa-file-text-o").addClass(icon)
45+
_rerenderOutlineNode = () => {
46+
const getCustomFileIcons = () => {
47+
const ICONS = new Map(
48+
this.config.SIDEBAR_ICONS
49+
.filter(item => item.enable && item.extensions.length && item.extensions.every(ext => !!ext))
50+
.flatMap(item => item.extensions.map(ext => [`.${ext.trim()}`, item.icon.trim()]))
51+
)
52+
const fn = ($node, info) => {
53+
if (!info.isFile) return
54+
const ext = this.utils.Package.Path.extname(info.name)
55+
const icon = ICONS.get(ext)
56+
if (icon) $node.find(".file-node-icon").removeClass("fa fa-file-text-o").addClass(icon)
57+
}
58+
return this.config.CUSTOMIZE_SIDEBAR_ICONS && ICONS.size ? fn : this.utils.identity
59+
}
60+
61+
const getHideFolders = () => {
62+
const newRegexp = p => {
63+
try {
64+
return new RegExp(p)
65+
} catch (e) {
66+
}
67+
}
68+
const REGEXPS = this.config.HIDDEN_FOLDER_PATTERNS.map(newRegexp).filter(Boolean)
69+
const fn = ($node, info) => {
70+
if (info.isDirectory && REGEXPS.some(reg => reg.test(info.name))) {
71+
$node.addClass("plugin-common-hidden")
72+
}
5673
}
74+
return REGEXPS.length ? fn : this.utils.identity
75+
}
76+
77+
const customizeFileIcons = getCustomFileIcons()
78+
const hideFolders = getHideFolders()
79+
this.utils.decorate(() => File?.editor?.library?.fileTree, "renderNode", null, ($node, info) => {
80+
customizeFileIcons($node, info)
81+
hideFolders($node, info)
5782
})
83+
5884
this.utils.eventHub.once(this.utils.eventHub.eventType.fileOpened, () => File.editor.library.refreshPanelCommand())
5985
}
6086

0 commit comments

Comments
 (0)