Skip to content

Commit

Permalink
Merge pull request #959 from merico-dev/958-add-vertical-alignment-op…
Browse files Browse the repository at this point in the history
…tion-to-viz-stats

958 add vertical alignment option to viz stats
  • Loading branch information
GerilLeto authored May 24, 2023
2 parents f26c370 + ab6335e commit b2061d9
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 33 deletions.
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/api",
"version": "9.2.1",
"version": "9.3.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/dashboard",
"version": "9.2.1",
"version": "9.3.0",
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/plugins/viz-components/stats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { VizStatsEditor } from './viz-stats-editor';
export const StatsVizComponent: VizComponent = {
createConfig() {
return {
version: 2,
version: 3,
config: DEFAULT_CONFIG,
};
},
Expand Down
13 changes: 7 additions & 6 deletions dashboard/src/plugins/viz-components/stats/type.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export const DEFAULT_CONFIG: IVizStatsConf = {
align: 'center',
template: 'The variable ${value} is defined in Variables section',
};

export interface IVizStatsConf {
align: 'center';
template: string;
vertical_align: 'top' | 'center' | 'bottom';
horizontal_align: 'left' | 'center' | 'right';
}
export const DEFAULT_CONFIG: IVizStatsConf = {
template: 'The variable ${value} is defined in Variables section',
vertical_align: 'center',
horizontal_align: 'left',
};
15 changes: 15 additions & 0 deletions dashboard/src/plugins/viz-components/stats/update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ function updateSchema2(legacyConf: IVizStatsConf & { variables: ITemplateVariabl
return omit(legacyConf, ['variables']);
}

// align -> horizontal_align
// add vertical_align
function v3(legacyConf: $TSFixMe): IVizStatsConf {
const { align, ...rest } = legacyConf;
return {
horizontal_align: align,
vertical_align: 'center',
...rest,
};
}

/**
* used when moving variables from stats to `panel.variables`
*/
Expand Down Expand Up @@ -96,5 +107,9 @@ export class VizStatsMigrator extends VersionBasedMigrator {
});
return { ...data, version: 2, config: updateSchema2(config) };
});
this.version(3, (data) => {
const { config } = data;
return { ...data, version: 3, config: v3(config) };
});
}
}
33 changes: 21 additions & 12 deletions dashboard/src/plugins/viz-components/stats/viz-stats-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActionIcon, Group, Select, Stack, Text } from '@mantine/core';
import { ActionIcon, Group, Select, SimpleGrid, Stack, Text } from '@mantine/core';
import React from 'react';
import { DeviceFloppy } from 'tabler-icons-react';
import { VizConfigProps } from '~/types/plugin';
Expand All @@ -14,15 +14,17 @@ const horizontalAlignmentOptions = [
{ label: 'Right', value: 'right' },
];

const verticalAlignmentOptions = [
{ label: 'Top', value: 'top' },
{ label: 'Center', value: 'center' },
{ label: 'Bottom', value: 'bottom' },
];

export function VizStatsEditor({ context }: VizConfigProps) {
const { value: conf, set: setConf } = useStorageData<IVizStatsConf>(context.instanceData, 'config');

const defaultValues = React.useMemo(() => {
const { align, template = '' } = defaultsDeep({}, conf, DEFAULT_CONFIG);
return {
template,
align,
};
return defaultsDeep({}, conf, DEFAULT_CONFIG);
}, [conf]);

const { control, handleSubmit, watch, getValues, reset } = useForm<IVizStatsConf>({ defaultValues });
Expand All @@ -31,7 +33,7 @@ export function VizStatsEditor({ context }: VizConfigProps) {
reset(defaultValues);
}, [defaultValues]);

watch(['template', 'align']);
watch(['template', 'horizontal_align', 'vertical_align']);
const values = getValues();
const changed = React.useMemo(() => {
return !_.isEqual(values, conf);
Expand All @@ -51,11 +53,18 @@ export function VizStatsEditor({ context }: VizConfigProps) {
control={control}
render={({ field }) => <TemplateInput label="Template" py="md" sx={{ flexGrow: 1 }} {...field} />}
/>
<Controller
name="align"
control={control}
render={({ field }) => <Select label="Horizontal Alignment" data={horizontalAlignmentOptions} {...field} />}
/>
<SimpleGrid cols={2}>
<Controller
name="horizontal_align"
control={control}
render={({ field }) => <Select label="Horizontal Alignment" data={horizontalAlignmentOptions} {...field} />}
/>
<Controller
control={control}
name="vertical_align"
render={({ field }) => <Select label="Vertical Alignment" data={verticalAlignmentOptions} {...field} />}
/>
</SimpleGrid>
</form>
</Stack>
);
Expand Down
36 changes: 29 additions & 7 deletions dashboard/src/plugins/viz-components/stats/viz-stats.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useMemo } from 'react';
import { Text } from '@mantine/core';
import { Box, Center, Text } from '@mantine/core';

import { VizViewProps } from '~/types/plugin';
import { templateToJSX } from '~/utils/template';
Expand All @@ -9,11 +9,24 @@ import { observer } from 'mobx-react-lite';
import { useContentModelContext } from '~/contexts';
import _ from 'lodash';

const horizontalAlignments = {
left: 'flex-start',
center: 'center',
right: 'flex-end',
};

const verticalAlignments = {
top: 'flex-start',
center: 'center',
bottom: 'flex-end',
};

export const VizStats = observer(({ context }: VizViewProps) => {
const contentModel = useContentModelContext();
const { value: conf = DEFAULT_CONFIG } = useStorageData<IVizStatsConf>(context.instanceData, 'config');
const { variables } = context;
const { template, align } = conf;
const { template, horizontal_align, vertical_align } = conf;
const { width, height } = context.viewport;

const semiTemplate = useMemo(() => {
try {
Expand All @@ -32,10 +45,19 @@ export const VizStats = observer(({ context }: VizViewProps) => {
}, [semiTemplate, variables, context.data, context]);

return (
<Text align={align}>
{Object.values(contents).map((c, i) => (
<React.Fragment key={i}>{c}</React.Fragment>
))}
</Text>
<Center
sx={{
width,
height,
justifyContent: horizontalAlignments[horizontal_align],
alignItems: verticalAlignments[vertical_align],
}}
>
<Box>
{Object.values(contents).map((c, i) => (
<React.Fragment key={i}>{c}</React.Fragment>
))}
</Box>
</Center>
);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/root",
"version": "9.2.1",
"version": "9.3.0",
"private": true,
"workspaces": [
"api",
Expand Down
2 changes: 1 addition & 1 deletion settings-form/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtable/settings-form",
"version": "9.2.1",
"version": "9.3.0",
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
Expand Down
6 changes: 3 additions & 3 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
"name": "@devtable/website",
"private": true,
"license": "Apache-2.0",
"version": "9.2.1",
"version": "9.3.0",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"@ahooksjs/use-url-state": "^3.3.11",
"@devtable/dashboard": "9.2.1",
"@devtable/settings-form": "9.2.1",
"@devtable/dashboard": "9.3.0",
"@devtable/settings-form": "9.3.0",
"@emotion/react": "11.10.6",
"@faker-js/faker": "7.6.0",
"@mantine/core": "5.9.5",
Expand Down

0 comments on commit b2061d9

Please sign in to comment.