为M3 Builder增加保存草稿的功能。
This commit is contained in:
parent
a1f63cd724
commit
0a5d475655
|
@ -23,6 +23,12 @@
|
|||
align-items: center;
|
||||
gap: var(--spacing-s);
|
||||
}
|
||||
.button_row {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
gap: var(--spacing-s);
|
||||
}
|
||||
h5 {
|
||||
font-size: var(--font-size-m);
|
||||
line-height: 1.7em;
|
||||
|
|
|
@ -2,8 +2,13 @@ import { includes, isEmpty, isNil } from 'lodash-es';
|
|||
import { useActionState, useCallback, useMemo, useState } from 'react';
|
||||
import { useColorFunction } from '../../../ColorFunctionContext';
|
||||
import { FloatColorPicker } from '../../../components/FloatColorPicker';
|
||||
import { NotificationType, useNotification } from '../../../components/Notifications';
|
||||
import { ScrollArea } from '../../../components/ScrollArea';
|
||||
import { MaterialDesign3Scheme, MaterialDesign3SchemeStorage } from '../../../material-3-scheme';
|
||||
import {
|
||||
MaterialDesign3Scheme,
|
||||
MaterialDesign3SchemeSource,
|
||||
MaterialDesign3SchemeStorage,
|
||||
} from '../../../material-3-scheme';
|
||||
import { SchemeContent } from '../../../models';
|
||||
import { useUpdateScheme } from '../../../stores/schemes';
|
||||
import { mapToObject } from '../../../utls';
|
||||
|
@ -16,6 +21,7 @@ type M3SchemeBuilderProps = {
|
|||
};
|
||||
|
||||
export function M3SchemeBuilder({ scheme, onBuildCompleted }: M3SchemeBuilderProps) {
|
||||
const { showToast } = useNotification();
|
||||
const { colorFn } = useColorFunction();
|
||||
const updateScheme = useUpdateScheme(scheme.id);
|
||||
const originalColors = useMemo(() => {
|
||||
|
@ -36,40 +42,61 @@ export function M3SchemeBuilder({ scheme, onBuildCompleted }: M3SchemeBuilderPro
|
|||
[originalColors, newColors, deleted],
|
||||
);
|
||||
|
||||
const collectSchemeSource = (formData: FormData): MaterialDesign3SchemeSource => {
|
||||
const sourceColor = formData.get('source') as string;
|
||||
const errorColor = formData.get('error') as string;
|
||||
const customColors: Record<string, string> = {};
|
||||
for (const key of colorKeys) {
|
||||
const name = formData.get(`name_${key}`) as string;
|
||||
const color = formData.get(`color_${key}`) as string;
|
||||
if (isNil(name) || isEmpty(name) || isNil(color) || isEmpty(color)) continue;
|
||||
customColors[name] = color;
|
||||
}
|
||||
|
||||
return {
|
||||
source: isNil(sourceColor) || isEmpty(sourceColor) ? null : sourceColor,
|
||||
error: isNil(errorColor) || isEmpty(errorColor) ? null : errorColor,
|
||||
custom_colors: customColors,
|
||||
};
|
||||
};
|
||||
|
||||
const [, handleDraftAction] = useActionState<Map<string, string>, FormData>(
|
||||
(_state, formData) => {
|
||||
const errMsg = new Map<string, string>();
|
||||
const collectedSource = collectSchemeSource(formData);
|
||||
|
||||
updateScheme((prev) => {
|
||||
prev.schemeStorage.source = collectedSource;
|
||||
return prev;
|
||||
});
|
||||
|
||||
showToast(NotificationType.SUCCESS, 'Scheme draft saved!', 'tabler:device-floppy', 3000);
|
||||
|
||||
return errMsg;
|
||||
},
|
||||
new Map<string, string>(),
|
||||
);
|
||||
const [errMsg, handleSubmitAction] = useActionState<Map<string, string>, FormData>(
|
||||
(_state, formData) => {
|
||||
const errMsg = new Map<string, string>();
|
||||
|
||||
try {
|
||||
const sourceColor = formData.get('source') as string;
|
||||
if (isNil(sourceColor) || isEmpty(sourceColor)) {
|
||||
const collectedSource = collectSchemeSource(formData);
|
||||
if (isNil(collectedSource.source) || isEmpty(collectedSource.source)) {
|
||||
errMsg.set('source', 'Source color is required');
|
||||
}
|
||||
const errorColor = formData.get('error') as string;
|
||||
if (isNil(errorColor) || isEmpty(errorColor)) {
|
||||
if (isNil(collectedSource.error) || isEmpty(collectedSource.error)) {
|
||||
errMsg.set('error', 'Error color is required');
|
||||
}
|
||||
if (!isEmpty(errMsg)) return errMsg;
|
||||
|
||||
const customColors: Record<string, string> = {};
|
||||
for (const key of colorKeys) {
|
||||
const name = formData.get(`name_${key}`) as string;
|
||||
const color = formData.get(`color_${key}`) as string;
|
||||
if (isNil(name) || isEmpty(name) || isNil(color) || isEmpty(color)) continue;
|
||||
customColors[name] = color;
|
||||
}
|
||||
|
||||
const generatedScheme = colorFn?.generate_material_design_3_scheme(
|
||||
sourceColor,
|
||||
errorColor,
|
||||
customColors,
|
||||
collectedSource.source,
|
||||
collectedSource.error,
|
||||
collectedSource.custom_colors,
|
||||
);
|
||||
updateScheme((prev) => {
|
||||
prev.schemeStorage.source = {
|
||||
source: sourceColor as string,
|
||||
error: errorColor as string,
|
||||
custom_colors: customColors,
|
||||
};
|
||||
prev.schemeStorage.source = collectedSource;
|
||||
prev.schemeStorage.scheme = {
|
||||
white: generatedScheme[0].white,
|
||||
black: generatedScheme[0].black,
|
||||
|
@ -155,10 +182,13 @@ export function M3SchemeBuilder({ scheme, onBuildCompleted }: M3SchemeBuilderPro
|
|||
onDelete={(index) => setDeleted((prev) => [...prev, index])}
|
||||
/>
|
||||
))}
|
||||
<div style={{ gridColumn: '2 / span 2' }}>
|
||||
<div className={styles.button_row} style={{ gridColumn: '2 / span 2' }}>
|
||||
<button type="submit" className="primary">
|
||||
Build Scheme
|
||||
</button>
|
||||
<button type="submit" className="secondary" formAction={handleDraftAction}>
|
||||
Save Draft
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</ScrollArea>
|
||||
|
|
Loading…
Reference in New Issue
Block a user