修正大部分的编译错误。
This commit is contained in:
@@ -10,7 +10,12 @@ import { ScrollArea } from '../../../components/ScrollArea';
|
||||
import { Switch } from '../../../components/Switch';
|
||||
import { SchemeContent } from '../../../models';
|
||||
import { useUpdateScheme } from '../../../stores/schemes';
|
||||
import { QSwatchEntry, QSwatchSchemeSetting, SwatchSchemeStorage } from '../../../swatch_scheme';
|
||||
import {
|
||||
QSwatchEntry,
|
||||
QSwatchSchemeSetting,
|
||||
SwatchScheme,
|
||||
SwatchSchemeStorage,
|
||||
} from '../../../swatch_scheme';
|
||||
import { mapToObject } from '../../../utls';
|
||||
import { ColorEntry, IdenticalColorEntry } from '../ColorEntry';
|
||||
import styles from './Builder.module.css';
|
||||
@@ -64,75 +69,78 @@ export function SwatchSchemeBuilder({ scheme, onBuildCompleted }: SwatchSchemeBu
|
||||
return null;
|
||||
}, [scheme.schemeStorage.source]);
|
||||
|
||||
const [errMsg, handleSubmitAction] = useActionState((state, formData) => {
|
||||
const errMsg = new Map<string, string>();
|
||||
const [errMsg, handleSubmitAction] = useActionState<Map<string, string>, FormData>(
|
||||
(_state, formData) => {
|
||||
const errMsg = new Map<string, string>();
|
||||
|
||||
try {
|
||||
const swatchAmount = Number(formData.get('amount'));
|
||||
if (isNaN(swatchAmount) || swatchAmount <= 0) {
|
||||
errMsg.set('amount', 'MUST be a positive number');
|
||||
}
|
||||
if (swatchAmount > 30) {
|
||||
errMsg.set('amount', 'MUST be less than 30');
|
||||
try {
|
||||
const swatchAmount = Number(formData.get('amount'));
|
||||
if (isNaN(swatchAmount) || swatchAmount <= 0) {
|
||||
errMsg.set('amount', 'MUST be a positive number');
|
||||
}
|
||||
if (swatchAmount > 30) {
|
||||
errMsg.set('amount', 'MUST be less than 30');
|
||||
}
|
||||
|
||||
const minLightness = Number(formData.get('min_lightness'));
|
||||
if (isNaN(minLightness) || minLightness < 0 || minLightness > 100) {
|
||||
errMsg.set('min', 'MUST be a number between 0 and 100');
|
||||
}
|
||||
|
||||
const maxLightness = Number(formData.get('max_lightness'));
|
||||
if (isNaN(maxLightness) || maxLightness < 0 || maxLightness > 100) {
|
||||
errMsg.set('max', 'MUST be a number between 0 and 100');
|
||||
}
|
||||
|
||||
const includePrimary = isEqual(formData.get('include_primary'), 'true');
|
||||
const darkConvertChroma = Number(formData.get('dark_chroma')) / 100.0;
|
||||
const darkConvertLightness = Number(formData.get('dark_lightness')) / 100.0;
|
||||
|
||||
const swatchSetting = new SwatchSchemeSetting(
|
||||
swatchAmount,
|
||||
minLightness / 100.0,
|
||||
maxLightness / 100.0,
|
||||
includePrimary,
|
||||
new ColorShifting(darkConvertChroma, darkConvertLightness),
|
||||
);
|
||||
const dumpedSettings = swatchSetting.toJsValue() as QSwatchSchemeSetting;
|
||||
const entries: SwatchEntry[] = [];
|
||||
for (const key of colorKeys) {
|
||||
const name = String(formData.get(`name_${key}`));
|
||||
const color = String(formData.get(`color_${key}`));
|
||||
if (isEmpty(name) || isEmpty(color)) continue;
|
||||
entries.push(new SwatchEntry(name, color));
|
||||
}
|
||||
const dumpedEntries = entries.map((entry) => entry.toJsValue() as QSwatchEntry);
|
||||
if (isEmpty(entries)) {
|
||||
errMsg.set('color', 'At least one color is required');
|
||||
}
|
||||
|
||||
if (!isEmpty(errMsg)) return errMsg;
|
||||
|
||||
const generatedScheme = colorFn?.generate_swatch_scheme(entries, swatchSetting);
|
||||
console.debug('[generated scheme]', generatedScheme);
|
||||
updateScheme((prev) => {
|
||||
prev.schemeStorage.source = {
|
||||
colors: dumpedEntries,
|
||||
setting: dumpedSettings,
|
||||
};
|
||||
prev.schemeStorage.scheme = mapToObject(generatedScheme[0]) as SwatchScheme;
|
||||
prev.schemeStorage.cssVariables = generatedScheme[1];
|
||||
prev.schemeStorage.scssVariables = generatedScheme[2];
|
||||
prev.schemeStorage.jsVariables = generatedScheme[3];
|
||||
return prev;
|
||||
});
|
||||
|
||||
onBuildCompleted?.();
|
||||
} catch (e) {
|
||||
console.error('[build swatch scheme]', e);
|
||||
}
|
||||
|
||||
const minLightness = Number(formData.get('min_lightness'));
|
||||
if (isNaN(minLightness) || minLightness < 0 || minLightness > 100) {
|
||||
errMsg.set('min', 'MUST be a number between 0 and 100');
|
||||
}
|
||||
|
||||
const maxLightness = Number(formData.get('max_lightness'));
|
||||
if (isNaN(maxLightness) || maxLightness < 0 || maxLightness > 100) {
|
||||
errMsg.set('max', 'MUST be a number between 0 and 100');
|
||||
}
|
||||
|
||||
const includePrimary = isEqual(formData.get('include_primary'), 'true');
|
||||
const darkConvertChroma = Number(formData.get('dark_chroma')) / 100.0;
|
||||
const darkConvertLightness = Number(formData.get('dark_lightness')) / 100.0;
|
||||
|
||||
const swatchSetting = new SwatchSchemeSetting(
|
||||
swatchAmount,
|
||||
minLightness / 100.0,
|
||||
maxLightness / 100.0,
|
||||
includePrimary,
|
||||
new ColorShifting(darkConvertChroma, darkConvertLightness),
|
||||
);
|
||||
const dumpedSettings = swatchSetting.toJsValue() as QSwatchSchemeSetting;
|
||||
const entries: SwatchEntry[] = [];
|
||||
for (const key of colorKeys) {
|
||||
const name = String(formData.get(`name_${key}`));
|
||||
const color = String(formData.get(`color_${key}`));
|
||||
if (isEmpty(name) || isEmpty(color)) continue;
|
||||
entries.push(new SwatchEntry(name, color));
|
||||
}
|
||||
const dumpedEntries = entries.map((entry) => entry.toJsValue() as QSwatchEntry);
|
||||
if (isEmpty(entries)) {
|
||||
errMsg.set('color', 'At least one color is required');
|
||||
}
|
||||
|
||||
if (!isEmpty(errMsg)) return errMsg;
|
||||
|
||||
const generatedScheme = colorFn?.generate_swatch_scheme(entries, swatchSetting);
|
||||
console.debug('[generated scheme]', generatedScheme);
|
||||
updateScheme((prev) => {
|
||||
prev.schemeStorage.source = {
|
||||
colors: dumpedEntries,
|
||||
setting: dumpedSettings,
|
||||
};
|
||||
prev.schemeStorage.scheme = mapToObject(generatedScheme[0]);
|
||||
prev.schemeStorage.cssVariables = generatedScheme[1];
|
||||
prev.schemeStorage.scssVariables = generatedScheme[2];
|
||||
prev.schemeStorage.jsVariables = generatedScheme[3];
|
||||
return prev;
|
||||
});
|
||||
|
||||
onBuildCompleted?.();
|
||||
} catch (e) {
|
||||
console.error('[build swatch scheme]', e);
|
||||
}
|
||||
|
||||
return errMsg;
|
||||
}, new Map<string, string>());
|
||||
return errMsg;
|
||||
},
|
||||
new Map<string, string>(),
|
||||
);
|
||||
|
||||
return (
|
||||
<ScrollArea enableY>
|
||||
|
@@ -54,12 +54,12 @@ export function SwatchSchemePreview({ scheme }: SwatchSchemePreviewProps) {
|
||||
<h2>Light Scheme</h2>
|
||||
<SchemeBlock
|
||||
amount={scheme.schemeStorage.source?.setting?.amount ?? 0}
|
||||
scheme={scheme.schemeStorage.scheme.light}
|
||||
scheme={scheme.schemeStorage.scheme!.light}
|
||||
/>
|
||||
<h2>Dark Scheme</h2>
|
||||
<SchemeBlock
|
||||
amount={scheme.schemeStorage.source?.setting?.amount ?? 0}
|
||||
scheme={scheme.schemeStorage.scheme.dark}
|
||||
scheme={scheme.schemeStorage.scheme!.dark}
|
||||
/>
|
||||
</div>
|
||||
</ScrollArea>
|
||||
|
Reference in New Issue
Block a user