DesignPic_20250812163611.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import React, { useState } from 'react'
  2. import { Button, Table, Input, Message } from '@alifd/next';
  3. export default function DesignPic() {
  4. // 添加状态管理表格数据
  5. const [tableData, setTableData] = useState(() => {
  6. const result = [];
  7. for (let i = 0; i < 5; i++) {
  8. result.push({
  9. title: { name: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible` },
  10. id: 100306660940 + i,
  11. time: 2e3 + i,
  12. // 添加名称和标签字段
  13. picName: '1235',
  14. picLabel: ''
  15. });
  16. }
  17. return result;
  18. });
  19. const pic_render = (value: any, index: any, record: any) => {
  20. return (
  21. <img className='size-20' src={record.url} alt={record.title} />
  22. );
  23. }
  24. // 修改图片名称渲染函数
  25. const name_render = (value: any, index: number, record: any) => {
  26. return <>
  27. <Input.TextArea
  28. value={record.picName}
  29. onChange={(value) => handleNameChange(value, index)}
  30. composition
  31. placeholder={'请输入图片名称'}
  32. />
  33. </>
  34. };
  35. // 修改图片标签渲染函数
  36. const label_render = (value: any, index: number, record: any) => {
  37. return <>
  38. <Input.TextArea
  39. value={record.picLabel}
  40. onChange={(value) => handleLabelChange(value, index)}
  41. composition
  42. placeholder={'请输入图片标签'}
  43. />
  44. </>
  45. };
  46. // 添加名称变化处理函数
  47. const handleNameChange = (value: string, index: number) => {
  48. const newData = [...tableData];
  49. newData[index].picName = value;
  50. setTableData(newData);
  51. };
  52. // 添加标签变化处理函数
  53. const handleLabelChange = (value: string, index: number) => {
  54. const newData = [...tableData];
  55. newData[index].picLabel = value;
  56. setTableData(newData);
  57. };
  58. // 完善删除功能
  59. const handleDelete = (index: number, record: any) => {
  60. // 方案1: 直接删除
  61. const newData = [...tableData];
  62. newData.splice(index, 1);
  63. setTableData(newData);
  64. // 方案2: 带确认的删除 (可选)
  65. // if (window.confirm(`确定要删除 "${record.title.name}" 吗?`)) {
  66. // const newData = [...tableData];
  67. // newData.splice(index, 1);
  68. // setTableData(newData);
  69. // Message.success('删除成功');
  70. // }
  71. };
  72. // 删除操作渲染函数
  73. const option_render = (value: any, index: number, record: any) => {
  74. return (
  75. <a
  76. className='text-red-600 cursor-pointer'
  77. onClick={() => handleDelete(index, record)}
  78. >
  79. 删除
  80. </a>
  81. );
  82. };
  83. // 添加行索引以确保正确删除
  84. const dataSourceWithIndex = tableData.map((item, index) => ({
  85. ...item,
  86. index
  87. }));
  88. const getPicData = async () => {
  89. try {
  90. // 获取当前活动标签页
  91. const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
  92. if (tab.id) {
  93. // 发送消息到content script请求获取tbody数据
  94. const response = await browser.tabs.sendMessage(tab.id, { action: "getTbodyData" });
  95. const table_datas = []
  96. // 在控制台输出结果
  97. if (response.tables && response.tables.length > 0) {
  98. console.log("找到的表格数据(包含背景图片):");
  99. // 显式声明类型
  100. let tables_arr: {
  101. good_info: any;
  102. sku_info: any;
  103. design_info: { region: any; text: any; picture: any }[];
  104. }[] = [];
  105. let table_boj: {
  106. good_info: any;
  107. sku_info: any;
  108. design_info: { region: any; text: any; picture: any }[];
  109. } = {
  110. good_info: null,
  111. sku_info: null,
  112. design_info: []
  113. };
  114. response.tables.forEach((table_node: any, index: number) => {
  115. console.log(`表格 ${index + 1}:`, table_node);
  116. // 长度大于4时,指针改变
  117. if (table_node.length > 4) {
  118. if (table_boj.design_info.length) {
  119. // 一个商品的定制信息
  120. tables_arr.push(table_boj)
  121. // 重置
  122. table_boj = {
  123. good_info: null,
  124. sku_info: null,
  125. design_info: []
  126. }
  127. }
  128. table_boj = {
  129. good_info: table_node[1],
  130. sku_info: table_node[2],
  131. design_info: [{
  132. region: table_node[9],
  133. text: table_node[10],
  134. picture: table_node[11],
  135. }]
  136. }
  137. } else {
  138. table_boj.design_info.push({
  139. region: table_node[0],
  140. text: table_node[1],
  141. picture: table_node[2],
  142. })
  143. }
  144. // good_info sku_info design_info: region text picture
  145. });
  146. // const table_arrs = response.tables // 表格数组
  147. // let i = 0, table_node = table_arrs[i], tables_arr = [];
  148. // let table_boj: {
  149. // good_info: any;
  150. // sku_info: any;
  151. // design_info: { region: any; text: any; picture: any }[];
  152. // } = {
  153. // good_info: null,
  154. // sku_info: null,
  155. // design_info: []
  156. // };
  157. // while (i < response.tables.length) {
  158. // // 长度大于4时,指针改变
  159. // if (table_node.length > 4) {
  160. // if (table_boj.design_info.length) {
  161. // // 一个商品的定制信息
  162. // tables_arr.push(table_boj)
  163. // // 重置
  164. // table_boj = {
  165. // good_info: null,
  166. // sku_info: null,
  167. // design_info: []
  168. // }
  169. // }
  170. // table_boj = {
  171. // good_info: table_node[1],
  172. // sku_info: table_node[2],
  173. // design_info: [{
  174. // region: table_node[9],
  175. // text: table_node[10],
  176. // picture: table_node[11],
  177. // }]
  178. // }
  179. // i++
  180. // table_node = table_arrs[i]
  181. // } else {
  182. // table_boj.design_info.push({
  183. // region: table_node[0],
  184. // text: table_node[1],
  185. // picture: table_node[2],
  186. // })
  187. // i++
  188. // table_node = table_arrs[i]
  189. // }
  190. // }
  191. console.log(tables_arr, "tables_arrddd")
  192. } else {
  193. console.log("页面上未找到表格数据");
  194. }
  195. }
  196. } catch (error) {
  197. console.error("获取表格数据失败:", error);
  198. }
  199. }
  200. return (
  201. <div className='w-3xl h-fit'>
  202. <Button style={{ marginBottom: '10px' }} onClick={() => {
  203. getPicData()
  204. }}>获取图片数据</Button>
  205. <Table dataSource={dataSourceWithIndex}>
  206. <Table.Column title="图片" cell={pic_render} />
  207. <Table.Column title="图片名称" cell={name_render} />
  208. <Table.Column title="图片标签" cell={label_render} />
  209. <Table.Column title="操作" cell={option_render} />
  210. </Table>
  211. </div>
  212. )
  213. }