@xleddyl/443-components-vue 0.16.93 → 0.16.94

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.
@@ -0,0 +1,251 @@
1
+ <template>
2
+ <div
3
+ :id="control.id.value"
4
+ role="group"
5
+ data-k-shake
6
+ class="k-focal-point-input"
7
+ :class="[
8
+ {
9
+ 'is-disabled': control.disabled.value,
10
+ 'is-readonly': readonly,
11
+ 'is-invalid': control.invalid.value,
12
+ 'is-dragging': dragging,
13
+ 'is-editable': editable
14
+ },
15
+ props.class
16
+ ]"
17
+ :style="props.style"
18
+ :aria-label="label"
19
+ :aria-labelledby="label || $attrs['aria-label'] ? void 0 : control.labelledBy.value"
20
+ v-bind="$attrs"
21
+ :aria-disabled="control.disabled.value ? 'true' : void 0"
22
+ >
23
+ <div
24
+ ref="areaRef"
25
+ class="k-focal-point-input-area"
26
+ :class="{ 'is-loaded': loaded, 'is-failed': !showImage }"
27
+ @pointerdown="onPointerDown"
28
+ @pointermove="onPointerMove"
29
+ @pointerup="endDrag"
30
+ @pointercancel="endDrag"
31
+ @lostpointercapture="endDrag"
32
+ >
33
+ <img
34
+ v-if="showImage"
35
+ ref="imageRef"
36
+ class="k-focal-point-input-image"
37
+ :src="src"
38
+ :alt="alt ?? ''"
39
+ draggable="false"
40
+ @load="loaded = true"
41
+ @error="failed = true"
42
+ />
43
+ <span v-else class="k-focal-point-input-placeholder" aria-hidden="true">
44
+ <Icon name="photo" />
45
+ </span>
46
+ <span
47
+ v-if="point"
48
+ class="k-focal-point-input-marker"
49
+ :style="{ left: `${point.x * 100}%`, top: `${point.y * 100}%` }"
50
+ aria-hidden="true"
51
+ />
52
+ <input
53
+ v-for="axis in AXES"
54
+ :key="axis"
55
+ :ref="(el) => inputs[axis] = el"
56
+ type="range"
57
+ class="k-focal-point-input-axis k-visually-hidden"
58
+ min="0"
59
+ max="100"
60
+ step="1"
61
+ :value="Math.round((point?.[axis] ?? 0.5) * 100)"
62
+ :aria-label="axis === 'x' ? horizontalCopy : verticalCopy"
63
+ :aria-valuetext="point ? percent(point[axis]) : emptyCopy"
64
+ :aria-describedby="joinIds(hintId, control.describedBy.value)"
65
+ :aria-invalid="control.invalid.value ? 'true' : void 0"
66
+ :aria-readonly="readonly ? 'true' : void 0"
67
+ :disabled="control.disabled.value || void 0"
68
+ :tabindex="axis === activeAxis ? 0 : -1"
69
+ @focus="activeAxis = axis"
70
+ @keydown="onKeydown($event, axis)"
71
+ @input="onInput($event, axis)"
72
+ />
73
+ </div>
74
+ <span :id="hintId" class="k-visually-hidden">{{ t("focalPoint.hint") }}</span>
75
+ <div class="k-focal-point-input-bar">
76
+ <span class="k-focal-point-input-value k-tabular" aria-hidden="true">{{ valueCopy }}</span>
77
+ <Button
78
+ v-if="clearable && point && editable"
79
+ size="xs"
80
+ variant="ghost"
81
+ tone="neutral"
82
+ icon="arrow-path"
83
+ :label="clearCopy"
84
+ @click="clear"
85
+ />
86
+ </div>
87
+ </div>
88
+ </template>
89
+
90
+ <script setup>
91
+ import { computed, onMounted, ref, useAttrs, useId, useTemplateRef, watch } from "vue";
92
+ import { useFieldControl } from "../../composables/useField";
93
+ import { useLocale } from "../../composables/useLocale";
94
+ import { numberFormat } from "../../internal/core/intl";
95
+ import { joinIds } from "../../internal/forms/control";
96
+ import { hasNameAttrs, warnUnnamed } from "../../internal/forms/name";
97
+ import Button from "../core/Button.vue";
98
+ import Icon from "../core/Icon.vue";
99
+ defineOptions({ inheritAttrs: false });
100
+ const props = defineProps({
101
+ src: { type: String, required: false },
102
+ alt: { type: String, required: false },
103
+ label: { type: String, required: false },
104
+ clearable: { type: Boolean, required: false },
105
+ clearLabel: { type: String, required: false },
106
+ horizontalLabel: { type: String, required: false },
107
+ verticalLabel: { type: String, required: false },
108
+ emptyText: { type: String, required: false },
109
+ step: { type: Number, required: false, default: 0.01 },
110
+ invalid: { type: Boolean, required: false, default: void 0 },
111
+ disabled: { type: Boolean, required: false, default: void 0 },
112
+ readonly: { type: Boolean, required: false },
113
+ class: { type: null, required: false },
114
+ style: { type: null, required: false }
115
+ });
116
+ const model = defineModel({ type: null });
117
+ const AXES = ["x", "y"];
118
+ const KEY_DELTAS = {
119
+ ArrowLeft: [-1, 0],
120
+ ArrowRight: [1, 0],
121
+ ArrowUp: [0, -1],
122
+ ArrowDown: [0, 1]
123
+ };
124
+ const AXIS_KEYS = {
125
+ Home: () => 0,
126
+ End: () => 1,
127
+ PageUp: (value, page) => value - page,
128
+ PageDown: (value, page) => value + page
129
+ };
130
+ const { t, locale } = useLocale();
131
+ const attrs = useAttrs();
132
+ const hintId = useId();
133
+ const areaRef = useTemplateRef("areaRef");
134
+ const imageRef = useTemplateRef("imageRef");
135
+ const inputs = { x: null, y: null };
136
+ const activeAxis = ref("x");
137
+ const dragging = ref(false);
138
+ const loaded = ref(false);
139
+ const failed = ref(false);
140
+ const control = useFieldControl({
141
+ invalid: () => props.invalid,
142
+ disabled: () => props.disabled,
143
+ labelable: false
144
+ });
145
+ warnUnnamed(
146
+ "FocalPointInput",
147
+ () => Boolean(props.label || control.labelledBy.value) || hasNameAttrs(attrs)
148
+ );
149
+ const editable = computed(() => !control.disabled.value && !props.readonly);
150
+ const showImage = computed(() => Boolean(props.src) && !failed.value);
151
+ const point = computed(() => model.value ? normalize(model.value) : null);
152
+ const horizontalCopy = computed(() => props.horizontalLabel ?? t("focalPoint.horizontal"));
153
+ const verticalCopy = computed(() => props.verticalLabel ?? t("focalPoint.vertical"));
154
+ const emptyCopy = computed(() => props.emptyText ?? t("focalPoint.empty"));
155
+ const clearCopy = computed(() => props.clearLabel ?? t("focalPoint.reset"));
156
+ const valueCopy = computed(
157
+ () => point.value ? t("focalPoint.value", { x: percent(point.value.x), y: percent(point.value.y) }) : emptyCopy.value
158
+ );
159
+ function clamp(value) {
160
+ return Math.min(1, Math.max(0, Number.isFinite(value) ? value : 0.5));
161
+ }
162
+ function normalize(value) {
163
+ return { x: clamp(value.x), y: clamp(value.y) };
164
+ }
165
+ function round(value) {
166
+ return Math.round(clamp(value) * 1e3) / 1e3;
167
+ }
168
+ function percent(value) {
169
+ return numberFormat(locale.value.code, { style: "percent", maximumFractionDigits: 0 }).format(
170
+ value
171
+ );
172
+ }
173
+ function set(x, y) {
174
+ const next = { x: round(x), y: round(y) };
175
+ if (point.value && point.value.x === next.x && point.value.y === next.y) return;
176
+ model.value = next;
177
+ }
178
+ function fromPointer(event) {
179
+ const rect = areaRef.value?.getBoundingClientRect();
180
+ if (!rect || !rect.width || !rect.height) return;
181
+ set((event.clientX - rect.left) / rect.width, (event.clientY - rect.top) / rect.height);
182
+ }
183
+ function onPointerDown(event) {
184
+ if (!editable.value || event.button !== 0) return;
185
+ event.preventDefault();
186
+ areaRef.value?.setPointerCapture(event.pointerId);
187
+ dragging.value = true;
188
+ fromPointer(event);
189
+ inputs[activeAxis.value]?.focus({ preventScroll: true });
190
+ }
191
+ function onPointerMove(event) {
192
+ if (dragging.value) fromPointer(event);
193
+ }
194
+ function endDrag() {
195
+ dragging.value = false;
196
+ }
197
+ function onKeydown(event, axis) {
198
+ if (event.altKey || event.ctrlKey || event.metaKey) return;
199
+ const current = point.value ?? { x: 0.5, y: 0.5 };
200
+ const delta = KEY_DELTAS[event.key];
201
+ if (delta) {
202
+ event.preventDefault();
203
+ if (!editable.value) return;
204
+ const step = props.step * (event.shiftKey ? 10 : 1);
205
+ set(current.x + delta[0] * step, current.y + delta[1] * step);
206
+ const target = delta[0] ? "x" : "y";
207
+ if (target !== axis) inputs[target]?.focus();
208
+ return;
209
+ }
210
+ const jump = AXIS_KEYS[event.key];
211
+ if (jump) {
212
+ event.preventDefault();
213
+ if (!editable.value) return;
214
+ const value = jump(current[axis], props.step * 10);
215
+ set(axis === "x" ? value : current.x, axis === "y" ? value : current.y);
216
+ return;
217
+ }
218
+ if ((event.key === "Delete" || event.key === "Backspace") && props.clearable && editable.value) {
219
+ event.preventDefault();
220
+ model.value = null;
221
+ }
222
+ }
223
+ function onInput(event, axis) {
224
+ const target = event.target;
225
+ if (!editable.value) {
226
+ target.value = String(Math.round((point.value?.[axis] ?? 0.5) * 100));
227
+ return;
228
+ }
229
+ const value = Number(target.value) / 100;
230
+ const current = point.value ?? { x: 0.5, y: 0.5 };
231
+ set(axis === "x" ? value : current.x, axis === "y" ? value : current.y);
232
+ }
233
+ function clear() {
234
+ inputs[activeAxis.value]?.focus();
235
+ model.value = null;
236
+ }
237
+ watch(
238
+ () => props.src,
239
+ () => {
240
+ failed.value = false;
241
+ loaded.value = false;
242
+ }
243
+ );
244
+ onMounted(() => {
245
+ const element = imageRef.value;
246
+ if (!element?.complete) return;
247
+ if (element.naturalWidth === 0) failed.value = true;
248
+ else loaded.value = true;
249
+ });
250
+ defineExpose({ focus: () => inputs[activeAxis.value]?.focus() });
251
+ </script>