-
Notifications
You must be signed in to change notification settings - Fork 33
feat: add export tab to settings dialog #3976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4d386bd
feat: add export tab to settings dialog
tpoisseau fe79905
feat: add advanced mode fields
tpoisseau ac24a2b
fix: types and move ExportFields in a dedicated file
tpoisseau 880d4bb
refactor: use radio group integration
tpoisseau 32ec814
fix: use named functions to not have to disable `react-hooks/rules-of…
tpoisseau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
238 changes: 238 additions & 0 deletions
238
src/component/modal/setting/tanstack_general_settings/tabs/export_tab.fields.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| import { Checkbox, Tag } from '@blueprintjs/core'; | ||
| import { useStore } from '@tanstack/react-form'; | ||
| import type { PageSizeName, Unit } from '@zakodium/nmrium-core'; | ||
| import { units } from '@zakodium/nmrium-core'; | ||
| import { memo, useMemo } from 'react'; | ||
| import { FormGroup, assertUnreachable, withFieldGroup } from 'react-science/ui'; | ||
| import type { z } from 'zod/v4'; | ||
|
|
||
| import { convertToPixels } from '../../../../elements/export/units.ts'; | ||
| import { useExportConfigurer } from '../../../../elements/export/useExportConfigurer.tsx'; | ||
| import { | ||
| getExportDefaultOptionsByMode, | ||
| getExportOptions, | ||
| } from '../../../../elements/export/utilities/getExportOptions.ts'; | ||
| import { pageSizes } from '../../../../elements/print/pageSize.ts'; | ||
| import { | ||
| defaultGeneralSettingsFormValues, | ||
| exportSettingsValidation, | ||
| } from '../validation.ts'; | ||
|
|
||
| type Mode = 'basic' | 'advance'; | ||
| type Layout = 'portrait' | 'landscape'; | ||
|
|
||
| interface SelectItem<Value extends string> { | ||
| label: string; | ||
| value: Value; | ||
| } | ||
|
|
||
| const pageSizeItems: Record<Layout, Array<SelectItem<PageSizeName>>> = { | ||
| portrait: pageSizes.map((item) => ({ | ||
| value: item.name, | ||
| label: `${item.name} (${item.portrait.width} cm x ${item.portrait.height} cm)`, | ||
| })), | ||
| landscape: pageSizes.map((item) => ({ | ||
| value: item.name, | ||
| label: `${item.name} (${item.landscape.width} cm x ${item.landscape.height} cm)`, | ||
| })), | ||
| }; | ||
| const modeItems: Array<SelectItem<Mode>> = [ | ||
| { label: 'Basic', value: 'basic' }, | ||
| { label: 'Advanced', value: 'advance' }, | ||
| ]; | ||
| const unitItems: Array<SelectItem<Unit>> = units.map((u) => ({ | ||
| label: u.name, | ||
| value: u.unit, | ||
| })); | ||
| const layoutItems: Array<SelectItem<Layout>> = [ | ||
| { value: 'portrait', label: 'Portrait' }, | ||
| { value: 'landscape', label: 'Landscape' }, | ||
| ]; | ||
|
|
||
| export const ExportFields = withFieldGroup({ | ||
| defaultValues: defaultGeneralSettingsFormValues.export.png, | ||
| render: function ExportFields({ group }) { | ||
| const inputValues = useStore(group.store, (s) => s.values); | ||
| const outputValues = useMemo(() => { | ||
| return exportSettingsValidation.decode(inputValues); | ||
| }, [inputValues]); | ||
| const advancedTransforms = useExportConfigurer(outputValues); | ||
|
|
||
| function onModeChange({ value }: { value: Mode }) { | ||
| const newOptions = getExportDefaultOptionsByMode(value); | ||
|
|
||
| for (const [key, value] of Object.entries(newOptions)) { | ||
| group.setFieldValue(key as keyof typeof newOptions, value, { | ||
| dontRunListeners: true, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| function onChangeUnit({ value }: { value: Unit }) { | ||
| const { width, height } = advancedTransforms.changeUnit({ unit: value }); | ||
| group.setFieldValue('width', String(width), { | ||
| dontRunListeners: true, | ||
| }); | ||
| group.setFieldValue('height', String(height), { | ||
| dontRunListeners: true, | ||
| }); | ||
| } | ||
| function onWidthChange({ value }: { value: string }) { | ||
| const height = advancedTransforms.changeSize( | ||
| Number(value), | ||
| 'height', | ||
| 'width', | ||
| ); | ||
| if (!advancedTransforms.isAspectRatioEnabled) { | ||
| return; | ||
| } | ||
| group.setFieldValue('height', String(height), { | ||
| dontRunListeners: true, | ||
| }); | ||
| } | ||
| function onHeightChange({ value }: { value: string }) { | ||
| const width = advancedTransforms.changeSize( | ||
| Number(value), | ||
| 'width', | ||
| 'height', | ||
| ); | ||
| if (!advancedTransforms.isAspectRatioEnabled) { | ||
| return; | ||
| } | ||
| group.setFieldValue('width', String(width), { | ||
| dontRunListeners: true, | ||
| }); | ||
| } | ||
|
|
||
| function onDPIChange({ value }: { value: string }) { | ||
| if (group.state.values.mode !== 'advance') return; | ||
| if (group.state.values.unit !== 'px') return; | ||
|
|
||
| const { width, height } = advancedTransforms.changeDPI(Number(value)); | ||
| group.setFieldValue('width', String(width), { | ||
| dontRunListeners: true, | ||
| }); | ||
| group.setFieldValue('height', String(height), { | ||
| dontRunListeners: true, | ||
| }); | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <group.AppField name="mode" listeners={{ onChange: onModeChange }}> | ||
| {(field) => <field.RadioGroup label="Mode" options={modeItems} />} | ||
| </group.AppField> | ||
| <DescriptionPreview {...outputValues} /> | ||
| <group.Subscribe | ||
| selector={(state) => { | ||
| const mode = state.values.mode; | ||
| switch (state.values.mode) { | ||
| case 'basic': | ||
| return { mode: state.values.mode, layout: state.values.layout }; | ||
| case 'advance': | ||
| return { mode: state.values.mode, unit: state.values.unit }; | ||
| default: | ||
| assertUnreachable(mode as never); | ||
| } | ||
| }} | ||
| > | ||
| {({ mode, layout, unit }) => { | ||
| switch (mode) { | ||
| case 'basic': | ||
| return ( | ||
| <> | ||
| <group.AppField name="size"> | ||
| {(field) => ( | ||
| <field.Select | ||
| label="Size" | ||
| items={pageSizeItems[layout]} | ||
| /> | ||
| )} | ||
| </group.AppField> | ||
| <group.AppField name="layout"> | ||
| {(field) => ( | ||
| <field.RadioGroup | ||
| label="Layout" | ||
| options={layoutItems} | ||
| /> | ||
| )} | ||
| </group.AppField> | ||
| </> | ||
| ); | ||
| case 'advance': | ||
| return ( | ||
| <> | ||
| <group.AppField | ||
| name="unit" | ||
| listeners={{ onChange: onChangeUnit }} | ||
| > | ||
| {(field) => ( | ||
| <field.Select label="Unit" items={unitItems} /> | ||
| )} | ||
| </group.AppField> | ||
| <Checkbox | ||
| label="Keep ratio" | ||
| checked={advancedTransforms.isAspectRatioEnabled} | ||
| onChange={(event) => { | ||
| advancedTransforms.enableAspectRatio( | ||
| event.currentTarget.checked, | ||
| ); | ||
| }} | ||
| /> | ||
| <group.AppField | ||
| name="width" | ||
| listeners={{ onChange: onWidthChange }} | ||
| > | ||
| {(field) => ( | ||
| <field.NumericInput | ||
| label="Width" | ||
| rightElement={<Tag>{unit}</Tag>} | ||
| /> | ||
| )} | ||
| </group.AppField> | ||
| <group.AppField | ||
| name="height" | ||
| listeners={{ onChange: onHeightChange }} | ||
| > | ||
| {(field) => ( | ||
| <field.NumericInput | ||
| label="Height" | ||
| rightElement={<Tag>{unit}</Tag>} | ||
| /> | ||
| )} | ||
| </group.AppField> | ||
| </> | ||
| ); | ||
| default: | ||
| assertUnreachable(mode); | ||
| } | ||
| }} | ||
| </group.Subscribe> | ||
| <group.AppField name="dpi" listeners={{ onChange: onDPIChange }}> | ||
| {(field) => <field.NumericInput label="DPI" />} | ||
| </group.AppField> | ||
| <group.AppField name="useDefaultSettings"> | ||
| {(field) => ( | ||
| <field.Checkbox label="Don't show the options dialog during export and use current settings" /> | ||
| )} | ||
| </group.AppField> | ||
| </> | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| type DescriptionPreviewProps = z.output<typeof exportSettingsValidation>; | ||
| const DescriptionPreview = memo(function DescriptionPreview( | ||
| props: DescriptionPreviewProps, | ||
| ) { | ||
| const { width, height, dpi, unit } = getExportOptions(props); | ||
| const convertOptions = { precision: 0 }; | ||
| const widthInPixel = convertToPixels(width, unit, dpi, convertOptions); | ||
| const heightInPixel = convertToPixels(height, unit, dpi, convertOptions); | ||
|
|
||
| return ( | ||
| <FormGroup label="Description"> | ||
| <Tag>{`${widthInPixel} px x ${heightInPixel} px @ ${dpi}DPI`}</Tag> | ||
| </FormGroup> | ||
| ); | ||
| }); |
26 changes: 26 additions & 0 deletions
26
src/component/modal/setting/tanstack_general_settings/tabs/export_tab.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { withForm } from 'react-science/ui'; | ||
|
|
||
| import { defaultGeneralSettingsFormValues } from '../validation.ts'; | ||
|
|
||
| import { ExportFields } from './export_tab.fields.tsx'; | ||
|
|
||
| export const ExportTab = withForm({ | ||
| defaultValues: defaultGeneralSettingsFormValues, | ||
| render: ({ form }) => { | ||
| return ( | ||
| <> | ||
| <div style={{ marginTop: '-15px' }}> | ||
| <form.Section title="Export SVG to file options"> | ||
| <ExportFields form={form} fields="export.svg" /> | ||
| </form.Section> | ||
| </div> | ||
| <form.Section title="Export PNG to file options"> | ||
| <ExportFields form={form} fields="export.png" /> | ||
| </form.Section> | ||
| <form.Section title="Export PNG to clipboard options"> | ||
| <ExportFields form={form} fields="export.clipboard" /> | ||
| </form.Section> | ||
| </> | ||
| ); | ||
| }, | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.