fix(file-tree): improve item sorting logic

// ignore-for-release
This commit is contained in:
Cell
2026-01-15 15:32:34 +08:00
parent b4008081d7
commit 03f3500c35
2 changed files with 32 additions and 12 deletions
+31 -11
View File
@@ -1,11 +1,11 @@
{{- /*
Recursive function to render tree from structured data
@param {array} items - Array of tree items
@param {int} depth - Current depth level
@param {int} level - The expand level of the tree (expand all: -1, collapse all: 0)
@param {bool} folderSlash - Whether to append a trailing "/" to folder names
@param {array} ignoreList - List of file or folder names to ignore
@param {array} highlightList - List of file or folder names to highlight
@param {int} [depth=0] - Current depth level
@param {bool} [folderSlash=false] - Whether to append a trailing "/" to folder names
@param {array} [ignoreList] - List of file or folder names to ignore
@param {array} [highlightList] - List of file or folder names to highlight
File tree item structure:
name: string - The name of the file or folder
@@ -14,17 +14,28 @@
*/ -}}
{{- define "render-file-tree" -}}
{{- $items := .items -}}
{{- $depth := .depth -}}
{{- $level := .level -}}
{{- $folderSlash := .folderSlash -}}
{{- $depth := .depth | default 0 -}}
{{- $folderSlash := .folderSlash | default false -}}
{{- $ignoreList := .ignoreList | default slice -}}
{{- $highlightList := .highlightList -}}
{{- $highlightList := .highlightList | default slice -}}
<ul class="file-tree" data-level="{{ $depth }}">
{{- /* Sort items: directories first, then files */ -}}
{{- $dirItems := slice -}}
{{- $fileItems := slice -}}
{{- range $items -}}
{{- if in $ignoreList .name -}}
{{- continue -}}
{{- end -}}
{{- if eq .type "dir" -}}
{{- $dirItems = $dirItems | append . -}}
{{- else -}}
{{- $fileItems = $fileItems | append . -}}
{{- end -}}
{{- end -}}
{{- range (append $fileItems $dirItems) -}}
{{- $isDir := eq .type "dir" -}}
{{- $isCollapsed := $isDir | and (ge $depth $level) | and (ge $level 0) -}}
{{- $isHighlighted := in $highlightList .name -}}
@@ -62,8 +73,17 @@
{{- end -}}
{{- if .items -}}
<div class="file-tree-wrapper">
{{- $options := dict "depth" 0 | merge . -}}
{{- template "render-file-tree" $options -}}
</div>
{{- $ignoreList := .ignoreList | default slice -}}
{{- $notEmpty := false -}}
{{- range .items -}}
{{- if not (in $ignoreList .name) -}}
{{- $notEmpty = true -}}
{{- break -}}
{{- end -}}
{{- end -}}
{{- if $notEmpty -}}
<div class="file-tree-wrapper">
{{- template "render-file-tree" . -}}
</div>
{{- end -}}
{{- end -}}