From e2eec7b05e4386e16e1041ab5676f226f6962961 Mon Sep 17 00:00:00 2001 From: wisonic-s Date: Wed, 28 Feb 2024 14:46:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=A7=86=E5=9B=BE):=20=E8=A1=A8=E6=A0=BC?= =?UTF-8?q?=E5=8F=B3=E9=94=AE=E5=A4=8D=E5=88=B6=E6=8C=87=E5=AE=9A=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=A0=BC=E5=86=85=E5=AE=B9=20#5884?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js/panel/charts/table/table-info.ts | 3 ++ .../js/panel/charts/table/table-normal.ts | 4 ++- .../js/panel/charts/table/table-pivot.ts | 4 ++- .../js/panel/common/common_table.ts | 28 ++++++++++++++++++- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-info.ts b/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-info.ts index 91800b02bf..63239b5133 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-info.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-info.ts @@ -5,6 +5,7 @@ import { S2ChartView, S2DrawOptions } from '../../types/impl/s2' import { TABLE_EDITOR_PROPERTY, TABLE_EDITOR_PROPERTY_INNER } from './common' import { useI18n } from '@/hooks/web/useI18n' import { isNumber } from 'lodash-es' +import { copyContent } from '@/views/chart/components/js/panel/common/common_table' const { t } = useI18n() @@ -161,6 +162,8 @@ export class TableInfo extends S2ChartView { // header resize newChart.on(S2Event.LAYOUT_RESIZE_COL_WIDTH, ev => resizeAction(ev)) + // right click + newChart.on(S2Event.GLOBAL_CONTEXT_MENU, event => copyContent(newChart, event, meta)) // theme const customTheme = this.configTheme(chart) newChart.setThemeCfg({ theme: customTheme }) diff --git a/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-normal.ts b/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-normal.ts index 0b88390ea7..ba5ee4075a 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-normal.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-normal.ts @@ -2,7 +2,7 @@ import { S2ChartView, S2DrawOptions } from '@/views/chart/components/js/panel/ty import { S2Event, S2Options, TableSheet, TableColCell } from '@antv/s2' import { parseJson } from '@/views/chart/components/js/util' import { formatterItem, valueFormatter } from '@/views/chart/components/js/formatter' -import { getCurrentField } from '@/views/chart/components/js/panel/common/common_table' +import { copyContent, getCurrentField } from '@/views/chart/components/js/panel/common/common_table' import { TABLE_EDITOR_PROPERTY, TABLE_EDITOR_PROPERTY_INNER } from './common' import { useI18n } from '@/hooks/web/useI18n' import { isNumber } from 'lodash-es' @@ -155,6 +155,8 @@ export class TableNormal extends S2ChartView { }) // header resize newChart.on(S2Event.LAYOUT_RESIZE_COL_WIDTH, ev => resizeAction(ev)) + // right click + newChart.on(S2Event.GLOBAL_CONTEXT_MENU, event => copyContent(newChart, event, meta)) // theme const customTheme = this.configTheme(chart) newChart.setThemeCfg({ theme: customTheme }) diff --git a/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-pivot.ts b/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-pivot.ts index 915993f697..8a79f602ec 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-pivot.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-pivot.ts @@ -5,6 +5,7 @@ import { S2ChartView, S2DrawOptions } from '../../types/impl/s2' import { TABLE_EDITOR_PROPERTY_INNER } from './common' import { useI18n } from '@/hooks/web/useI18n' import { maxBy, merge, minBy } from 'lodash-es' +import { copyContent } from '../../common/common_table' const { t } = useI18n() @@ -189,7 +190,8 @@ export class TablePivot extends S2ChartView { s2.on(S2Event.DATA_CELL_CLICK, ev => this.dataCellClickAction(chart, ev, s2, action)) s2.on(S2Event.ROW_CELL_CLICK, ev => this.headerCellClickAction(chart, ev, s2, action)) s2.on(S2Event.COL_CELL_CLICK, ev => this.headerCellClickAction(chart, ev, s2, action)) - + // right click + s2.on(S2Event.GLOBAL_CONTEXT_MENU, event => copyContent(s2, event, meta)) // theme const customTheme = this.configTheme(chart) s2.setThemeCfg({ theme: customTheme }) diff --git a/core/core-frontend/src/views/chart/components/js/panel/common/common_table.ts b/core/core-frontend/src/views/chart/components/js/panel/common/common_table.ts index 1b6ca6aad5..d21ff51469 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/common/common_table.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/common/common_table.ts @@ -14,7 +14,7 @@ import { S2Options, SERIES_NUMBER_FIELD } from '@antv/s2' -import { keys, intersection, filter, cloneDeep, merge } from 'lodash-es' +import { keys, intersection, filter, cloneDeep, merge, find } from 'lodash-es' import { createVNode, render } from 'vue' import TableTooltip from '@/views/chart/components/editor/common/TableTooltip.vue' @@ -658,3 +658,29 @@ export function configTooltip(option: S2Options) { } ] } + +export function copyContent(s2Instance, event, fieldMap) { + event.preventDefault() + const cell = s2Instance.getCell(event.target) + const valueField = cell.getMeta().valueField + const cellMeta = cell.getMeta() + let content + // 单元格 + if (cellMeta?.data) { + const value = cellMeta.data[valueField] + const metaObj = find(fieldMap, m => m.field === valueField) + content = value?.toString() + if (metaObj) { + content = metaObj.formatter(value) + } + } else { + // 列头&行头 + content = cellMeta.value + if (fieldMap?.[content]) { + content = fieldMap[content] + } + } + if (content) { + navigator.clipboard.writeText(content) + } +}