@10x-media/fields 0.1.0-beta.0 → 0.1.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +173 -0
- package/README.md +8 -4
- package/dist/exports/client.d.ts +10 -1
- package/dist/exports/client.js +9 -1
- package/dist/exports/color-utils.d.ts +4 -2
- package/dist/exports/color-utils.js +3 -1
- package/dist/exports/color.d.ts +5 -2
- package/dist/exports/color.js +4 -1
- package/dist/exports/encrypted.d.ts +7 -4
- package/dist/exports/encrypted.js +6 -3
- package/dist/exports/icon-adapters/radix.d.ts +2 -2
- package/dist/exports/icon-adapters/radix.js +2 -2
- package/dist/exports/icon-react.d.ts +2 -2
- package/dist/exports/icon.d.ts +6 -2
- package/dist/exports/icon.js +6 -2
- package/dist/exports/measurement-utils.d.ts +9 -0
- package/dist/exports/measurement-utils.js +8 -0
- package/dist/exports/measurement.d.ts +11 -0
- package/dist/exports/measurement.js +10 -0
- package/dist/exports/phone-utils.d.ts +4 -0
- package/dist/exports/phone-utils.js +4 -0
- package/dist/exports/phone.d.ts +6 -0
- package/dist/exports/phone.js +3 -0
- package/dist/exports/rsc.d.ts +5 -1
- package/dist/exports/rsc.js +5 -1
- package/dist/exports/types.d.ts +3 -2
- package/dist/fields/color/client/ColorField.d.ts +2 -0
- package/dist/fields/color/client/ColorField.js +35 -288
- package/dist/fields/color/client/ColorField.js.map +1 -1
- package/dist/fields/color/client/ColorInput.d.ts +44 -0
- package/dist/fields/color/client/ColorInput.js +374 -0
- package/dist/fields/color/client/ColorInput.js.map +1 -0
- package/dist/fields/color/client/ColorPickerPanel.d.ts +3 -1
- package/dist/fields/color/client/ColorPickerPanel.js +31 -5
- package/dist/fields/color/client/ColorPickerPanel.js.map +1 -1
- package/dist/fields/color/client/colorField.css +30 -0
- package/dist/fields/color/colorField.d.ts +9 -2
- package/dist/fields/color/colorField.js +68 -15
- package/dist/fields/color/colorField.js.map +1 -1
- package/dist/fields/color/engine/index.d.ts +2 -1
- package/dist/fields/color/engine/scheme.d.ts +13 -0
- package/dist/fields/color/engine/scheme.js +12 -0
- package/dist/fields/color/engine/scheme.js.map +1 -0
- package/dist/fields/color/options.d.ts +5 -4
- package/dist/fields/color/options.js.map +1 -1
- package/dist/fields/color/paths.d.ts +1 -0
- package/dist/fields/color/paths.js +32 -0
- package/dist/fields/color/paths.js.map +1 -0
- package/dist/fields/color/presetChip.js +10 -9
- package/dist/fields/color/presetChip.js.map +1 -1
- package/dist/fields/color/presetReference.d.ts +13 -0
- package/dist/fields/color/presetReference.js +94 -0
- package/dist/fields/color/presetReference.js.map +1 -0
- package/dist/fields/color/presetsFromArray.d.ts +30 -0
- package/dist/fields/color/presetsFromArray.js +58 -0
- package/dist/fields/color/presetsFromArray.js.map +1 -0
- package/dist/fields/color/presetsFromDoc.js +1 -23
- package/dist/fields/color/presetsFromDoc.js.map +1 -1
- package/dist/fields/color/resolvePresets.d.ts +2 -2
- package/dist/fields/color/resolvePresets.js +38 -8
- package/dist/fields/color/resolvePresets.js.map +1 -1
- package/dist/fields/color/schemeValue.js +30 -0
- package/dist/fields/color/schemeValue.js.map +1 -0
- package/dist/fields/color/server/ColorCell.js +9 -3
- package/dist/fields/color/server/ColorCell.js.map +1 -1
- package/dist/fields/color/server/colorCell.css +5 -0
- package/dist/fields/color/valueIssue.js +23 -0
- package/dist/fields/color/valueIssue.js.map +1 -0
- package/dist/fields/encrypted/boot.js +2 -1
- package/dist/fields/encrypted/boot.js.map +1 -1
- package/dist/fields/encrypted/components/EncryptedInput.js +4 -1
- package/dist/fields/encrypted/components/EncryptedInput.js.map +1 -1
- package/dist/fields/encrypted/components/EncryptedTextarea.js +6 -2
- package/dist/fields/encrypted/components/EncryptedTextarea.js.map +1 -1
- package/dist/fields/encrypted/components/ProtectedCell.d.ts +15 -6
- package/dist/fields/encrypted/components/ProtectedCell.js +37 -8
- package/dist/fields/encrypted/components/ProtectedCell.js.map +1 -1
- package/dist/fields/encrypted/components/ProtectedField.css +134 -39
- package/dist/fields/encrypted/components/ProtectedField.d.ts +3 -0
- package/dist/fields/encrypted/components/ProtectedField.js +14 -0
- package/dist/fields/encrypted/components/ProtectedField.js.map +1 -1
- package/dist/fields/encrypted/components/WriteOnlyActions.js +65 -0
- package/dist/fields/encrypted/components/WriteOnlyActions.js.map +1 -0
- package/dist/fields/encrypted/components/WriteOnlyField.js +286 -0
- package/dist/fields/encrypted/components/WriteOnlyField.js.map +1 -0
- package/dist/fields/encrypted/components/writeOnlyIntent.js +69 -0
- package/dist/fields/encrypted/components/writeOnlyIntent.js.map +1 -0
- package/dist/fields/encrypted/crypto/wire.d.ts +10 -1
- package/dist/fields/encrypted/crypto/wire.js +1 -1
- package/dist/fields/encrypted/encryptedField.js +63 -6
- package/dist/fields/encrypted/encryptedField.js.map +1 -1
- package/dist/fields/encrypted/generateSecret.d.ts +10 -0
- package/dist/fields/encrypted/generateSecret.js +41 -0
- package/dist/fields/encrypted/generateSecret.js.map +1 -0
- package/dist/fields/encrypted/hint.js +67 -0
- package/dist/fields/encrypted/hint.js.map +1 -0
- package/dist/fields/encrypted/hooks.js +59 -31
- package/dist/fields/encrypted/hooks.js.map +1 -1
- package/dist/fields/encrypted/queryRewrite.d.ts +10 -2
- package/dist/fields/encrypted/queryRewrite.js +34 -8
- package/dist/fields/encrypted/queryRewrite.js.map +1 -1
- package/dist/fields/encrypted/rawRead.d.ts +24 -0
- package/dist/fields/encrypted/rawRead.js +40 -0
- package/dist/fields/encrypted/rawRead.js.map +1 -0
- package/dist/fields/encrypted/scan.js +21 -4
- package/dist/fields/encrypted/scan.js.map +1 -1
- package/dist/fields/encrypted/types.d.ts +67 -2
- package/dist/fields/encrypted/types.js +8 -1
- package/dist/fields/encrypted/types.js.map +1 -1
- package/dist/fields/encrypted/utils/decryptAllData.js +2 -2
- package/dist/fields/encrypted/utils/decryptAllData.js.map +1 -1
- package/dist/fields/encrypted/utils/decryptFieldValue.d.ts +30 -0
- package/dist/fields/encrypted/utils/decryptFieldValue.js +56 -0
- package/dist/fields/encrypted/utils/decryptFieldValue.js.map +1 -0
- package/dist/fields/encrypted/utils/readEncryptedField.d.ts +51 -0
- package/dist/fields/encrypted/utils/readEncryptedField.js +96 -0
- package/dist/fields/encrypted/utils/readEncryptedField.js.map +1 -0
- package/dist/fields/icon/adapters/lucide/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/lucide/generated/nodes.js.map +1 -1
- package/dist/fields/icon/adapters/radix/adapter.js +7 -0
- package/dist/fields/icon/adapters/radix/adapter.js.map +1 -1
- package/dist/fields/icon/adapters/radix/client.d.ts +3 -2
- package/dist/fields/icon/adapters/radix/client.js +3 -2
- package/dist/fields/icon/adapters/radix/client.js.map +1 -1
- package/dist/fields/icon/adapters/radix/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/radix/generated/nodes.js +3625 -0
- package/dist/fields/icon/adapters/radix/generated/nodes.js.map +1 -0
- package/dist/fields/icon/adapters/tabler/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/tabler/generated/nodes.js.map +1 -1
- package/dist/fields/icon/client/DrawerGlyph.js +34 -17
- package/dist/fields/icon/client/DrawerGlyph.js.map +1 -1
- package/dist/fields/icon/client/IconDrawerContent.js +9 -45
- package/dist/fields/icon/client/IconDrawerContent.js.map +1 -1
- package/dist/fields/icon/client/IconFieldClient.d.ts +7 -0
- package/dist/fields/icon/client/IconFieldClient.js +15 -7
- package/dist/fields/icon/client/IconFieldClient.js.map +1 -1
- package/dist/fields/icon/client/IconGridCell.js +67 -0
- package/dist/fields/icon/client/IconGridCell.js.map +1 -0
- package/dist/fields/icon/client/icon-field.css +12 -2
- package/dist/fields/icon/codegen/emit.js +5 -4
- package/dist/fields/icon/codegen/emit.js.map +1 -1
- package/dist/fields/icon/codegen/generate.js +32 -6
- package/dist/fields/icon/codegen/generate.js.map +1 -1
- package/dist/fields/icon/layers/coalesce.js +41 -0
- package/dist/fields/icon/layers/coalesce.js.map +1 -0
- package/dist/fields/icon/layers/manifestCache.d.ts +6 -0
- package/dist/fields/icon/layers/manifestCache.js +49 -0
- package/dist/fields/icon/layers/manifestCache.js.map +1 -0
- package/dist/fields/icon/layers/resolve.d.ts +20 -0
- package/dist/fields/icon/layers/resolve.js +63 -0
- package/dist/fields/icon/layers/resolve.js.map +1 -0
- package/dist/fields/icon/layers/upload.d.ts +52 -0
- package/dist/fields/icon/layers/upload.js +116 -0
- package/dist/fields/icon/layers/upload.js.map +1 -0
- package/dist/fields/icon/plugin.js +24 -0
- package/dist/fields/icon/plugin.js.map +1 -1
- package/dist/fields/icon/react/createIcon.d.ts +5 -0
- package/dist/fields/icon/react/createIcon.js +22 -4
- package/dist/fields/icon/react/createIcon.js.map +1 -1
- package/dist/fields/icon/react/createRscIcon.js +22 -1
- package/dist/fields/icon/react/createRscIcon.js.map +1 -1
- package/dist/fields/icon/react/resolveRendered.js +21 -0
- package/dist/fields/icon/react/resolveRendered.js.map +1 -0
- package/dist/fields/icon/react/types.d.ts +15 -1
- package/dist/fields/icon/rsc/IconCell.d.ts +2 -2
- package/dist/fields/icon/rsc/IconCell.js +12 -5
- package/dist/fields/icon/rsc/IconCell.js.map +1 -1
- package/dist/fields/icon/rsc/IconFieldServer.js +16 -2
- package/dist/fields/icon/rsc/IconFieldServer.js.map +1 -1
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js +1 -1
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js.map +1 -1
- package/dist/fields/icon/server/endpoint.d.ts +12 -0
- package/dist/fields/icon/server/endpoint.js +52 -0
- package/dist/fields/icon/server/endpoint.js.map +1 -0
- package/dist/fields/icon/server/manifestCache.js +9 -4
- package/dist/fields/icon/server/manifestCache.js.map +1 -1
- package/dist/fields/icon/server/resolveIconMeta.js +31 -0
- package/dist/fields/icon/server/resolveIconMeta.js.map +1 -0
- package/dist/fields/icon/server/validate.js +5 -2
- package/dist/fields/icon/server/validate.js.map +1 -1
- package/dist/fields/icon/shared/adapterComponents.d.ts +3 -2
- package/dist/fields/icon/shared/iconLabel.js +26 -0
- package/dist/fields/icon/shared/iconLabel.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementCell.d.ts +18 -0
- package/dist/fields/measurement/client/MeasurementCell.js +47 -0
- package/dist/fields/measurement/client/MeasurementCell.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementField.d.ts +13 -0
- package/dist/fields/measurement/client/MeasurementField.js +70 -0
- package/dist/fields/measurement/client/MeasurementField.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementInput.d.ts +42 -0
- package/dist/fields/measurement/client/MeasurementInput.js +327 -0
- package/dist/fields/measurement/client/MeasurementInput.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.d.ts +24 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.js +61 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.js.map +1 -0
- package/dist/fields/measurement/client/editModel.js +124 -0
- package/dist/fields/measurement/client/editModel.js.map +1 -0
- package/dist/fields/measurement/client/measurementCell.css +4 -0
- package/dist/fields/measurement/client/measurementField.css +215 -0
- package/dist/fields/measurement/client/useMeasurementModel.d.ts +19 -0
- package/dist/fields/measurement/client/useMeasurementModel.js +85 -0
- package/dist/fields/measurement/client/useMeasurementModel.js.map +1 -0
- package/dist/fields/measurement/engine/convert.d.ts +8 -0
- package/dist/fields/measurement/engine/convert.js +21 -0
- package/dist/fields/measurement/engine/convert.js.map +1 -0
- package/dist/fields/measurement/engine/format.d.ts +37 -0
- package/dist/fields/measurement/engine/format.js +156 -0
- package/dist/fields/measurement/engine/format.js.map +1 -0
- package/dist/fields/measurement/engine/locale.d.ts +25 -0
- package/dist/fields/measurement/engine/locale.js +79 -0
- package/dist/fields/measurement/engine/locale.js.map +1 -0
- package/dist/fields/measurement/engine/precision.d.ts +34 -0
- package/dist/fields/measurement/engine/precision.js +63 -0
- package/dist/fields/measurement/engine/precision.js.map +1 -0
- package/dist/fields/measurement/engine/registry.d.ts +55 -0
- package/dist/fields/measurement/engine/registry.js +117 -0
- package/dist/fields/measurement/engine/registry.js.map +1 -0
- package/dist/fields/measurement/engine/units.d.ts +178 -0
- package/dist/fields/measurement/engine/units.js +198 -0
- package/dist/fields/measurement/engine/units.js.map +1 -0
- package/dist/fields/measurement/measurementField.d.ts +17 -0
- package/dist/fields/measurement/measurementField.js +139 -0
- package/dist/fields/measurement/measurementField.js.map +1 -0
- package/dist/fields/measurement/options.d.ts +76 -0
- package/dist/fields/measurement/options.js +9 -0
- package/dist/fields/measurement/options.js.map +1 -0
- package/dist/fields/measurement/presets.d.ts +117 -0
- package/dist/fields/measurement/presets.js +136 -0
- package/dist/fields/measurement/presets.js.map +1 -0
- package/dist/fields/measurement/server/MeasurementCellServer.d.ts +21 -0
- package/dist/fields/measurement/server/MeasurementCellServer.js +53 -0
- package/dist/fields/measurement/server/MeasurementCellServer.js.map +1 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.d.ts +16 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.js +45 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.js.map +1 -0
- package/dist/fields/measurement/server/getUserUnits.js +39 -0
- package/dist/fields/measurement/server/getUserUnits.js.map +1 -0
- package/dist/fields/measurement/server/resolvePrecisionSafe.js +24 -0
- package/dist/fields/measurement/server/resolvePrecisionSafe.js.map +1 -0
- package/dist/fields/measurement/server/resolveRegistryDefault.js +26 -0
- package/dist/fields/measurement/server/resolveRegistryDefault.js.map +1 -0
- package/dist/fields/phoneNumber/client/CountryFlag.d.ts +16 -0
- package/dist/fields/phoneNumber/client/CountryFlag.js +40 -0
- package/dist/fields/phoneNumber/client/CountryFlag.js.map +1 -0
- package/dist/fields/phoneNumber/client/CountryPicker.d.ts +21 -0
- package/dist/fields/phoneNumber/client/CountryPicker.js +255 -0
- package/dist/fields/phoneNumber/client/CountryPicker.js.map +1 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.d.ts +26 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.js +318 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.js.map +1 -0
- package/dist/fields/phoneNumber/client/editModel.js +86 -0
- package/dist/fields/phoneNumber/client/editModel.js.map +1 -0
- package/dist/fields/phoneNumber/client/phoneNumberField.css +305 -0
- package/dist/fields/phoneNumber/engine/countries.d.ts +36 -0
- package/dist/fields/phoneNumber/engine/countries.js +68 -0
- package/dist/fields/phoneNumber/engine/countries.js.map +1 -0
- package/dist/fields/phoneNumber/engine/draft.d.ts +13 -0
- package/dist/fields/phoneNumber/engine/draft.js +72 -0
- package/dist/fields/phoneNumber/engine/draft.js.map +1 -0
- package/dist/fields/phoneNumber/engine/metadata.d.ts +11 -0
- package/dist/fields/phoneNumber/engine/metadata.js +32 -0
- package/dist/fields/phoneNumber/engine/metadata.js.map +1 -0
- package/dist/fields/phoneNumber/engine/phone.d.ts +37 -0
- package/dist/fields/phoneNumber/engine/phone.js +59 -0
- package/dist/fields/phoneNumber/engine/phone.js.map +1 -0
- package/dist/fields/phoneNumber/options.d.ts +72 -0
- package/dist/fields/phoneNumber/options.js +37 -0
- package/dist/fields/phoneNumber/options.js.map +1 -0
- package/dist/fields/phoneNumber/phoneNumberField.d.ts +14 -0
- package/dist/fields/phoneNumber/phoneNumberField.js +167 -0
- package/dist/fields/phoneNumber/phoneNumberField.js.map +1 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.d.ts +19 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.js +73 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.js.map +1 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.d.ts +16 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.js +57 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.js.map +1 -0
- package/dist/fields/phoneNumber/server/flagsEndpoint.js +51 -0
- package/dist/fields/phoneNumber/server/flagsEndpoint.js.map +1 -0
- package/dist/fields/phoneNumber/server/readPhoneOptions.js +18 -0
- package/dist/fields/phoneNumber/server/readPhoneOptions.js.map +1 -0
- package/dist/fields/phoneNumber/server/resolvePhoneOptionsSafe.js +33 -0
- package/dist/fields/phoneNumber/server/resolvePhoneOptionsSafe.js.map +1 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +35 -1
- package/dist/index.js.map +1 -1
- package/dist/translations/ar.js +62 -0
- package/dist/translations/ar.js.map +1 -0
- package/dist/translations/de.js +19 -1
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +19 -1
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.js +62 -0
- package/dist/translations/es.js.map +1 -0
- package/dist/translations/fr.js +62 -0
- package/dist/translations/fr.js.map +1 -0
- package/dist/translations/id.js +62 -0
- package/dist/translations/id.js.map +1 -0
- package/dist/translations/index.d.ts +10 -1
- package/dist/translations/index.js +19 -1
- package/dist/translations/index.js.map +1 -1
- package/dist/translations/keys.d.ts +19 -1
- package/dist/translations/keys.js +20 -2
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.js +62 -0
- package/dist/translations/ko.js.map +1 -0
- package/dist/translations/pt.js +62 -0
- package/dist/translations/pt.js.map +1 -0
- package/dist/translations/ru.js +62 -0
- package/dist/translations/ru.js.map +1 -0
- package/dist/translations/uk.js +62 -0
- package/dist/translations/uk.js.map +1 -0
- package/dist/translations/zh.js +62 -0
- package/dist/translations/zh.js.map +1 -0
- package/dist/types.d.ts +165 -8
- package/dist/utils/resolveStaticLabel.js +13 -0
- package/dist/utils/resolveStaticLabel.js.map +1 -0
- package/package.json +31 -7
- package/dist/fields/icon/rsc/resolveStaticLabel.js +0 -16
- package/dist/fields/icon/rsc/resolveStaticLabel.js.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ColorFieldOptions, ColorLinkedOptions } from "./options.js";
|
|
2
|
-
import { TextField } from "payload";
|
|
2
|
+
import { JSONField, TextField } from "payload";
|
|
3
3
|
|
|
4
4
|
//#region src/fields/color/colorField.d.ts
|
|
5
5
|
/**
|
|
@@ -13,8 +13,15 @@ declare function colorField(options?: ColorFieldOptions & {
|
|
|
13
13
|
linked?: false;
|
|
14
14
|
}): TextField;
|
|
15
15
|
declare function colorField(options: ColorFieldOptions & {
|
|
16
|
-
linked: ColorLinkedOptions
|
|
16
|
+
linked: (ColorLinkedOptions & {
|
|
17
|
+
resolve?: 'value';
|
|
18
|
+
}) | true;
|
|
17
19
|
}): [TextField, TextField];
|
|
20
|
+
declare function colorField(options: ColorFieldOptions & {
|
|
21
|
+
linked: ColorLinkedOptions & {
|
|
22
|
+
resolve: 'schemes';
|
|
23
|
+
};
|
|
24
|
+
}): [TextField, JSONField];
|
|
18
25
|
//#endregion
|
|
19
26
|
export { colorField };
|
|
20
27
|
//# sourceMappingURL=colorField.d.ts.map
|
|
@@ -1,23 +1,41 @@
|
|
|
1
1
|
import { keys } from "../../translations/keys.js";
|
|
2
2
|
import { parseColor } from "./engine/parse.js";
|
|
3
3
|
import { formatColor } from "./engine/format.js";
|
|
4
|
-
import {
|
|
4
|
+
import { isColorSchemeValue } from "./engine/scheme.js";
|
|
5
|
+
import { COLOR_CUSTOM_KEY } from "./options.js";
|
|
6
|
+
import { applyReferenceAlpha, formatPresetReference, parsePresetReference, presetReferenceIssue, presetReferenceParts } from "./presetReference.js";
|
|
7
|
+
import { flatValue } from "./schemeValue.js";
|
|
5
8
|
import { asTranslate } from "../../translations/server.js";
|
|
6
9
|
import { resolveColorFormat } from "./resolveFormat.js";
|
|
7
10
|
import { resolvePresets } from "./resolvePresets.js";
|
|
8
11
|
import { text } from "payload/shared";
|
|
9
12
|
//#region src/fields/color/colorField.ts
|
|
10
|
-
const buildValidate = (opts) => (value, args) => {
|
|
13
|
+
const buildValidate = (opts) => async (value, args) => {
|
|
11
14
|
if (typeof value === "string" && value !== "") {
|
|
12
15
|
if (opts.linked && value.startsWith("preset:")) {
|
|
13
|
-
|
|
16
|
+
let hasKey = null;
|
|
17
|
+
if (presetReferenceParts(value)?.explicit) try {
|
|
18
|
+
const all = await resolvePresets({
|
|
19
|
+
memoKey: opts.memoKey,
|
|
20
|
+
req: args.req,
|
|
21
|
+
source: opts.presets
|
|
22
|
+
});
|
|
23
|
+
if (all.length > 0) hasKey = (key) => all.some((preset) => preset.key === key);
|
|
24
|
+
} catch {}
|
|
25
|
+
const issue = presetReferenceIssue(value, hasKey);
|
|
26
|
+
if (issue === "invalidAlpha") return asTranslate(args.req.t)(keys.invalidColor);
|
|
27
|
+
if (issue !== null) return asTranslate(args.req.t)(keys.missingPreset);
|
|
14
28
|
} else if (!parseColor(value)) return asTranslate(args.req.t)(keys.invalidColor);
|
|
15
29
|
}
|
|
16
30
|
return text(value, args);
|
|
17
31
|
};
|
|
18
32
|
const buildNormalizeHook = (opts) => ({ req, value }) => {
|
|
19
33
|
if (typeof value !== "string" || value === "") return value;
|
|
20
|
-
if (opts.linked && value.startsWith("preset:"))
|
|
34
|
+
if (opts.linked && value.startsWith("preset:")) {
|
|
35
|
+
const parts = presetReferenceParts(value);
|
|
36
|
+
if (!parts) return value;
|
|
37
|
+
return formatPresetReference(parts.key, opts.alpha ? parts.alpha : 100);
|
|
38
|
+
}
|
|
21
39
|
const parsed = parseColor(value);
|
|
22
40
|
if (!parsed) return value;
|
|
23
41
|
return formatColor(parsed, resolveColorFormat(opts.format, req), { alpha: opts.alpha });
|
|
@@ -25,7 +43,9 @@ const buildNormalizeHook = (opts) => ({ req, value }) => {
|
|
|
25
43
|
function colorField(options = {}) {
|
|
26
44
|
const { name = "color", alpha = true, enableEyedropper = true, format, isClearable = true, label, linked: linkedOption = false, localized, overrides, presets, presetsLabel, required } = options;
|
|
27
45
|
const linked = linkedOption !== false;
|
|
28
|
-
const
|
|
46
|
+
const linkedOptions = typeof linkedOption === "object" ? linkedOption : {};
|
|
47
|
+
const linkedFallback = linkedOptions.fallback ?? null;
|
|
48
|
+
const resolveMode = linkedOptions.resolve ?? "value";
|
|
29
49
|
const memoKey = Symbol(`colorField:${name}`);
|
|
30
50
|
const clientOptions = {
|
|
31
51
|
alpha,
|
|
@@ -59,39 +79,72 @@ function colorField(options = {}) {
|
|
|
59
79
|
format,
|
|
60
80
|
linked
|
|
61
81
|
})] },
|
|
62
|
-
validate: buildValidate({
|
|
82
|
+
validate: buildValidate({
|
|
83
|
+
linked,
|
|
84
|
+
memoKey,
|
|
85
|
+
presets
|
|
86
|
+
})
|
|
63
87
|
};
|
|
64
88
|
const field = typeof overrides === "function" ? overrides({ field: base }) : base;
|
|
65
89
|
if (!linked) return field;
|
|
66
90
|
const loggedRequests = /* @__PURE__ */ new WeakSet();
|
|
91
|
+
/**
|
|
92
|
+
* Shapes a resolved value for the configured sibling. 'value' flattens a
|
|
93
|
+
* scheme to its light member so a field configured before schemes existed
|
|
94
|
+
* keeps working when its resolver starts returning them; 'schemes' inflates
|
|
95
|
+
* a flat color so consumers never branch on the shape.
|
|
96
|
+
*/
|
|
97
|
+
const shape = (value) => {
|
|
98
|
+
if (value === null) return null;
|
|
99
|
+
if (resolveMode === "value") return flatValue(value);
|
|
100
|
+
return isColorSchemeValue(value) ? value : {
|
|
101
|
+
dark: value,
|
|
102
|
+
light: value
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
/** The stored reference alpha, applied to whatever the reference resolved to. */
|
|
106
|
+
const withAlpha = (resolved, refAlpha, req) => {
|
|
107
|
+
const effective = alpha ? refAlpha : 100;
|
|
108
|
+
if (resolved === null || effective === 100) return resolved;
|
|
109
|
+
return applyReferenceAlpha(resolved, effective, resolveColorFormat(format, req));
|
|
110
|
+
};
|
|
67
111
|
const resolveHook = async ({ collection, global, req, siblingData }) => {
|
|
68
112
|
const raw = siblingData?.[field.name];
|
|
69
113
|
if (typeof raw !== "string" || raw === "") return null;
|
|
70
|
-
if (!raw.startsWith("preset:")) return raw;
|
|
71
|
-
const
|
|
114
|
+
if (!raw.startsWith("preset:")) return shape(raw);
|
|
115
|
+
const ref = parsePresetReference(raw);
|
|
116
|
+
const key = ref?.key ?? raw.slice(7);
|
|
117
|
+
const refAlpha = ref?.alpha ?? 100;
|
|
72
118
|
try {
|
|
73
119
|
const match = (await resolvePresets({
|
|
74
120
|
memoKey,
|
|
75
121
|
req,
|
|
76
122
|
source: presets
|
|
77
123
|
})).find((preset) => preset.key === key);
|
|
78
|
-
return match ? match.value : linkedFallback;
|
|
124
|
+
return withAlpha(shape(match ? match.value : linkedFallback), refAlpha, req);
|
|
79
125
|
} catch (error) {
|
|
80
126
|
if (!loggedRequests.has(req)) {
|
|
81
127
|
loggedRequests.add(req);
|
|
82
128
|
const slug = collection?.slug ?? global?.slug ?? "unknown";
|
|
83
129
|
req.payload.logger.error({ err: error }, `colorField preset resolver failed for ${slug}.${field.name}`);
|
|
84
130
|
}
|
|
85
|
-
return linkedFallback;
|
|
131
|
+
return withAlpha(shape(linkedFallback), refAlpha, req);
|
|
86
132
|
}
|
|
87
133
|
};
|
|
88
|
-
|
|
134
|
+
const resolvedAdmin = {
|
|
135
|
+
disableListColumn: true,
|
|
136
|
+
hidden: true
|
|
137
|
+
};
|
|
138
|
+
return [field, resolveMode === "schemes" ? {
|
|
139
|
+
name: `${field.name}Resolved`,
|
|
140
|
+
type: "json",
|
|
141
|
+
admin: resolvedAdmin,
|
|
142
|
+
hooks: { afterRead: [resolveHook] },
|
|
143
|
+
virtual: true
|
|
144
|
+
} : {
|
|
89
145
|
name: `${field.name}Resolved`,
|
|
90
146
|
type: "text",
|
|
91
|
-
admin:
|
|
92
|
-
disableListColumn: true,
|
|
93
|
-
hidden: true
|
|
94
|
-
},
|
|
147
|
+
admin: resolvedAdmin,
|
|
95
148
|
hooks: { afterRead: [resolveHook] },
|
|
96
149
|
virtual: true
|
|
97
150
|
}];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"colorField.js","names":[],"sources":["../../../src/fields/color/colorField.ts"],"sourcesContent":["import type { FieldHook, PayloadRequest, TextField, TextFieldValidation } from 'payload'\nimport { text } from 'payload/shared'\nimport { keys } from '../../translations/keys'\nimport { asTranslate } from '../../translations/server'\nimport type { ColorFormat } from '../../types'\nimport { formatColor, parseColor } from './engine'\nimport {\n\tCOLOR_CUSTOM_KEY,\n\ttype ColorFieldCustom,\n\ttype ColorFieldOptions,\n\ttype ColorFieldServerOptions,\n\ttype ColorLinkedOptions,\n\tPRESET_PREFIX,\n} from './options'\nimport { resolveColorFormat } from './resolveFormat'\nimport { resolvePresets } from './resolvePresets'\n\nconst buildValidate =\n\t(opts: { linked: boolean }): TextFieldValidation =>\n\t(value, args) => {\n\t\tif (typeof value === 'string' && value !== '') {\n\t\t\tif (opts.linked && value.startsWith(PRESET_PREFIX)) {\n\t\t\t\tif (value.length === PRESET_PREFIX.length) {\n\t\t\t\t\treturn asTranslate(args.req.t)(keys.missingPreset)\n\t\t\t\t}\n\t\t\t} else if (!parseColor(value)) {\n\t\t\t\treturn asTranslate(args.req.t)(keys.invalidColor)\n\t\t\t}\n\t\t}\n\t\treturn text(value, args)\n\t}\n\nconst buildNormalizeHook =\n\t(opts: { alpha: boolean; format: ColorFormat | undefined; linked: boolean }): FieldHook =>\n\t({ req, value }) => {\n\t\tif (typeof value !== 'string' || value === '') return value\n\t\tif (opts.linked && value.startsWith(PRESET_PREFIX)) return value\n\t\tconst parsed = parseColor(value)\n\t\t// Unparseable input passes through so validate rejects it with a proper message\n\t\tif (!parsed) return value\n\t\treturn formatColor(parsed, resolveColorFormat(opts.format, req), { alpha: opts.alpha })\n\t}\n\n/**\n * Text-backed color field storing a CSS string in the configured format.\n * Linked mode returns a tuple: the field plus a hidden virtual sibling\n * `${name}Resolved` populated on read (spread it into `fields`). The sibling\n * keeps the stored `preset:<key>` reference round-trip safe in the admin form\n * while consumers read the resolved CSS value.\n */\nexport function colorField(options?: ColorFieldOptions & { linked?: false }): TextField\nexport function colorField(\n\toptions: ColorFieldOptions & { linked: ColorLinkedOptions | true }\n): [TextField, TextField]\nexport function colorField(options: ColorFieldOptions = {}): TextField | [TextField, TextField] {\n\tconst {\n\t\tname = 'color',\n\t\talpha = true,\n\t\tenableEyedropper = true,\n\t\tformat,\n\t\tisClearable = true,\n\t\tlabel,\n\t\tlinked: linkedOption = false,\n\t\tlocalized,\n\t\toverrides,\n\t\tpresets,\n\t\tpresetsLabel,\n\t\trequired,\n\t} = options\n\n\tconst linked = linkedOption !== false\n\tconst linkedFallback = typeof linkedOption === 'object' ? (linkedOption.fallback ?? null) : null\n\tconst memoKey = Symbol(`colorField:${name}`)\n\n\t// format stays possibly-undefined here; ColorFieldServer resolves the effective\n\t// format (field-level, else plugin registry, else hex) per request for the client.\n\tconst clientOptions: ColorFieldServerOptions = {\n\t\talpha,\n\t\tenableEyedropper,\n\t\tformat,\n\t\tisClearable,\n\t\tlinked,\n\t\tlinkedFallback,\n\t}\n\n\tconst custom: ColorFieldCustom = { memoKey, presets, presetsLabel }\n\n\tconst base: TextField = {\n\t\tname,\n\t\ttype: 'text',\n\t\t...(label !== undefined ? { label } : {}),\n\t\t...(required !== undefined ? { required } : {}),\n\t\t...(localized !== undefined ? { localized } : {}),\n\t\tadmin: {\n\t\t\tcomponents: {\n\t\t\t\tCell: { path: '@10x-media/fields/rsc#ColorCell' },\n\t\t\t\tField: {\n\t\t\t\t\tclientProps: { colorOptions: clientOptions },\n\t\t\t\t\tpath: '@10x-media/fields/rsc#ColorFieldServer',\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tcustom: { [COLOR_CUSTOM_KEY]: custom },\n\t\thooks: { beforeValidate: [buildNormalizeHook({ alpha, format, linked })] },\n\t\tvalidate: buildValidate({ linked }),\n\t}\n\n\tconst field = typeof overrides === 'function' ? overrides({ field: base }) : base\n\n\tif (!linked) return field\n\n\tconst loggedRequests = new WeakSet<PayloadRequest>()\n\n\tconst resolveHook: FieldHook = async ({ collection, global, req, siblingData }) => {\n\t\tconst raw = siblingData?.[field.name]\n\t\tif (typeof raw !== 'string' || raw === '') return null\n\t\tif (!raw.startsWith(PRESET_PREFIX)) return raw\n\t\tconst key = raw.slice(PRESET_PREFIX.length)\n\t\ttry {\n\t\t\tconst all = await resolvePresets({ memoKey, req, source: presets })\n\t\t\tconst match = all.find((preset) => preset.key === key)\n\t\t\treturn match ? match.value : linkedFallback\n\t\t} catch (error) {\n\t\t\t// A broken resolver must not take down reads; degrade like a missing preset\n\t\t\tif (!loggedRequests.has(req)) {\n\t\t\t\tloggedRequests.add(req)\n\t\t\t\tconst slug = collection?.slug ?? global?.slug ?? 'unknown'\n\t\t\t\treq.payload.logger.error(\n\t\t\t\t\t{ err: error },\n\t\t\t\t\t`colorField preset resolver failed for ${slug}.${field.name}`\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn linkedFallback\n\t\t}\n\t}\n\n\tconst resolvedField: TextField = {\n\t\tname: `${field.name}Resolved`,\n\t\ttype: 'text',\n\t\tadmin: { disableListColumn: true, hidden: true },\n\t\thooks: { afterRead: [resolveHook] },\n\t\tvirtual: true,\n\t}\n\n\treturn [field, resolvedField]\n}\n"],"mappings":";;;;;;;;;AAiBA,MAAM,iBACJ,UACA,OAAO,SAAS;CAChB,IAAI,OAAO,UAAU,YAAY,UAAU;MACtC,KAAK,UAAU,MAAM,WAAA,SAAwB;OAC5C,MAAM,WAAA,GACT,OAAO,YAAY,KAAK,IAAI,CAAC,EAAE,KAAK,aAAa;EAAA,OAE5C,IAAI,CAAC,WAAW,KAAK,GAC3B,OAAO,YAAY,KAAK,IAAI,CAAC,EAAE,KAAK,YAAY;CAAA;CAGlD,OAAO,KAAK,OAAO,IAAI;AACxB;AAED,MAAM,sBACJ,UACA,EAAE,KAAK,YAAY;CACnB,IAAI,OAAO,UAAU,YAAY,UAAU,IAAI,OAAO;CACtD,IAAI,KAAK,UAAU,MAAM,WAAA,SAAwB,GAAG,OAAO;CAC3D,MAAM,SAAS,WAAW,KAAK;CAE/B,IAAI,CAAC,QAAQ,OAAO;CACpB,OAAO,YAAY,QAAQ,mBAAmB,KAAK,QAAQ,GAAG,GAAG,EAAE,OAAO,KAAK,MAAM,CAAC;AACvF;AAaD,SAA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AC1C,IAAI;GAEH,MAAM,SAAQ,MADI,eAAe;IAAE;IAAS;IAAK,QAAQ;GAAQ,CAAC,GAChD,MAAM,WAAW,OAAO,QAAQ,GAAG;GACrD,OAAO,QAAQ,MAAM,QAAQ;EAC9B,SAAS,OAAO;GAEf,IAAI,CAAC,eAAe,IAAI,GAAG,GAAG;IAC7B,eAAe,IAAI,GAAG;IACtB,MAAM,OAAO,YAAY,QAAQ,QAAQ,QAAQ;IACjD,IAAI,QAAQ,OAAO,MAClB,EAAE,KAAK,MAAM,GACb,yCAAyC,KAAK,GAAG,MAAM,MACxD;GACD;GACA,OAAO;EACR;CACD;CAUA,OAAO,CAAC,OAAO;EAPd,MAAM,GAAG,MAAM,KAAK;EACpB,MAAM;EACN,OAAO;GAAE,mBAAmB;GAAM,QAAQ;EAAK;EAC/C,OAAO,EAAE,WAAW,CAAC,WAAW,EAAE;EAClC,SAAS;CAGiB,CAAC;AAC7B"}
|
|
1
|
+
{"version":3,"file":"colorField.js","names":[],"sources":["../../../src/fields/color/colorField.ts"],"sourcesContent":["import type { FieldHook, JSONField, PayloadRequest, TextField, TextFieldValidation } from 'payload'\nimport { text } from 'payload/shared'\nimport { keys } from '../../translations/keys'\nimport { asTranslate } from '../../translations/server'\nimport type { ColorFormat, ColorSchemeValue } from '../../types'\nimport { formatColor, isColorSchemeValue, parseColor } from './engine'\nimport {\n\tCOLOR_CUSTOM_KEY,\n\ttype ColorFieldCustom,\n\ttype ColorFieldOptions,\n\ttype ColorFieldServerOptions,\n\ttype ColorLinkedOptions,\n\ttype ColorPresetsSource,\n\tPRESET_PREFIX,\n} from './options'\nimport {\n\tapplyReferenceAlpha,\n\tformatPresetReference,\n\tparsePresetReference,\n\tpresetReferenceIssue,\n\tpresetReferenceParts,\n} from './presetReference'\nimport { resolveColorFormat } from './resolveFormat'\nimport { resolvePresets } from './resolvePresets'\nimport { flatValue } from './schemeValue'\n\nconst buildValidate =\n\t(opts: {\n\t\tlinked: boolean\n\t\tmemoKey: symbol\n\t\tpresets: ColorPresetsSource | undefined\n\t}): TextFieldValidation =>\n\tasync (value, args) => {\n\t\tif (typeof value === 'string' && value !== '') {\n\t\t\tif (opts.linked && value.startsWith(PRESET_PREFIX)) {\n\t\t\t\tlet hasKey: ((key: string) => boolean) | null = null\n\t\t\t\tif (presetReferenceParts(value)?.explicit) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst all = await resolvePresets({\n\t\t\t\t\t\t\tmemoKey: opts.memoKey,\n\t\t\t\t\t\t\treq: args.req,\n\t\t\t\t\t\t\tsource: opts.presets,\n\t\t\t\t\t\t})\n\t\t\t\t\t\t// An empty list means no presets are configured (or none resolved);\n\t\t\t\t\t\t// skip the existence check rather than rejecting every reference\n\t\t\t\t\t\tif (all.length > 0) hasKey = (key) => all.some((preset) => preset.key === key)\n\t\t\t\t\t} catch {\n\t\t\t\t\t\t// A broken resolver must not block writes; reads degrade and log separately\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tconst issue = presetReferenceIssue(value, hasKey)\n\t\t\t\tif (issue === 'invalidAlpha') return asTranslate(args.req.t)(keys.invalidColor)\n\t\t\t\tif (issue !== null) return asTranslate(args.req.t)(keys.missingPreset)\n\t\t\t} else if (!parseColor(value)) {\n\t\t\t\treturn asTranslate(args.req.t)(keys.invalidColor)\n\t\t\t}\n\t\t}\n\t\treturn text(value, args)\n\t}\n\nconst buildNormalizeHook =\n\t(opts: { alpha: boolean; format: ColorFormat | undefined; linked: boolean }): FieldHook =>\n\t({ req, value }) => {\n\t\tif (typeof value !== 'string' || value === '') return value\n\t\tif (opts.linked && value.startsWith(PRESET_PREFIX)) {\n\t\t\tconst parts = presetReferenceParts(value)\n\t\t\t// Empty keys pass through so validate rejects them with a proper message\n\t\t\tif (!parts) return value\n\t\t\treturn formatPresetReference(parts.key, opts.alpha ? parts.alpha : 100)\n\t\t}\n\t\tconst parsed = parseColor(value)\n\t\t// Unparseable input passes through so validate rejects it with a proper message\n\t\tif (!parsed) return value\n\t\treturn formatColor(parsed, resolveColorFormat(opts.format, req), { alpha: opts.alpha })\n\t}\n\n/**\n * Text-backed color field storing a CSS string in the configured format.\n * Linked mode returns a tuple: the field plus a hidden virtual sibling\n * `${name}Resolved` populated on read (spread it into `fields`). The sibling\n * keeps the stored `preset:<key>` reference round-trip safe in the admin form\n * while consumers read the resolved CSS value.\n */\nexport function colorField(options?: ColorFieldOptions & { linked?: false }): TextField\nexport function colorField(\n\toptions: ColorFieldOptions & { linked: (ColorLinkedOptions & { resolve?: 'value' }) | true }\n): [TextField, TextField]\nexport function colorField(\n\toptions: ColorFieldOptions & { linked: ColorLinkedOptions & { resolve: 'schemes' } }\n): [TextField, JSONField]\nexport function colorField(\n\toptions: ColorFieldOptions = {}\n): TextField | [TextField, JSONField | TextField] {\n\tconst {\n\t\tname = 'color',\n\t\talpha = true,\n\t\tenableEyedropper = true,\n\t\tformat,\n\t\tisClearable = true,\n\t\tlabel,\n\t\tlinked: linkedOption = false,\n\t\tlocalized,\n\t\toverrides,\n\t\tpresets,\n\t\tpresetsLabel,\n\t\trequired,\n\t} = options\n\n\tconst linked = linkedOption !== false\n\tconst linkedOptions: ColorLinkedOptions = typeof linkedOption === 'object' ? linkedOption : {}\n\tconst linkedFallback = linkedOptions.fallback ?? null\n\tconst resolveMode = linkedOptions.resolve ?? 'value'\n\tconst memoKey = Symbol(`colorField:${name}`)\n\n\t// format stays possibly-undefined here; ColorFieldServer resolves the effective\n\t// format (field-level, else plugin registry, else hex) per request for the client.\n\tconst clientOptions: ColorFieldServerOptions = {\n\t\talpha,\n\t\tenableEyedropper,\n\t\tformat,\n\t\tisClearable,\n\t\tlinked,\n\t\tlinkedFallback,\n\t}\n\n\tconst custom: ColorFieldCustom = { memoKey, presets, presetsLabel }\n\n\tconst base: TextField = {\n\t\tname,\n\t\ttype: 'text',\n\t\t...(label !== undefined ? { label } : {}),\n\t\t...(required !== undefined ? { required } : {}),\n\t\t...(localized !== undefined ? { localized } : {}),\n\t\tadmin: {\n\t\t\tcomponents: {\n\t\t\t\tCell: { path: '@10x-media/fields/rsc#ColorCell' },\n\t\t\t\tField: {\n\t\t\t\t\tclientProps: { colorOptions: clientOptions },\n\t\t\t\t\tpath: '@10x-media/fields/rsc#ColorFieldServer',\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tcustom: { [COLOR_CUSTOM_KEY]: custom },\n\t\thooks: { beforeValidate: [buildNormalizeHook({ alpha, format, linked })] },\n\t\tvalidate: buildValidate({ linked, memoKey, presets }),\n\t}\n\n\tconst field = typeof overrides === 'function' ? overrides({ field: base }) : base\n\n\tif (!linked) return field\n\n\tconst loggedRequests = new WeakSet<PayloadRequest>()\n\n\t/**\n\t * Shapes a resolved value for the configured sibling. 'value' flattens a\n\t * scheme to its light member so a field configured before schemes existed\n\t * keeps working when its resolver starts returning them; 'schemes' inflates\n\t * a flat color so consumers never branch on the shape.\n\t */\n\tconst shape = (value: null | string | ColorSchemeValue): null | string | ColorSchemeValue => {\n\t\tif (value === null) return null\n\t\tif (resolveMode === 'value') return flatValue(value)\n\t\treturn isColorSchemeValue(value) ? value : { dark: value, light: value }\n\t}\n\n\t/** The stored reference alpha, applied to whatever the reference resolved to. */\n\tconst withAlpha = (\n\t\tresolved: null | string | ColorSchemeValue,\n\t\trefAlpha: number,\n\t\treq: PayloadRequest\n\t): null | string | ColorSchemeValue => {\n\t\tconst effective = alpha ? refAlpha : 100\n\t\tif (resolved === null || effective === 100) return resolved\n\t\treturn applyReferenceAlpha(resolved, effective, resolveColorFormat(format, req))\n\t}\n\n\tconst resolveHook: FieldHook = async ({ collection, global, req, siblingData }) => {\n\t\tconst raw = siblingData?.[field.name]\n\t\tif (typeof raw !== 'string' || raw === '') return null\n\t\tif (!raw.startsWith(PRESET_PREFIX)) return shape(raw)\n\t\tconst ref = parsePresetReference(raw)\n\t\tconst key = ref?.key ?? raw.slice(PRESET_PREFIX.length)\n\t\tconst refAlpha = ref?.alpha ?? 100\n\t\ttry {\n\t\t\tconst all = await resolvePresets({ memoKey, req, source: presets })\n\t\t\tconst match = all.find((preset) => preset.key === key)\n\t\t\treturn withAlpha(shape(match ? match.value : linkedFallback), refAlpha, req)\n\t\t} catch (error) {\n\t\t\t// A broken resolver must not take down reads; degrade like a missing preset\n\t\t\tif (!loggedRequests.has(req)) {\n\t\t\t\tloggedRequests.add(req)\n\t\t\t\tconst slug = collection?.slug ?? global?.slug ?? 'unknown'\n\t\t\t\treq.payload.logger.error(\n\t\t\t\t\t{ err: error },\n\t\t\t\t\t`colorField preset resolver failed for ${slug}.${field.name}`\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn withAlpha(shape(linkedFallback), refAlpha, req)\n\t\t}\n\t}\n\n\tconst resolvedAdmin = { disableListColumn: true, hidden: true } as const\n\n\tconst resolvedField: JSONField | TextField =\n\t\tresolveMode === 'schemes'\n\t\t\t? {\n\t\t\t\t\tname: `${field.name}Resolved`,\n\t\t\t\t\ttype: 'json',\n\t\t\t\t\tadmin: resolvedAdmin,\n\t\t\t\t\thooks: { afterRead: [resolveHook] },\n\t\t\t\t\tvirtual: true,\n\t\t\t\t}\n\t\t\t: {\n\t\t\t\t\tname: `${field.name}Resolved`,\n\t\t\t\t\ttype: 'text',\n\t\t\t\t\tadmin: resolvedAdmin,\n\t\t\t\t\thooks: { afterRead: [resolveHook] },\n\t\t\t\t\tvirtual: true,\n\t\t\t\t}\n\n\treturn [field, resolvedField]\n}\n"],"mappings":";;;;;;;;;;;;AA0BA,MAAM,iBACJ,SAKD,OAAO,OAAO,SAAS;CACtB,IAAI,OAAO,UAAU,YAAY,UAAU;MACtC,KAAK,UAAU,MAAM,WAAA,SAAwB,GAAG;GACnD,IAAI,SAA4C;GAChD,IAAI,qBAAqB,KAAK,GAAG,UAChC,IAAI;IACH,MAAM,MAAM,MAAM,eAAe;KAChC,SAAS,KAAK;KACd,KAAK,KAAK;KACV,QAAQ,KAAK;IACd,CAAC;IAGD,IAAI,IAAI,SAAS,GAAG,UAAU,QAAQ,IAAI,MAA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AG;EACpC,MAAM,MAAM,KAAK,OAAO,IAAI,MAAA,CAA0B;EACtD,MAAM,WAAW,KAAK,SAAS;EAC/B,IAAI;GAEH,MAAM,SAAQ,MADI,eAAe;IAAE;IAAS;IAAK,QAAQ;GAAQ,CAAC,GAChD,MAAM,WAAW,OAAO,QAAQ,GAAG;GACrD,OAAO,UAAU,MAAM,QAAQ,MAAM,QAAQ,cAAc,GAAG,UAAU,GAAG;EAC5E,SAAS,OAAO;GAEf,IAAI,CAAC,eAAe,IAAI,GAAG,GAAG;IAC7B,eAAe,IAAI,GAAG;IACtB,MAAM,OAAO,YAAY,QAAQ,QAAQ,QAAQ;IACjD,IAAI,QAAQ,OAAO,MAClB,EAAE,KAAK,MAAM,GACb,yCAAyC,KAAK,GAAG,MAAM,MACxD;GACD;GACA,OAAO,UAAU,MAAM,cAAc,GAAG,UAAU,GAAG;EACtD;CACD;CAEA,MAAM,gBAAgB;EAAE,mBAAmB;EAAM,QAAQ;CAAK;CAmB9D,OAAO,CAAC,OAhBP,gBAAgB,YACb;EACA,MAAM,GAAG,MAAM,KAAK;EACpB,MAAM;EACN,OAAO;EACP,OAAO,EAAE,WAAW,CAAC,WAAW,EAAE;EAClC,SAAS;CACV,IACC;EACA,MAAM,GAAG,MAAM,KAAK;EACpB,MAAM;EACN,OAAO;EACP,OAAO,EAAE,WAAW,CAAC,WAAW,EAAE;EAClC,SAAS;CACV,CAEyB;AAC7B"}
|
|
@@ -5,4 +5,5 @@ import { hslToRgb, hsvToRgb, isInSrgbGamut, oklchToRgb, rgbToHsl, rgbToHsv, rgbT
|
|
|
5
5
|
import { convertColor, formatColor } from "./format.js";
|
|
6
6
|
import { namedColors } from "./namedColors.js";
|
|
7
7
|
import { parseColor } from "./parse.js";
|
|
8
|
-
import { salvageColor } from "./salvage.js";
|
|
8
|
+
import { salvageColor } from "./salvage.js";
|
|
9
|
+
import { isColorSchemeValue, lightDark } from "./scheme.js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ColorSchemeValue } from "../../../types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/fields/color/engine/scheme.d.ts
|
|
4
|
+
/** Narrows the preset and resolved-value unions to a scheme-aware color. */
|
|
5
|
+
declare const isColorSchemeValue: (value: unknown) => value is ColorSchemeValue;
|
|
6
|
+
/**
|
|
7
|
+
* CSS for a scheme-aware color. Flat strings pass through unchanged. The
|
|
8
|
+
* two-argument form needs `color-scheme` set on the element to resolve.
|
|
9
|
+
*/
|
|
10
|
+
declare const lightDark: (value: ColorSchemeValue | string) => string;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { isColorSchemeValue, lightDark };
|
|
13
|
+
//# sourceMappingURL=scheme.d.ts.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/fields/color/engine/scheme.ts
|
|
2
|
+
/** Narrows the preset and resolved-value unions to a scheme-aware color. */
|
|
3
|
+
const isColorSchemeValue = (value) => typeof value === "object" && value !== null && typeof value.light === "string" && typeof value.dark === "string";
|
|
4
|
+
/**
|
|
5
|
+
* CSS for a scheme-aware color. Flat strings pass through unchanged. The
|
|
6
|
+
* two-argument form needs `color-scheme` set on the element to resolve.
|
|
7
|
+
*/
|
|
8
|
+
const lightDark = (value) => isColorSchemeValue(value) ? `light-dark(${value.light}, ${value.dark})` : value;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { isColorSchemeValue, lightDark };
|
|
11
|
+
|
|
12
|
+
//# sourceMappingURL=scheme.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scheme.js","names":[],"sources":["../../../../src/fields/color/engine/scheme.ts"],"sourcesContent":["import type { ColorSchemeValue } from '../../../types'\n\n/** Narrows the preset and resolved-value unions to a scheme-aware color. */\nexport const isColorSchemeValue = (value: unknown): value is ColorSchemeValue =>\n\ttypeof value === 'object' &&\n\tvalue !== null &&\n\ttypeof (value as Partial<ColorSchemeValue>).light === 'string' &&\n\ttypeof (value as Partial<ColorSchemeValue>).dark === 'string'\n\n/**\n * CSS for a scheme-aware color. Flat strings pass through unchanged. The\n * two-argument form needs `color-scheme` set on the element to resolve.\n */\nexport const lightDark = (value: ColorSchemeValue | string): string =>\n\tisColorSchemeValue(value) ? `light-dark(${value.light}, ${value.dark})` : value\n"],"mappings":";;AAGA,MAAa,sBAAsB,UAClC,OAAO,UAAU,YACjB,UAAU,QACV,OAAQ,MAAoC,UAAU,YACtD,OAAQ,MAAoC,SAAS;;;;;AAMtD,MAAa,aAAa,UACzB,mBAAmB,KAAK,IAAI,cAAc,MAAM,MAAM,IAAI,MAAM,KAAK,KAAK"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ColorFormat, ColorPreset, FieldsResolverArgs } from "../../types.js";
|
|
1
|
+
import { ColorFormat, ColorPreset, ColorSchemeValue, FieldsResolverArgs } from "../../types.js";
|
|
2
2
|
import { StaticLabel, TextField } from "payload";
|
|
3
3
|
|
|
4
4
|
//#region src/fields/color/options.d.ts
|
|
@@ -8,7 +8,8 @@ declare const COLOR_CUSTOM_KEY = "@10x-media/fields";
|
|
|
8
8
|
type ColorPresetsResolver = (args: FieldsResolverArgs) => Promise<ColorPreset[]>;
|
|
9
9
|
type ColorPresetsSource = ColorPreset[] | ColorPresetsResolver;
|
|
10
10
|
type ColorLinkedOptions = {
|
|
11
|
-
/** Resolved value when a stored preset reference no longer exists. Defaults to null. */fallback?: null | string;
|
|
11
|
+
/** Resolved value when a stored preset reference no longer exists. Defaults to null. */fallback?: null | string | ColorSchemeValue; /** Shape of the virtual sibling. 'value' emits text, 'schemes' emits json. Defaults to 'value'. */
|
|
12
|
+
resolve?: 'schemes' | 'value';
|
|
12
13
|
};
|
|
13
14
|
type ColorFieldOptions = {
|
|
14
15
|
name?: string;
|
|
@@ -33,7 +34,7 @@ type ColorFieldClientOptions = {
|
|
|
33
34
|
format: ColorFormat;
|
|
34
35
|
isClearable: boolean;
|
|
35
36
|
linked: boolean;
|
|
36
|
-
linkedFallback: null | string;
|
|
37
|
+
linkedFallback: null | string | ColorSchemeValue;
|
|
37
38
|
};
|
|
38
39
|
/**
|
|
39
40
|
* clientProps shape the factory hands to `ColorFieldServer`. `format` stays the
|
|
@@ -48,7 +49,7 @@ type ColorFieldServerOptions = Omit<ColorFieldClientOptions, 'format'> & {
|
|
|
48
49
|
type ResolvedColorPreset = {
|
|
49
50
|
key: string;
|
|
50
51
|
label: string;
|
|
51
|
-
value: string;
|
|
52
|
+
value: string | ColorSchemeValue;
|
|
52
53
|
};
|
|
53
54
|
//#endregion
|
|
54
55
|
export { COLOR_CUSTOM_KEY, ColorFieldClientOptions, ColorFieldOptions, ColorFieldServerOptions, ColorLinkedOptions, ColorPresetsResolver, ColorPresetsSource, PRESET_PREFIX, ResolvedColorPreset };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"options.js","names":[],"sources":["../../../src/fields/color/options.ts"],"sourcesContent":["import type { StaticLabel, TextField } from 'payload'\nimport type { ColorFormat, ColorPreset, FieldsResolverArgs } from '../../types'\n\nexport const PRESET_PREFIX = 'preset:'\n\n/** Key under `field.custom` carrying server-only color config (may contain functions). */\nexport const COLOR_CUSTOM_KEY = '@10x-media/fields'\n\nexport type ColorPresetsResolver = (args: FieldsResolverArgs) => Promise<ColorPreset[]>\nexport type ColorPresetsSource = ColorPreset[] | ColorPresetsResolver\n\nexport type ColorLinkedOptions = {\n\t/** Resolved value when a stored preset reference no longer exists. Defaults to null. */\n\tfallback?: null | string\n}\n\nexport type ColorFieldOptions = {\n\tname?: string\n\tlabel?: TextField['label']\n\trequired?: boolean\n\tlocalized?: boolean\n\t/** Stored format. Any parseable CSS input is normalized to this. Default 'hex'. */\n\tformat?: ColorFormat\n\t/** Default true. False hides the opacity slider and strips alpha on write. */\n\talpha?: boolean\n\tpresets?: ColorPresetsSource\n\tpresetsLabel?: StaticLabel\n\tlinked?: boolean | ColorLinkedOptions\n\t/** Default true. False removes the inline clear control; required fields never render it. */\n\tisClearable?: boolean\n\t/** Default true. Chromium EyeDropper API, feature-detected at runtime. */\n\tenableEyedropper?: boolean\n\toverrides?: (args: { field: TextField }) => TextField\n}\n\n/** Serializable subset shipped to the admin client via clientProps. */\nexport type ColorFieldClientOptions = {\n\talpha: boolean\n\tenableEyedropper: boolean\n\tformat: ColorFormat\n\tisClearable: boolean\n\tlinked: boolean\n\tlinkedFallback: null | string\n}\n\n/**\n * clientProps shape the factory hands to `ColorFieldServer`. `format` stays the\n * field-level value (possibly unset) so the server can resolve the effective\n * format against the plugin registry per request before handing the client a\n * concrete `ColorFieldClientOptions`.\n */\nexport type ColorFieldServerOptions = Omit<ColorFieldClientOptions, 'format'> & {\n\tformat?: ColorFormat\n}\n\n/** Preset with its label resolved to a plain string, safe for clientProps. */\nexport type ResolvedColorPreset = { key: string; label: string; value: string }\n\nexport type ColorFieldCustom = {\n\tmemoKey: symbol\n\tpresets?: ColorPresetsSource\n\tpresetsLabel?: StaticLabel\n}\n"],"mappings":";AAGA,MAAa,gBAAgB;;AAG7B,MAAa,mBAAmB"}
|
|
1
|
+
{"version":3,"file":"options.js","names":[],"sources":["../../../src/fields/color/options.ts"],"sourcesContent":["import type { StaticLabel, TextField } from 'payload'\nimport type { ColorFormat, ColorPreset, ColorSchemeValue, FieldsResolverArgs } from '../../types'\n\nexport const PRESET_PREFIX = 'preset:'\n\n/** Key under `field.custom` carrying server-only color config (may contain functions). */\nexport const COLOR_CUSTOM_KEY = '@10x-media/fields'\n\nexport type ColorPresetsResolver = (args: FieldsResolverArgs) => Promise<ColorPreset[]>\nexport type ColorPresetsSource = ColorPreset[] | ColorPresetsResolver\n\nexport type ColorLinkedOptions = {\n\t/** Resolved value when a stored preset reference no longer exists. Defaults to null. */\n\tfallback?: null | string | ColorSchemeValue\n\t/** Shape of the virtual sibling. 'value' emits text, 'schemes' emits json. Defaults to 'value'. */\n\tresolve?: 'schemes' | 'value'\n}\n\nexport type ColorFieldOptions = {\n\tname?: string\n\tlabel?: TextField['label']\n\trequired?: boolean\n\tlocalized?: boolean\n\t/** Stored format. Any parseable CSS input is normalized to this. Default 'hex'. */\n\tformat?: ColorFormat\n\t/** Default true. False hides the opacity slider and strips alpha on write. */\n\talpha?: boolean\n\tpresets?: ColorPresetsSource\n\tpresetsLabel?: StaticLabel\n\tlinked?: boolean | ColorLinkedOptions\n\t/** Default true. False removes the inline clear control; required fields never render it. */\n\tisClearable?: boolean\n\t/** Default true. Chromium EyeDropper API, feature-detected at runtime. */\n\tenableEyedropper?: boolean\n\toverrides?: (args: { field: TextField }) => TextField\n}\n\n/** Serializable subset shipped to the admin client via clientProps. */\nexport type ColorFieldClientOptions = {\n\talpha: boolean\n\tenableEyedropper: boolean\n\tformat: ColorFormat\n\tisClearable: boolean\n\tlinked: boolean\n\tlinkedFallback: null | string | ColorSchemeValue\n}\n\n/**\n * clientProps shape the factory hands to `ColorFieldServer`. `format` stays the\n * field-level value (possibly unset) so the server can resolve the effective\n * format against the plugin registry per request before handing the client a\n * concrete `ColorFieldClientOptions`.\n */\nexport type ColorFieldServerOptions = Omit<ColorFieldClientOptions, 'format'> & {\n\tformat?: ColorFormat\n}\n\n/** Preset with its label resolved to a plain string, safe for clientProps. */\nexport type ResolvedColorPreset = { key: string; label: string; value: string | ColorSchemeValue }\n\nexport type ColorFieldCustom = {\n\tmemoKey: symbol\n\tpresets?: ColorPresetsSource\n\tpresetsLabel?: StaticLabel\n}\n"],"mappings":";AAGA,MAAa,gBAAgB;;AAG7B,MAAa,mBAAmB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//#region src/fields/color/paths.ts
|
|
2
|
+
/**
|
|
3
|
+
* Rows, collapsibles, and unnamed tabs are already inlined by Payload's
|
|
4
|
+
* flattening, so each dot segment only ever names a data field or a named
|
|
5
|
+
* container (group, named tab, array) carrying nested flattenedFields.
|
|
6
|
+
*/
|
|
7
|
+
const findNamedField = (fields, path) => {
|
|
8
|
+
let scope = fields;
|
|
9
|
+
let found;
|
|
10
|
+
for (const segment of path.split(".")) {
|
|
11
|
+
found = scope?.find((field) => field.name === segment);
|
|
12
|
+
if (!found) return void 0;
|
|
13
|
+
scope = "flattenedFields" in found ? found.flattenedFields : void 0;
|
|
14
|
+
}
|
|
15
|
+
return found;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Walks a dot path over doc data. The array guard rejects indexing *into* an
|
|
19
|
+
* array, so a path ending at an array field still returns the array itself.
|
|
20
|
+
*/
|
|
21
|
+
const readPath = (doc, path) => {
|
|
22
|
+
let value = doc;
|
|
23
|
+
for (const segment of path.split(".")) {
|
|
24
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return void 0;
|
|
25
|
+
value = value[segment];
|
|
26
|
+
}
|
|
27
|
+
return value;
|
|
28
|
+
};
|
|
29
|
+
//#endregion
|
|
30
|
+
export { findNamedField, readPath };
|
|
31
|
+
|
|
32
|
+
//# sourceMappingURL=paths.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paths.js","names":[],"sources":["../../../src/fields/color/paths.ts"],"sourcesContent":["import type { FlattenedField } from 'payload'\n\n/**\n * Rows, collapsibles, and unnamed tabs are already inlined by Payload's\n * flattening, so each dot segment only ever names a data field or a named\n * container (group, named tab, array) carrying nested flattenedFields.\n */\nexport const findNamedField = (\n\tfields: FlattenedField[] | undefined,\n\tpath: string\n): FlattenedField | undefined => {\n\tlet scope = fields\n\tlet found: FlattenedField | undefined\n\tfor (const segment of path.split('.')) {\n\t\tfound = scope?.find((field) => field.name === segment)\n\t\tif (!found) return undefined\n\t\tscope = 'flattenedFields' in found ? found.flattenedFields : undefined\n\t}\n\treturn found\n}\n\n/**\n * Walks a dot path over doc data. The array guard rejects indexing *into* an\n * array, so a path ending at an array field still returns the array itself.\n */\nexport const readPath = (doc: Record<string, unknown>, path: string): unknown => {\n\tlet value: unknown = doc\n\tfor (const segment of path.split('.')) {\n\t\tif (value === null || typeof value !== 'object' || Array.isArray(value)) return undefined\n\t\tvalue = (value as Record<string, unknown>)[segment]\n\t}\n\treturn value\n}\n"],"mappings":";;;;;;AAOA,MAAa,kBACZ,QACA,SACgC;CAChC,IAAI,QAAQ;CACZ,IAAI;CACJ,KAAK,MAAM,WAAW,KAAK,MAAM,GAAG,GAAG;EACtC,QAAQ,OAAO,MAAM,UAAU,MAAM,SAAS,OAAO;EACrD,IAAI,CAAC,OAAO,OAAO,KAAA;EACnB,QAAQ,qBAAqB,QAAQ,MAAM,kBAAkB,KAAA;CAC9D;CACA,OAAO;AACR;;;;;AAMA,MAAa,YAAY,KAA8B,SAA0B;CAChF,IAAI,QAAiB;CACrB,KAAK,MAAM,WAAW,KAAK,MAAM,GAAG,GAAG;EACtC,IAAI,UAAU,QAAQ,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,GAAG,OAAO,KAAA;EAChF,QAAS,MAAkC;CAC5C;CACA,OAAO;AACR"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parsePresetReference } from "./presetReference.js";
|
|
2
2
|
//#region src/fields/color/presetChip.ts
|
|
3
3
|
/**
|
|
4
|
-
* Derives the chip shown in place of a raw `preset:<key
|
|
5
|
-
* input. Returns null outside chip mode: non-linked fields, active text
|
|
4
|
+
* Derives the chip shown in place of a raw `preset:<key>[/<alpha>]` ref in the
|
|
5
|
+
* color input. Returns null outside chip mode: non-linked fields, active text
|
|
6
6
|
* editing, non-ref values, or an empty key (which validate rejects anyway).
|
|
7
7
|
* A ref whose key no longer resolves still chips (missing: true, label falls
|
|
8
8
|
* back to the key) so the stored ref stays visible without leaking raw text.
|
|
@@ -10,13 +10,14 @@ import { PRESET_PREFIX } from "./options.js";
|
|
|
10
10
|
const derivePresetChip = (args) => {
|
|
11
11
|
const { editing, linked, presets, value } = args;
|
|
12
12
|
if (!linked || editing) return null;
|
|
13
|
-
if (typeof value !== "string"
|
|
14
|
-
const
|
|
15
|
-
if (
|
|
16
|
-
const preset = presets.find((entry) => entry.key === key);
|
|
13
|
+
if (typeof value !== "string") return null;
|
|
14
|
+
const ref = parsePresetReference(value);
|
|
15
|
+
if (!ref) return null;
|
|
16
|
+
const preset = presets.find((entry) => entry.key === ref.key);
|
|
17
17
|
return {
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
alpha: ref.alpha,
|
|
19
|
+
key: ref.key,
|
|
20
|
+
label: preset?.label ?? ref.key,
|
|
20
21
|
missing: !preset,
|
|
21
22
|
value: preset?.value ?? null
|
|
22
23
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"presetChip.js","names":[],"sources":["../../../src/fields/color/presetChip.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"presetChip.js","names":[],"sources":["../../../src/fields/color/presetChip.ts"],"sourcesContent":["import type { ColorSchemeValue } from '../../types'\nimport type { ResolvedColorPreset } from './options'\nimport { parsePresetReference } from './presetReference'\n\nexport type PresetChip = {\n\talpha: number\n\tkey: string\n\tlabel: string\n\tmissing: boolean\n\tvalue: null | string | ColorSchemeValue\n}\n\n/**\n * Derives the chip shown in place of a raw `preset:<key>[/<alpha>]` ref in the\n * color input. Returns null outside chip mode: non-linked fields, active text\n * editing, non-ref values, or an empty key (which validate rejects anyway).\n * A ref whose key no longer resolves still chips (missing: true, label falls\n * back to the key) so the stored ref stays visible without leaking raw text.\n */\nexport const derivePresetChip = (args: {\n\tediting: boolean\n\tlinked: boolean\n\tpresets: ResolvedColorPreset[]\n\tvalue: unknown\n}): null | PresetChip => {\n\tconst { editing, linked, presets, value } = args\n\tif (!linked || editing) return null\n\tif (typeof value !== 'string') return null\n\tconst ref = parsePresetReference(value)\n\tif (!ref) return null\n\tconst preset = presets.find((entry) => entry.key === ref.key)\n\treturn {\n\t\talpha: ref.alpha,\n\t\tkey: ref.key,\n\t\tlabel: preset?.label ?? ref.key,\n\t\tmissing: !preset,\n\t\tvalue: preset?.value ?? null,\n\t}\n}\n"],"mappings":";;;;;;;;;AAmBA,MAAa,oBAAoB,SAKR;CACxB,MAAM,EAAE,SAAS,QAAQ,SAAS,UAAU;CAC5C,IAAI,CAAC,UAAU,SAAS,OAAO;CAC/B,IAAI,OAAO,UAAU,UAAU,OAAO;CACtC,MAAM,MAAM,qBAAqB,KAAK;CACtC,IAAI,CAAC,KAAK,OAAO;CACjB,MAAM,SAAS,QAAQ,MAAM,UAAU,MAAM,QAAQ,IAAI,GAAG;CAC5D,OAAO;EACN,OAAO,IAAI;EACX,KAAK,IAAI;EACT,OAAO,QAAQ,SAAS,IAAI;EAC5B,SAAS,CAAC;EACV,OAAO,QAAQ,SAAS;CACzB;AACD"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/fields/color/presetReference.d.ts
|
|
2
|
+
type PresetReference = {
|
|
3
|
+
alpha: number;
|
|
4
|
+
key: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Client-safe parser for stored preset references. Returns null when the value
|
|
8
|
+
* is not a reference; alpha defaults to 100 when no suffix is present.
|
|
9
|
+
*/
|
|
10
|
+
declare const parsePresetReference: (value: string) => null | PresetReference;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { PresetReference, parsePresetReference };
|
|
13
|
+
//# sourceMappingURL=presetReference.d.ts.map
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { parseColor } from "./engine/parse.js";
|
|
2
|
+
import { formatColor } from "./engine/format.js";
|
|
3
|
+
import { isColorSchemeValue } from "./engine/scheme.js";
|
|
4
|
+
import { PRESET_PREFIX } from "./options.js";
|
|
5
|
+
//#region src/fields/color/presetReference.ts
|
|
6
|
+
const ALPHA_SUFFIX = /^(100|[1-9]?[0-9])$/;
|
|
7
|
+
const ALPHA_LIKE = /^[0-9.]+$/;
|
|
8
|
+
/**
|
|
9
|
+
* Splits a `preset:<key>[/<alpha>]` string into key and alpha, tracking whether
|
|
10
|
+
* the alpha was written out. The suffix is read off the last slash and counts
|
|
11
|
+
* as alpha only when it matches the integer grammar 0-100; anything else stays
|
|
12
|
+
* part of the key, so slash-containing keys keep working.
|
|
13
|
+
*/
|
|
14
|
+
const presetReferenceParts = (value) => {
|
|
15
|
+
if (!value.startsWith("preset:")) return null;
|
|
16
|
+
const remainder = value.slice(PRESET_PREFIX.length);
|
|
17
|
+
if (remainder === "") return null;
|
|
18
|
+
const slash = remainder.lastIndexOf("/");
|
|
19
|
+
if (slash !== -1 && ALPHA_SUFFIX.test(remainder.slice(slash + 1))) {
|
|
20
|
+
const key = remainder.slice(0, slash);
|
|
21
|
+
if (key === "") return null;
|
|
22
|
+
return {
|
|
23
|
+
alpha: Number(remainder.slice(slash + 1)),
|
|
24
|
+
explicit: true,
|
|
25
|
+
key
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
alpha: 100,
|
|
30
|
+
explicit: false,
|
|
31
|
+
key: remainder
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Client-safe parser for stored preset references. Returns null when the value
|
|
36
|
+
* is not a reference; alpha defaults to 100 when no suffix is present.
|
|
37
|
+
*/
|
|
38
|
+
const parsePresetReference = (value) => {
|
|
39
|
+
const parts = presetReferenceParts(value);
|
|
40
|
+
return parts ? {
|
|
41
|
+
alpha: parts.alpha,
|
|
42
|
+
key: parts.key
|
|
43
|
+
} : null;
|
|
44
|
+
};
|
|
45
|
+
/** Canonical stored form: bare reference at alpha 100, `/<alpha>` suffix below. */
|
|
46
|
+
const formatPresetReference = (key, alpha = 100) => {
|
|
47
|
+
const bounded = Math.min(100, Math.max(0, Math.round(alpha)));
|
|
48
|
+
return bounded === 100 ? `${PRESET_PREFIX}${key}` : `${PRESET_PREFIX}${key}/${bounded}`;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Validation triage for a linked value starting with `preset:`. Bare references
|
|
52
|
+
* stay lenient (a stale key degrades to the fallback at read time), but an
|
|
53
|
+
* explicit alpha suffix requires the key to exist, and a numeric-looking
|
|
54
|
+
* suffix outside the 0-100 grammar is rejected as an attempted alpha rather
|
|
55
|
+
* than silently treated as a key. Pass `hasKey: null` to skip the existence
|
|
56
|
+
* check (no presets available, or the resolver failed).
|
|
57
|
+
*/
|
|
58
|
+
const presetReferenceIssue = (value, hasKey) => {
|
|
59
|
+
const parts = presetReferenceParts(value);
|
|
60
|
+
if (!parts) return "missingKey";
|
|
61
|
+
if (!parts.explicit) {
|
|
62
|
+
const slash = parts.key.lastIndexOf("/");
|
|
63
|
+
const tail = slash === -1 ? "" : parts.key.slice(slash + 1);
|
|
64
|
+
if (tail !== "" && ALPHA_LIKE.test(tail)) return "invalidAlpha";
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
if (hasKey && !hasKey(parts.key)) return "unknownKey";
|
|
68
|
+
return null;
|
|
69
|
+
};
|
|
70
|
+
const applyToCss = (css, alpha, format) => {
|
|
71
|
+
const parsed = parseColor(css);
|
|
72
|
+
if (!parsed) return css;
|
|
73
|
+
const base = Number.isFinite(parsed.alpha) ? parsed.alpha : 1;
|
|
74
|
+
return formatColor({
|
|
75
|
+
...parsed,
|
|
76
|
+
alpha: base * (alpha / 100)
|
|
77
|
+
}, format);
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Applies a reference alpha to a resolved preset value, multiplying into any
|
|
81
|
+
* alpha the color already carries (matching CSS `color-mix` toward
|
|
82
|
+
* transparent). Unparseable members pass through untouched.
|
|
83
|
+
*/
|
|
84
|
+
const applyReferenceAlpha = (value, alpha, format) => {
|
|
85
|
+
if (isColorSchemeValue(value)) return {
|
|
86
|
+
dark: applyToCss(value.dark, alpha, format),
|
|
87
|
+
light: applyToCss(value.light, alpha, format)
|
|
88
|
+
};
|
|
89
|
+
return applyToCss(value, alpha, format);
|
|
90
|
+
};
|
|
91
|
+
//#endregion
|
|
92
|
+
export { applyReferenceAlpha, formatPresetReference, parsePresetReference, presetReferenceIssue, presetReferenceParts };
|
|
93
|
+
|
|
94
|
+
//# sourceMappingURL=presetReference.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presetReference.js","names":[],"sources":["../../../src/fields/color/presetReference.ts"],"sourcesContent":["import type { ColorFormat, ColorSchemeValue } from '../../types'\nimport { formatColor, isColorSchemeValue, parseColor } from './engine'\nimport { PRESET_PREFIX } from './options'\n\nexport type PresetReference = { alpha: number; key: string }\n\nexport type PresetReferenceParts = PresetReference & { explicit: boolean }\n\nexport type PresetReferenceIssue = 'invalidAlpha' | 'missingKey' | 'unknownKey'\n\nconst ALPHA_SUFFIX = /^(100|[1-9]?[0-9])$/\nconst ALPHA_LIKE = /^[0-9.]+$/\n\n/**\n * Splits a `preset:<key>[/<alpha>]` string into key and alpha, tracking whether\n * the alpha was written out. The suffix is read off the last slash and counts\n * as alpha only when it matches the integer grammar 0-100; anything else stays\n * part of the key, so slash-containing keys keep working.\n */\nexport const presetReferenceParts = (value: string): null | PresetReferenceParts => {\n\tif (!value.startsWith(PRESET_PREFIX)) return null\n\tconst remainder = value.slice(PRESET_PREFIX.length)\n\tif (remainder === '') return null\n\tconst slash = remainder.lastIndexOf('/')\n\tif (slash !== -1 && ALPHA_SUFFIX.test(remainder.slice(slash + 1))) {\n\t\tconst key = remainder.slice(0, slash)\n\t\tif (key === '') return null\n\t\treturn { alpha: Number(remainder.slice(slash + 1)), explicit: true, key }\n\t}\n\treturn { alpha: 100, explicit: false, key: remainder }\n}\n\n/**\n * Client-safe parser for stored preset references. Returns null when the value\n * is not a reference; alpha defaults to 100 when no suffix is present.\n */\nexport const parsePresetReference = (value: string): null | PresetReference => {\n\tconst parts = presetReferenceParts(value)\n\treturn parts ? { alpha: parts.alpha, key: parts.key } : null\n}\n\n/** Canonical stored form: bare reference at alpha 100, `/<alpha>` suffix below. */\nexport const formatPresetReference = (key: string, alpha = 100): string => {\n\tconst bounded = Math.min(100, Math.max(0, Math.round(alpha)))\n\treturn bounded === 100 ? `${PRESET_PREFIX}${key}` : `${PRESET_PREFIX}${key}/${bounded}`\n}\n\n/**\n * Validation triage for a linked value starting with `preset:`. Bare references\n * stay lenient (a stale key degrades to the fallback at read time), but an\n * explicit alpha suffix requires the key to exist, and a numeric-looking\n * suffix outside the 0-100 grammar is rejected as an attempted alpha rather\n * than silently treated as a key. Pass `hasKey: null` to skip the existence\n * check (no presets available, or the resolver failed).\n */\nexport const presetReferenceIssue = (\n\tvalue: string,\n\thasKey: ((key: string) => boolean) | null\n): null | PresetReferenceIssue => {\n\tconst parts = presetReferenceParts(value)\n\tif (!parts) return 'missingKey'\n\tif (!parts.explicit) {\n\t\tconst slash = parts.key.lastIndexOf('/')\n\t\tconst tail = slash === -1 ? '' : parts.key.slice(slash + 1)\n\t\tif (tail !== '' && ALPHA_LIKE.test(tail)) return 'invalidAlpha'\n\t\treturn null\n\t}\n\tif (hasKey && !hasKey(parts.key)) return 'unknownKey'\n\treturn null\n}\n\nconst applyToCss = (css: string, alpha: number, format: ColorFormat): string => {\n\tconst parsed = parseColor(css)\n\tif (!parsed) return css\n\tconst base = Number.isFinite(parsed.alpha) ? parsed.alpha : 1\n\treturn formatColor({ ...parsed, alpha: base * (alpha / 100) }, format)\n}\n\n/**\n * Applies a reference alpha to a resolved preset value, multiplying into any\n * alpha the color already carries (matching CSS `color-mix` toward\n * transparent). Unparseable members pass through untouched.\n */\nexport const applyReferenceAlpha = (\n\tvalue: string | ColorSchemeValue,\n\talpha: number,\n\tformat: ColorFormat\n): string | ColorSchemeValue => {\n\tif (isColorSchemeValue(value)) {\n\t\treturn {\n\t\t\tdark: applyToCss(value.dark, alpha, format),\n\t\t\tlight: applyToCss(value.light, alpha, format),\n\t\t}\n\t}\n\treturn applyToCss(value, alpha, format)\n}\n"],"mappings":";;;;;AAUA,MAAM,eAAe;AACrB,MAAM,aAAa;;;;;;;AAQnB,MAAa,wBAAwB,UAA+C;CACnF,IAAI,CAAC,MAAM,WAAA,SAAwB,GAAG,OAAO;CAC7C,MAAM,YAAY,MAAM,MAAM,cAAc,MAAM;CAClD,IAAI,cAAc,I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AAO,QAAQ;CAC5D,OAAO,YAAY;EAAE,GAAG;EAAQ,OAAO,QAAQ,QAAQ;CAAK,GAAG,MAAM;AACtE;;;;;;AAOA,MAAa,uBACZ,OACA,OACA,WAC+B;CAC/B,IAAI,mBAAmB,KAAK,GAC3B,OAAO;EACN,MAAM,WAAW,MAAM,MAAM,OAAO,MAAM;EAC1C,OAAO,WAAW,MAAM,OAAO,OAAO,MAAM;CAC7C;CAED,OAAO,WAAW,OAAO,OAAO,MAAM;AACvC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ColorPreset } from "../../types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/fields/color/presetsFromArray.d.ts
|
|
4
|
+
type PresetsFromArrayArgs = {
|
|
5
|
+
/** The fetched doc holding the array. */doc: Record<string, unknown>; /** Row field holding the stable key. */
|
|
6
|
+
key: string; /** Prepended to every preset key, e.g. `${tenant.slug}/` to namespace per doc. */
|
|
7
|
+
keyPrefix?: string; /** Row field holding the label. Falls back to the unprefixed key. */
|
|
8
|
+
label?: string; /** Dot path to the array field. Reaches into groups and named tabs. */
|
|
9
|
+
path: string; /** Row field holding the value, or the pair of row fields holding light and dark. */
|
|
10
|
+
value: string | {
|
|
11
|
+
light: string;
|
|
12
|
+
dark: string;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Lifts an array field's rows into linked-mode presets, which is how a
|
|
17
|
+
* repeatable brand palette is authored. Unlike `presetsFromDoc` it needs no
|
|
18
|
+
* `req` or `collection`: keys and labels come from row data rather than field
|
|
19
|
+
* config, so there is nothing to look up or localize.
|
|
20
|
+
*
|
|
21
|
+
* Rows are skipped when the key is missing, non-string or empty, when the value
|
|
22
|
+
* fields yield nothing usable, and when a value already carries `PRESET_PREFIX`.
|
|
23
|
+
* A scheme pair with one member present becomes that color in both schemes,
|
|
24
|
+
* matching how every other layer treats a half-filled value. On duplicate keys
|
|
25
|
+
* the first row wins, so callers generating keys must keep them unique.
|
|
26
|
+
*/
|
|
27
|
+
declare const presetsFromArray: (args: PresetsFromArrayArgs) => ColorPreset[];
|
|
28
|
+
//#endregion
|
|
29
|
+
export { PresetsFromArrayArgs, presetsFromArray };
|
|
30
|
+
//# sourceMappingURL=presetsFromArray.d.ts.map
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import "./options.js";
|
|
2
|
+
import { readPath } from "./paths.js";
|
|
3
|
+
//#region src/fields/color/presetsFromArray.ts
|
|
4
|
+
const rowString = (row, field) => {
|
|
5
|
+
const value = row[field];
|
|
6
|
+
if (typeof value !== "string" || value === "" || value.startsWith("preset:")) return null;
|
|
7
|
+
return value;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Lifts an array field's rows into linked-mode presets, which is how a
|
|
11
|
+
* repeatable brand palette is authored. Unlike `presetsFromDoc` it needs no
|
|
12
|
+
* `req` or `collection`: keys and labels come from row data rather than field
|
|
13
|
+
* config, so there is nothing to look up or localize.
|
|
14
|
+
*
|
|
15
|
+
* Rows are skipped when the key is missing, non-string or empty, when the value
|
|
16
|
+
* fields yield nothing usable, and when a value already carries `PRESET_PREFIX`.
|
|
17
|
+
* A scheme pair with one member present becomes that color in both schemes,
|
|
18
|
+
* matching how every other layer treats a half-filled value. On duplicate keys
|
|
19
|
+
* the first row wins, so callers generating keys must keep them unique.
|
|
20
|
+
*/
|
|
21
|
+
const presetsFromArray = (args) => {
|
|
22
|
+
const { doc, key, keyPrefix = "", label, path, value } = args;
|
|
23
|
+
const rows = readPath(doc, path);
|
|
24
|
+
if (!Array.isArray(rows)) return [];
|
|
25
|
+
const presets = [];
|
|
26
|
+
const seen = /* @__PURE__ */ new Set();
|
|
27
|
+
for (const entry of rows) {
|
|
28
|
+
if (entry === null || typeof entry !== "object" || Array.isArray(entry)) continue;
|
|
29
|
+
const row = entry;
|
|
30
|
+
const rawKey = row[key];
|
|
31
|
+
if (typeof rawKey !== "string" || rawKey === "") continue;
|
|
32
|
+
const presetKey = `${keyPrefix}${rawKey}`;
|
|
33
|
+
if (seen.has(presetKey)) continue;
|
|
34
|
+
let presetValue;
|
|
35
|
+
if (typeof value === "string") presetValue = rowString(row, value) ?? void 0;
|
|
36
|
+
else {
|
|
37
|
+
const light = rowString(row, value.light);
|
|
38
|
+
const dark = rowString(row, value.dark);
|
|
39
|
+
if (light || dark) presetValue = {
|
|
40
|
+
dark: dark ?? light,
|
|
41
|
+
light: light ?? dark
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (presetValue === void 0) continue;
|
|
45
|
+
seen.add(presetKey);
|
|
46
|
+
const rowLabel = label === void 0 ? void 0 : row[label];
|
|
47
|
+
presets.push({
|
|
48
|
+
key: presetKey,
|
|
49
|
+
label: typeof rowLabel === "string" && rowLabel !== "" ? rowLabel : rawKey,
|
|
50
|
+
value: presetValue
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return presets;
|
|
54
|
+
};
|
|
55
|
+
//#endregion
|
|
56
|
+
export { presetsFromArray };
|
|
57
|
+
|
|
58
|
+
//# sourceMappingURL=presetsFromArray.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presetsFromArray.js","names":[],"sources":["../../../src/fields/color/presetsFromArray.ts"],"sourcesContent":["import type { ColorPreset } from '../../types'\nimport { PRESET_PREFIX } from './options'\nimport { readPath } from './paths'\n\nexport type PresetsFromArrayArgs = {\n\t/** The fetched doc holding the array. */\n\tdoc: Record<string, unknown>\n\t/** Row field holding the stable key. */\n\tkey: string\n\t/** Prepended to every preset key, e.g. `${tenant.slug}/` to namespace per doc. */\n\tkeyPrefix?: string\n\t/** Row field holding the label. Falls back to the unprefixed key. */\n\tlabel?: string\n\t/** Dot path to the array field. Reaches into groups and named tabs. */\n\tpath: string\n\t/** Row field holding the value, or the pair of row fields holding light and dark. */\n\tvalue: string | { light: string; dark: string }\n}\n\nconst rowString = (row: Record<string, unknown>, field: string): null | string => {\n\tconst value = row[field]\n\tif (typeof value !== 'string' || value === '' || value.startsWith(PRESET_PREFIX)) return null\n\treturn value\n}\n\n/**\n * Lifts an array field's rows into linked-mode presets, which is how a\n * repeatable brand palette is authored. Unlike `presetsFromDoc` it needs no\n * `req` or `collection`: keys and labels come from row data rather than field\n * config, so there is nothing to look up or localize.\n *\n * Rows are skipped when the key is missing, non-string or empty, when the value\n * fields yield nothing usable, and when a value already carries `PRESET_PREFIX`.\n * A scheme pair with one member present becomes that color in both schemes,\n * matching how every other layer treats a half-filled value. On duplicate keys\n * the first row wins, so callers generating keys must keep them unique.\n */\nexport const presetsFromArray = (args: PresetsFromArrayArgs): ColorPreset[] => {\n\tconst { doc, key, keyPrefix = '', label, path, value } = args\n\tconst rows = readPath(doc, path)\n\tif (!Array.isArray(rows)) return []\n\n\tconst presets: ColorPreset[] = []\n\tconst seen = new Set<string>()\n\n\tfor (const entry of rows) {\n\t\tif (entry === null || typeof entry !== 'object' || Array.isArray(entry)) continue\n\t\tconst row = entry as Record<string, unknown>\n\n\t\tconst rawKey = row[key]\n\t\tif (typeof rawKey !== 'string' || rawKey === '') continue\n\t\tconst presetKey = `${keyPrefix}${rawKey}`\n\t\tif (seen.has(presetKey)) continue\n\n\t\tlet presetValue: undefined | string | { dark: string; light: string }\n\t\tif (typeof value === 'string') {\n\t\t\tpresetValue = rowString(row, value) ?? undefined\n\t\t} else {\n\t\t\tconst light = rowString(row, value.light)\n\t\t\tconst dark = rowString(row, value.dark)\n\t\t\tif (light || dark) {\n\t\t\t\tpresetValue = { dark: dark ?? (light as string), light: light ?? (dark as string) }\n\t\t\t}\n\t\t}\n\t\tif (presetValue === undefined) continue\n\n\t\tseen.add(presetKey)\n\t\tconst rowLabel = label === undefined ? undefined : row[label]\n\t\tpresets.push({\n\t\t\tkey: presetKey,\n\t\t\tlabel: typeof rowLabel === 'string' && rowLabel !== '' ? rowLabel : rawKey,\n\t\t\tvalue: presetValue,\n\t\t})\n\t}\n\n\treturn presets\n}\n"],"mappings":";;;AAmBA,MAAM,aAAa,KAA8B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