-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsvg_editor.edit_form.tsx
More file actions
162 lines (146 loc) · 4.32 KB
/
svg_editor.edit_form.tsx
File metadata and controls
162 lines (146 loc) · 4.32 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { offset, shift, useFloating } from '@floating-ui/react-dom';
import type { MouseEvent, SubmitEvent } from 'react';
import { useRef } from 'react';
import {
AtomLabelEditButtonStyled,
AtomLabelEditDialogStyled,
AtomLabelEditFormStyled,
AtomLabelEditInputStyled,
greekLetters,
primes,
} from './svg_editor.styled.ts';
interface AtomLabelEditFormProps {
defaultValue: string;
atomCoords: { x: number; y: number };
onSubmit: (value: string) => void;
onCancel: () => void;
}
export function AtomLabelEditForm(props: AtomLabelEditFormProps) {
const { defaultValue, atomCoords, onSubmit, onCancel } = props;
const floating = useFloating({
placement: 'bottom-start',
strategy: 'absolute',
transform: false,
middleware: [
offset({ crossAxis: 5 }),
shift({
crossAxis: true,
altBoundary: true,
}),
],
});
const formRef = useRef<HTMLFormElement>(null);
function onFormSubmit(event: SubmitEvent<HTMLFormElement>) {
event.preventDefault();
event.stopPropagation();
const formData = new FormData(event.currentTarget);
const value = formData.get('label') as string;
onSubmit(value);
}
function onCancelClick(event: MouseEvent<HTMLButtonElement>) {
event.preventDefault();
event.stopPropagation();
onCancel();
}
function onShortcut(event: MouseEvent<HTMLButtonElement>) {
event.preventDefault();
event.stopPropagation();
if (!formRef.current) return;
const form = formRef.current;
const input = form.querySelector('input[type="text"]') as HTMLInputElement;
if (!input) return;
const value = event.currentTarget.textContent.trim();
input.setRangeText(
value,
input.selectionStart ?? 0,
input.selectionEnd ?? input.value.length,
'end',
);
input.focus();
}
function handleDialogLightDismiss(event: MouseEvent<HTMLDialogElement>) {
if (event.target !== floating.refs.floating.current) return;
// The dialog has no padding, if it is the target of the click,
// it means we click on the backdrop.
onCancel();
}
function onDialogRef(node: HTMLDialogElement | null) {
node?.showModal();
floating.refs.setFloating(node);
}
return (
<>
{/* dom node for floating ui to hook on */}
<span
ref={floating.refs.setReference}
style={{
position: 'absolute',
top: atomCoords.y,
left: atomCoords.x,
}}
/>
{/* The floating dialog (open at mount with `.showModal()`) */}
<AtomLabelEditDialogStyled
ref={onDialogRef}
// eslint-disable-next-line react-hooks/refs
style={floating.floatingStyles}
closedby="any" // supports dismiss with `Escape` key
onClose={onCancel}
onClick={handleDialogLightDismiss}
>
<AtomLabelEditFormStyled
ref={formRef}
onSubmit={onFormSubmit}
method="dialog"
>
<AtomLabelEditInputStyled
type="text"
name="label"
defaultValue={defaultValue}
size={5}
autoFocus
ref={autoSelectText}
/>
<AtomLabelEditButtonStyled
area="submit"
type="submit"
aria-label="Submit"
>
✔️
</AtomLabelEditButtonStyled>
<AtomLabelEditButtonStyled
area="cancel"
type="button"
aria-label="Cancel"
onClick={onCancelClick}
>
❌
</AtomLabelEditButtonStyled>
{Object.entries(greekLetters).map(([charName, greekChar]) => (
<AtomLabelEditButtonStyled
key={charName}
area={charName}
type="button"
onClick={onShortcut}
>
{greekChar}
</AtomLabelEditButtonStyled>
))}
{Object.entries(primes).map(([primeName, primeChar]) => (
<AtomLabelEditButtonStyled
key={primeName}
area={primeName}
type="button"
onClick={onShortcut}
>
{primeChar}
</AtomLabelEditButtonStyled>
))}
</AtomLabelEditFormStyled>
</AtomLabelEditDialogStyled>
</>
);
}
function autoSelectText(node: HTMLInputElement | null) {
node?.select();
}