@alulore/kumiko 0.0.0-stage → 0.14.1
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/dist/badge/badge.css +287 -0
- package/dist/badge/badge.d.ts +40 -0
- package/dist/badge/badge.js +51 -0
- package/dist/badge/index.d.ts +1 -0
- package/dist/badge/index.js +1 -0
- package/dist/board/board-api.d.ts +147 -0
- package/dist/board/board-api.js +139 -0
- package/dist/board/board-card-menu.d.ts +17 -0
- package/dist/board/board-card-menu.js +53 -0
- package/dist/board/board-drag-events.d.ts +12 -0
- package/dist/board/board-drag-events.js +86 -0
- package/dist/board/board-fold.d.ts +10 -0
- package/dist/board/board-fold.js +30 -0
- package/dist/board/board-group.d.ts +10 -0
- package/dist/board/board-group.js +28 -0
- package/dist/board/board-keys.d.ts +16 -0
- package/dist/board/board-keys.js +65 -0
- package/dist/board/board-locked-rows.d.ts +23 -0
- package/dist/board/board-locked-rows.js +77 -0
- package/dist/board/board-motion.d.ts +7 -0
- package/dist/board/board-motion.js +102 -0
- package/dist/board/board-primitive.d.ts +53 -0
- package/dist/board/board-primitive.js +66 -0
- package/dist/board/board-pull.d.ts +6 -0
- package/dist/board/board-pull.js +94 -0
- package/dist/board/board-selection.d.ts +42 -0
- package/dist/board/board-selection.js +130 -0
- package/dist/board/board-slots.d.ts +1003 -0
- package/dist/board/board-slots.js +250 -0
- package/dist/board/board-types.d.ts +99 -0
- package/dist/board/board-types.js +1 -0
- package/dist/board/board.css +343 -0
- package/dist/board/board.d.ts +290 -0
- package/dist/board/board.js +23 -0
- package/dist/board/card-menu.d.ts +21 -0
- package/dist/board/card-menu.js +79 -0
- package/dist/board/cell-key.d.ts +6 -0
- package/dist/board/cell-key.js +16 -0
- package/dist/board/index.d.ts +6 -0
- package/dist/board/index.js +5 -0
- package/dist/board/interactive.d.ts +18 -0
- package/dist/board/interactive.js +51 -0
- package/dist/board/keep-capture.d.ts +16 -0
- package/dist/board/keep-capture.js +57 -0
- package/dist/board/landing.d.ts +5 -0
- package/dist/board/landing.js +10 -0
- package/dist/board/press-to-drop.d.ts +9 -0
- package/dist/board/press-to-drop.js +28 -0
- package/dist/board/refused-home.d.ts +16 -0
- package/dist/board/refused-home.js +50 -0
- package/dist/board/repeat-gate.d.ts +4 -0
- package/dist/board/repeat-gate.js +12 -0
- package/dist/board/scrollbar-gutter.d.ts +1 -0
- package/dist/board/scrollbar-gutter.js +27 -0
- package/dist/board/touch-hold.d.ts +19 -0
- package/dist/board/touch-hold.js +103 -0
- package/dist/button/button.css +576 -0
- package/dist/button/button.d.ts +50 -0
- package/dist/button/button.js +70 -0
- package/dist/button/index.d.ts +2 -0
- package/dist/button/index.js +1 -0
- package/dist/checkbox/checkbox-mark.d.ts +8 -0
- package/dist/checkbox/checkbox-mark.js +17 -0
- package/dist/checkbox/checkbox-marks.d.ts +33 -0
- package/dist/checkbox/checkbox-marks.js +30 -0
- package/dist/checkbox/checkbox.css +201 -0
- package/dist/checkbox/checkbox.d.ts +26 -0
- package/dist/checkbox/checkbox.js +11 -0
- package/dist/checkbox/index.d.ts +3 -0
- package/dist/checkbox/index.js +3 -0
- package/dist/chip/chip.css +251 -0
- package/dist/chip/chip.d.ts +39 -0
- package/dist/chip/chip.js +25 -0
- package/dist/chip/index.d.ts +1 -0
- package/dist/chip/index.js +1 -0
- package/dist/dialog/dialog.css +247 -0
- package/dist/dialog/index.d.ts +3 -0
- package/dist/dialog/index.js +3 -0
- package/dist/hover-card/hover-card.css +149 -0
- package/dist/hover-card/hover-card.d.ts +3 -0
- package/dist/hover-card/hover-card.js +8 -0
- package/dist/hover-card/index.d.ts +2 -0
- package/dist/hover-card/index.js +2 -0
- package/dist/input/index.d.ts +5 -0
- package/dist/input/index.js +5 -0
- package/dist/input/input-attributes.d.ts +12 -0
- package/dist/input/input-attributes.js +9 -0
- package/dist/input/input.css +752 -0
- package/dist/input/input.d.ts +28 -0
- package/dist/input/input.js +9 -0
- package/dist/input/search-input.d.ts +41 -0
- package/dist/input/search-input.js +47 -0
- package/dist/kbd/index.d.ts +1 -0
- package/dist/kbd/index.js +1 -0
- package/dist/kbd/kbd.css +77 -0
- package/dist/kbd/kbd.d.ts +21 -0
- package/dist/kbd/kbd.js +37 -0
- package/dist/label/index.d.ts +1 -0
- package/dist/label/index.js +1 -0
- package/dist/label/label.css +37 -0
- package/dist/label/label.d.ts +13 -0
- package/dist/label/label.js +10 -0
- package/dist/menu/index.d.ts +3 -0
- package/dist/menu/index.js +3 -0
- package/dist/menu/menu.css +349 -0
- package/dist/menu/menu.d.ts +11 -0
- package/dist/menu/menu.js +86 -0
- package/dist/menu/point-menu.d.ts +26 -0
- package/dist/menu/point-menu.js +51 -0
- package/dist/motion/flip-math.d.ts +19 -0
- package/dist/motion/flip-math.js +33 -0
- package/dist/motion/flip.d.ts +40 -0
- package/dist/motion/flip.js +203 -0
- package/dist/motion/index.d.ts +2 -0
- package/dist/motion/index.js +2 -0
- package/dist/motion/motion.css +169 -0
- package/dist/popover/index.d.ts +1 -0
- package/dist/popover/index.js +1 -0
- package/dist/popover/popover.css +208 -0
- package/dist/progress/index.d.ts +2 -0
- package/dist/progress/index.js +2 -0
- package/dist/progress/progress.css +390 -0
- package/dist/progress/progress.d.ts +36 -0
- package/dist/progress/progress.js +107 -0
- package/dist/select/index.d.ts +3 -0
- package/dist/select/index.js +2 -0
- package/dist/select/select-labels.d.ts +6 -0
- package/dist/select/select-labels.js +64 -0
- package/dist/select/select-search.d.ts +18 -0
- package/dist/select/select-search.js +10 -0
- package/dist/select/select.css +470 -0
- package/dist/select/select.d.ts +44 -0
- package/dist/select/select.js +92 -0
- package/dist/separator/index.d.ts +2 -0
- package/dist/separator/index.js +1 -0
- package/dist/separator/separator.css +32 -0
- package/dist/separator/separator.d.ts +17 -0
- package/dist/separator/separator.js +10 -0
- package/dist/slider/index.d.ts +2 -0
- package/dist/slider/index.js +2 -0
- package/dist/slider/slider-bubble.css +203 -0
- package/dist/slider/slider-feel.d.ts +33 -0
- package/dist/slider/slider-feel.js +153 -0
- package/dist/slider/slider-format.d.ts +13 -0
- package/dist/slider/slider-format.js +15 -0
- package/dist/slider/slider-marks.d.ts +16 -0
- package/dist/slider/slider-marks.js +32 -0
- package/dist/slider/slider.css +335 -0
- package/dist/slider/slider.d.ts +34 -0
- package/dist/slider/slider.js +94 -0
- package/dist/smooth-caret/caret-box.d.ts +7 -0
- package/dist/smooth-caret/caret-box.js +8 -0
- package/dist/smooth-caret/caret-controller.d.ts +7 -0
- package/dist/smooth-caret/caret-controller.js +222 -0
- package/dist/smooth-caret/caret-geometry.d.ts +11 -0
- package/dist/smooth-caret/caret-geometry.js +230 -0
- package/dist/smooth-caret/caret-glide.d.ts +13 -0
- package/dist/smooth-caret/caret-glide.js +76 -0
- package/dist/smooth-caret/caret-host.d.ts +5 -0
- package/dist/smooth-caret/caret-host.js +40 -0
- package/dist/smooth-caret/index.d.ts +7 -0
- package/dist/smooth-caret/index.js +7 -0
- package/dist/smooth-caret/remote-carets.d.ts +40 -0
- package/dist/smooth-caret/remote-carets.js +130 -0
- package/dist/smooth-caret/smooth-caret.css +157 -0
- package/dist/smooth-caret/smooth-caret.d.ts +5 -0
- package/dist/smooth-caret/smooth-caret.js +9 -0
- package/dist/smooth-caret/use-smooth-caret.d.ts +5 -0
- package/dist/smooth-caret/use-smooth-caret.js +9 -0
- package/dist/switch/index.d.ts +2 -0
- package/dist/switch/index.js +2 -0
- package/dist/switch/switch-drag.d.ts +21 -0
- package/dist/switch/switch-drag.js +23 -0
- package/dist/switch/switch-gesture.d.ts +28 -0
- package/dist/switch/switch-gesture.js +131 -0
- package/dist/switch/switch.css +189 -0
- package/dist/switch/switch.d.ts +15 -0
- package/dist/switch/switch.js +24 -0
- package/dist/textarea/index.d.ts +2 -0
- package/dist/textarea/index.js +2 -0
- package/dist/textarea/textarea.css +37 -0
- package/dist/textarea/textarea.d.ts +35 -0
- package/dist/textarea/textarea.js +11 -0
- package/dist/toast/index.d.ts +2 -0
- package/dist/toast/index.js +2 -0
- package/dist/toast/toast.css +293 -0
- package/dist/tooltip/index.d.ts +2 -0
- package/dist/tooltip/index.js +2 -0
- package/dist/tooltip/tooltip.css +143 -0
- package/dist/tooltip/tooltip.d.ts +14 -0
- package/dist/tooltip/tooltip.js +13 -0
- package/package.json +246 -5
- package/README.md +0 -3
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { disposeMirror, findField, measureCaret } from "./caret-geometry";
|
|
2
|
+
import { createGlider, prefersReducedMotion } from "./caret-glide";
|
|
3
|
+
const GLIDE_LIMIT = 400;
|
|
4
|
+
const IDLE_MS = 500;
|
|
5
|
+
const STILL_FRAMES = 12;
|
|
6
|
+
const STILL_POLL_MS = 120;
|
|
7
|
+
const FLOURISH_FALLBACK_MS = 160;
|
|
8
|
+
const FLOURISH_KEYFRAMES = [
|
|
9
|
+
{ transform: "scaleY(1)" },
|
|
10
|
+
{ transform: "scaleY(1.08)" },
|
|
11
|
+
{ transform: "scaleY(1)" }
|
|
12
|
+
];
|
|
13
|
+
function listen(target, type, handler, options) {
|
|
14
|
+
target.addEventListener(type, handler, options);
|
|
15
|
+
return () => target.removeEventListener(type, handler, options);
|
|
16
|
+
}
|
|
17
|
+
function insertsText(event) {
|
|
18
|
+
return event instanceof InputEvent && event.inputType === "insertText";
|
|
19
|
+
}
|
|
20
|
+
/* The flourish lasts as long as the `lift` motion token. */
|
|
21
|
+
function flourishMs(bar) {
|
|
22
|
+
const value = getComputedStyle(bar).getPropertyValue("--kumiko-lift-duration").trim();
|
|
23
|
+
const ms = value.endsWith("ms")
|
|
24
|
+
? Number(value.replace("ms", ""))
|
|
25
|
+
: Number(value.replace("s", "")) * 1000;
|
|
26
|
+
return Number.isFinite(ms) && value !== "" ? ms : FLOURISH_FALLBACK_MS;
|
|
27
|
+
}
|
|
28
|
+
/* The caret takes the field's own `--kumiko-smooth-caret-color`, or its accent, so themes reach it. */
|
|
29
|
+
function colourOf(field) {
|
|
30
|
+
const style = getComputedStyle(field);
|
|
31
|
+
return (style.getPropertyValue("--kumiko-smooth-caret-color").trim() ||
|
|
32
|
+
style.getPropertyValue("--kumiko-input-accent").trim());
|
|
33
|
+
}
|
|
34
|
+
function hasArea(field) {
|
|
35
|
+
const box = field.getBoundingClientRect();
|
|
36
|
+
return box.width > 0 && box.height > 0;
|
|
37
|
+
}
|
|
38
|
+
function boxOf(field) {
|
|
39
|
+
const box = field.getBoundingClientRect();
|
|
40
|
+
return `${box.left},${box.top},${box.width},${box.height},${field.scrollTop},${field.scrollLeft}`;
|
|
41
|
+
}
|
|
42
|
+
/* Follows the focused field and draws one gliding caret over it. */
|
|
43
|
+
class CaretController {
|
|
44
|
+
tracked = null;
|
|
45
|
+
frame = 0;
|
|
46
|
+
idleTimer = 0;
|
|
47
|
+
composing = false;
|
|
48
|
+
flourish = false;
|
|
49
|
+
visible = false;
|
|
50
|
+
last = null;
|
|
51
|
+
watchFrame = 0;
|
|
52
|
+
watchTimer = 0;
|
|
53
|
+
stillFrames = 0;
|
|
54
|
+
fieldBox = "";
|
|
55
|
+
resizeObserver = new ResizeObserver(() => this.schedule());
|
|
56
|
+
glider;
|
|
57
|
+
caret;
|
|
58
|
+
bar;
|
|
59
|
+
constructor(caret, bar) {
|
|
60
|
+
this.caret = caret;
|
|
61
|
+
this.bar = bar;
|
|
62
|
+
this.glider = createGlider((x, y, height) => {
|
|
63
|
+
caret.style.transform = `translate3d(${x}px, ${y}px, 0)`;
|
|
64
|
+
caret.style.height = `${height}px`;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
start() {
|
|
68
|
+
const passive = { capture: true, passive: true };
|
|
69
|
+
const disposers = [
|
|
70
|
+
listen(document, "selectionchange", () => this.activity()),
|
|
71
|
+
listen(document, "input", (event) => this.noteInput(event), passive),
|
|
72
|
+
listen(document, "keydown", () => this.activity(), passive),
|
|
73
|
+
listen(document, "keyup", () => this.schedule(), passive),
|
|
74
|
+
listen(document, "pointerup", () => this.schedule(), passive),
|
|
75
|
+
listen(document, "focusin", () => this.activity()),
|
|
76
|
+
listen(document, "focusout", () => this.schedule()),
|
|
77
|
+
listen(document, "compositionstart", () => this.setComposing(true), passive),
|
|
78
|
+
listen(document, "compositionend", () => this.setComposing(false), passive),
|
|
79
|
+
listen(window, "scroll", () => this.schedule(), passive),
|
|
80
|
+
listen(document, "scroll", () => this.schedule(), passive),
|
|
81
|
+
listen(window, "resize", () => this.schedule()),
|
|
82
|
+
listen(window, "blur", () => this.schedule()),
|
|
83
|
+
listen(window, "focus", () => this.schedule())
|
|
84
|
+
];
|
|
85
|
+
this.schedule();
|
|
86
|
+
return () => {
|
|
87
|
+
this.stop();
|
|
88
|
+
for (const dispose of disposers) {
|
|
89
|
+
dispose();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
stop() {
|
|
94
|
+
cancelAnimationFrame(this.frame);
|
|
95
|
+
cancelAnimationFrame(this.watchFrame);
|
|
96
|
+
window.clearTimeout(this.watchTimer);
|
|
97
|
+
window.clearTimeout(this.idleTimer);
|
|
98
|
+
this.glider.stop();
|
|
99
|
+
this.resizeObserver.disconnect();
|
|
100
|
+
delete this.tracked?.dataset.smoothCaret;
|
|
101
|
+
disposeMirror();
|
|
102
|
+
}
|
|
103
|
+
setTracked(field) {
|
|
104
|
+
if (this.tracked === field) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
delete this.tracked?.dataset.smoothCaret;
|
|
108
|
+
this.resizeObserver.disconnect();
|
|
109
|
+
this.tracked = field;
|
|
110
|
+
if (field) {
|
|
111
|
+
field.dataset.smoothCaret = "";
|
|
112
|
+
this.resizeObserver.observe(field);
|
|
113
|
+
this.stillFrames = 0;
|
|
114
|
+
this.watchField();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
watchField() {
|
|
118
|
+
if (this.watchFrame || this.watchTimer) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (this.stillFrames < STILL_FRAMES) {
|
|
122
|
+
this.watchFrame = requestAnimationFrame(() => this.followField());
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
this.watchTimer = window.setTimeout(() => {
|
|
126
|
+
this.watchTimer = 0;
|
|
127
|
+
this.watchFrame = requestAnimationFrame(() => this.followField());
|
|
128
|
+
}, STILL_POLL_MS);
|
|
129
|
+
}
|
|
130
|
+
wakeWatch() {
|
|
131
|
+
this.stillFrames = 0;
|
|
132
|
+
if (this.watchTimer) {
|
|
133
|
+
window.clearTimeout(this.watchTimer);
|
|
134
|
+
this.watchTimer = 0;
|
|
135
|
+
this.watchField();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
followField() {
|
|
139
|
+
this.watchFrame = 0;
|
|
140
|
+
const { tracked } = this;
|
|
141
|
+
if (!tracked || !document.hasFocus() || document.hidden) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const stale = !tracked.isConnected || document.activeElement !== tracked;
|
|
145
|
+
const moved = stale || boxOf(tracked) !== this.fieldBox;
|
|
146
|
+
this.stillFrames = moved ? 0 : this.stillFrames + 1;
|
|
147
|
+
if (moved && (stale || !this.frame)) {
|
|
148
|
+
this.render(!stale);
|
|
149
|
+
}
|
|
150
|
+
this.watchField();
|
|
151
|
+
}
|
|
152
|
+
hide() {
|
|
153
|
+
this.visible = false;
|
|
154
|
+
delete this.caret.dataset.visible;
|
|
155
|
+
}
|
|
156
|
+
show(rect, follow, field) {
|
|
157
|
+
const snap = follow ||
|
|
158
|
+
!this.visible ||
|
|
159
|
+
!this.last ||
|
|
160
|
+
Math.hypot(rect.x - this.last.x, rect.y - this.last.y) > GLIDE_LIMIT;
|
|
161
|
+
const reduced = prefersReducedMotion(field);
|
|
162
|
+
this.caret.style.setProperty("--_caret-color", colourOf(field));
|
|
163
|
+
this.glider.to(rect.x, rect.y, rect.height, snap || reduced);
|
|
164
|
+
this.caret.dataset.visible = "true";
|
|
165
|
+
this.visible = true;
|
|
166
|
+
this.last = rect;
|
|
167
|
+
if (this.flourish && !reduced) {
|
|
168
|
+
this.bar.animate(FLOURISH_KEYFRAMES, {
|
|
169
|
+
duration: flourishMs(this.bar),
|
|
170
|
+
easing: "ease-out"
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
this.flourish = false;
|
|
174
|
+
}
|
|
175
|
+
render(follow) {
|
|
176
|
+
const usable = document.hasFocus() && !this.composing;
|
|
177
|
+
const candidate = usable ? findField(document.activeElement) : null;
|
|
178
|
+
const field = candidate?.isConnected && hasArea(candidate) ? candidate : null;
|
|
179
|
+
this.setTracked(field);
|
|
180
|
+
this.fieldBox = field ? boxOf(field) : "";
|
|
181
|
+
const rect = field ? measureCaret(field) : null;
|
|
182
|
+
if (field && rect) {
|
|
183
|
+
this.show(rect, follow, field);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
this.flourish = false;
|
|
187
|
+
this.hide();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
schedule() {
|
|
191
|
+
this.wakeWatch();
|
|
192
|
+
if (!this.frame) {
|
|
193
|
+
this.frame = requestAnimationFrame(() => {
|
|
194
|
+
this.frame = 0;
|
|
195
|
+
this.render(false);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
activity() {
|
|
200
|
+
delete this.caret.dataset.idle;
|
|
201
|
+
window.clearTimeout(this.idleTimer);
|
|
202
|
+
this.idleTimer = window.setTimeout(() => {
|
|
203
|
+
this.caret.dataset.idle = "true";
|
|
204
|
+
}, IDLE_MS);
|
|
205
|
+
this.schedule();
|
|
206
|
+
}
|
|
207
|
+
setComposing(value) {
|
|
208
|
+
this.composing = value;
|
|
209
|
+
this.schedule();
|
|
210
|
+
}
|
|
211
|
+
noteInput(event) {
|
|
212
|
+
this.flourish = insertsText(event);
|
|
213
|
+
this.activity();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Starts the gliding caret over the given caret and bar elements. Returns a function that stops it and gives
|
|
218
|
+
* the native caret back.
|
|
219
|
+
*/
|
|
220
|
+
export function startSmoothCaret(caret, bar) {
|
|
221
|
+
return new CaretController(caret, bar).start();
|
|
222
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Box } from "./caret-box";
|
|
2
|
+
export interface CaretRect {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
height: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function findField(active: Element | null): HTMLElement | null;
|
|
8
|
+
export declare function disposeMirror(): void;
|
|
9
|
+
export type ClipBox = Box;
|
|
10
|
+
export declare function caretInsideBox(caret: CaretRect, box: ClipBox): boolean;
|
|
11
|
+
export declare function measureCaret(field: HTMLElement): CaretRect | null;
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { intersectBoxes } from "./caret-box";
|
|
2
|
+
const TEXT_INPUT_TYPES = new Set(["text", "search", "url", "email", "password", "tel"]);
|
|
3
|
+
const MIRRORED_PROPERTIES = [
|
|
4
|
+
"box-sizing",
|
|
5
|
+
"border-top-width",
|
|
6
|
+
"border-right-width",
|
|
7
|
+
"border-bottom-width",
|
|
8
|
+
"border-left-width",
|
|
9
|
+
"padding-top",
|
|
10
|
+
"padding-right",
|
|
11
|
+
"padding-bottom",
|
|
12
|
+
"padding-left",
|
|
13
|
+
"font-family",
|
|
14
|
+
"font-size",
|
|
15
|
+
"font-style",
|
|
16
|
+
"font-weight",
|
|
17
|
+
"font-variant",
|
|
18
|
+
"font-stretch",
|
|
19
|
+
"font-kerning",
|
|
20
|
+
"font-feature-settings",
|
|
21
|
+
"letter-spacing",
|
|
22
|
+
"word-spacing",
|
|
23
|
+
"text-transform",
|
|
24
|
+
"text-indent",
|
|
25
|
+
"text-align",
|
|
26
|
+
"text-rendering",
|
|
27
|
+
"tab-size",
|
|
28
|
+
"line-height",
|
|
29
|
+
"direction",
|
|
30
|
+
"overflow-wrap",
|
|
31
|
+
"word-break"
|
|
32
|
+
];
|
|
33
|
+
const PASSWORD_MASK = "•";
|
|
34
|
+
const MARKER_TEXT = "";
|
|
35
|
+
let mirror = null;
|
|
36
|
+
function isTextInput(element) {
|
|
37
|
+
return element instanceof HTMLInputElement && TEXT_INPUT_TYPES.has(element.type);
|
|
38
|
+
}
|
|
39
|
+
function isTextField(element) {
|
|
40
|
+
return isTextInput(element) || element instanceof HTMLTextAreaElement;
|
|
41
|
+
}
|
|
42
|
+
export function findField(active) {
|
|
43
|
+
if (!(active instanceof HTMLElement) ||
|
|
44
|
+
active.closest('[data-native-caret], [data-kumiko-caret="native"]')) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
if (isTextField(active)) {
|
|
48
|
+
const supported = active.selectionStart !== null && !active.readOnly && !active.disabled;
|
|
49
|
+
return supported ? active : null;
|
|
50
|
+
}
|
|
51
|
+
return active.isContentEditable && active.contentEditable !== "plaintext-only" ? active : null;
|
|
52
|
+
}
|
|
53
|
+
function pixels(value) {
|
|
54
|
+
const parsed = Number(value.replace("px", ""));
|
|
55
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
56
|
+
}
|
|
57
|
+
function lineHeightOf(style, fallback) {
|
|
58
|
+
const parsed = Number(style.lineHeight.replace("px", ""));
|
|
59
|
+
return Number.isFinite(parsed) ? parsed : fallback;
|
|
60
|
+
}
|
|
61
|
+
function ensureMirror() {
|
|
62
|
+
if (mirror?.isConnected) {
|
|
63
|
+
return mirror;
|
|
64
|
+
}
|
|
65
|
+
const element = document.createElement("div");
|
|
66
|
+
element.setAttribute("aria-hidden", "true");
|
|
67
|
+
element.style.position = "fixed";
|
|
68
|
+
element.style.top = "0";
|
|
69
|
+
element.style.left = "0";
|
|
70
|
+
element.style.visibility = "hidden";
|
|
71
|
+
element.style.pointerEvents = "none";
|
|
72
|
+
element.style.contain = "layout style paint";
|
|
73
|
+
document.body.append(element);
|
|
74
|
+
mirror = element;
|
|
75
|
+
return element;
|
|
76
|
+
}
|
|
77
|
+
export function disposeMirror() {
|
|
78
|
+
mirror?.remove();
|
|
79
|
+
mirror = null;
|
|
80
|
+
}
|
|
81
|
+
function displayedText(field, text) {
|
|
82
|
+
return field instanceof HTMLInputElement && field.type === "password"
|
|
83
|
+
? PASSWORD_MASK.repeat(text.length)
|
|
84
|
+
: text;
|
|
85
|
+
}
|
|
86
|
+
function fillMirror(field, style) {
|
|
87
|
+
const host = ensureMirror();
|
|
88
|
+
for (const property of MIRRORED_PROPERTIES) {
|
|
89
|
+
host.style.setProperty(property, style.getPropertyValue(property));
|
|
90
|
+
}
|
|
91
|
+
const scrollbar = Math.max(0, field.offsetWidth -
|
|
92
|
+
field.clientWidth -
|
|
93
|
+
pixels(style.borderLeftWidth) -
|
|
94
|
+
pixels(style.borderRightWidth));
|
|
95
|
+
host.style.borderStyle = "solid";
|
|
96
|
+
host.style.borderColor = "transparent";
|
|
97
|
+
host.style.boxSizing = "border-box";
|
|
98
|
+
host.style.width = `${field.offsetWidth}px`;
|
|
99
|
+
host.style.paddingRight = `${pixels(style.paddingRight) + scrollbar}px`;
|
|
100
|
+
host.style.whiteSpace = field instanceof HTMLTextAreaElement ? style.whiteSpace : "pre";
|
|
101
|
+
const caret = field.selectionStart ?? 0;
|
|
102
|
+
const marker = document.createElement("span");
|
|
103
|
+
marker.textContent = MARKER_TEXT;
|
|
104
|
+
host.replaceChildren(document.createTextNode(displayedText(field, field.value.slice(0, caret))), marker, document.createTextNode(displayedText(field, field.value.slice(caret))));
|
|
105
|
+
return marker;
|
|
106
|
+
}
|
|
107
|
+
function measureTextField(field) {
|
|
108
|
+
if (field.selectionStart !== field.selectionEnd) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
const style = getComputedStyle(field);
|
|
112
|
+
const marker = fillMirror(field, style);
|
|
113
|
+
const host = ensureMirror();
|
|
114
|
+
const hostRect = host.getBoundingClientRect();
|
|
115
|
+
const markerRect = marker.getBoundingClientRect();
|
|
116
|
+
const rect = field.getBoundingClientRect();
|
|
117
|
+
const borderTop = pixels(style.borderTopWidth);
|
|
118
|
+
const paddingTop = pixels(style.paddingTop);
|
|
119
|
+
const paddingBottom = pixels(style.paddingBottom);
|
|
120
|
+
const markerCenter = markerRect.top + markerRect.height / 2 - hostRect.top;
|
|
121
|
+
const x = rect.left + markerRect.left - hostRect.left - field.scrollLeft;
|
|
122
|
+
let centerY = rect.top + markerCenter - field.scrollTop;
|
|
123
|
+
if (field instanceof HTMLInputElement) {
|
|
124
|
+
const contentHeight = field.clientHeight - paddingTop - paddingBottom;
|
|
125
|
+
const lineHeight = host.clientHeight - paddingTop - paddingBottom;
|
|
126
|
+
const lineCenter = borderTop + paddingTop + lineHeight / 2;
|
|
127
|
+
centerY =
|
|
128
|
+
rect.top + borderTop + paddingTop + contentHeight / 2 + (markerCenter - lineCenter);
|
|
129
|
+
}
|
|
130
|
+
const height = lineHeightOf(style, markerRect.height);
|
|
131
|
+
return { x, y: centerY - height / 2, height };
|
|
132
|
+
}
|
|
133
|
+
function textRangeRect(range) {
|
|
134
|
+
const rects = range.getClientRects();
|
|
135
|
+
const last = rects.item(rects.length - 1);
|
|
136
|
+
return last && last.height > 0 ? last : null;
|
|
137
|
+
}
|
|
138
|
+
function previousCharRect(container, offset) {
|
|
139
|
+
const previous = container.childNodes[offset - 1];
|
|
140
|
+
if (!previous || previous.nodeType !== Node.TEXT_NODE || !previous.textContent) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
const range = document.createRange();
|
|
144
|
+
range.setStart(previous, previous.textContent.length - 1);
|
|
145
|
+
range.setEnd(previous, previous.textContent.length);
|
|
146
|
+
const rect = textRangeRect(range);
|
|
147
|
+
return rect ? new DOMRect(rect.right, rect.top, 0, rect.height) : null;
|
|
148
|
+
}
|
|
149
|
+
function containerElement(container) {
|
|
150
|
+
return container instanceof Element ? container : container.parentElement;
|
|
151
|
+
}
|
|
152
|
+
function emptyLineRect(range) {
|
|
153
|
+
const element = containerElement(range.startContainer);
|
|
154
|
+
if (!element) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
const style = getComputedStyle(element);
|
|
158
|
+
const rect = element.getBoundingClientRect();
|
|
159
|
+
const height = lineHeightOf(style, pixels(style.fontSize) * 1.25);
|
|
160
|
+
return {
|
|
161
|
+
x: rect.left + pixels(style.borderLeftWidth) + pixels(style.paddingLeft),
|
|
162
|
+
y: rect.top + pixels(style.borderTopWidth) + pixels(style.paddingTop),
|
|
163
|
+
height
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function measureEditable(field) {
|
|
167
|
+
const selection = field.ownerDocument.getSelection();
|
|
168
|
+
if (!selection || selection.rangeCount === 0 || !selection.isCollapsed) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
const range = selection.getRangeAt(0);
|
|
172
|
+
if (!field.contains(range.startContainer)) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
const rect = textRangeRect(range) ??
|
|
176
|
+
(range.startContainer instanceof Element
|
|
177
|
+
? previousCharRect(range.startContainer, range.startOffset)
|
|
178
|
+
: null);
|
|
179
|
+
if (!rect) {
|
|
180
|
+
return emptyLineRect(range);
|
|
181
|
+
}
|
|
182
|
+
const element = containerElement(range.startContainer);
|
|
183
|
+
const style = element ? getComputedStyle(element) : null;
|
|
184
|
+
const height = style ? lineHeightOf(style, rect.height) : rect.height;
|
|
185
|
+
return { x: rect.left, y: rect.top + rect.height / 2 - height / 2, height };
|
|
186
|
+
}
|
|
187
|
+
export function caretInsideBox(caret, box) {
|
|
188
|
+
const middle = caret.y + caret.height / 2;
|
|
189
|
+
return (caret.x >= box.left - 1 &&
|
|
190
|
+
caret.x <= box.right + 1 &&
|
|
191
|
+
middle >= box.top &&
|
|
192
|
+
middle <= box.bottom);
|
|
193
|
+
}
|
|
194
|
+
function paddingBoxOf(element) {
|
|
195
|
+
const style = getComputedStyle(element);
|
|
196
|
+
const rect = element.getBoundingClientRect();
|
|
197
|
+
return {
|
|
198
|
+
left: rect.left + pixels(style.borderLeftWidth),
|
|
199
|
+
right: rect.right - pixels(style.borderRightWidth),
|
|
200
|
+
top: rect.top + pixels(style.borderTopWidth),
|
|
201
|
+
bottom: rect.bottom - pixels(style.borderBottomWidth)
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function clipsOverflow(element) {
|
|
205
|
+
const style = getComputedStyle(element);
|
|
206
|
+
return style.overflowX !== "visible" || style.overflowY !== "visible";
|
|
207
|
+
}
|
|
208
|
+
function visibleBoxOf(field) {
|
|
209
|
+
let box = intersectBoxes(paddingBoxOf(field), {
|
|
210
|
+
left: 0,
|
|
211
|
+
right: window.innerWidth,
|
|
212
|
+
top: 0,
|
|
213
|
+
bottom: window.innerHeight
|
|
214
|
+
});
|
|
215
|
+
let ancestor = field.parentElement;
|
|
216
|
+
while (ancestor && ancestor !== document.documentElement) {
|
|
217
|
+
if (clipsOverflow(ancestor)) {
|
|
218
|
+
box = intersectBoxes(box, paddingBoxOf(ancestor));
|
|
219
|
+
}
|
|
220
|
+
ancestor = ancestor.parentElement;
|
|
221
|
+
}
|
|
222
|
+
return box;
|
|
223
|
+
}
|
|
224
|
+
function insideField(field, caret) {
|
|
225
|
+
return caretInsideBox(caret, visibleBoxOf(field));
|
|
226
|
+
}
|
|
227
|
+
export function measureCaret(field) {
|
|
228
|
+
const caret = isTextField(field) ? measureTextField(field) : measureEditable(field);
|
|
229
|
+
return caret && insideField(field, caret) ? caret : null;
|
|
230
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface GlideAxis {
|
|
2
|
+
at: number;
|
|
3
|
+
speed: number;
|
|
4
|
+
}
|
|
5
|
+
export declare function glideStep(axis: GlideAxis, target: number, seconds: number): GlideAxis;
|
|
6
|
+
export declare function leash(axis: GlideAxis, target: number): GlideAxis;
|
|
7
|
+
export declare function atRest(axis: GlideAxis, target: number): boolean;
|
|
8
|
+
export declare function prefersReducedMotion(from?: Element | null): boolean;
|
|
9
|
+
export interface Glider {
|
|
10
|
+
to: (x: number, y: number, height: number, snap: boolean) => void;
|
|
11
|
+
stop: () => void;
|
|
12
|
+
}
|
|
13
|
+
export declare function createGlider(paint: (x: number, y: number, height: number) => void): Glider;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
const OMEGA = 52;
|
|
2
|
+
const MAX_LAG = 5;
|
|
3
|
+
const MAX_STEP_S = 1 / 30;
|
|
4
|
+
const REST_DISTANCE = 0.05;
|
|
5
|
+
const REST_SPEED = 2;
|
|
6
|
+
export function glideStep(axis, target, seconds) {
|
|
7
|
+
const step = Math.min(Math.max(seconds, 0), MAX_STEP_S);
|
|
8
|
+
const offset = axis.at - target;
|
|
9
|
+
const drift = axis.speed + OMEGA * offset;
|
|
10
|
+
const fade = Math.exp(-OMEGA * step);
|
|
11
|
+
return {
|
|
12
|
+
at: target + (offset + drift * step) * fade,
|
|
13
|
+
speed: (drift - OMEGA * (offset + drift * step)) * fade
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export function leash(axis, target) {
|
|
17
|
+
const behind = target - axis.at;
|
|
18
|
+
if (Math.abs(behind) <= MAX_LAG) {
|
|
19
|
+
return axis;
|
|
20
|
+
}
|
|
21
|
+
return { at: target - Math.sign(behind) * MAX_LAG, speed: axis.speed };
|
|
22
|
+
}
|
|
23
|
+
export function atRest(axis, target) {
|
|
24
|
+
return Math.abs(target - axis.at) < REST_DISTANCE && Math.abs(axis.speed) < REST_SPEED;
|
|
25
|
+
}
|
|
26
|
+
/* Reduced motion and the `still` preset both turn the glide off: the caret snaps. */
|
|
27
|
+
export function prefersReducedMotion(from) {
|
|
28
|
+
return (window.matchMedia("(prefers-reduced-motion: reduce)").matches ||
|
|
29
|
+
Boolean(from?.closest('[data-kumiko-motion="still"]')));
|
|
30
|
+
}
|
|
31
|
+
export function createGlider(paint) {
|
|
32
|
+
let axes = [];
|
|
33
|
+
let targets = [];
|
|
34
|
+
let frame = 0;
|
|
35
|
+
let previous = 0;
|
|
36
|
+
function draw() {
|
|
37
|
+
paint(axes[0]?.at ?? 0, axes[1]?.at ?? 0, axes[2]?.at ?? 0);
|
|
38
|
+
}
|
|
39
|
+
function tick(now) {
|
|
40
|
+
const seconds = (now - previous) / 1000;
|
|
41
|
+
previous = now;
|
|
42
|
+
axes = axes.map((axis, index) => {
|
|
43
|
+
const target = targets[index] ?? axis.at;
|
|
44
|
+
return leash(glideStep(axis, target, seconds), target);
|
|
45
|
+
});
|
|
46
|
+
const resting = axes.every((axis, index) => atRest(axis, targets[index] ?? axis.at));
|
|
47
|
+
if (resting) {
|
|
48
|
+
axes = targets.map((at) => ({ at, speed: 0 }));
|
|
49
|
+
frame = 0;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
frame = requestAnimationFrame(tick);
|
|
53
|
+
}
|
|
54
|
+
draw();
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
to(x, y, height, snap) {
|
|
58
|
+
targets = [x, y, height];
|
|
59
|
+
if (snap || axes.length === 0) {
|
|
60
|
+
cancelAnimationFrame(frame);
|
|
61
|
+
frame = 0;
|
|
62
|
+
axes = targets.map((at) => ({ at, speed: 0 }));
|
|
63
|
+
draw();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!frame) {
|
|
67
|
+
previous = performance.now();
|
|
68
|
+
frame = requestAnimationFrame(tick);
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
stop() {
|
|
72
|
+
cancelAnimationFrame(frame);
|
|
73
|
+
frame = 0;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { startSmoothCaret } from "./caret-controller";
|
|
2
|
+
let users = 0;
|
|
3
|
+
let stopHost = null;
|
|
4
|
+
/* Builds the one caret element and starts following the focused field. Returns what undoes both. */
|
|
5
|
+
function mountCaret() {
|
|
6
|
+
const caret = document.createElement("div");
|
|
7
|
+
caret.dataset.slot = "smooth-caret";
|
|
8
|
+
caret.setAttribute("aria-hidden", "true");
|
|
9
|
+
const bar = document.createElement("div");
|
|
10
|
+
bar.dataset.slot = "smooth-caret-bar";
|
|
11
|
+
caret.append(bar);
|
|
12
|
+
document.body.append(caret);
|
|
13
|
+
const stop = startSmoothCaret(caret, bar);
|
|
14
|
+
return () => {
|
|
15
|
+
stop();
|
|
16
|
+
caret.remove();
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Asks for the gliding caret. The first ask creates the single caret and the last release removes it, so any
|
|
21
|
+
* number of fields and `SmoothCaret` mounts share one. Returns the release.
|
|
22
|
+
*/
|
|
23
|
+
export function acquireSmoothCaret() {
|
|
24
|
+
users += 1;
|
|
25
|
+
if (users === 1) {
|
|
26
|
+
stopHost = mountCaret();
|
|
27
|
+
}
|
|
28
|
+
let released = false;
|
|
29
|
+
return () => {
|
|
30
|
+
if (released) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
released = true;
|
|
34
|
+
users -= 1;
|
|
35
|
+
if (users === 0) {
|
|
36
|
+
stopHost?.();
|
|
37
|
+
stopHost = null;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { SmoothCaret } from "./smooth-caret";
|
|
2
|
+
export { acquireSmoothCaret } from "./caret-host";
|
|
3
|
+
export { startSmoothCaret } from "./caret-controller";
|
|
4
|
+
export { measureCaret, type CaretRect } from "./caret-geometry";
|
|
5
|
+
export { useSmoothCaret } from "./use-smooth-caret";
|
|
6
|
+
export { createGlider, prefersReducedMotion, type Glider } from "./caret-glide";
|
|
7
|
+
export { createRemoteCarets, spotJumped, spotMoved, type RemoteCaret, type RemoteCaretSpot, type RemoteCarets, type RemoteCaretsOptions } from "./remote-carets";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { SmoothCaret } from "./smooth-caret";
|
|
2
|
+
export { acquireSmoothCaret } from "./caret-host";
|
|
3
|
+
export { startSmoothCaret } from "./caret-controller";
|
|
4
|
+
export { measureCaret } from "./caret-geometry";
|
|
5
|
+
export { useSmoothCaret } from "./use-smooth-caret";
|
|
6
|
+
export { createGlider, prefersReducedMotion } from "./caret-glide";
|
|
7
|
+
export { createRemoteCarets, spotJumped, spotMoved } from "./remote-carets";
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export declare const GLIDE_LIMIT = 400;
|
|
2
|
+
export declare const TAG_STILL_MS = 1800;
|
|
3
|
+
/** Where a caret is drawn, in viewport pixels: the left edge and top of its line, and the line's height. */
|
|
4
|
+
export interface RemoteCaretSpot {
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
height: number;
|
|
8
|
+
}
|
|
9
|
+
/** One remote person. A `null` spot hides their caret without removing them. */
|
|
10
|
+
export interface RemoteCaret {
|
|
11
|
+
id: string | number;
|
|
12
|
+
name: string;
|
|
13
|
+
colour: string;
|
|
14
|
+
spot: RemoteCaretSpot | null;
|
|
15
|
+
}
|
|
16
|
+
export interface RemoteCaretsOptions {
|
|
17
|
+
/**
|
|
18
|
+
* The element the overlay is added to. The `still` preset is read from it and its ancestors.
|
|
19
|
+
* @defaultValue `document.body`
|
|
20
|
+
*/
|
|
21
|
+
host?: HTMLElement;
|
|
22
|
+
/**
|
|
23
|
+
* Milliseconds a caret stays still before its name tag fades.
|
|
24
|
+
* @defaultValue 1800
|
|
25
|
+
*/
|
|
26
|
+
stillMs?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface RemoteCarets {
|
|
29
|
+
/** Shows these people. Anyone missing from the list fades out and is removed. `snap` skips the glide. */
|
|
30
|
+
place: (carets: readonly RemoteCaret[], snap: boolean) => void;
|
|
31
|
+
/** Removes the overlay and every timer. */
|
|
32
|
+
destroy: () => void;
|
|
33
|
+
}
|
|
34
|
+
export declare function spotMoved(before: RemoteCaretSpot | null, after: RemoteCaretSpot): boolean;
|
|
35
|
+
export declare function spotJumped(before: RemoteCaretSpot | null, after: RemoteCaretSpot): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* The overlay for remote collaborators: one gliding caret bar and name tag per person, in a fixed layer over
|
|
38
|
+
* the page. Framework-free, so any editor can drive it from its own awareness data.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createRemoteCarets(options?: RemoteCaretsOptions): RemoteCarets;
|