antd-processor.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // antd-processor.js
  2. import { domProcessor } from './text-processor'
  3. // Ant Design Vue 特定处理器
  4. export const antdProcessor = {
  5. // Ant Design 组件选择器
  6. antdSelectors: [
  7. '.ant-btn', // 按钮
  8. '.ant-btn > span', // 按钮文本
  9. '.ant-modal-title', // 模态框标题
  10. '.ant-menu-item', // 菜单项
  11. '.ant-tabs-tab', // 标签页
  12. '.ant-table-title', // 表格标题
  13. '.ant-pagination-item', // 分页项
  14. '.ant-select-item', // 选择器选项
  15. '.ant-form-item-label > label', // 表单标签
  16. '.ant-card-head-title', // 卡片标题
  17. '.ant-descriptions-item-label', // 描述列表标签
  18. '.ant-descriptions-item-content', // 描述列表内容
  19. '.ant-result-title', // 结果页标题
  20. '.ant-result-subtitle', // 结果页副标题
  21. '.ant-empty-description', // 空状态描述
  22. '.ant-alert-message', // 警告框消息
  23. '.ant-alert-description', // 警告框描述
  24. '.ant-table',
  25. '.ant-table-cell'
  26. ],
  27. // 初始化 DOM 观察器
  28. initObserver() {
  29. if (this.observer) return
  30. this.observer = new MutationObserver((mutations) => {
  31. mutations.forEach((mutation) => {
  32. mutation.addedNodes.forEach((node) => {
  33. if (node.nodeType === Node.ELEMENT_NODE) {
  34. this.processAntdComponent(node)
  35. this.processDynamicContent(node)
  36. }
  37. })
  38. })
  39. })
  40. this.observer.observe(document.body, {
  41. childList: true,
  42. subtree: true,
  43. attributes: true,
  44. attributeFilter: ['title', 'placeholder', 'aria-label']
  45. })
  46. },
  47. // 处理所有 Ant Design 组件
  48. processAntdComponents() {
  49. this.antdSelectors.forEach(selector => {
  50. try {
  51. document.querySelectorAll(selector).forEach(element => {
  52. this.processAntdComponent(element)
  53. })
  54. } catch (error) {
  55. console.warn(`处理选择器 ${selector} 失败:`, error)
  56. }
  57. })
  58. // 处理动态内容
  59. this.processDynamicComponents()
  60. },
  61. // 处理单个 Ant Design 组件
  62. processAntdComponent(element) {
  63. if (!element || element._antdProcessed) return
  64. element._antdProcessed = true
  65. domProcessor.processElement(element)
  66. },
  67. // 处理动态生成的组件
  68. processDynamicComponents() {
  69. // 处理消息提示
  70. this.processSelector('.ant-message .ant-message-notice-content')
  71. // 处理通知
  72. this.processSelector('.ant-notification-notice')
  73. // 处理工具提示
  74. this.processSelector('.ant-tooltip-inner')
  75. // 处理弹出框
  76. this.processSelector('.ant-popover-inner')
  77. },
  78. // 处理选择器
  79. processSelector(selector) {
  80. document.querySelectorAll(selector).forEach(element => {
  81. this.processAntdComponent(element)
  82. })
  83. },
  84. // 处理动态内容
  85. processDynamicContent(element) {
  86. // 处理消息提示
  87. if (element.classList?.contains('ant-message')) {
  88. element.querySelectorAll('.ant-message-notice-content').forEach(notice => {
  89. this.processAntdComponent(notice)
  90. })
  91. }
  92. // 处理通知
  93. if (element.classList?.contains('ant-notification')) {
  94. element.querySelectorAll('.ant-notification-notice').forEach(notice => {
  95. this.processAntdComponent(notice)
  96. })
  97. }
  98. },
  99. // 销毁观察器
  100. destroy() {
  101. if (this.observer) {
  102. this.observer.disconnect()
  103. this.observer = null
  104. }
  105. }
  106. }