feat: establish color slider geometry and picker composition
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
import { nextTick, onBeforeUnmount, watch, type ShallowRef } from "vue";
|
||||
import {
|
||||
computed,
|
||||
nextTick,
|
||||
onBeforeUnmount,
|
||||
watch,
|
||||
type ShallowRef,
|
||||
} from "vue";
|
||||
import { useChoiceForm } from "../choice/shared";
|
||||
import { useConfig } from "../../providers/context";
|
||||
import { useColorChannel, type ColorChannelOptions } from "./channel";
|
||||
@@ -9,6 +15,7 @@ export function useColorNativeChannel(
|
||||
state: ColorState,
|
||||
props: ColorChannelOptions & {
|
||||
dir?: "ltr" | "rtl";
|
||||
orientation?: "horizontal" | "vertical";
|
||||
},
|
||||
input: ShallowRef<HTMLInputElement | undefined>,
|
||||
) {
|
||||
@@ -66,7 +73,11 @@ export function useColorNativeChannel(
|
||||
return;
|
||||
}
|
||||
const { minValue, maxValue, step, pageSize } = channel.range.value;
|
||||
const sign = (props.dir ?? config?.dir.value) === "rtl" ? -1 : 1;
|
||||
const sign =
|
||||
(props.dir ?? config?.dir.value) === "rtl" &&
|
||||
props.orientation !== "vertical"
|
||||
? -1
|
||||
: 1;
|
||||
const delta = event.shiftKey ? pageSize : step;
|
||||
let next = channel.value.value;
|
||||
switch (event.key) {
|
||||
@@ -113,6 +124,7 @@ export function useColorNativeChannel(
|
||||
() => props.channel,
|
||||
() => props.colorSpace,
|
||||
() => props.dir,
|
||||
() => props.orientation,
|
||||
() => config?.dir.value,
|
||||
input,
|
||||
],
|
||||
@@ -143,5 +155,11 @@ export function useColorNativeChannel(
|
||||
keyup,
|
||||
commit: () => stop(true),
|
||||
cancel: () => stop(),
|
||||
// Private custom-track bridge; keyboard, native AT and pointer share one token.
|
||||
transaction: {
|
||||
begin,
|
||||
propose,
|
||||
active: computed(() => state.active.value === token),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { colorChannel } from "./channel";
|
||||
import { Color, type ColorChannel, type ColorSpace } from "./value";
|
||||
export function colorSliderDisplay(
|
||||
color: Color,
|
||||
channel: ColorChannel,
|
||||
space?: ColorSpace,
|
||||
) {
|
||||
const native = colorChannel(color, channel, space).color;
|
||||
return channel === "hue"
|
||||
? new Color({
|
||||
space: "hsl",
|
||||
h: native.getChannelValue("hue"),
|
||||
s: 100,
|
||||
l: 50,
|
||||
alpha: 1,
|
||||
})
|
||||
: channel === "alpha"
|
||||
? native
|
||||
: native.withAlpha(1);
|
||||
}
|
||||
/** Exact piecewise sRGB ramps; HSL lightness needs its chromatic midpoint. */
|
||||
export function colorSliderBackground(
|
||||
color: Color,
|
||||
channel: ColorChannel,
|
||||
space?: ColorSpace,
|
||||
orientation: "horizontal" | "vertical" = "horizontal",
|
||||
dir: "ltr" | "rtl" = "ltr",
|
||||
) {
|
||||
const d = colorChannel(
|
||||
colorSliderDisplay(color, channel, space),
|
||||
channel,
|
||||
space,
|
||||
),
|
||||
max = d.range.maxValue;
|
||||
const values =
|
||||
channel === "hue"
|
||||
? [0, 60, 120, 180, 240, 300, 360]
|
||||
: channel === "lightness"
|
||||
? [0, 50, 100]
|
||||
: [0, max];
|
||||
const css = (value: number) =>
|
||||
d.color.withChannelValue(channel, value).toString("css");
|
||||
const direction =
|
||||
orientation === "vertical"
|
||||
? "to top"
|
||||
: dir === "rtl"
|
||||
? "to left"
|
||||
: "to right";
|
||||
const ramp = `linear-gradient(${direction}, ${values.map((value) => `${css(value)} ${(value / max) * 100}%`).join(", ")})`;
|
||||
return {
|
||||
background: `${ramp}, repeating-conic-gradient(#efefef 0% 25%, #f7f7f7 0% 50%) 50% / 16px 16px`,
|
||||
"--track-start-color": css(0),
|
||||
"--track-end-color": css(max),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
import {
|
||||
computed,
|
||||
onBeforeUnmount,
|
||||
shallowRef,
|
||||
watch,
|
||||
type ShallowRef,
|
||||
} from "vue";
|
||||
import { useConfig } from "../../providers/context";
|
||||
import { useColorNativeChannel } from "./native-channel";
|
||||
import type { ColorChannelOptions } from "./channel";
|
||||
import type { ColorState } from "./model";
|
||||
|
||||
export interface ColorSliderOptions extends ColorChannelOptions {
|
||||
dir?: "ltr" | "rtl";
|
||||
orientation?: "horizontal" | "vertical";
|
||||
}
|
||||
/** One native input plus a stable custom track; all edits share the native channel token. */
|
||||
export function useColorSlider(
|
||||
state: ColorState,
|
||||
props: ColorSliderOptions,
|
||||
elements: {
|
||||
track: ShallowRef<HTMLElement | undefined>;
|
||||
thumb: ShallowRef<HTMLElement | undefined>;
|
||||
input: ShallowRef<HTMLInputElement | undefined>;
|
||||
},
|
||||
) {
|
||||
const control = useColorNativeChannel(state, props, elements.input);
|
||||
const config = useConfig();
|
||||
const dir = computed(() => props.dir ?? config?.dir.value ?? "ltr");
|
||||
const orientation = computed(() => props.orientation ?? "horizontal");
|
||||
const vertical = computed(() => orientation.value === "vertical");
|
||||
const percent = computed(() => {
|
||||
const r = control.range.value;
|
||||
return (control.value.value - r.minValue) / (r.maxValue - r.minValue);
|
||||
});
|
||||
const position = computed(() =>
|
||||
vertical.value
|
||||
? { left: "50%", top: `${(1 - percent.value) * 100}%` }
|
||||
: {
|
||||
left: `${(dir.value === "rtl" ? 1 - percent.value : percent.value) * 100}%`,
|
||||
top: "50%",
|
||||
},
|
||||
);
|
||||
const dragging = shallowRef(false);
|
||||
let pointer: number | undefined,
|
||||
owner: HTMLElement | undefined,
|
||||
grab = 0,
|
||||
last: number | undefined;
|
||||
function stop(commit = false) {
|
||||
const id = pointer,
|
||||
node = owner;
|
||||
pointer = undefined;
|
||||
owner = undefined;
|
||||
dragging.value = false;
|
||||
grab = 0;
|
||||
last = undefined;
|
||||
if (id !== undefined && node?.hasPointerCapture?.(id))
|
||||
node.releasePointerCapture(id);
|
||||
if (commit) control.commit();
|
||||
else control.cancel();
|
||||
}
|
||||
function local(event: PointerEvent) {
|
||||
const node = elements.track.value;
|
||||
if (!node) return;
|
||||
const rect = node.getBoundingClientRect(),
|
||||
css = getComputedStyle(node);
|
||||
const length = vertical.value ? rect.height : rect.width;
|
||||
const scale =
|
||||
length / (vertical.value ? node.offsetHeight : node.offsetWidth) || 1;
|
||||
const before =
|
||||
(parseFloat(vertical.value ? css.borderTopWidth : css.borderLeftWidth) ||
|
||||
0) * scale;
|
||||
const after =
|
||||
(parseFloat(
|
||||
vertical.value ? css.borderBottomWidth : css.borderRightWidth,
|
||||
) || 0) * scale;
|
||||
const size = length - before - after;
|
||||
if (size <= 0) return;
|
||||
let p =
|
||||
((vertical.value ? event.clientY - rect.top : event.clientX - rect.left) -
|
||||
before) /
|
||||
size;
|
||||
if (vertical.value || dir.value === "rtl") p = 1 - p;
|
||||
const range = control.range.value;
|
||||
return range.minValue + p * (range.maxValue - range.minValue);
|
||||
}
|
||||
function update(raw: number) {
|
||||
const { minValue, maxValue, step } = control.range.value;
|
||||
const next = Math.min(
|
||||
maxValue,
|
||||
Math.max(
|
||||
minValue,
|
||||
Number(
|
||||
(
|
||||
Math.round((raw - grab - minValue) / step) * step +
|
||||
minValue
|
||||
).toFixed(12),
|
||||
),
|
||||
),
|
||||
);
|
||||
if (next === last) return;
|
||||
last = next;
|
||||
control.transaction.propose(next);
|
||||
}
|
||||
function pointerdown(event: PointerEvent) {
|
||||
if (
|
||||
event.defaultPrevented ||
|
||||
event.button !== 0 ||
|
||||
event.isPrimary === false ||
|
||||
pointer !== undefined ||
|
||||
state.disabled.value ||
|
||||
state.readOnly.value ||
|
||||
(event.pointerType === "mouse" &&
|
||||
(event.ctrlKey || event.metaKey || event.altKey))
|
||||
)
|
||||
return;
|
||||
const point = local(event),
|
||||
track = elements.track.value;
|
||||
if (point === undefined || !track || !elements.input.value) return;
|
||||
stop();
|
||||
if (!control.transaction.begin()) return;
|
||||
pointer = event.pointerId;
|
||||
owner = track;
|
||||
dragging.value = true;
|
||||
const hit = elements.thumb.value?.contains(event.target as Node);
|
||||
grab = hit ? point - control.value.value : 0;
|
||||
event.preventDefault();
|
||||
elements.input.value.focus({ preventScroll: true });
|
||||
owner.setPointerCapture?.(event.pointerId);
|
||||
if (!hit) update(point);
|
||||
}
|
||||
function pointermove(event: PointerEvent) {
|
||||
if (event.defaultPrevented || event.pointerId !== pointer) return;
|
||||
const point = local(event);
|
||||
if (point === undefined) return;
|
||||
event.preventDefault();
|
||||
update(point);
|
||||
}
|
||||
function pointerup(event: PointerEvent) {
|
||||
if (event.pointerId !== pointer) return;
|
||||
if (!event.defaultPrevented) {
|
||||
const point = local(event);
|
||||
if (point !== undefined) update(point);
|
||||
}
|
||||
stop(!event.defaultPrevented);
|
||||
}
|
||||
function cancel(event?: PointerEvent) {
|
||||
if (!event || event.pointerId === pointer) stop();
|
||||
}
|
||||
watch(
|
||||
[
|
||||
state.disabled,
|
||||
state.readOnly,
|
||||
dir,
|
||||
orientation,
|
||||
() => props.channel,
|
||||
() => props.colorSpace,
|
||||
elements.track,
|
||||
elements.thumb,
|
||||
elements.input,
|
||||
],
|
||||
() => stop(),
|
||||
{ flush: "sync" },
|
||||
);
|
||||
watch(
|
||||
control.transaction.active,
|
||||
(active) => {
|
||||
if (!active && pointer !== undefined) stop();
|
||||
},
|
||||
{ flush: "sync" },
|
||||
);
|
||||
onBeforeUnmount(() => stop());
|
||||
return {
|
||||
...control,
|
||||
dir,
|
||||
orientation,
|
||||
percent,
|
||||
position,
|
||||
dragging,
|
||||
pointerdown,
|
||||
pointermove,
|
||||
pointerup,
|
||||
cancel,
|
||||
commit: () => stop(true),
|
||||
keydown: (event: KeyboardEvent) => {
|
||||
if (pointer !== undefined && event.key !== "Escape") return;
|
||||
control.keydown(event);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import type { Color } from "./value";
|
||||
/** Full JS numeric precision, expanded so the strict functional parser can round-trip exponents. */
|
||||
function decimal(value: number) {
|
||||
const text = String(value),
|
||||
parts = text.split("e");
|
||||
if (parts.length === 1) return text;
|
||||
const coefficient = parts[0]!,
|
||||
exponent = Number(parts[1]);
|
||||
const dot = coefficient.indexOf(".");
|
||||
const digits = coefficient.replace(".", "");
|
||||
const point = (dot === -1 ? digits.length : dot) + exponent;
|
||||
if (point <= 0) return `0.${"0".repeat(-point)}${digits}`;
|
||||
if (point >= digits.length) return digits + "0".repeat(point - digits.length);
|
||||
return `${digits.slice(0, point)}.${digits.slice(point)}`;
|
||||
}
|
||||
/** Private owner serialization proof, separate from display CSS and final public field formats. */
|
||||
export function serializeColorValue(color: Color | null) {
|
||||
if (!color) return "";
|
||||
const space = color.getColorSpace();
|
||||
const channels = color
|
||||
.getChannels()
|
||||
.map(
|
||||
(channel, index) =>
|
||||
decimal(color.getChannelValue(channel)) +
|
||||
(space !== "rgb" && index > 0 ? "%" : ""),
|
||||
);
|
||||
return `${space}a(${channels.join(", ")}, ${decimal(color.getChannelValue("alpha"))})`;
|
||||
}
|
||||
@@ -171,8 +171,10 @@ export function useColorText(
|
||||
return;
|
||||
}
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
cancel();
|
||||
if (draft.value !== formatted.value) {
|
||||
event.preventDefault();
|
||||
cancel();
|
||||
}
|
||||
return;
|
||||
}
|
||||
const direction =
|
||||
|
||||
Reference in New Issue
Block a user