Skip to content

Commit 6d62d34

Browse files
committed
feat: implement alt copy feature in trends chart
1 parent 63b166a commit 6d62d34

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

app/components/Package/TrendsChart.vue

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import type { VueUiXyConfig, VueUiXyDatasetItem } from 'vue-data-ui'
2+
import type { Theme as VueDataUiTheme, VueUiXyConfig, VueUiXyDatasetItem } from 'vue-data-ui'
33
import { VueUiXy } from 'vue-data-ui/vue-ui-xy'
44
import { useDebounceFn, useElementSize } from '@vueuse/core'
55
import { useCssVariables } from '~/composables/useColors'
@@ -50,6 +50,8 @@ const props = withDefaults(
5050
5151
const { locale } = useI18n()
5252
const { accentColors, selectedAccentColor } = useAccentColor()
53+
const { copy, copied } = useClipboard()
54+
5355
const colorMode = useColorMode()
5456
const resolvedMode = shallowRef<'light' | 'dark'>('light')
5557
const rootEl = shallowRef<HTMLElement | null>(null)
@@ -321,6 +323,7 @@ const {
321323
fetchPackageLikesEvolution,
322324
fetchRepoContributorsEvolution,
323325
fetchRepoRefsForPackages,
326+
copyAltTextForTrendLineChart,
324327
} = useCharts()
325328
326329
const repoRefsByPackage = shallowRef<Record<string, RepoRef | null>>({})
@@ -1406,7 +1409,7 @@ function drawSvgPrintLegend(svg: Record<string, any>) {
14061409
// VueUiXy chart component configuration
14071410
const chartConfig = computed<VueUiXyConfig>(() => {
14081411
return {
1409-
theme: isDarkMode.value ? 'dark' : '',
1412+
theme: isDarkMode.value ? 'dark' : ('' as VueDataUiTheme),
14101413
chart: {
14111414
height: isMobile.value ? 950 : 600,
14121415
backgroundColor: colors.value.bg,
@@ -1418,14 +1421,14 @@ const chartConfig = computed<VueUiXyConfig>(() => {
14181421
fullscreen: false,
14191422
table: false,
14201423
tooltip: false,
1421-
altCopy: false, // TODO: set to true to enable the alt copy feature
1424+
altCopy: true,
14221425
},
14231426
buttonTitles: {
14241427
csv: $t('package.trends.download_file', { fileType: 'CSV' }),
14251428
img: $t('package.trends.download_file', { fileType: 'PNG' }),
14261429
svg: $t('package.trends.download_file', { fileType: 'SVG' }),
14271430
annotator: $t('package.trends.toggle_annotator'),
1428-
altCopy: undefined, // TODO: set to proper translation key
1431+
altCopy: $t('package.trends.copy_alt.button_label'), // Do not make this text dependant on the `copied` variable, since this would re-render the component, which is undesirable if the minimap was used to select a time frame.
14291432
},
14301433
callbacks: {
14311434
img: args => {
@@ -1458,10 +1461,20 @@ const chartConfig = computed<VueUiXyConfig>(() => {
14581461
loadFile(url, buildExportFilename('svg'))
14591462
URL.revokeObjectURL(url)
14601463
},
1461-
// altCopy: ({ dataset: dst, config: cfg }: { dataset: Array<VueUiXyDatasetItem>; config: VueUiXyConfig}) => {
1462-
// // TODO: implement a reusable copy-alt-text-to-clipboard feature based on the dataset & configuration
1463-
// console.log({ dst, cfg})
1464-
// }
1464+
altCopy: ({ dataset: dst, config: cfg }) =>
1465+
copyAltTextForTrendLineChart({
1466+
dataset: dst,
1467+
config: {
1468+
...cfg,
1469+
formattedDatasetValues: (dst?.lines || []).map(d =>
1470+
d.series.map(n => compactNumberFormatter.value.format(n ?? 0)),
1471+
),
1472+
hasEstimation:
1473+
supportsEstimation.value && !isEndDateOnPeriodEnd.value && !isZoomed.value,
1474+
granularity: displayedGranularity.value,
1475+
copy,
1476+
},
1477+
}),
14651478
},
14661479
},
14671480
grid: {
@@ -1870,7 +1883,10 @@ watch(selectedMetric, value => {
18701883
</template>
18711884
<template #optionAltCopy>
18721885
<span
1873-
class="i-lucide:person-standing w-6 h-6 text-fg-subtle"
1886+
class="w-6 h-6"
1887+
:class="
1888+
copied ? 'i-lucide:check text-accent' : 'i-lucide:person-standing text-fg-subtle'
1889+
"
18741890
style="pointer-events: none"
18751891
aria-hidden="true"
18761892
/>

0 commit comments

Comments
 (0)