@perkyzz/emvb 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +59 -2
- package/THIRD_PARTY_NOTICES.md +32 -0
- package/licenses/fontawesome-free.LICENSE +165 -0
- package/licenses/lucide-static.LICENSE +43 -0
- package/licenses/remixicon.LICENSE +201 -0
- package/licenses/tabler-icons.LICENSE +21 -0
- package/package.json +76 -4
- package/src/admin/access/RequireEditor.tsx +39 -0
- package/src/admin/access/role.ts +36 -0
- package/src/admin/access/sidebar-shim.ts +108 -0
- package/src/admin/api.ts +75 -0
- package/src/admin/content-api.ts +210 -0
- package/src/admin/editor/Editor.tsx +532 -0
- package/src/admin/editor/EditorOverlay.tsx +66 -0
- package/src/admin/editor/ShortcutsDialog.tsx +30 -0
- package/src/admin/editor/SmallScreenNotice.tsx +21 -0
- package/src/admin/editor/TopBar.tsx +243 -0
- package/src/admin/editor/canvas/CanvasFrame.tsx +536 -0
- package/src/admin/editor/canvas/CanvasTextEdit.tsx +87 -0
- package/src/admin/editor/canvas/DeviceBar.tsx +91 -0
- package/src/admin/editor/canvas/EmptyCanvas.tsx +49 -0
- package/src/admin/editor/canvas/SelectionOverlay.tsx +239 -0
- package/src/admin/editor/canvas/canvas-events.ts +245 -0
- package/src/admin/editor/canvas/state-preview.ts +19 -0
- package/src/admin/editor/canvas/tab-reveal.ts +101 -0
- package/src/admin/editor/canvas/vnode-react.tsx +69 -0
- package/src/admin/editor/clipboard-store.ts +44 -0
- package/src/admin/editor/dialogs.tsx +164 -0
- package/src/admin/editor/dnd/drag-stash.ts +27 -0
- package/src/admin/editor/dnd/drop-target.ts +148 -0
- package/src/admin/editor/dnd/new-element.ts +107 -0
- package/src/admin/editor/editor-css.ts +625 -0
- package/src/admin/editor/exit.ts +27 -0
- package/src/admin/editor/host.ts +30 -0
- package/src/admin/editor/icons/IconLibraryDialog.tsx +850 -0
- package/src/admin/editor/icons/library.ts +243 -0
- package/src/admin/editor/icons/set-format.ts +29 -0
- package/src/admin/editor/icons/sets/fontawesome.json +1 -0
- package/src/admin/editor/icons/sets/lucide.json +1 -0
- package/src/admin/editor/icons/sets/remix.json +1 -0
- package/src/admin/editor/icons/sets/tabler.json +1 -0
- package/src/admin/editor/panels/AddPanel.tsx +146 -0
- package/src/admin/editor/panels/ClassesSection.tsx +372 -0
- package/src/admin/editor/panels/ColorControl.tsx +477 -0
- package/src/admin/editor/panels/ConditionsEditor.tsx +201 -0
- package/src/admin/editor/panels/ElementPanel.tsx +773 -0
- package/src/admin/editor/panels/LayersPanel.tsx +573 -0
- package/src/admin/editor/panels/LeftPanel.tsx +81 -0
- package/src/admin/editor/panels/PageSettings.tsx +255 -0
- package/src/admin/editor/panels/SettingsPanel.tsx +113 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +548 -0
- package/src/admin/editor/panels/TriggerSettings.tsx +132 -0
- package/src/admin/editor/panels/TriggersEditor.tsx +214 -0
- package/src/admin/editor/panels/VariableSection.tsx +530 -0
- package/src/admin/editor/panels/color-space.ts +195 -0
- package/src/admin/editor/panels/settings/AdvancedFields.tsx +119 -0
- package/src/admin/editor/panels/settings/BackgroundControls.tsx +418 -0
- package/src/admin/editor/panels/settings/BackgroundFill.tsx +221 -0
- package/src/admin/editor/panels/settings/BoxControl.tsx +271 -0
- package/src/admin/editor/panels/settings/ChoiceGroup.tsx +138 -0
- package/src/admin/editor/panels/settings/ClassChip.tsx +167 -0
- package/src/admin/editor/panels/settings/ClassChipInput.tsx +451 -0
- package/src/admin/editor/panels/settings/DraftTextField.tsx +242 -0
- package/src/admin/editor/panels/settings/EffectsControls.tsx +354 -0
- package/src/admin/editor/panels/settings/FieldBindControl.tsx +119 -0
- package/src/admin/editor/panels/settings/FieldControl.tsx +270 -0
- package/src/admin/editor/panels/settings/FormBindControl.tsx +106 -0
- package/src/admin/editor/panels/settings/IconControls.tsx +438 -0
- package/src/admin/editor/panels/settings/IconPicker.tsx +170 -0
- package/src/admin/editor/panels/settings/ItemList.tsx +199 -0
- package/src/admin/editor/panels/settings/LengthRow.tsx +217 -0
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +130 -0
- package/src/admin/editor/panels/settings/MediaPicker.tsx +220 -0
- package/src/admin/editor/panels/settings/NumberRow.tsx +154 -0
- package/src/admin/editor/panels/settings/StateSwitcher.tsx +87 -0
- package/src/admin/editor/panels/settings/StyleRow.tsx +563 -0
- package/src/admin/editor/panels/settings/VariableBinding.tsx +171 -0
- package/src/admin/editor/panels/settings/advanced-rules.ts +63 -0
- package/src/admin/editor/panels/settings/background-mode.ts +112 -0
- package/src/admin/editor/panels/settings/box-link-name.ts +38 -0
- package/src/admin/editor/panels/settings/box-sides.ts +153 -0
- package/src/admin/editor/panels/settings/button-url.ts +27 -0
- package/src/admin/editor/panels/settings/class-suggest.ts +53 -0
- package/src/admin/editor/panels/settings/form-names.ts +38 -0
- package/src/admin/editor/panels/settings/heading-warning.ts +25 -0
- package/src/admin/editor/panels/settings/image-alt.ts +25 -0
- package/src/admin/editor/panels/settings/length-units.ts +116 -0
- package/src/admin/editor/panels/settings/loop-context.ts +26 -0
- package/src/admin/editor/panels/settings/node-props.ts +11 -0
- package/src/admin/editor/panels/settings/style-sections.ts +394 -0
- package/src/admin/editor/panels/settings/svg-images.ts +14 -0
- package/src/admin/editor/panels/site-list.tsx +209 -0
- package/src/admin/editor/panels/variable-values.ts +73 -0
- package/src/admin/editor/restore-deleted.ts +80 -0
- package/src/admin/editor/section-templates.ts +50 -0
- package/src/admin/editor/shortcuts.ts +20 -0
- package/src/admin/editor/store.ts +240 -0
- package/src/admin/editor/useClipboardActions.ts +136 -0
- package/src/admin/editor/useEditorCommands.ts +203 -0
- package/src/admin/editor/useEditorData.ts +193 -0
- package/src/admin/editor/useEditorShortcuts.ts +180 -0
- package/src/admin/editor/useMediaQuery.ts +20 -0
- package/src/admin/editor/useNodeActions.ts +184 -0
- package/src/admin/editor/useSave.ts +242 -0
- package/src/admin/entry-status.ts +19 -0
- package/src/admin/forms-api.ts +73 -0
- package/src/admin/icon-uploads-api.ts +44 -0
- package/src/admin/index.tsx +20 -0
- package/src/admin/media-api.ts +68 -0
- package/src/admin/pages/CreateDialog.tsx +150 -0
- package/src/admin/pages/EditorPage.tsx +57 -0
- package/src/admin/pages/NewPageDialog.tsx +83 -0
- package/src/admin/pages/NewThemePartDialog.tsx +61 -0
- package/src/admin/pages/PageList.tsx +91 -0
- package/src/admin/pages/PagesPage.tsx +25 -0
- package/src/admin/pages/SetupShell.tsx +90 -0
- package/src/admin/pages/ThemeBuilderPage.tsx +25 -0
- package/src/admin/pages/ThemePartList.tsx +221 -0
- package/src/admin/pages/list-kit.tsx +125 -0
- package/src/admin/setup/plan.ts +150 -0
- package/src/admin/setup/run.ts +61 -0
- package/src/admin/setup/spec.ts +107 -0
- package/src/admin/setup/useSetupStatus.ts +62 -0
- package/src/admin/theme-api.ts +233 -0
- package/src/admin/ui.ts +244 -0
- package/src/admin/widgets/LayoutField.tsx +67 -0
- package/src/astro/EmVBPage.astro +63 -0
- package/src/astro/env.d.ts +5 -0
- package/src/astro/floats/EmVBFloatsRuntime.astro +9 -0
- package/src/astro/floats/index.ts +1 -0
- package/src/astro/floats/runtime.ts +78 -0
- package/src/astro/forms/EmVBFormsRuntime.astro +9 -0
- package/src/astro/forms/index.ts +1 -0
- package/src/astro/forms-definitions.ts +126 -0
- package/src/astro/index.ts +16 -0
- package/src/astro/menu/EmVBMenuRuntime.astro +9 -0
- package/src/astro/menu/index.ts +1 -0
- package/src/astro/menu/runtime.ts +37 -0
- package/src/astro/popups/EmVBPopupsRuntime.astro +9 -0
- package/src/astro/popups/index.ts +1 -0
- package/src/astro/popups/runtime.ts +296 -0
- package/src/astro/render.ts +108 -0
- package/src/astro/resolve-theme.ts +515 -0
- package/src/astro/resolve.ts +65 -0
- package/src/astro/tabs/EmVBTabsRuntime.astro +10 -0
- package/src/astro/tabs/index.ts +1 -0
- package/src/astro/tabs/runtime.ts +116 -0
- package/src/astro/theme-context.ts +89 -0
- package/src/astro/theme-posts.ts +84 -0
- package/src/constants.ts +12 -0
- package/src/core/arrange.ts +448 -0
- package/src/core/clipboard.ts +504 -0
- package/src/core/css/generate.ts +236 -0
- package/src/core/css/scope.ts +88 -0
- package/src/core/design/cascade.ts +28 -0
- package/src/core/design/classes.ts +224 -0
- package/src/core/design/devices.ts +56 -0
- package/src/core/design/states.ts +49 -0
- package/src/core/design/transfer.ts +132 -0
- package/src/core/design/variables.ts +359 -0
- package/src/core/elements/definition.ts +12 -0
- package/src/core/elements/dynamic-elements.ts +225 -0
- package/src/core/elements/field-id.ts +6 -0
- package/src/core/elements/form-elements.ts +219 -0
- package/src/core/elements/index.ts +960 -0
- package/src/core/elements/menu.ts +190 -0
- package/src/core/elements/plain-text.ts +32 -0
- package/src/core/forms/binding.ts +61 -0
- package/src/core/forms/definition.ts +45 -0
- package/src/core/forms/success-layout.ts +63 -0
- package/src/core/icons/catalog.ts +273 -0
- package/src/core/index.ts +380 -0
- package/src/core/limits.ts +9 -0
- package/src/core/migrate/index.ts +126 -0
- package/src/core/nesting.ts +51 -0
- package/src/core/render/context.ts +21 -0
- package/src/core/render/dynamic-post.ts +175 -0
- package/src/core/render/extras.ts +98 -0
- package/src/core/render/form.ts +235 -0
- package/src/core/render/index.ts +372 -0
- package/src/core/render/loop.ts +43 -0
- package/src/core/render/pagination.ts +76 -0
- package/src/core/render/section.ts +37 -0
- package/src/core/render/tabs.ts +76 -0
- package/src/core/render/vnode.ts +87 -0
- package/src/core/sanitize/css.ts +771 -0
- package/src/core/sanitize/embed-url.ts +160 -0
- package/src/core/sanitize/escape.ts +10 -0
- package/src/core/sanitize/href.ts +6 -0
- package/src/core/sanitize/media-url.ts +7 -0
- package/src/core/sanitize/svg-upload.ts +203 -0
- package/src/core/sanitize/svg.ts +374 -0
- package/src/core/sanitize/url.ts +31 -0
- package/src/core/schema/descriptors.ts +37 -0
- package/src/core/schema/design.ts +93 -0
- package/src/core/schema/layout.ts +1008 -0
- package/src/core/schema/state-names.ts +4 -0
- package/src/core/schema/style.ts +336 -0
- package/src/core/stats.ts +18 -0
- package/src/core/tabs/presence.ts +25 -0
- package/src/core/theme/conditions.ts +324 -0
- package/src/core/theme/dynamic.ts +143 -0
- package/src/core/theme/float.ts +113 -0
- package/src/core/theme/pagination.ts +97 -0
- package/src/core/theme/parse-doc.ts +36 -0
- package/src/core/theme/part-types.ts +81 -0
- package/src/core/theme/popup-markup.ts +41 -0
- package/src/core/theme/popup-rules.ts +62 -0
- package/src/core/theme/triggers.ts +104 -0
- package/src/core/tree-ops.ts +139 -0
- package/src/core/validate.ts +185 -0
- package/src/index.ts +18 -0
- package/src/server/design-routes.ts +125 -0
- package/src/server/hooks.ts +118 -0
- package/src/server/icon-routes.ts +79 -0
- package/src/server/plugin.ts +43 -0
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
import { Button, Input, Select } from "@cloudflare/kumo";
|
|
2
|
+
import { PlusIcon, WarningCircleIcon } from "@phosphor-icons/react";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import {
|
|
5
|
+
cleanClassName,
|
|
6
|
+
slugify,
|
|
7
|
+
variableListFull,
|
|
8
|
+
type DesignSystem,
|
|
9
|
+
type StyleProps,
|
|
10
|
+
} from "../../../core/index.ts";
|
|
11
|
+
|
|
12
|
+
type ColorValue = StyleProps["color"];
|
|
13
|
+
import { BUTTON, FIELD } from "../../ui.ts";
|
|
14
|
+
import {
|
|
15
|
+
formatOklch,
|
|
16
|
+
formatRgb,
|
|
17
|
+
oklchClipped,
|
|
18
|
+
parseHex,
|
|
19
|
+
parseOklch,
|
|
20
|
+
parseRgb,
|
|
21
|
+
toHex,
|
|
22
|
+
} from "./color-space.ts";
|
|
23
|
+
import { pickerHex } from "./variable-values.ts";
|
|
24
|
+
|
|
25
|
+
const HEX = /^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
26
|
+
const NONE = "__none__";
|
|
27
|
+
const CUSTOM = "__custom__";
|
|
28
|
+
/** Swatch buttons shown under the Select; the rest stay in its list (W-136). */
|
|
29
|
+
const SWATCH_LIMIT = 16;
|
|
30
|
+
const BAD_HEX = "Enter a hex color such as #1a2b3c.";
|
|
31
|
+
|
|
32
|
+
const errorText = (error: unknown) =>
|
|
33
|
+
error instanceof Error ? error.message : "Couldn't save site styles. Try again.";
|
|
34
|
+
|
|
35
|
+
function InlineError({ children }: { children: string }) {
|
|
36
|
+
return (
|
|
37
|
+
<p className="emvb-inline-error" role="alert">
|
|
38
|
+
<WarningCircleIcon size={16} aria-hidden="true" />
|
|
39
|
+
{children}
|
|
40
|
+
</p>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function Swatch({ color }: { color: string }) {
|
|
45
|
+
return <span className="emvb-swatch" style={{ background: color }} aria-hidden="true" />;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const uniqueId = (design: DesignSystem, name: string) => {
|
|
49
|
+
const base = slugify(name).slice(0, 34) || "color";
|
|
50
|
+
const taken = new Set(design.variables.colors.map((color) => color.id));
|
|
51
|
+
let id = base;
|
|
52
|
+
for (let n = 2; taken.has(id); n++) id = `${base}-${n}`;
|
|
53
|
+
return id;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Colour: Default, a design variable (R-004) or a custom hex (W-136, D-043). The Select keeps its
|
|
58
|
+
* visible label and shows the current swatch; the site's colour variables also show as swatch
|
|
59
|
+
* buttons under it, and "Custom color" opens a validated hex field with the browser's picker.
|
|
60
|
+
* Creating a variable or editing a bound variable's value saves the design at once (D-013).
|
|
61
|
+
*/
|
|
62
|
+
export function ColorControl({
|
|
63
|
+
label,
|
|
64
|
+
value,
|
|
65
|
+
design,
|
|
66
|
+
placeholder,
|
|
67
|
+
onChange,
|
|
68
|
+
onDesignChange,
|
|
69
|
+
}: {
|
|
70
|
+
label: string;
|
|
71
|
+
value: ColorValue;
|
|
72
|
+
design: DesignSystem;
|
|
73
|
+
/** Shown instead of Default when unset: the Normal value while a state is edited (W-089). */
|
|
74
|
+
placeholder?: string;
|
|
75
|
+
onChange: (value: ColorValue) => void;
|
|
76
|
+
onDesignChange: (design: DesignSystem) => Promise<void>;
|
|
77
|
+
}) {
|
|
78
|
+
const colors = design.variables.colors;
|
|
79
|
+
const bound = value && typeof value === "object" ? colors.find((c) => c.id === value.var) : null;
|
|
80
|
+
const [creating, setCreating] = React.useState(false);
|
|
81
|
+
const [customOpen, setCustomOpen] = React.useState(false);
|
|
82
|
+
// The hex an out-of-gamut OKLCH entry was clipped to. It lives here because committing a custom
|
|
83
|
+
// colour remounts CustomColor (W-183).
|
|
84
|
+
const [clipped, setClipped] = React.useState<string | null>(null);
|
|
85
|
+
const custom = typeof value === "string" ? value : null;
|
|
86
|
+
const showCustom = custom !== null || customOpen;
|
|
87
|
+
|
|
88
|
+
const selectValue = showCustom ? CUSTOM : value && typeof value === "object" ? value.var : NONE;
|
|
89
|
+
const pick = (next: unknown) => {
|
|
90
|
+
if (next === CUSTOM) {
|
|
91
|
+
setCustomOpen(true);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
setCustomOpen(false);
|
|
95
|
+
onChange(next === NONE || next === null ? undefined : { var: String(next) });
|
|
96
|
+
};
|
|
97
|
+
return (
|
|
98
|
+
<div className="emvb-field-group" data-emvb-control="color">
|
|
99
|
+
<Select
|
|
100
|
+
label={label}
|
|
101
|
+
className={FIELD}
|
|
102
|
+
value={selectValue}
|
|
103
|
+
onValueChange={pick}
|
|
104
|
+
renderValue={(current: unknown) => {
|
|
105
|
+
if (current === NONE) return placeholder ?? "Default";
|
|
106
|
+
if (current === CUSTOM)
|
|
107
|
+
return custom ? (
|
|
108
|
+
<>
|
|
109
|
+
<Swatch color={custom} />
|
|
110
|
+
{custom}
|
|
111
|
+
</>
|
|
112
|
+
) : (
|
|
113
|
+
"Custom color"
|
|
114
|
+
);
|
|
115
|
+
const color = colors.find((c) => c.id === current);
|
|
116
|
+
return color ? (
|
|
117
|
+
<>
|
|
118
|
+
<Swatch color={color.value} />
|
|
119
|
+
{color.name}
|
|
120
|
+
</>
|
|
121
|
+
) : (
|
|
122
|
+
String(current)
|
|
123
|
+
);
|
|
124
|
+
}}
|
|
125
|
+
>
|
|
126
|
+
<Select.Option value={NONE}>Default</Select.Option>
|
|
127
|
+
{colors.map((color) => (
|
|
128
|
+
<Select.Option key={color.id} value={color.id}>
|
|
129
|
+
<Swatch color={color.value} />
|
|
130
|
+
{color.name}
|
|
131
|
+
</Select.Option>
|
|
132
|
+
))}
|
|
133
|
+
<Select.Option value={CUSTOM}>Custom color</Select.Option>
|
|
134
|
+
</Select>
|
|
135
|
+
{colors.length > 0 && (
|
|
136
|
+
<div
|
|
137
|
+
className="emvb-swatches"
|
|
138
|
+
role="group"
|
|
139
|
+
aria-label={`${label}: colour variables`}
|
|
140
|
+
data-emvb-swatches=""
|
|
141
|
+
>
|
|
142
|
+
{colors.slice(0, SWATCH_LIMIT).map((color) => (
|
|
143
|
+
<button
|
|
144
|
+
key={color.id}
|
|
145
|
+
type="button"
|
|
146
|
+
className="emvb-swatch-button"
|
|
147
|
+
aria-label={`${color.name} (${color.value})`}
|
|
148
|
+
aria-pressed={bound?.id === color.id}
|
|
149
|
+
title={`${color.name} · ${color.value}`}
|
|
150
|
+
onClick={() => pick(color.id)}
|
|
151
|
+
>
|
|
152
|
+
<Swatch color={color.value} />
|
|
153
|
+
</button>
|
|
154
|
+
))}
|
|
155
|
+
{colors.length > SWATCH_LIMIT && (
|
|
156
|
+
<span className="emvb-helper">+{colors.length - SWATCH_LIMIT} more in the list</span>
|
|
157
|
+
)}
|
|
158
|
+
</div>
|
|
159
|
+
)}
|
|
160
|
+
{showCustom && (
|
|
161
|
+
<CustomColor
|
|
162
|
+
key={custom ?? "new"}
|
|
163
|
+
value={custom ?? ""}
|
|
164
|
+
label={label}
|
|
165
|
+
clipped={clipped}
|
|
166
|
+
onClipped={setClipped}
|
|
167
|
+
onCommit={onChange}
|
|
168
|
+
/>
|
|
169
|
+
)}
|
|
170
|
+
{bound && (
|
|
171
|
+
<VariableValue
|
|
172
|
+
key={bound.id}
|
|
173
|
+
name={bound.name}
|
|
174
|
+
value={bound.value}
|
|
175
|
+
onCommit={async (hex) => {
|
|
176
|
+
await onDesignChange({
|
|
177
|
+
...design,
|
|
178
|
+
variables: {
|
|
179
|
+
...design.variables,
|
|
180
|
+
colors: colors.map((c) => (c.id === bound.id ? { ...c, value: hex } : c)),
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
}}
|
|
184
|
+
/>
|
|
185
|
+
)}
|
|
186
|
+
{creating ? (
|
|
187
|
+
<NewVariable
|
|
188
|
+
onCancel={() => setCreating(false)}
|
|
189
|
+
onCreate={async (raw, hex) => {
|
|
190
|
+
const name = cleanClassName(raw);
|
|
191
|
+
// Past the cap or with no name left the save would fail (W-219).
|
|
192
|
+
if (!name || variableListFull(design, "color")) return;
|
|
193
|
+
const id = uniqueId(design, name);
|
|
194
|
+
await onDesignChange({
|
|
195
|
+
...design,
|
|
196
|
+
variables: { ...design.variables, colors: [...colors, { id, name, value: hex }] },
|
|
197
|
+
});
|
|
198
|
+
onChange({ var: id });
|
|
199
|
+
setCreating(false);
|
|
200
|
+
}}
|
|
201
|
+
/>
|
|
202
|
+
) : (
|
|
203
|
+
<Button
|
|
204
|
+
variant="ghost"
|
|
205
|
+
className={BUTTON}
|
|
206
|
+
icon={<PlusIcon aria-hidden="true" />}
|
|
207
|
+
onClick={() => setCreating(true)}
|
|
208
|
+
disabled={variableListFull(design, "color")}
|
|
209
|
+
>
|
|
210
|
+
New variable
|
|
211
|
+
</Button>
|
|
212
|
+
)}
|
|
213
|
+
</div>
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The custom hex field with the browser's colour picker beside it (W-136). */
|
|
218
|
+
function CustomColor({
|
|
219
|
+
value,
|
|
220
|
+
label,
|
|
221
|
+
clipped,
|
|
222
|
+
onClipped,
|
|
223
|
+
onCommit,
|
|
224
|
+
}: {
|
|
225
|
+
value: string;
|
|
226
|
+
label: string;
|
|
227
|
+
clipped: string | null;
|
|
228
|
+
onClipped: (hex: string | null) => void;
|
|
229
|
+
onCommit: (hex: string) => void;
|
|
230
|
+
}) {
|
|
231
|
+
const [draft, setDraft] = React.useState(value);
|
|
232
|
+
// The field that refused its entry is the one marked, not always the hex (W-174).
|
|
233
|
+
const [error, setError] = React.useState<{ field: Notation; message: string } | null>(null);
|
|
234
|
+
const picker = React.useRef<HTMLInputElement | null>(null);
|
|
235
|
+
const commit = (next: string) => {
|
|
236
|
+
const hex = next.trim();
|
|
237
|
+
if (hex === value || hex === "") {
|
|
238
|
+
setError(null);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (!HEX.test(hex)) {
|
|
242
|
+
setError({ field: "hex", message: BAD_HEX });
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
setError(null);
|
|
246
|
+
onCommit(hex);
|
|
247
|
+
};
|
|
248
|
+
// The picker applies on its native change event (the dialog closed), not on every drag step.
|
|
249
|
+
React.useEffect(() => {
|
|
250
|
+
const el = picker.current;
|
|
251
|
+
if (!el) return;
|
|
252
|
+
const onPicked = () => {
|
|
253
|
+
setDraft(el.value);
|
|
254
|
+
commit(el.value);
|
|
255
|
+
};
|
|
256
|
+
el.addEventListener("change", onPicked);
|
|
257
|
+
return () => el.removeEventListener("change", onPicked);
|
|
258
|
+
});
|
|
259
|
+
return (
|
|
260
|
+
<div className="emvb-custom-color" data-emvb-custom-color="">
|
|
261
|
+
<div className="emvb-custom-color-row">
|
|
262
|
+
<Input
|
|
263
|
+
label="Custom color"
|
|
264
|
+
className={`${FIELD} emvb-mono`}
|
|
265
|
+
placeholder="#1a2b3c"
|
|
266
|
+
value={draft}
|
|
267
|
+
variant={error?.field === "hex" ? "error" : undefined}
|
|
268
|
+
aria-invalid={error?.field === "hex" ? true : undefined}
|
|
269
|
+
onChange={(event) => setDraft(event.target.value)}
|
|
270
|
+
onBlur={() => commit(draft)}
|
|
271
|
+
onKeyDown={(event) => {
|
|
272
|
+
if (event.key === "Enter") commit(draft);
|
|
273
|
+
}}
|
|
274
|
+
/>
|
|
275
|
+
<input
|
|
276
|
+
type="color"
|
|
277
|
+
className="emvb-color-picker"
|
|
278
|
+
aria-label={`Pick ${label.toLowerCase()}`}
|
|
279
|
+
ref={picker}
|
|
280
|
+
value={pickerHex(HEX.test(draft) ? draft : value || "#000000")}
|
|
281
|
+
onChange={(event) => setDraft(event.target.value)}
|
|
282
|
+
/>
|
|
283
|
+
</div>
|
|
284
|
+
{error?.field === "hex" && <InlineError>{error.message}</InlineError>}
|
|
285
|
+
<NotationRow
|
|
286
|
+
label="RGB"
|
|
287
|
+
text={formatRgb(parseHex(HEX.test(draft) ? draft : value) ?? { r: 0, g: 0, b: 0, a: 1 })}
|
|
288
|
+
placeholder="255, 128, 0"
|
|
289
|
+
error={error?.field === "rgb" ? error.message : undefined}
|
|
290
|
+
onCommit={(text) => {
|
|
291
|
+
const next = parseRgb(text);
|
|
292
|
+
if (!next) {
|
|
293
|
+
setError({ field: "rgb", message: RGB_RANGE });
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
setError(null);
|
|
297
|
+
const hex = toHex(next);
|
|
298
|
+
setDraft(hex);
|
|
299
|
+
onCommit(hex);
|
|
300
|
+
}}
|
|
301
|
+
/>
|
|
302
|
+
<NotationRow
|
|
303
|
+
label="OKLCH"
|
|
304
|
+
text={formatOklch(parseHex(HEX.test(draft) ? draft : value) ?? { r: 0, g: 0, b: 0, a: 1 })}
|
|
305
|
+
placeholder="62.8, 0.150, 29"
|
|
306
|
+
error={error?.field === "oklch" ? error.message : undefined}
|
|
307
|
+
onCommit={(text) => {
|
|
308
|
+
const next = parseOklch(text);
|
|
309
|
+
if (!next) {
|
|
310
|
+
setError({ field: "oklch", message: OKLCH_RANGE });
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
setError(null);
|
|
314
|
+
const hex = toHex(next);
|
|
315
|
+
onClipped(oklchClipped(text) ? hex : null);
|
|
316
|
+
setDraft(hex);
|
|
317
|
+
onCommit(hex);
|
|
318
|
+
}}
|
|
319
|
+
/>
|
|
320
|
+
{clipped !== null && clipped === draft && !error && (
|
|
321
|
+
<p className="emvb-helper" role="status" data-emvb-clipped="">
|
|
322
|
+
Clipped to {clipped}: that OKLCH colour is outside sRGB, so its out-of-range channels were
|
|
323
|
+
cut to fit.
|
|
324
|
+
</p>
|
|
325
|
+
)}
|
|
326
|
+
</div>
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
type Notation = "hex" | "rgb" | "oklch";
|
|
331
|
+
|
|
332
|
+
// A refusal says the valid ranges, not only an example (W-181).
|
|
333
|
+
const RGB_RANGE =
|
|
334
|
+
"Enter red, green and blue from 0 to 255, such as 255, 128, 0. Alpha is optional: 0–1 or 0–100%.";
|
|
335
|
+
const OKLCH_RANGE =
|
|
336
|
+
"Enter lightness 0–100 (or 0–1), chroma 0–0.5 and hue 0–360, such as 62.8, 0.150, 29.";
|
|
337
|
+
|
|
338
|
+
/** A second view of the same colour. Commits on Enter or blur, like the hex field. */
|
|
339
|
+
function NotationRow({
|
|
340
|
+
label,
|
|
341
|
+
text,
|
|
342
|
+
placeholder,
|
|
343
|
+
error,
|
|
344
|
+
onCommit,
|
|
345
|
+
}: {
|
|
346
|
+
label: string;
|
|
347
|
+
text: string;
|
|
348
|
+
placeholder: string;
|
|
349
|
+
error: string | undefined;
|
|
350
|
+
onCommit: (text: string) => void;
|
|
351
|
+
}) {
|
|
352
|
+
const [draft, setDraft] = React.useState(text);
|
|
353
|
+
React.useEffect(() => {
|
|
354
|
+
setDraft(text);
|
|
355
|
+
}, [text]);
|
|
356
|
+
return (
|
|
357
|
+
<>
|
|
358
|
+
<Input
|
|
359
|
+
label={label}
|
|
360
|
+
className={`${FIELD} emvb-mono`}
|
|
361
|
+
placeholder={placeholder}
|
|
362
|
+
value={draft}
|
|
363
|
+
variant={error ? "error" : undefined}
|
|
364
|
+
aria-invalid={error ? true : undefined}
|
|
365
|
+
onChange={(event) => setDraft(event.target.value)}
|
|
366
|
+
onBlur={() => {
|
|
367
|
+
if (draft.trim() !== text) onCommit(draft);
|
|
368
|
+
}}
|
|
369
|
+
onKeyDown={(event) => {
|
|
370
|
+
if (event.key === "Enter") onCommit(draft);
|
|
371
|
+
}}
|
|
372
|
+
/>
|
|
373
|
+
{error && <InlineError>{error}</InlineError>}
|
|
374
|
+
</>
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function VariableValue({
|
|
379
|
+
name,
|
|
380
|
+
value,
|
|
381
|
+
onCommit,
|
|
382
|
+
}: {
|
|
383
|
+
name: string;
|
|
384
|
+
value: string;
|
|
385
|
+
onCommit: (hex: string) => Promise<void>;
|
|
386
|
+
}) {
|
|
387
|
+
const [draft, setDraft] = React.useState(value);
|
|
388
|
+
const [error, setError] = React.useState<string | null>(null);
|
|
389
|
+
const commit = async () => {
|
|
390
|
+
if (draft === value) return;
|
|
391
|
+
if (!HEX.test(draft)) {
|
|
392
|
+
setError(BAD_HEX);
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
setError(null);
|
|
396
|
+
try {
|
|
397
|
+
await onCommit(draft);
|
|
398
|
+
} catch (failure) {
|
|
399
|
+
setError(errorText(failure));
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
return (
|
|
403
|
+
<>
|
|
404
|
+
<Input
|
|
405
|
+
label={`${name} value`}
|
|
406
|
+
className={`${FIELD} emvb-mono`}
|
|
407
|
+
value={draft}
|
|
408
|
+
variant={error ? "error" : undefined}
|
|
409
|
+
onChange={(event) => setDraft(event.target.value)}
|
|
410
|
+
onBlur={() => void commit()}
|
|
411
|
+
onKeyDown={(event) => {
|
|
412
|
+
if (event.key === "Enter") void commit();
|
|
413
|
+
}}
|
|
414
|
+
/>
|
|
415
|
+
{error ? (
|
|
416
|
+
<InlineError>{error}</InlineError>
|
|
417
|
+
) : (
|
|
418
|
+
<p className="emvb-helper">Style changes stay unpublished until you publish styles.</p>
|
|
419
|
+
)}
|
|
420
|
+
</>
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function NewVariable({
|
|
425
|
+
onCancel,
|
|
426
|
+
onCreate,
|
|
427
|
+
}: {
|
|
428
|
+
onCancel: () => void;
|
|
429
|
+
onCreate: (name: string, hex: string) => Promise<void>;
|
|
430
|
+
}) {
|
|
431
|
+
const [name, setName] = React.useState("");
|
|
432
|
+
const [hex, setHex] = React.useState("");
|
|
433
|
+
const [error, setError] = React.useState<string | null>(null);
|
|
434
|
+
const [busy, setBusy] = React.useState(false);
|
|
435
|
+
const create = async () => {
|
|
436
|
+
if (!name.trim()) return setError("Enter a name for the variable.");
|
|
437
|
+
if (!HEX.test(hex)) return setError(BAD_HEX);
|
|
438
|
+
setError(null);
|
|
439
|
+
setBusy(true);
|
|
440
|
+
try {
|
|
441
|
+
await onCreate(name.trim(), hex);
|
|
442
|
+
} catch (failure) {
|
|
443
|
+
setError(errorText(failure));
|
|
444
|
+
setBusy(false);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
return (
|
|
448
|
+
<div className="emvb-new-variable" data-emvb-form="new-variable">
|
|
449
|
+
<Input
|
|
450
|
+
label="Variable name"
|
|
451
|
+
className={FIELD}
|
|
452
|
+
value={name}
|
|
453
|
+
autoFocus
|
|
454
|
+
onChange={(event) => setName(event.target.value)}
|
|
455
|
+
/>
|
|
456
|
+
<Input
|
|
457
|
+
label="Value"
|
|
458
|
+
className={`${FIELD} emvb-mono`}
|
|
459
|
+
placeholder="#1a2b3c"
|
|
460
|
+
value={hex}
|
|
461
|
+
onChange={(event) => setHex(event.target.value)}
|
|
462
|
+
onKeyDown={(event) => {
|
|
463
|
+
if (event.key === "Enter") void create();
|
|
464
|
+
}}
|
|
465
|
+
/>
|
|
466
|
+
{error && <InlineError>{error}</InlineError>}
|
|
467
|
+
<div className="emvb-row-actions">
|
|
468
|
+
<Button variant="secondary" className={BUTTON} onClick={onCancel}>
|
|
469
|
+
Cancel
|
|
470
|
+
</Button>
|
|
471
|
+
<Button variant="secondary" className={BUTTON} loading={busy} onClick={() => void create()}>
|
|
472
|
+
Create variable
|
|
473
|
+
</Button>
|
|
474
|
+
</div>
|
|
475
|
+
</div>
|
|
476
|
+
);
|
|
477
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { Button } from "@cloudflare/kumo";
|
|
2
|
+
import { PlusIcon, TrashIcon } from "@phosphor-icons/react";
|
|
3
|
+
import {
|
|
4
|
+
defaultConditions,
|
|
5
|
+
MAX_CONDITION_RULES,
|
|
6
|
+
type ConditionRule,
|
|
7
|
+
type ConditionsDoc,
|
|
8
|
+
} from "../../../core/index.ts";
|
|
9
|
+
import { BUTTON, FIELD } from "../../ui.ts";
|
|
10
|
+
|
|
11
|
+
type Props = {
|
|
12
|
+
conditions: ConditionsDoc | undefined;
|
|
13
|
+
onChange: (conditions: ConditionsDoc) => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const GROUP_OPTIONS = [
|
|
17
|
+
{ value: "general:entire_site", label: "Entire site", group: "general", name: "entire_site" },
|
|
18
|
+
{ value: "singular:front", label: "Front page", group: "singular", name: "front" },
|
|
19
|
+
{ value: "singular:all", label: "All singular", group: "singular", name: "all" },
|
|
20
|
+
{ value: "singular:not_found", label: "404 page", group: "singular", name: "not_found" },
|
|
21
|
+
{
|
|
22
|
+
value: "singular:collection:pages",
|
|
23
|
+
label: "Pages (all)",
|
|
24
|
+
group: "singular",
|
|
25
|
+
name: "collection",
|
|
26
|
+
args: { collection: "pages" },
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
value: "singular:collection:posts",
|
|
30
|
+
label: "Posts (all)",
|
|
31
|
+
group: "singular",
|
|
32
|
+
name: "collection",
|
|
33
|
+
args: { collection: "posts" },
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
value: "singular:collection:emvb_pages",
|
|
37
|
+
label: "Visual pages (all)",
|
|
38
|
+
group: "singular",
|
|
39
|
+
name: "collection",
|
|
40
|
+
args: { collection: "emvb_pages" },
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
value: "archive:collection:posts",
|
|
44
|
+
label: "Posts archive",
|
|
45
|
+
group: "archive",
|
|
46
|
+
name: "collection",
|
|
47
|
+
args: { collection: "posts" },
|
|
48
|
+
},
|
|
49
|
+
{ value: "archive:search", label: "Search results", group: "archive", name: "search" },
|
|
50
|
+
{
|
|
51
|
+
value: "archive:taxonomy:category",
|
|
52
|
+
label: "All categories",
|
|
53
|
+
group: "archive",
|
|
54
|
+
name: "taxonomy",
|
|
55
|
+
args: { taxonomy: "category" },
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
value: "archive:taxonomy:tag",
|
|
59
|
+
label: "All tags",
|
|
60
|
+
group: "archive",
|
|
61
|
+
name: "taxonomy",
|
|
62
|
+
args: { taxonomy: "tag" },
|
|
63
|
+
},
|
|
64
|
+
] as const;
|
|
65
|
+
|
|
66
|
+
export function optionValue(rule: ConditionRule): string {
|
|
67
|
+
if (rule.group === "singular" && rule.name === "collection") {
|
|
68
|
+
return `singular:collection:${String(rule.args["collection"] ?? "")}`;
|
|
69
|
+
}
|
|
70
|
+
if (rule.group === "archive" && rule.name === "collection") {
|
|
71
|
+
return `archive:collection:${String(rule.args["collection"] ?? "")}`;
|
|
72
|
+
}
|
|
73
|
+
if (rule.group === "archive" && rule.name === "taxonomy") {
|
|
74
|
+
return `archive:taxonomy:${String(rule.args["taxonomy"] ?? "")}`;
|
|
75
|
+
}
|
|
76
|
+
return `${rule.group}:${rule.name}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function ruleFromOption(
|
|
80
|
+
value: string,
|
|
81
|
+
op: "include" | "exclude",
|
|
82
|
+
id: string,
|
|
83
|
+
): ConditionRule {
|
|
84
|
+
const option = GROUP_OPTIONS.find((o) => o.value === value) ?? GROUP_OPTIONS[0];
|
|
85
|
+
return {
|
|
86
|
+
id,
|
|
87
|
+
op,
|
|
88
|
+
group: option.group,
|
|
89
|
+
name: option.name,
|
|
90
|
+
args: "args" in option && option.args ? { ...option.args } : {},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let ruleSeq = 0;
|
|
95
|
+
const newConditionId = () => `r-${Date.now().toString(36)}-${(++ruleSeq).toString(36)}`;
|
|
96
|
+
|
|
97
|
+
/** Include/Exclude display conditions for theme parts (Elementor-like MVP set). */
|
|
98
|
+
export function ConditionsEditor({ conditions, onChange }: Props) {
|
|
99
|
+
const doc = conditions ?? defaultConditions();
|
|
100
|
+
const setRules = (rules: ConditionRule[]) => onChange({ schemaVersion: 1, rules });
|
|
101
|
+
|
|
102
|
+
/** The schema allows at most 20 rules, so a 21st would make the part unsavable (W-092). */
|
|
103
|
+
const full = doc.rules.length >= MAX_CONDITION_RULES;
|
|
104
|
+
const fullReason = `${MAX_CONDITION_RULES} conditions is the most one part can have. Remove one to add another.`;
|
|
105
|
+
|
|
106
|
+
const add = (op: "include" | "exclude") => {
|
|
107
|
+
if (full) return;
|
|
108
|
+
setRules([...doc.rules, ruleFromOption("general:entire_site", op, newConditionId())]);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const update = (id: string, patch: Partial<ConditionRule>) => {
|
|
112
|
+
setRules(doc.rules.map((rule) => (rule.id === id ? { ...rule, ...patch } : rule)));
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const remove = (id: string) => {
|
|
116
|
+
setRules(doc.rules.filter((rule) => rule.id !== id));
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
<div className="emvb-conditions" data-emvb-panel="conditions">
|
|
121
|
+
<h3 className="emvb-section-label">Display conditions</h3>
|
|
122
|
+
<p className="emvb-helper">
|
|
123
|
+
Include where this part should appear. Exclude overrides include for the same part.
|
|
124
|
+
</p>
|
|
125
|
+
{doc.rules.length === 0 && (
|
|
126
|
+
<p className="emvb-helper" role="status">
|
|
127
|
+
No conditions yet — this part will not appear anywhere until you add an Include.
|
|
128
|
+
</p>
|
|
129
|
+
)}
|
|
130
|
+
<ul className="emvb-condition-list">
|
|
131
|
+
{doc.rules.map((rule) => (
|
|
132
|
+
<li key={rule.id} className="emvb-condition-row" data-emvb-condition={rule.id}>
|
|
133
|
+
<select
|
|
134
|
+
className="emvb-condition-op"
|
|
135
|
+
aria-label="Include or exclude"
|
|
136
|
+
value={rule.op}
|
|
137
|
+
onChange={(event) =>
|
|
138
|
+
update(rule.id, { op: event.target.value === "exclude" ? "exclude" : "include" })
|
|
139
|
+
}
|
|
140
|
+
>
|
|
141
|
+
<option value="include">Include</option>
|
|
142
|
+
<option value="exclude">Exclude</option>
|
|
143
|
+
</select>
|
|
144
|
+
<select
|
|
145
|
+
className={`emvb-condition-where ${FIELD}`}
|
|
146
|
+
aria-label="Where"
|
|
147
|
+
value={optionValue(rule)}
|
|
148
|
+
onChange={(event) => {
|
|
149
|
+
const next = ruleFromOption(event.target.value, rule.op, rule.id);
|
|
150
|
+
update(rule.id, next);
|
|
151
|
+
}}
|
|
152
|
+
>
|
|
153
|
+
{GROUP_OPTIONS.map((option) => (
|
|
154
|
+
<option key={option.value} value={option.value}>
|
|
155
|
+
{option.label}
|
|
156
|
+
</option>
|
|
157
|
+
))}
|
|
158
|
+
</select>
|
|
159
|
+
<Button
|
|
160
|
+
type="button"
|
|
161
|
+
variant="ghost"
|
|
162
|
+
className={BUTTON}
|
|
163
|
+
aria-label="Remove condition"
|
|
164
|
+
icon={<TrashIcon aria-hidden="true" />}
|
|
165
|
+
onClick={() => remove(rule.id)}
|
|
166
|
+
/>
|
|
167
|
+
</li>
|
|
168
|
+
))}
|
|
169
|
+
</ul>
|
|
170
|
+
<div className="emvb-condition-actions">
|
|
171
|
+
<Button
|
|
172
|
+
type="button"
|
|
173
|
+
variant="secondary"
|
|
174
|
+
className={BUTTON}
|
|
175
|
+
icon={<PlusIcon aria-hidden="true" />}
|
|
176
|
+
disabled={full}
|
|
177
|
+
title={full ? fullReason : undefined}
|
|
178
|
+
onClick={() => add("include")}
|
|
179
|
+
>
|
|
180
|
+
Add Include
|
|
181
|
+
</Button>
|
|
182
|
+
<Button
|
|
183
|
+
type="button"
|
|
184
|
+
variant="secondary"
|
|
185
|
+
className={BUTTON}
|
|
186
|
+
icon={<PlusIcon aria-hidden="true" />}
|
|
187
|
+
disabled={full}
|
|
188
|
+
title={full ? fullReason : undefined}
|
|
189
|
+
onClick={() => add("exclude")}
|
|
190
|
+
>
|
|
191
|
+
Add Exclude
|
|
192
|
+
</Button>
|
|
193
|
+
</div>
|
|
194
|
+
{full && (
|
|
195
|
+
<p className="emvb-helper" data-emvb-conditions-full="">
|
|
196
|
+
{fullReason}
|
|
197
|
+
</p>
|
|
198
|
+
)}
|
|
199
|
+
</div>
|
|
200
|
+
);
|
|
201
|
+
}
|