This commit is contained in:
220
update_context_menu.py
Normal file
220
update_context_menu.py
Normal file
@@ -0,0 +1,220 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
批量为表格页面添加右键菜单和悬浮提示功能
|
||||
"""
|
||||
|
||||
import re
|
||||
import os
|
||||
|
||||
def add_imports(content):
|
||||
"""添加必要的导入"""
|
||||
# 检查是否已经导入
|
||||
if 'useTableContextMenu' in content:
|
||||
return content
|
||||
|
||||
# 找到 useAuth 导入位置
|
||||
import_pattern = r"(import\s+{\s+useAuth\s+}\s+from\s+'@/composables/useAuth')"
|
||||
|
||||
if re.search(import_pattern, content):
|
||||
# 在 useAuth 后面添加 useTableContextMenu
|
||||
content = re.sub(
|
||||
import_pattern,
|
||||
r"\1\n import { useTableContextMenu } from '@/composables/useTableContextMenu'",
|
||||
content
|
||||
)
|
||||
|
||||
# 添加 TableContextMenuHint 组件导入
|
||||
arttable_pattern = r"(import\s+ArtButtonTable\s+from\s+'@/components/core/forms/ArtButtonTable\.vue')"
|
||||
if re.search(arttable_pattern, content):
|
||||
content = re.sub(
|
||||
arttable_pattern,
|
||||
r"\1\n import TableContextMenuHint from '@/components/core/others/TableContextMenuHint.vue'",
|
||||
content
|
||||
)
|
||||
|
||||
# 如果没有 ArtMenuRight,添加它
|
||||
if 'ArtMenuRight' not in content:
|
||||
content = re.sub(
|
||||
arttable_pattern,
|
||||
r"\1\n import ArtMenuRight from '@/components/core/others/ArtMenuRight.vue'\n import type { MenuItemType } from '@/components/core/others/ArtMenuRight.vue'",
|
||||
content
|
||||
)
|
||||
|
||||
return content
|
||||
|
||||
def add_context_menu_usage(content):
|
||||
"""添加 useTableContextMenu 的使用"""
|
||||
if 'useTableContextMenu()' in content:
|
||||
return content
|
||||
|
||||
# 查找 const currentRow = ref 或类似的变量声明
|
||||
pattern = r"(const\s+currentRow\s*=\s*ref[^\n]+)"
|
||||
|
||||
usage_code = """
|
||||
// 使用表格右键菜单功能
|
||||
const {
|
||||
showContextMenuHint,
|
||||
hintPosition,
|
||||
getRowClassName,
|
||||
handleCellMouseEnter,
|
||||
handleCellMouseLeave
|
||||
} = useTableContextMenu()"""
|
||||
|
||||
if re.search(pattern, content):
|
||||
content = re.sub(pattern, r"\1" + usage_code, content)
|
||||
|
||||
return content
|
||||
|
||||
def add_table_events(content):
|
||||
"""为 ArtTable 添加事件监听"""
|
||||
# 查找 ArtTable 标签
|
||||
table_pattern = r"(<ArtTable[^>]*?)(\s*@row-contextmenu=\"[^\"]+\")?([^>]*>)"
|
||||
|
||||
if '@cell-mouse-enter' in content:
|
||||
return content
|
||||
|
||||
# 添加必要的属性和事件
|
||||
def replace_table(match):
|
||||
prefix = match.group(1)
|
||||
existing_contextmenu = match.group(2) or ''
|
||||
suffix = match.group(3)
|
||||
|
||||
# 如果没有 row-class-name,添加它
|
||||
if ':row-class-name' not in prefix and 'row-class-name' not in prefix:
|
||||
prefix += '\n :row-class-name="getRowClassName"'
|
||||
|
||||
# 如果没有 row-contextmenu,添加它
|
||||
if not existing_contextmenu and '@row-contextmenu' not in prefix:
|
||||
existing_contextmenu = '\n @row-contextmenu="handleRowContextMenu"'
|
||||
|
||||
# 添加 cell mouse 事件
|
||||
cell_events = '\n @cell-mouse-enter="handleCellMouseEnter"\n @cell-mouse-leave="handleCellMouseLeave"'
|
||||
|
||||
return prefix + existing_contextmenu + cell_events + suffix
|
||||
|
||||
content = re.sub(table_pattern, replace_table, content, flags=re.DOTALL)
|
||||
|
||||
return content
|
||||
|
||||
def add_hint_component(content):
|
||||
"""添加悬浮提示组件"""
|
||||
if 'TableContextMenuHint' in content and ':visible="showContextMenuHint"' in content:
|
||||
return content
|
||||
|
||||
# 在 </ArtTable> 后添加提示组件
|
||||
table_end_pattern = r"(</ArtTable>)"
|
||||
|
||||
hint_component = r"""\1
|
||||
|
||||
<!-- 鼠标悬浮提示 -->
|
||||
<TableContextMenuHint :visible="showContextMenuHint" :position="hintPosition" />"""
|
||||
|
||||
content = re.sub(table_end_pattern, hint_component, content)
|
||||
|
||||
return content
|
||||
|
||||
def add_context_menu_component(content):
|
||||
"""添加右键菜单组件"""
|
||||
if 'ArtMenuRight' in content and 'contextMenuRef' in content:
|
||||
return content
|
||||
|
||||
# 在提示组件后添加右键菜单
|
||||
hint_pattern = r"(<TableContextMenuHint[^>]+/>)"
|
||||
|
||||
menu_component = r"""\1
|
||||
|
||||
<!-- 右键菜单 -->
|
||||
<ArtMenuRight
|
||||
ref="contextMenuRef"
|
||||
:menu-items="contextMenuItems"
|
||||
:menu-width="120"
|
||||
@select="handleContextMenuSelect"
|
||||
/>"""
|
||||
|
||||
content = re.sub(hint_pattern, menu_component, content)
|
||||
|
||||
return content
|
||||
|
||||
def add_css_styles(content):
|
||||
"""添加 CSS 样式"""
|
||||
if 'table-row-with-context-menu' in content:
|
||||
return content
|
||||
|
||||
# 查找 <style> 标签
|
||||
style_pattern = r"(<style[^>]*>)"
|
||||
|
||||
css_code = r"""\1
|
||||
:deep(.el-table__row.table-row-with-context-menu) {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
"""
|
||||
|
||||
content = re.sub(style_pattern, css_code, content)
|
||||
|
||||
return content
|
||||
|
||||
def process_file(file_path):
|
||||
"""处理单个文件"""
|
||||
print(f"Processing: {file_path}")
|
||||
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
original_content = content
|
||||
|
||||
# 执行所有转换
|
||||
content = add_imports(content)
|
||||
content = add_context_menu_usage(content)
|
||||
content = add_table_events(content)
|
||||
content = add_hint_component(content)
|
||||
content = add_context_menu_component(content)
|
||||
content = add_css_styles(content)
|
||||
|
||||
# 如果内容有变化,写回文件
|
||||
if content != original_content:
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
print(f" ✓ Updated")
|
||||
return True
|
||||
else:
|
||||
print(f" - No changes needed")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f" ✗ Error: {e}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
# 定义需要处理的文件列表
|
||||
files_to_process = [
|
||||
"src/views/package-management/package-list/index.vue",
|
||||
"src/views/account-management/account/index.vue",
|
||||
"src/views/account-management/enterprise-customer/index.vue",
|
||||
"src/views/account-management/enterprise-cards/index.vue",
|
||||
"src/views/asset-management/iot-card-management/index.vue",
|
||||
"src/views/asset-management/iot-card-task/index.vue",
|
||||
"src/views/asset-management/device-task/index.vue",
|
||||
"src/views/asset-management/asset-assign/index.vue",
|
||||
"src/views/asset-management/authorization-records/index.vue",
|
||||
"src/views/finance/commission/agent-commission/index.vue",
|
||||
]
|
||||
|
||||
base_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
updated_count = 0
|
||||
for file_rel_path in files_to_process:
|
||||
file_path = os.path.join(base_dir, file_rel_path)
|
||||
if os.path.exists(file_path):
|
||||
if process_file(file_path):
|
||||
updated_count += 1
|
||||
else:
|
||||
print(f"File not found: {file_path}")
|
||||
|
||||
print(f"\n完成! 更新了 {updated_count} 个文件")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user