Browse Source

树节点搜索已经OK

master
hejl 4 weeks ago
parent
commit
63ddcf865f
  1. 3
      win_text_editor/lib/framework/models/file_node.dart
  2. 3
      win_text_editor/lib/modules/data_format/models/template_node.dart
  3. 4
      win_text_editor/lib/modules/outline/controllers/outline_provider.dart
  4. 9
      win_text_editor/lib/modules/outline/models/outline_node.dart
  5. 41
      win_text_editor/lib/modules/outline/widgets/outline_explorer.dart
  6. 6
      win_text_editor/lib/shared/components/tree_view.dart
  7. 3
      win_text_editor/lib/shared/models/template_node.dart

3
win_text_editor/lib/framework/models/file_node.dart

@ -19,6 +19,9 @@ class FileNode implements TreeNode { @@ -19,6 +19,9 @@ class FileNode implements TreeNode {
@override
String get title => name;
@override
bool get isVisible => true;
FileNode({
required this.name,
required this.path,

3
win_text_editor/lib/modules/data_format/models/template_node.dart

@ -40,6 +40,9 @@ class TemplateNode implements TreeNode { @@ -40,6 +40,9 @@ class TemplateNode implements TreeNode {
@override
String get id => path;
@override
bool get isVisible => true;
}
enum NodeType { element, attribute, text }

4
win_text_editor/lib/modules/outline/controllers/outline_provider.dart

@ -8,8 +8,8 @@ class OutlineProvider with ChangeNotifier { @@ -8,8 +8,8 @@ class OutlineProvider with ChangeNotifier {
bool _isLoading = true;
String? _currentRootPath; //
static OutlineNode rootNode = OutlineNode(
name: "所有",
title: "所有",
name: "所有Tag",
title: "所有Tag",
value: 'UFTTable',
frequency: 0,
isDirectory: true,

9
win_text_editor/lib/modules/outline/models/outline_node.dart

@ -23,6 +23,11 @@ class OutlineNode implements TreeNode { @@ -23,6 +23,11 @@ class OutlineNode implements TreeNode {
bool isExpanded;
final int frequency;
late String uuid;
@override
bool isVisible;
final String value;
String? wordClass;
@ -37,14 +42,16 @@ class OutlineNode implements TreeNode { @@ -37,14 +42,16 @@ class OutlineNode implements TreeNode {
this.wordClass,
List<OutlineNode>? children,
this.title = "",
this.isVisible = true,
}) : _children = children ?? [] {
uuid = DateTime.now().microsecondsSinceEpoch.toRadixString(36);
for (var child in _children) {
child.parent = this;
}
}
@override
String get id => '$depth-$name';
String get id => uuid;
//
@override

41
win_text_editor/lib/modules/outline/widgets/outline_explorer.dart

@ -48,31 +48,38 @@ class _OutlineExplorerState extends State<OutlineExplorer> { @@ -48,31 +48,38 @@ class _OutlineExplorerState extends State<OutlineExplorer> {
// -
List<OutlineNode> _filterNodes(List<OutlineNode> nodes) {
if (_searchQuery.isEmpty) {
return nodes;
void resetVisibility(List<OutlineNode> nodes) {
for (final node in nodes) {
node.isVisible = true;
if (node.isDirectory) {
resetVisibility(node.children);
}
}
}
//
List<OutlineNode> recursiveFilter(List<OutlineNode> nodesToFilter, int currentDepth) {
final List<OutlineNode> result = [];
for (final node in nodesToFilter) {
// (01)
if (node.title.toLowerCase().contains(_searchQuery.toLowerCase())) {
result.add(node);
void applySearchFilter(List<OutlineNode> nodes, String query) {
for (final node in nodes) {
//
if (node.isDirectory && node.depth < 2) {
applySearchFilter(node.children, query);
}
//
if (node.isDirectory && node.children.isNotEmpty) {
result.addAll(recursiveFilter(node.children, currentDepth + 1));
//
final bool isMatch = node.title.toLowerCase().contains(query.toLowerCase());
final bool hasVisibleChild =
node.depth < 2 && node.isDirectory && node.children.any((child) => child.isVisible);
node.isVisible = isMatch || hasVisibleChild;
}
}
return result;
if (_searchQuery.isEmpty) {
resetVisibility(nodes);
return nodes;
}
// 0
return recursiveFilter(nodes, 0);
applySearchFilter(nodes, _searchQuery);
return nodes;
}
@override
@ -94,7 +101,7 @@ class _OutlineExplorerState extends State<OutlineExplorer> { @@ -94,7 +101,7 @@ class _OutlineExplorerState extends State<OutlineExplorer> {
controller: _searchController,
style: const TextStyle(fontSize: 12), // 10
decoration: InputDecoration(
hintText: '搜索节点...',
hintText: '搜索Tag...',
hintStyle: const TextStyle(fontSize: 12), // 10
prefixIcon: const Icon(
Icons.search,

6
win_text_editor/lib/shared/components/tree_view.dart

@ -10,6 +10,7 @@ abstract class TreeNode { @@ -10,6 +10,7 @@ abstract class TreeNode {
List<TreeNode> get children;
int get depth;
IconData? get iconData;
bool get isVisible;
}
///
@ -141,6 +142,7 @@ class _TreeViewState extends State<TreeView> { @@ -141,6 +142,7 @@ class _TreeViewState extends State<TreeView> {
int _countVisibleNodes(List<TreeNode> nodes) {
int count = 0;
for (final node in nodes) {
if (!node.isVisible) continue; //
count++;
if (node.isDirectory && node.isExpanded) {
count += _countVisibleNodes(node.children);
@ -152,8 +154,11 @@ class _TreeViewState extends State<TreeView> { @@ -152,8 +154,11 @@ class _TreeViewState extends State<TreeView> {
TreeNode _getVisibleNode(List<TreeNode> nodes, int index) {
int current = 0;
for (final node in nodes) {
if (!node.isVisible) continue; //
if (current == index) return node;
current++;
if (node.isDirectory && node.isExpanded) {
final childCount = _countVisibleNodes(node.children);
if (index - current < childCount) {
@ -187,6 +192,7 @@ class TreeNodeWidget extends StatelessWidget { @@ -187,6 +192,7 @@ class TreeNodeWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
if (!node.isVisible) return const SizedBox.shrink(); //
return InkWell(
onTap: onTap,
onDoubleTap: onDoubleTap,

3
win_text_editor/lib/shared/models/template_node.dart

@ -21,6 +21,9 @@ class TemplateNode implements TreeNode { @@ -21,6 +21,9 @@ class TemplateNode implements TreeNode {
@override
String get title => name;
@override
bool get isVisible => true;
TemplateNode({
required this.name,
required this.children,

Loading…
Cancel
Save