Преглед изворни кода

feat: 增强文本选择器功能,支持搜索和下拉选择,优化用户体验

zhch158_admin пре 1 месец
родитељ
комит
4a1f58b5ee
1 измењених фајлова са 64 додато и 17 уклоњено
  1. 64 17
      ocr_validator_layout.py

+ 64 - 17
ocr_validator_layout.py

@@ -308,27 +308,74 @@ class OCRLayoutManager:
         left_col, right_col = st.columns([layout['content_width'], layout['sidebar_width']], vertical_alignment='top', border=True)
 
         with left_col:
-            # 快速定位文本选择器
+            # 快速定位文本选择器 - 增强版(搜索+下拉)
             if self.validator.text_bbox_mapping:
+                # 搜索输入框
+                search_col, select_col = st.columns([1, 2])
+                
+                # 初始化session state
+                if "compact_search_query" not in st.session_state:
+                    st.session_state.compact_search_query = None
+                
+                with search_col:
+                    search_query = st.text_input(
+                        "搜索文本",
+                        placeholder="输入关键词...",
+                        value=st.session_state.compact_search_query,
+                        key=f"{layout_type}_search_input",
+                        label_visibility="collapsed"
+                    )
+                    # 更新session state
+                    st.session_state.compact_search_query = search_query
+                
+                # 构建选项列表
                 text_options = ["请选择文本..."]
                 text_display = ["请选择文本..."]
                 
                 for text, info_list in self.validator.text_bbox_mapping.items():
+                    # 如果有搜索条件,进行过滤
+                    if search_query and search_query.strip():
+                        if search_query.lower() not in text.lower():
+                            continue  # 跳过不匹配的项
+                    
                     text_options.append(text)
-                    display_text = text[:47] + "..." if len(text) > 50 else text
+                    
+                    # 检查是否是表格单元格
+                    if info_list and isinstance(info_list[0], dict):
+                        first_info = info_list[0]
+                        if 'row' in first_info and 'col' in first_info:
+                            display_text = f"[R{first_info['row']},C{first_info['col']}] {text}"
+                            if len(display_text) > 47:
+                                display_text = display_text[:44] + "..."
+                        else:
+                            display_text = text[:47] + "..." if len(text) > 50 else text
+                    else:
+                        display_text = text[:47] + "..." if len(text) > 50 else text
+                        
                     text_display.append(display_text)
                 
-                selected_index = st.selectbox(
-                    "快速定位文本",
-                    range(len(text_options)),
-                    format_func=lambda x: text_display[x],
-                    label_visibility="collapsed",
-                    key="compact_quick_text_selector"
-                )
+                # 显示匹配数量
+                if search_query and search_query.strip():
+                    st.caption(f"找到 {len(text_options)-1} 个匹配项")
+                
+                # 确定默认选中的索引
+                default_index = 0
+                if st.session_state.selected_text and st.session_state.selected_text in text_options:
+                    default_index = text_options.index(st.session_state.selected_text)
+                
+                with select_col:
+                    selected_index = st.selectbox(
+                        "快速定位文本",
+                        range(len(text_options)),
+                        index=default_index,
+                        format_func=lambda x: text_display[x] if x < len(text_display) else "",
+                        label_visibility="collapsed",
+                        key=f"{layout_type}_quick_text_selector"
+                    )
                 
                 if selected_index > 0:
                     st.session_state.selected_text = text_options[selected_index]
-            
+        
             # 处理并显示OCR内容 - 只高亮选中的文本
             if self.validator.md_content:
                 highlighted_content = self.validator.md_content
@@ -343,7 +390,7 @@ class OCRLayoutManager:
                         )
                 
                 self.render_content_by_mode(highlighted_content, "HTML渲染", font_size, container_height, layout_type)
-    
+        
         with right_col:
             # 修复的对齐图片显示
             self.create_aligned_image_display(zoom_level, "compact")
@@ -371,10 +418,8 @@ class OCRLayoutManager:
                 self.show_all_boxes = show_all_boxes
 
         with col2:
-            # if st.button("应用手动角度", key=f"{layout_type}_apply_manual"):
             if st.button("🔄 旋转90度", type="secondary", key=f"{layout_type}_manual_angle"):
                 self.rotated_angle = (self.rotated_angle + 90) % 360
-                # st.success(f"已设置旋转角度为 {manual_angle}")
                 # 需要清除图片缓存,以及text_bbox_mapping中的bbox
                 self.clear_image_cache()
                 self.validator.process_data()
@@ -395,8 +440,11 @@ class OCRLayoutManager:
                 st.rerun()
  
         with col5:
-            if st.button("🧹 清除选择"):
+            if st.button("🧹 清除选择", key=f"{layout_type}_clear_selection"):
+                # 清除选中的文本
                 st.session_state.selected_text = None
+                # 清除搜索框内容
+                st.session_state.compact_search_query = None
                 st.rerun()
 
         # 使用增强的图像加载方法
@@ -419,13 +467,12 @@ class OCRLayoutManager:
                         'filename': 'ocr_image',
                         'height': None,  # 使用当前高度
                         'width': None,   # 使用当前宽度
-                            'scale': 1
-                        }
+                        'scale': 1
+                    }
                 }
                 
                 st.plotly_chart(
                     fig, 
-                    # use_container_width=fit_to_container,
                     use_container_width=False,
                     config=plot_config,
                     key=f"{layout_type}_plot"