@folijs/view 0.5.0 → 0.6.0
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/CHANGELOG.md +33 -0
- package/dist/clipboard.d.ts +3 -1
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js.map +1 -1
- package/dist/layout/flow.d.ts +193 -20
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +789 -101
- package/dist/layout/flow.js.map +1 -1
- package/dist/print.d.ts +22 -0
- package/dist/print.d.ts.map +1 -0
- package/dist/print.js +31 -0
- package/dist/print.js.map +1 -0
- package/dist/view.d.ts +220 -7
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +1753 -208
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +30 -1
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +193 -40
- package/dist/word/blocks.js.map +1 -1
- package/dist/word/columns.d.ts +23 -0
- package/dist/word/columns.d.ts.map +1 -0
- package/dist/word/columns.js +51 -0
- package/dist/word/columns.js.map +1 -0
- package/dist/word/controls.d.ts +63 -0
- package/dist/word/controls.d.ts.map +1 -0
- package/dist/word/controls.js +411 -0
- package/dist/word/controls.js.map +1 -0
- package/dist/word/edit.d.ts +17 -5
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +157 -26
- package/dist/word/edit.js.map +1 -1
- package/dist/word/matches.d.ts +23 -0
- package/dist/word/matches.d.ts.map +1 -0
- package/dist/word/matches.js +97 -0
- package/dist/word/matches.js.map +1 -0
- package/dist/word/notes.d.ts +105 -0
- package/dist/word/notes.d.ts.map +1 -0
- package/dist/word/notes.js +392 -0
- package/dist/word/notes.js.map +1 -0
- package/dist/word/pages.d.ts +18 -13
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +420 -229
- package/dist/word/pages.js.map +1 -1
- package/dist/word/regions.d.ts +113 -0
- package/dist/word/regions.d.ts.map +1 -0
- package/dist/word/regions.js +374 -0
- package/dist/word/regions.js.map +1 -0
- package/package.json +2 -2
- package/src/clipboard.ts +3 -0
- package/src/layout/flow.ts +945 -110
- package/src/print.ts +48 -0
- package/src/view.ts +1957 -229
- package/src/word/blocks.ts +219 -36
- package/src/word/columns.ts +66 -0
- package/src/word/controls.ts +447 -0
- package/src/word/edit.ts +175 -23
- package/src/word/matches.ts +99 -0
- package/src/word/notes.ts +451 -0
- package/src/word/pages.ts +501 -220
- package/src/word/regions.ts +469 -0
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the editable view draws for content controls (ADR-0007, 11.9): the bounding box and title
|
|
3
|
+
* of the control the caret is in or the pointer is over, its list or date button, and the pop-ups
|
|
4
|
+
* those open. Everything here is the view's own: it stands beside the blocks, marked
|
|
5
|
+
* `contenteditable="false"`, so it is neither document text nor a place positions map to.
|
|
6
|
+
*/
|
|
7
|
+
import {
|
|
8
|
+
addRepeatingItem,
|
|
9
|
+
type ControlJSON,
|
|
10
|
+
type ControlRange,
|
|
11
|
+
controlAt,
|
|
12
|
+
controlIsReadOnly,
|
|
13
|
+
controlProperties,
|
|
14
|
+
controlsIn,
|
|
15
|
+
type Editor,
|
|
16
|
+
type EditorState,
|
|
17
|
+
setControlValue,
|
|
18
|
+
TextSelection,
|
|
19
|
+
wordContext,
|
|
20
|
+
} from "@folijs/core";
|
|
21
|
+
|
|
22
|
+
export const CONTROL_UI_ATTR = "data-foli-control-ui";
|
|
23
|
+
|
|
24
|
+
export const CONTROL_STYLES =
|
|
25
|
+
`[${CONTROL_UI_ATTR}]{font:11px/16px system-ui,sans-serif;color:#1f3a5f;white-space:nowrap;user-select:none;-webkit-user-select:none}` +
|
|
26
|
+
`[data-foli-control-frame]{position:absolute;box-sizing:border-box;border:1px solid #8aa4c8;pointer-events:none}` +
|
|
27
|
+
`[data-foli-control-frame=active]{border-color:#2b579a}` +
|
|
28
|
+
`[data-foli-control-tab]{position:absolute;box-sizing:border-box;height:17px;padding:0 5px;background:#dce6f4;border:1px solid #2b579a;pointer-events:auto;cursor:default}` +
|
|
29
|
+
`[data-foli-control-popup]{position:fixed;z-index:1000;box-sizing:border-box;min-width:140px;max-height:240px;overflow:auto;padding:4px;background:Canvas;color:CanvasText;border:1px solid GrayText;box-shadow:0 2px 8px rgba(0,0,0,.25);font:13px/20px system-ui,sans-serif}` +
|
|
30
|
+
`[data-foli-control-popup] [role=option]{padding:2px 8px;cursor:default}` +
|
|
31
|
+
`[data-foli-control-popup] [role=option][data-active]{background:#0b5cad;color:#fff}` +
|
|
32
|
+
`[data-foli-control-popup] form{display:flex;gap:6px;align-items:center;margin:0}` +
|
|
33
|
+
`[data-foli-control-status]{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%)}` +
|
|
34
|
+
`@media print{[${CONTROL_UI_ATTR}]{display:none}}`;
|
|
35
|
+
|
|
36
|
+
/** A rectangle of the screen. */
|
|
37
|
+
export type Box = Pick<DOMRect, "left" | "top" | "right" | "bottom" | "width" | "height">;
|
|
38
|
+
export const boxOf = (left: number, top: number, width: number, height: number): Box => ({
|
|
39
|
+
left,
|
|
40
|
+
top,
|
|
41
|
+
width,
|
|
42
|
+
height,
|
|
43
|
+
right: left + width,
|
|
44
|
+
bottom: top + height,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
/** What the chrome needs of its view. */
|
|
48
|
+
export interface ControlChromeHost {
|
|
49
|
+
readonly host: HTMLElement;
|
|
50
|
+
readonly editor: Editor;
|
|
51
|
+
state(): EditorState;
|
|
52
|
+
editable(): boolean;
|
|
53
|
+
pages(): readonly HTMLElement[];
|
|
54
|
+
scale(): number;
|
|
55
|
+
/** The screen rectangles of a control: its lines, or its blocks. */
|
|
56
|
+
rects(range: ControlRange): Box[];
|
|
57
|
+
/** The screen rectangles of what a control's content shows, line by line. */
|
|
58
|
+
contentRects(range: ControlRange): Box[];
|
|
59
|
+
posFromDOM(node: Node, offset: number): number | null;
|
|
60
|
+
/** Gives the focus back to the document, its selection shown. */
|
|
61
|
+
focus(): void;
|
|
62
|
+
injectStyles(): void;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const NAMES: Partial<Record<ControlJSON["kind"], string>> = {
|
|
66
|
+
text: "Plain text",
|
|
67
|
+
richText: "Rich text",
|
|
68
|
+
dropDown: "Drop-down list",
|
|
69
|
+
comboBox: "Combo box",
|
|
70
|
+
date: "Date picker",
|
|
71
|
+
checkbox: "Check box",
|
|
72
|
+
picture: "Picture",
|
|
73
|
+
repeating: "Repeating section",
|
|
74
|
+
repeatingItem: "Repeating section item",
|
|
75
|
+
};
|
|
76
|
+
const HAS_POPUP = new Set<ControlJSON["kind"]>(["dropDown", "comboBox", "date"]);
|
|
77
|
+
|
|
78
|
+
/** Rectangles of one line made one. */
|
|
79
|
+
function lines(rects: readonly Box[]): Box[] {
|
|
80
|
+
const out: Box[] = [];
|
|
81
|
+
for (const rect of [...rects].sort((a, b) => a.top - b.top || a.left - b.left)) {
|
|
82
|
+
const last = out[out.length - 1];
|
|
83
|
+
if (last && rect.top < last.bottom - last.height / 2) {
|
|
84
|
+
const left = Math.min(last.left, rect.left);
|
|
85
|
+
const top = Math.min(last.top, rect.top);
|
|
86
|
+
out[out.length - 1] = boxOf(
|
|
87
|
+
left,
|
|
88
|
+
top,
|
|
89
|
+
Math.max(last.right, rect.right) - left,
|
|
90
|
+
Math.max(last.bottom, rect.bottom) - top,
|
|
91
|
+
);
|
|
92
|
+
} else out.push(rect);
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export class ControlChrome {
|
|
98
|
+
private drawn: HTMLElement[] = [];
|
|
99
|
+
private hovered: ControlRange | null = null;
|
|
100
|
+
private popup: HTMLElement | null = null;
|
|
101
|
+
private status: HTMLElement | null = null;
|
|
102
|
+
private said = "";
|
|
103
|
+
private stopWatching: (() => void) | null = null;
|
|
104
|
+
|
|
105
|
+
constructor(private readonly view: ControlChromeHost) {}
|
|
106
|
+
|
|
107
|
+
private get doc(): Document {
|
|
108
|
+
return this.view.host.ownerDocument;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Where pop-ups go: out of the editing host. */
|
|
112
|
+
private get root(): ParentNode & Node {
|
|
113
|
+
const root = this.view.host.getRootNode();
|
|
114
|
+
return root instanceof ShadowRoot ? root : (this.doc.body ?? this.doc.documentElement);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The control the selection is in, if it is all in one. */
|
|
118
|
+
active(): ControlRange | null {
|
|
119
|
+
const { selection, doc } = this.view.state();
|
|
120
|
+
if (!(selection instanceof TextSelection)) return null;
|
|
121
|
+
const range = controlAt(doc, selection.from);
|
|
122
|
+
return range && selection.to < range.to ? range : null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private properties(range: ControlRange): ControlJSON | undefined {
|
|
126
|
+
return controlProperties(this.view.state(), range.id);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Draws the frames as the state and the pages now are (`show` false: none). */
|
|
130
|
+
draw(show = true): void {
|
|
131
|
+
for (const element of this.drawn) element.remove();
|
|
132
|
+
this.drawn = [];
|
|
133
|
+
const state = this.view.state();
|
|
134
|
+
if (!wordContext(state)?.envelope.controls) return;
|
|
135
|
+
const active = show ? this.active() : null;
|
|
136
|
+
this.announce(active);
|
|
137
|
+
if (this.hovered && !controlsIn(state.doc).some((r) => same(r, this.hovered))) {
|
|
138
|
+
this.hovered = null;
|
|
139
|
+
}
|
|
140
|
+
const wanted: { range: ControlRange; role: "active" | "hover" }[] = [];
|
|
141
|
+
if (active) wanted.push({ range: active, role: "active" });
|
|
142
|
+
if (show && this.hovered && !same(this.hovered, active))
|
|
143
|
+
wanted.push({ range: this.hovered, role: "hover" });
|
|
144
|
+
if (!wanted.length) return;
|
|
145
|
+
this.view.injectStyles();
|
|
146
|
+
for (const { range, role } of wanted) this.frame(range, role);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private frame(range: ControlRange, role: "active" | "hover"): void {
|
|
150
|
+
const control = this.properties(range);
|
|
151
|
+
if (!control) return;
|
|
152
|
+
const { view } = this;
|
|
153
|
+
const scale = view.scale();
|
|
154
|
+
const rects = lines(view.rects(range));
|
|
155
|
+
const boxes = view.pages().map((page) => ({ page, box: page.getBoundingClientRect() }));
|
|
156
|
+
const place = (element: HTMLElement, rect: Box) => {
|
|
157
|
+
const middle = rect.top + rect.height / 2;
|
|
158
|
+
const found = boxes.find(({ box }) => middle >= box.top && middle <= box.bottom) ?? boxes[0];
|
|
159
|
+
if (!found) return false;
|
|
160
|
+
element.style.left = `${(rect.left - found.box.left) / scale - 1}px`;
|
|
161
|
+
element.style.top = `${(rect.top - found.box.top) / scale - 1}px`;
|
|
162
|
+
element.style.width = `${rect.width / scale + 2}px`;
|
|
163
|
+
element.style.height = `${rect.height / scale + 2}px`;
|
|
164
|
+
found.page.append(element);
|
|
165
|
+
this.drawn.push(element);
|
|
166
|
+
return true;
|
|
167
|
+
};
|
|
168
|
+
const tab = (label: string, name: string, onPress: () => void) => {
|
|
169
|
+
const element = this.element("span");
|
|
170
|
+
element.dataset.foliControlTab = name;
|
|
171
|
+
element.textContent = label;
|
|
172
|
+
element.addEventListener("mousedown", (event) => {
|
|
173
|
+
event.preventDefault();
|
|
174
|
+
event.stopPropagation();
|
|
175
|
+
onPress();
|
|
176
|
+
});
|
|
177
|
+
return element;
|
|
178
|
+
};
|
|
179
|
+
rects.forEach((rect, i) => {
|
|
180
|
+
const frame = this.element("div");
|
|
181
|
+
frame.dataset.foliControlFrame = role;
|
|
182
|
+
if (!place(frame, rect)) return;
|
|
183
|
+
if (i === 0 && control.alias) {
|
|
184
|
+
const title = tab(control.alias, "title", () => this.selectWhole(range));
|
|
185
|
+
title.style.left = "-1px";
|
|
186
|
+
title.style.bottom = "100%";
|
|
187
|
+
frame.append(title);
|
|
188
|
+
}
|
|
189
|
+
if (i !== rects.length - 1 || role !== "active" || !view.editable()) return;
|
|
190
|
+
if (HAS_POPUP.has(control.kind) && !controlIsReadOnly(control)) {
|
|
191
|
+
const open = tab("▾", "open", () => this.open());
|
|
192
|
+
open.style.left = "100%";
|
|
193
|
+
open.style.top = "-1px";
|
|
194
|
+
frame.append(open);
|
|
195
|
+
} else if (
|
|
196
|
+
(control.kind === "repeating" || control.kind === "repeatingItem") &&
|
|
197
|
+
view.editor.can(addRepeatingItem())
|
|
198
|
+
) {
|
|
199
|
+
const add = tab("+", "add", () => {
|
|
200
|
+
view.editor.run(addRepeatingItem());
|
|
201
|
+
view.focus();
|
|
202
|
+
});
|
|
203
|
+
add.style.right = "-1px";
|
|
204
|
+
add.style.top = "100%";
|
|
205
|
+
frame.append(add);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private element<K extends keyof HTMLElementTagNameMap>(tag: K): HTMLElementTagNameMap[K] {
|
|
211
|
+
const element = this.doc.createElement(tag);
|
|
212
|
+
element.setAttribute(CONTROL_UI_ATTR, "");
|
|
213
|
+
element.setAttribute("contenteditable", "false");
|
|
214
|
+
element.setAttribute("aria-hidden", "true");
|
|
215
|
+
return element;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private selectWhole(range: ControlRange): void {
|
|
219
|
+
const { state } = this.view.editor;
|
|
220
|
+
const [from, to] =
|
|
221
|
+
range.level === "inline" ? [range.from, range.to] : [range.contentFrom, range.contentTo];
|
|
222
|
+
this.view.editor.dispatch(state.tr.setSelection(TextSelection.create(state.doc, from, to)));
|
|
223
|
+
this.view.focus();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** The pointer over `target`: the control there gets its frame too. */
|
|
227
|
+
hover(target: EventTarget | null): void {
|
|
228
|
+
const { host } = this.view;
|
|
229
|
+
if (!wordContext(this.view.state())?.envelope.controls) return;
|
|
230
|
+
const pos =
|
|
231
|
+
target instanceof Node && host.contains(target) && !inChrome(target)
|
|
232
|
+
? this.view.posFromDOM(target, 0)
|
|
233
|
+
: null;
|
|
234
|
+
const range = pos === null ? null : controlAt(this.view.state().doc, pos);
|
|
235
|
+
if (same(range, this.hovered)) return;
|
|
236
|
+
this.hovered = range;
|
|
237
|
+
this.draw();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** A click on a check box's glyph changes it. Whether it was one. */
|
|
241
|
+
click(event: MouseEvent): boolean {
|
|
242
|
+
const { target, clientX: x, clientY: y } = event;
|
|
243
|
+
if (!(target instanceof Node) || !this.view.editable()) return false;
|
|
244
|
+
const pos = this.view.posFromDOM(target, 0);
|
|
245
|
+
const range = pos === null ? null : controlAt(this.view.state().doc, pos);
|
|
246
|
+
const control = range && this.properties(range);
|
|
247
|
+
if (!range || control?.kind !== "checkbox") return false;
|
|
248
|
+
// On its line but beside it is a click on the line.
|
|
249
|
+
const on = (box: Box) => x >= box.left && x <= box.right && y >= box.top && y <= box.bottom;
|
|
250
|
+
if (!this.view.contentRects(range).some(on)) return false;
|
|
251
|
+
this.view.editor.run(setControlValue(range.id, !control.checked));
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private announce(range: ControlRange | null): void {
|
|
256
|
+
const control = range && this.properties(range);
|
|
257
|
+
let text = "";
|
|
258
|
+
if (control) {
|
|
259
|
+
const name = NAMES[control.kind] ?? "Content control";
|
|
260
|
+
text = control.alias ? `${control.alias}, ${name}` : name;
|
|
261
|
+
if (controlIsReadOnly(control)) text += ", read only";
|
|
262
|
+
else if (HAS_POPUP.has(control.kind)) text += ", press Alt+Down to open";
|
|
263
|
+
else if (control.kind === "checkbox")
|
|
264
|
+
text += `, ${control.checked ? "checked" : "not checked"}, press Space to change`;
|
|
265
|
+
}
|
|
266
|
+
if (text === this.said) return;
|
|
267
|
+
this.said = text;
|
|
268
|
+
if (!this.status) {
|
|
269
|
+
if (!text) return;
|
|
270
|
+
this.view.injectStyles();
|
|
271
|
+
const status = this.doc.createElement("div");
|
|
272
|
+
status.setAttribute(CONTROL_UI_ATTR, "");
|
|
273
|
+
status.setAttribute("data-foli-control-status", "");
|
|
274
|
+
status.setAttribute("role", "status");
|
|
275
|
+
this.root.append(status);
|
|
276
|
+
this.status = status;
|
|
277
|
+
}
|
|
278
|
+
this.status.textContent = text;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// ---------- pop-ups ----------
|
|
282
|
+
|
|
283
|
+
/** Opens the list or date picker of the control the selection is in. Whether there is one. */
|
|
284
|
+
open(): boolean {
|
|
285
|
+
const range = this.active();
|
|
286
|
+
const control = range && this.properties(range);
|
|
287
|
+
if (!range || !control || !HAS_POPUP.has(control.kind)) return false;
|
|
288
|
+
if (controlIsReadOnly(control) || !this.view.editable()) return false;
|
|
289
|
+
this.close(false);
|
|
290
|
+
this.view.injectStyles();
|
|
291
|
+
const popup = this.doc.createElement("div");
|
|
292
|
+
popup.setAttribute(CONTROL_UI_ATTR, "");
|
|
293
|
+
popup.setAttribute("data-foli-control-popup", "");
|
|
294
|
+
popup.setAttribute("role", "dialog");
|
|
295
|
+
popup.setAttribute("aria-label", NAMES[control.kind] ?? "Content control");
|
|
296
|
+
const rects = lines(this.view.rects(range));
|
|
297
|
+
const last = rects[rects.length - 1];
|
|
298
|
+
popup.style.left = `${last?.left ?? 0}px`;
|
|
299
|
+
popup.style.top = `${(last?.bottom ?? 0) + 2}px`;
|
|
300
|
+
this.root.append(popup);
|
|
301
|
+
this.popup = popup;
|
|
302
|
+
// Above the control when it would not fit below.
|
|
303
|
+
const room = this.doc.defaultView?.innerHeight ?? 0;
|
|
304
|
+
const { height } = popup.getBoundingClientRect();
|
|
305
|
+
if (last && room && last.bottom + 2 + height > room && last.top > height)
|
|
306
|
+
popup.style.top = `${last.top - height - 2}px`;
|
|
307
|
+
const focus =
|
|
308
|
+
control.kind === "date"
|
|
309
|
+
? this.datePicker(popup, range, control)
|
|
310
|
+
: this.list(popup, range, control);
|
|
311
|
+
popup.addEventListener("focusout", (event) => {
|
|
312
|
+
if (!popup.contains(event.relatedTarget as Node | null)) this.close(false);
|
|
313
|
+
});
|
|
314
|
+
popup.addEventListener("keydown", (event) => event.stopPropagation());
|
|
315
|
+
focus.focus({ preventScroll: true });
|
|
316
|
+
// It stays where it was put: the page moving under it closes it.
|
|
317
|
+
const scrolled = (event: Event) => {
|
|
318
|
+
if (popup.isConnected && !popup.contains(event.target as Node | null)) this.close(false);
|
|
319
|
+
};
|
|
320
|
+
this.doc.addEventListener("scroll", scrolled, { capture: true, passive: true });
|
|
321
|
+
this.stopWatching = () => this.doc.removeEventListener("scroll", scrolled, true);
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
private choose(range: ControlRange, value: string | Date): void {
|
|
326
|
+
this.close(false);
|
|
327
|
+
this.view.editor.run(setControlValue(range.id, value));
|
|
328
|
+
this.view.focus();
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
private list(popup: HTMLElement, range: ControlRange, control: ControlJSON): HTMLElement {
|
|
332
|
+
const items = control.items ?? [];
|
|
333
|
+
const list = this.doc.createElement("div");
|
|
334
|
+
list.setAttribute("role", "listbox");
|
|
335
|
+
list.setAttribute("aria-label", control.alias || "Choose an item");
|
|
336
|
+
list.tabIndex = 0;
|
|
337
|
+
const current = this.view.editor.controlValues().find((v) => v.id === range.id)?.value;
|
|
338
|
+
let at = Math.max(
|
|
339
|
+
items.findIndex((item) => item.value === current),
|
|
340
|
+
0,
|
|
341
|
+
);
|
|
342
|
+
const options = items.map((item, i) => {
|
|
343
|
+
const option = this.doc.createElement("div");
|
|
344
|
+
option.id = `foli-control-option-${i}`;
|
|
345
|
+
option.setAttribute("role", "option");
|
|
346
|
+
option.setAttribute("aria-selected", String(item.value === current));
|
|
347
|
+
option.textContent = item.display ?? item.value;
|
|
348
|
+
option.addEventListener("mousedown", (event) => event.preventDefault());
|
|
349
|
+
option.addEventListener("click", () => this.choose(range, item.value));
|
|
350
|
+
list.append(option);
|
|
351
|
+
return option;
|
|
352
|
+
});
|
|
353
|
+
const show = () => {
|
|
354
|
+
options.forEach((option, i) => {
|
|
355
|
+
option.toggleAttribute("data-active", i === at);
|
|
356
|
+
});
|
|
357
|
+
const option = options[at];
|
|
358
|
+
if (option) {
|
|
359
|
+
list.setAttribute("aria-activedescendant", option.id);
|
|
360
|
+
option.scrollIntoView?.({ block: "nearest" });
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
list.addEventListener("keydown", (event) => {
|
|
364
|
+
const count = options.length;
|
|
365
|
+
const move: Record<string, number> = {
|
|
366
|
+
ArrowDown: Math.min(at + 1, count - 1),
|
|
367
|
+
ArrowUp: Math.max(at - 1, 0),
|
|
368
|
+
Home: 0,
|
|
369
|
+
End: count - 1,
|
|
370
|
+
};
|
|
371
|
+
const next = move[event.key];
|
|
372
|
+
if (next !== undefined && count) {
|
|
373
|
+
event.preventDefault();
|
|
374
|
+
at = next;
|
|
375
|
+
show();
|
|
376
|
+
} else if (event.key === "Enter" || event.key === " ") {
|
|
377
|
+
event.preventDefault();
|
|
378
|
+
const item = items[at];
|
|
379
|
+
if (item) this.choose(range, item.value);
|
|
380
|
+
else this.close(true);
|
|
381
|
+
} else if (event.key === "Escape" || event.key === "Tab") {
|
|
382
|
+
event.preventDefault();
|
|
383
|
+
this.close(true);
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
if (!items.length) {
|
|
387
|
+
const empty = this.doc.createElement("div");
|
|
388
|
+
empty.textContent = "No items";
|
|
389
|
+
list.append(empty);
|
|
390
|
+
list.removeAttribute("role");
|
|
391
|
+
list.setAttribute("role", "group");
|
|
392
|
+
}
|
|
393
|
+
popup.append(list);
|
|
394
|
+
show();
|
|
395
|
+
return list;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
private datePicker(popup: HTMLElement, range: ControlRange, control: ControlJSON): HTMLElement {
|
|
399
|
+
const form = this.doc.createElement("form");
|
|
400
|
+
const label = this.doc.createElement("label");
|
|
401
|
+
label.textContent = control.alias || "Date";
|
|
402
|
+
const input = this.doc.createElement("input");
|
|
403
|
+
input.type = "date";
|
|
404
|
+
input.value = control.date?.slice(0, 10) ?? "";
|
|
405
|
+
label.append(" ", input);
|
|
406
|
+
const ok = this.doc.createElement("button");
|
|
407
|
+
ok.type = "submit";
|
|
408
|
+
ok.textContent = "OK";
|
|
409
|
+
form.append(label, ok);
|
|
410
|
+
form.addEventListener("submit", (event) => {
|
|
411
|
+
event.preventDefault();
|
|
412
|
+
if (input.value) this.choose(range, input.value);
|
|
413
|
+
else this.close(true);
|
|
414
|
+
});
|
|
415
|
+
form.addEventListener("keydown", (event) => {
|
|
416
|
+
if (event.key !== "Escape") return;
|
|
417
|
+
event.preventDefault();
|
|
418
|
+
this.close(true);
|
|
419
|
+
});
|
|
420
|
+
popup.append(form);
|
|
421
|
+
return input;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
close(focus: boolean): void {
|
|
425
|
+
const { popup } = this;
|
|
426
|
+
if (!popup) return;
|
|
427
|
+
this.popup = null;
|
|
428
|
+
this.stopWatching?.();
|
|
429
|
+
this.stopWatching = null;
|
|
430
|
+
popup.remove();
|
|
431
|
+
if (focus) this.view.focus();
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
destroy(): void {
|
|
435
|
+
this.close(false);
|
|
436
|
+
for (const element of this.drawn) element.remove();
|
|
437
|
+
this.drawn = [];
|
|
438
|
+
this.status?.remove();
|
|
439
|
+
this.status = null;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const same = (a: ControlRange | null | undefined, b: ControlRange | null | undefined) =>
|
|
444
|
+
a?.id === b?.id && a?.from === b?.from && a?.to === b?.to;
|
|
445
|
+
|
|
446
|
+
const inChrome = (node: Node) =>
|
|
447
|
+
!!(node instanceof Element ? node : node.parentElement)?.closest(`[${CONTROL_UI_ATTR}]`);
|