feat(视图): 表格右键复制指定单元格内容 #5884

This commit is contained in:
wisonic-s 2024-02-28 14:46:44 +08:00
parent eeb81c981e
commit e2eec7b05e
4 changed files with 36 additions and 3 deletions

View File

@ -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<TableSheet> {
// 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 })

View File

@ -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<TableSheet> {
})
// 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 })

View File

@ -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<PivotSheet> {
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 })

View File

@ -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)
}
}