Skip to content

Commit 5e0d1bc

Browse files
authored
Refactor code and fix warning (#524)
1 parent 39f32f4 commit 5e0d1bc

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

packages/ui/src/components/Designer/RightSidebar/DetailView/index.tsx

+29-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import FormRender, { useForm } from 'form-render';
2-
import React, { useContext, useEffect, useState } from 'react';
2+
import React, { useContext, useEffect } from 'react';
33
import type { ChangeSchemaItem, Dict, SchemaForUI, PropPanelWidgetProps, PropPanelSchema } from '@pdfme/common';
44
import type { SidebarProps } from '../../../../types';
55
import { MenuOutlined } from '@ant-design/icons';
@@ -14,31 +14,31 @@ import { InternalNamePath, ValidateErrorEntity } from "rc-field-form/es/interfac
1414
const { Text } = Typography;
1515

1616
type DetailViewProps = Pick<SidebarProps,
17-
'size' | 'schemas' | 'pageSize' | 'changeSchemas' | 'activeElements' | 'deselectSchema'
17+
'size' | 'schemas' | 'pageSize' | 'changeSchemas' | 'activeElements' | 'deselectSchema'
1818
> & {
1919
activeSchema: SchemaForUI;
2020
};
2121

2222
const DetailView = (props: DetailViewProps) => {
2323
const { token } = theme.useToken();
2424

25-
const { size, changeSchemas, deselectSchema, activeSchema, activeElements } = props;
25+
const { size, changeSchemas, deselectSchema, activeSchema } = props;
2626
const form = useForm();
2727

2828
const i18n = useContext(I18nContext);
2929
const pluginsRegistry = useContext(PluginsRegistry);
3030
const options = useContext(OptionsContext);
3131

32-
const [widgets, setWidgets] = useState<{
33-
[key: string]: (props: PropPanelWidgetProps) => React.JSX.Element;
34-
}>({});
32+
useEffect(() => {
33+
const values: any = { ...activeSchema };
34+
// [position] Change the nested position object into a flat, as a three-column layout is difficult to implement
35+
values.x = values.position.x;
36+
values.y = values.position.y;
37+
delete values.position;
38+
form.setValues(values);
39+
40+
}, [activeSchema, form]);
3541

36-
const values: any = { ...activeSchema };
37-
// [position] Change the nested position object into a flat, as a three-column layout is difficult to implement
38-
values.x = values.position.x;
39-
values.y = values.position.y;
40-
delete values.position;
41-
form.setValues(values);
4242

4343
const handleWatch = (formSchema: any) => {
4444
const formAndSchemaValuesDiffer = (formValue: any, schemaValue: any): boolean => {
@@ -67,7 +67,7 @@ const DetailView = (props: DetailViewProps) => {
6767
// FIXME memo: https://github.com/pdfme/pdfme/pull/367#issuecomment-1857468274
6868
if (value === null && ['rotate', 'opacity'].includes(key)) value = undefined;
6969

70-
changes.push({key, value, schemaId: activeSchema.id});
70+
changes.push({ key, value, schemaId: activeSchema.id });
7171
}
7272
}
7373

@@ -76,11 +76,11 @@ const DetailView = (props: DetailViewProps) => {
7676
form.validateFields()
7777
.then(() => changeSchemas(changes))
7878
.catch((reason: ValidateErrorEntity) => {
79-
if (reason.errorFields.length) {
79+
if (reason.errorFields.length) {
8080
changes = changes.filter((change: ChangeSchemaItem) => !reason.errorFields.find((field: {
81-
name: InternalNamePath;
82-
errors: string[];
83-
}) => field.name.includes(change.key)
81+
name: InternalNamePath;
82+
errors: string[];
83+
}) => field.name.includes(change.key)
8484
));
8585
}
8686
if (changes.length) {
@@ -181,25 +181,27 @@ Check this document: https://pdfme.com/docs/custom-schemas`);
181181
};
182182
}
183183

184-
const allWidgets: typeof widgets = {
184+
const allWidgets: {
185+
[key: string]: (props: PropPanelWidgetProps) => React.JSX.Element;
186+
} = {
185187
AlignWidget: (p) => <AlignWidget {...p} {...props} options={options} />,
186188
Divider: () => (
187-
<Divider style={{ marginTop: token.marginXS, marginBottom: token.marginXS }} />
189+
<Divider style={{ marginTop: token.marginXS, marginBottom: token.marginXS }} />
188190
),
189191
ButtonGroup: (p) => <ButtonGroupWidget {...p} {...props} options={options} />,
190192
};
191193
for (const plugin of Object.values(pluginsRegistry)) {
192194
const widgets = plugin?.propPanel.widgets || {};
193195
Object.entries(widgets).forEach(([widgetKey, widgetValue]) => {
194196
allWidgets[widgetKey] = (p) => (
195-
<WidgetRenderer
196-
{...p}
197-
{...props}
198-
options={options}
199-
theme={token}
200-
i18n={i18n as (key: keyof Dict | string) => string}
201-
widget={widgetValue}
202-
/>
197+
<WidgetRenderer
198+
{...p}
199+
{...props}
200+
options={options}
201+
theme={token}
202+
i18n={i18n as (key: keyof Dict | string) => string}
203+
widget={widgetValue}
204+
/>
203205
);
204206
});
205207
}

packages/ui/src/components/Designer/index.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { I18nContext, PluginsRegistry } from '../../contexts';
1919
import {
2020
schemasList2template,
2121
uuid,
22+
round,
2223
cloneDeep,
2324
template2SchemasList,
2425
getPagesScrollTopByIndex,
@@ -51,7 +52,7 @@ const TemplateEditor = ({
5152
onSaveTemplate: (t: Template) => void;
5253
onChangeTemplate: (t: Template) => void;
5354
} & {
54-
onChangeTemplate: (t: Template) => void
55+
onChangeTemplate: (t: Template) => void
5556
onPageCursorChange: (newPageCursor: number) => void
5657
}) => {
5758
const past = useRef<SchemaForUI[][]>([]);
@@ -253,7 +254,7 @@ const TemplateEditor = ({
253254
const moveY = (event.delta.y - canvasTopOffsetFromPageCorner) / scale;
254255
const moveX = (event.delta.x - canvasLeftOffsetFromPageCorner) / scale;
255256

256-
const position = { x: px2mm(Math.max(0, moveX)), y: px2mm(Math.max(0, moveY)) }
257+
const position = { x: round(px2mm(Math.max(0, moveX)), 2), y: round(px2mm(Math.max(0, moveY)), 2) }
257258

258259
addSchema({ ...(active.data.current as Schema), position });
259260
}}

packages/ui/src/helper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -489,4 +489,4 @@ export const changeSchemas = (args: {
489489
return acc;
490490
}, cloneDeep(schemas));
491491
commitSchemas(newSchemas);
492-
};
492+
};

0 commit comments

Comments
 (0)