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