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. 43
      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 {
@override @override
String get title => name; String get title => name;
@override
bool get isVisible => true;
FileNode({ FileNode({
required this.name, required this.name,
required this.path, required this.path,

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

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

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

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save