-
Notifications
You must be signed in to change notification settings - Fork 593
/
Copy pathutil.tsx
137 lines (117 loc) · 3.66 KB
/
util.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as React from "react";
export interface ControlProps {
id?: string;
className?: string;
ariaLabel?: string;
ariaHidden?: boolean;
ariaDescribedBy?: string;
role?: string;
}
export interface ContainerProps extends React.PropsWithChildren<ControlProps> {
}
export function jsxLF(loc: string, ...rest: JSX.Element[]) {
const indices: number[] = [];
loc.replace(/\{\d\}/g, match => {
indices.push(parseInt(match.substr(1, 1)));
return match;
});
const out: JSX.Element[] = [];
let parts: string[];
let i = 0;
for (const index of indices) {
parts = loc.split(`{${index}}`);
pxt.U.assert(parts.length === 2);
out.push(<span key={i++}>{parts[0]}</span>);
out.push(<span key={i++}>{rest[index]}</span>);
loc = parts[1]
}
out.push(<span key={i++}>{loc}</span>);
return out;
}
export function fireClickOnEnter(e: React.KeyboardEvent<HTMLElement>) {
const charCode = (typeof e.which == "number") ? e.which : e.keyCode;
if (charCode === 13 /* enter */ || charCode === 32 /* space */) {
e.preventDefault();
e.currentTarget.click();
}
}
export function classList(...classes: (string | undefined)[]) {
return classes
.filter(c => typeof c === "string")
.reduce((prev, c) => prev.concat(c.split(" ")), [] as string[])
.map(c => c.trim())
.filter(c => !!c)
.join(" ");
}
export function nodeListToArray<U extends Node>(list: NodeListOf<U>): U[] {
const out: U[] = [];
for (const node of list) {
out.push(node);
}
return out;
}
export enum CheckboxStatus {
Selected,
Unselected,
Waiting
}
export interface ClientCoordinates {
clientX: number;
clientY: number;
}
export function clientCoord(ev: PointerEvent | MouseEvent | TouchEvent): ClientCoordinates {
if ((ev as TouchEvent).touches) {
const te = ev as TouchEvent;
if (te.touches.length) {
return te.touches[0];
}
return te.changedTouches[0];
}
return (ev as PointerEvent | MouseEvent);
}
export function screenToSVGCoord(ref: SVGSVGElement, coord: ClientCoordinates) {
const screenCoord = ref.createSVGPoint();
screenCoord.x = coord.clientX;
screenCoord.y = coord.clientY;
return screenCoord.matrixTransform(ref.getScreenCTM().inverse());
}
export function findNextFocusableElement(elements: HTMLElement[], focusedIndex: number, index: number, forward: boolean, isFocusable?: (e: HTMLElement) => boolean): HTMLElement {
const increment = forward ? 1 : -1;
const element = elements[index];
// in this case, there are no focusable elements
if (focusedIndex === index) {
return element;
}
if (isFocusable ? isFocusable(element) : getComputedStyle(element).display !== "none") {
return element;
} else {
if (index + increment >= elements.length) {
index = 0;
} else if (index + increment < 0) {
index = elements.length - 1;
} else {
index += increment;
}
}
return findNextFocusableElement(elements, focusedIndex, index, forward, isFocusable);
}
export function isFocusable(e: HTMLElement) {
if (e) {
return (e.getAttribute("data-isfocusable") === "true"
|| e.tabIndex !== -1)
&& getComputedStyle(e).display !== "none";
} else {
return false;
}
}
export function focusLastActive(el: HTMLElement) {
while (el && !isFocusable(el)) {
const toFocusParent = el.parentElement;
if (toFocusParent) {
el = toFocusParent;
} else {
break;
}
}
el.focus();
}