@yaks/ux 0.0.0-stage → 0.2.36
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/Answer.js +43 -0
- package/Answer.js.map +1 -0
- package/Answer.ts +84 -0
- package/Conversation.js +520 -0
- package/Conversation.js.map +1 -0
- package/Conversation.ts +649 -0
- package/ConversationBox.js +106 -0
- package/ConversationBox.js.map +1 -0
- package/ConversationBox.ts +198 -0
- package/ConversationLines.js +29 -0
- package/ConversationLines.js.map +1 -0
- package/ConversationLines.ts +48 -0
- package/Disclosure.js +79 -0
- package/Disclosure.js.map +1 -0
- package/Disclosure.ts +130 -0
- package/Edit.js +330 -0
- package/Edit.js.map +1 -0
- package/Edit.ts +488 -0
- package/EntityPopup.js +35 -0
- package/EntityPopup.js.map +1 -0
- package/EntityPopup.ts +52 -0
- package/Fields.js +161 -0
- package/Fields.js.map +1 -0
- package/Fields.ts +276 -0
- package/LICENSE +202 -0
- package/Order.js +105 -0
- package/Order.js.map +1 -0
- package/Order.ts +159 -0
- package/README.md +1023 -2
- package/Save.js +50 -0
- package/Save.js.map +1 -0
- package/Save.ts +115 -0
- package/Sort.js +49 -0
- package/Sort.js.map +1 -0
- package/Sort.ts +98 -0
- package/Submit.js +115 -0
- package/Submit.js.map +1 -0
- package/Submit.ts +174 -0
- package/Text.js +114 -0
- package/Text.js.map +1 -0
- package/Text.ts +140 -0
- package/TokenPicker.js +65 -0
- package/TokenPicker.js.map +1 -0
- package/TokenPicker.ts +117 -0
- package/_dist/Answer.d.ts +39 -0
- package/_dist/Answer.d.ts.map +1 -0
- package/_dist/Conversation.d.ts +131 -0
- package/_dist/Conversation.d.ts.map +1 -0
- package/_dist/ConversationBox.d.ts +29 -0
- package/_dist/ConversationBox.d.ts.map +1 -0
- package/_dist/ConversationLines.d.ts +13 -0
- package/_dist/ConversationLines.d.ts.map +1 -0
- package/_dist/Disclosure.d.ts +36 -0
- package/_dist/Disclosure.d.ts.map +1 -0
- package/_dist/Edit.d.ts +87 -0
- package/_dist/Edit.d.ts.map +1 -0
- package/_dist/EntityPopup.d.ts +12 -0
- package/_dist/EntityPopup.d.ts.map +1 -0
- package/_dist/Fields.d.ts +29 -0
- package/_dist/Fields.d.ts.map +1 -0
- package/_dist/Order.d.ts +40 -0
- package/_dist/Order.d.ts.map +1 -0
- package/_dist/Save.d.ts +28 -0
- package/_dist/Save.d.ts.map +1 -0
- package/_dist/Sort.d.ts +28 -0
- package/_dist/Sort.d.ts.map +1 -0
- package/_dist/Submit.d.ts +48 -0
- package/_dist/Submit.d.ts.map +1 -0
- package/_dist/Text.d.ts +27 -0
- package/_dist/Text.d.ts.map +1 -0
- package/_dist/TokenPicker.d.ts +19 -0
- package/_dist/TokenPicker.d.ts.map +1 -0
- package/_dist/anchor.d.ts +52 -0
- package/_dist/anchor.d.ts.map +1 -0
- package/_dist/anchored.d.ts +30 -0
- package/_dist/anchored.d.ts.map +1 -0
- package/_dist/completion-state.d.ts +65 -0
- package/_dist/completion-state.d.ts.map +1 -0
- package/_dist/completion.d.ts +73 -0
- package/_dist/completion.d.ts.map +1 -0
- package/_dist/composer-browser.d.ts +1 -0
- package/_dist/composer-browser.d.ts.map +1 -0
- package/_dist/composer.d.ts +31 -0
- package/_dist/composer.d.ts.map +1 -0
- package/_dist/context-menu-browser.d.ts +4 -0
- package/_dist/context-menu-browser.d.ts.map +1 -0
- package/_dist/context-menu.d.ts +95 -0
- package/_dist/context-menu.d.ts.map +1 -0
- package/_dist/drag.d.ts +29 -0
- package/_dist/drag.d.ts.map +1 -0
- package/_dist/edit-keys.d.ts +42 -0
- package/_dist/edit-keys.d.ts.map +1 -0
- package/_dist/emit.d.ts +7 -0
- package/_dist/emit.d.ts.map +1 -0
- package/_dist/entity-popup-browser.d.ts +3 -0
- package/_dist/entity-popup-browser.d.ts.map +1 -0
- package/_dist/face/FaceTagger.d.ts +25 -0
- package/_dist/face/FaceTagger.d.ts.map +1 -0
- package/_dist/face/review.d.ts +5 -0
- package/_dist/face/review.d.ts.map +1 -0
- package/_dist/face/tags.d.ts +99 -0
- package/_dist/face/tags.d.ts.map +1 -0
- package/_dist/field-values.d.ts +4 -0
- package/_dist/field-values.d.ts.map +1 -0
- package/_dist/fit-list.d.ts +4 -0
- package/_dist/fit-list.d.ts.map +1 -0
- package/_dist/hits.d.ts +27 -0
- package/_dist/hits.d.ts.map +1 -0
- package/_dist/host.d.ts +91 -0
- package/_dist/host.d.ts.map +1 -0
- package/_dist/image-fallback.d.ts +5 -0
- package/_dist/image-fallback.d.ts.map +1 -0
- package/_dist/keys.d.ts +95 -0
- package/_dist/keys.d.ts.map +1 -0
- package/_dist/kit.d.ts +23 -0
- package/_dist/kit.d.ts.map +1 -0
- package/_dist/lightbox-browser.d.ts +2 -0
- package/_dist/lightbox-browser.d.ts.map +1 -0
- package/_dist/lightbox.d.ts +42 -0
- package/_dist/lightbox.d.ts.map +1 -0
- package/_dist/live.d.ts +21 -0
- package/_dist/live.d.ts.map +1 -0
- package/_dist/mod.d.ts +67 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/read.d.ts +12 -0
- package/_dist/read.d.ts.map +1 -0
- package/_dist/resize.d.ts +43 -0
- package/_dist/resize.d.ts.map +1 -0
- package/_dist/rows.d.ts +31 -0
- package/_dist/rows.d.ts.map +1 -0
- package/_dist/save-browser.d.ts +29 -0
- package/_dist/save-browser.d.ts.map +1 -0
- package/_dist/select-browser.d.ts +1 -0
- package/_dist/select-browser.d.ts.map +1 -0
- package/_dist/select.d.ts +12 -0
- package/_dist/select.d.ts.map +1 -0
- package/_dist/sending.d.ts +10 -0
- package/_dist/sending.d.ts.map +1 -0
- package/_dist/sort-browser.d.ts +11 -0
- package/_dist/sort-browser.d.ts.map +1 -0
- package/_dist/state.d.ts +74 -0
- package/_dist/state.d.ts.map +1 -0
- package/_dist/suggest.d.ts +18 -0
- package/_dist/suggest.d.ts.map +1 -0
- package/_dist/swipe.d.ts +60 -0
- package/_dist/swipe.d.ts.map +1 -0
- package/_dist/ui.d.ts +6 -0
- package/_dist/ui.d.ts.map +1 -0
- package/_dist/upload-browser.d.ts +13 -0
- package/_dist/upload-browser.d.ts.map +1 -0
- package/_dist/views.d.ts +8 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/anchor.js +58 -0
- package/anchor.js.map +1 -0
- package/anchor.ts +87 -0
- package/anchored.js +180 -0
- package/anchored.js.map +1 -0
- package/anchored.ts +232 -0
- package/completion-state.js +76 -0
- package/completion-state.js.map +1 -0
- package/completion-state.ts +120 -0
- package/completion.js +301 -0
- package/completion.js.map +1 -0
- package/completion.ts +433 -0
- package/composer-browser.js +4 -0
- package/composer-browser.js.map +1 -0
- package/composer-browser.ts +5 -0
- package/composer.js +42 -0
- package/composer.js.map +1 -0
- package/composer.ts +53 -0
- package/context-menu-browser.js +221 -0
- package/context-menu-browser.js.map +1 -0
- package/context-menu-browser.ts +231 -0
- package/context-menu.js +146 -0
- package/context-menu.js.map +1 -0
- package/context-menu.ts +194 -0
- package/deno.json +1 -0
- package/drag.js +33 -0
- package/drag.js.map +1 -0
- package/drag.ts +41 -0
- package/edit-keys.js +51 -0
- package/edit-keys.js.map +1 -0
- package/edit-keys.ts +81 -0
- package/emit.js +34 -0
- package/emit.js.map +1 -0
- package/emit.ts +55 -0
- package/entity-popup-browser.js +94 -0
- package/entity-popup-browser.js.map +1 -0
- package/entity-popup-browser.ts +100 -0
- package/face/FaceTagger.js +361 -0
- package/face/FaceTagger.js.map +1 -0
- package/face/FaceTagger.ts +507 -0
- package/face/NameProposal.js +61 -0
- package/face/NameProposal.js.map +1 -0
- package/face/NameProposal.ts +100 -0
- package/face/review.js +33 -0
- package/face/review.js.map +1 -0
- package/face/review.ts +49 -0
- package/face/tags.js +344 -0
- package/face/tags.js.map +1 -0
- package/face/tags.ts +357 -0
- package/field-values.js +20 -0
- package/field-values.js.map +1 -0
- package/field-values.ts +35 -0
- package/fit-list.js +66 -0
- package/fit-list.js.map +1 -0
- package/fit-list.ts +81 -0
- package/hits.js +65 -0
- package/hits.js.map +1 -0
- package/hits.ts +75 -0
- package/host.js +88 -0
- package/host.js.map +1 -0
- package/host.ts +212 -0
- package/image-fallback.js +32 -0
- package/image-fallback.js.map +1 -0
- package/image-fallback.ts +37 -0
- package/keys.js +418 -0
- package/keys.js.map +1 -0
- package/keys.ts +444 -0
- package/kit.js +41 -0
- package/kit.js.map +1 -0
- package/kit.ts +71 -0
- package/lightbox-browser.js +248 -0
- package/lightbox-browser.js.map +1 -0
- package/lightbox-browser.ts +232 -0
- package/lightbox.js +84 -0
- package/lightbox.js.map +1 -0
- package/lightbox.ts +103 -0
- package/live.js +111 -0
- package/live.js.map +1 -0
- package/live.ts +139 -0
- package/mod.js +67 -0
- package/mod.js.map +1 -0
- package/mod.ts +180 -0
- package/package.json +143 -4
- package/read.js +33 -0
- package/read.js.map +1 -0
- package/read.ts +56 -0
- package/resize.js +159 -0
- package/resize.js.map +1 -0
- package/resize.ts +203 -0
- package/rows.js +73 -0
- package/rows.js.map +1 -0
- package/rows.ts +97 -0
- package/save-browser.js +469 -0
- package/save-browser.js.map +1 -0
- package/save-browser.ts +530 -0
- package/select-browser.js +4 -0
- package/select-browser.js.map +1 -0
- package/select-browser.ts +5 -0
- package/select.js +296 -0
- package/select.js.map +1 -0
- package/select.ts +343 -0
- package/sending.js +36 -0
- package/sending.js.map +1 -0
- package/sending.ts +44 -0
- package/sort-browser.js +51 -0
- package/sort-browser.js.map +1 -0
- package/sort-browser.ts +70 -0
- package/state.js +99 -0
- package/state.js.map +1 -0
- package/state.ts +121 -0
- package/suggest.js +28 -0
- package/suggest.js.map +1 -0
- package/suggest.ts +32 -0
- package/swipe.js +186 -0
- package/swipe.js.map +1 -0
- package/swipe.ts +250 -0
- package/ui.js +507 -0
- package/ui.js.map +1 -0
- package/ui.ts +378 -0
- package/upload-browser.js +195 -0
- package/upload-browser.js.map +1 -0
- package/upload-browser.ts +303 -0
- package/upload-files.js +41 -0
- package/upload-files.js.map +1 -0
- package/upload-files.ts +49 -0
- package/views.js +8 -0
- package/views.js.map +1 -0
- package/views.ts +9 -0
- package/vocab.js +16 -0
- package/vocab.js.map +1 -0
- package/vocab.json +287 -0
- package/vocab.ts +17 -0
package/read.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read.js","sources":["./read.ts"],"names":[],"mappings":"AAAA;;;;;;CAMC,GAED,SAAS,KAAK,wBAA6B;AAG3C;iDACiD,GACjD,OAAO,IAAI,UAAU,CAAC,OAAc,MAAc;EAChD,IAAI,OAAO,MAAM,IAAI,CAAC;EACtB,OAAO,CAAC,CAAC,MAAM,QAAQ,KAAK,QAAQ,CAAC,QAAQ,CAAC;AAChD,EAAC;AAED,2EAA2E;AAC3E,yCAAyC;AACzC,IAAI,MAAM,CAAC,OAAc,MAAc,OACpC,MAAM,GAAG,CAAC,OAAO,YAAY,CAAC,KAAK,IAAI,CAAC;AAK3C;6BAC6B,GAC7B,OAAO,IAAI,SAAS,CAAC,OAAc,MAAc,OAC/C,IAAI,OAAO,MAAM,MAAM,KAAK,IAAI,OAAM;AAExC,iEAAiE,GACjE,OAAO,IAAI,SAAS,CAAC,OAAc,MAAc,OAC/C,IAAI,OAAO,MAAM,MAAM,IAAI,IAAI,GAAE;AAEnC;;;;CAIC,GACD,OAAO,IAAI,aAAa,CACtB,OACA,MACA,MACA,OACA,OAA4C,IAAM,SAAS;EAE3D,IAAI,SAAS,QAAQ,UAAU,IAAI,OAAO;EAC1C,IAAI,IAAI,MAAM,IAAI,CAAC,MAAM;EACzB,IAAI,OAAO,GAAG,YAAY,WAAW,EAAE,MAAM,GAAG,GAAG;EACnD,IAAI,QAAQ,YAAY,OAAO,CAAC,CAAC,EAAE,OAAO,QAAQ;EAClD,IAAI,QAAQ,QAAQ,OAAO,QAAQ,SAAS;EAC5C,IAAI,QAAQ,OAAO,OAAO,KAAK,OAAO,WAAW,MAAM,OAAO;EAC9D,IAAI,OAAO,SAAS,UAAU,OAAO,KAAK,SAAS,CAAC;EACpD,OAAO,OAAO;AAChB,EAAC"}
|
package/read.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an `Edit` reads off the vocabulary: pure functions. Whether a property
|
|
3
|
+
* may be written, whether it is a body, the well its suggestions come from, a
|
|
4
|
+
* value as its face says it.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { short } from '@yaks/id'
|
|
10
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
11
|
+
|
|
12
|
+
/** Whether a client may write this property: its component is on the wire and
|
|
13
|
+
* the property is neither stamped nor computed. */
|
|
14
|
+
export let canEdit = (vocab: Vocab, comp: string, prop: string): boolean => {
|
|
15
|
+
let info = vocab.comp(comp)
|
|
16
|
+
return !!info?.wire && info.writable.includes(prop)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// A property's declaration as written, for the keywords only some packages
|
|
20
|
+
// read (@yaks/blob's `store`, a `well`).
|
|
21
|
+
let raw = (vocab: Vocab, comp: string, prop: string) =>
|
|
22
|
+
(vocab.def(comp)?.properties?.[prop] ?? {}) as {
|
|
23
|
+
store?: string
|
|
24
|
+
well?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Whether a property is a body: long text kept apart (`store: 'blob'`),
|
|
28
|
+
* typed over on many lines. */
|
|
29
|
+
export let isBody = (vocab: Vocab, comp: string, prop: string): boolean =>
|
|
30
|
+
raw(vocab, comp, prop).store == 'blob'
|
|
31
|
+
|
|
32
|
+
/** The well a property's suggestions come from, or '' for none. */
|
|
33
|
+
export let wellOf = (vocab: Vocab, comp: string, prop: string): string =>
|
|
34
|
+
raw(vocab, comp, prop).well ?? ''
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A value as its face says it, or null for nothing to show: a priority with
|
|
38
|
+
* its P, a flag as true or false, a reference as what it is called, a JSON
|
|
39
|
+
* value as its text.
|
|
40
|
+
*/
|
|
41
|
+
export let formatProp = (
|
|
42
|
+
vocab: Vocab,
|
|
43
|
+
comp: string,
|
|
44
|
+
prop: string,
|
|
45
|
+
value: unknown,
|
|
46
|
+
name: (eid: string) => string | undefined = () => undefined,
|
|
47
|
+
): string | null => {
|
|
48
|
+
if (value == null || value === '') return null
|
|
49
|
+
let p = vocab.prop(comp, prop)
|
|
50
|
+
let type = p?.category == 'scalar' ? p.scalar : p?.category
|
|
51
|
+
if (type == 'priority') return `P${Number(value)}`
|
|
52
|
+
if (type == 'bool') return value ? 'true' : 'false'
|
|
53
|
+
if (type == 'ref') return name(String(value)) ?? short(String(value))
|
|
54
|
+
if (typeof value == 'object') return JSON.stringify(value)
|
|
55
|
+
return String(value)
|
|
56
|
+
}
|
package/resize.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A pane the person resizes by its edge: a grip (`data-resize`, a focusable
|
|
3
|
+
* `separator`) stands on the edge of the pane it names (`aria-controls`), on
|
|
4
|
+
* the pane's `left` or `right` (`data-resize`'s value, the side the grip is
|
|
5
|
+
* on). Dragging the grip moves the edge with the pointer; with the grip
|
|
6
|
+
* focused, the arrow keys move it a step, a bigger one with Shift, and Home
|
|
7
|
+
* and End take it to its narrowest and widest. The pane's width is its
|
|
8
|
+
* `--shell-aside` (@yaks/ui `Shell`), held between the pane's own computed
|
|
9
|
+
* `min-width` and `max-width`, so its sheet says how narrow and wide it goes.
|
|
10
|
+
*
|
|
11
|
+
* Once a drag ends, or a moment after the last key, the width is kept: posted
|
|
12
|
+
* as the grip's `data-resize-name` to its `data-resize-action`, where the
|
|
13
|
+
* page keeps it for the person and draws it again on every page. A double
|
|
14
|
+
* press on the grip gives the pane back its sheet's width, posting none.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { held, keyed, widened } from '@yaks/ux/resize'
|
|
18
|
+
* import { equal } from '@yaks/testing'
|
|
19
|
+
*
|
|
20
|
+
* // A pane on the page's right, its grip on its left: moving left widens.
|
|
21
|
+
* equal(widened('left', 320, -40), 360)
|
|
22
|
+
* equal(widened('right', 320, -40), 280)
|
|
23
|
+
* equal(held(500, 256, 480), 480)
|
|
24
|
+
* equal(keyed('left', 'ArrowLeft', false, 320, 256, 640), 336)
|
|
25
|
+
* equal(keyed('left', 'ArrowRight', true, 320, 256, 640), 256)
|
|
26
|
+
* equal(keyed('left', 'End', false, 320, 256, 640), 640)
|
|
27
|
+
* equal(keyed('left', 'a', false, 320, 256, 640), undefined)
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @module
|
|
31
|
+
*/ /** The side of its pane a grip stands on. */ /** How far an arrow key moves the edge, and with Shift, px. */ export let STEP = 16;
|
|
32
|
+
export let LEAP = 64;
|
|
33
|
+
/** How long after the last key the width is kept, ms. */ export let SETTLE = 400;
|
|
34
|
+
/** `width` held between `min` and `max`. */ export let held = (width, min, max)=>Math.round(Math.min(Math.max(width, min), max));
|
|
35
|
+
/** The width of a pane `from` wide once its grip on `side` has moved `dx`:
|
|
36
|
+
* a grip on the left widens the pane moving left. */ export let widened = (side, from, dx)=>side == 'left' ? from - dx : from + dx;
|
|
37
|
+
/** The width a key on a grip on `side` takes a pane `width` wide to, held
|
|
38
|
+
* between `min` and `max`, or nothing for a key that is not the grip's. */ export let keyed = (side, key, shift, width, min, max)=>{
|
|
39
|
+
if (key == 'Home') return min;
|
|
40
|
+
if (key == 'End') return max;
|
|
41
|
+
let way = key == 'ArrowLeft' ? -1 : key == 'ArrowRight' ? 1 : 0;
|
|
42
|
+
if (!way) return undefined;
|
|
43
|
+
return held(widened(side, width, way * (shift ? LEAP : STEP)), min, max);
|
|
44
|
+
};
|
|
45
|
+
let px = (s, or)=>{
|
|
46
|
+
let n = parseFloat(s);
|
|
47
|
+
return Number.isFinite(n) ? n : or;
|
|
48
|
+
};
|
|
49
|
+
/** Resize `doc`'s panes by their grips, keeping a width `settle` ms after
|
|
50
|
+
* the last key. */ export let resize = (doc, win, settle = SETTLE)=>{
|
|
51
|
+
let timer;
|
|
52
|
+
let grip = (e)=>e.target?.closest?.('[data-resize]');
|
|
53
|
+
let paneOf = (g)=>doc.getElementById(g.getAttribute('aria-controls') ?? '');
|
|
54
|
+
let bounds = (pane)=>{
|
|
55
|
+
let style = win.getComputedStyle(pane);
|
|
56
|
+
let width = pane.getBoundingClientRect().width;
|
|
57
|
+
return {
|
|
58
|
+
width,
|
|
59
|
+
min: px(style.minWidth, 0),
|
|
60
|
+
max: px(style.maxWidth, Infinity)
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
// The grip says how wide its pane is, and how narrow and wide it goes.
|
|
64
|
+
let tell = (g, pane)=>{
|
|
65
|
+
let { width, min, max } = bounds(pane);
|
|
66
|
+
g.setAttribute('aria-valuenow', String(Math.round(width)));
|
|
67
|
+
g.setAttribute('aria-valuemin', String(Math.round(min)));
|
|
68
|
+
if (Number.isFinite(max)) {
|
|
69
|
+
g.setAttribute('aria-valuemax', String(Math.round(max)));
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
let put = (g, pane, width)=>{
|
|
73
|
+
if (width) pane.style.setProperty('--shell-aside', `${width}px`);
|
|
74
|
+
else pane.style.removeProperty('--shell-aside');
|
|
75
|
+
tell(g, pane);
|
|
76
|
+
};
|
|
77
|
+
let keep = (g, width)=>{
|
|
78
|
+
let action = g.dataset.resizeAction;
|
|
79
|
+
if (!action) return;
|
|
80
|
+
let body = new FormData();
|
|
81
|
+
body.set(g.dataset.resizeName ?? 'width', width ? String(width) : '');
|
|
82
|
+
win.fetch(action, {
|
|
83
|
+
method: 'POST',
|
|
84
|
+
body
|
|
85
|
+
}).catch(()=>{});
|
|
86
|
+
};
|
|
87
|
+
let drag;
|
|
88
|
+
let down = (e)=>{
|
|
89
|
+
let g = grip(e);
|
|
90
|
+
let pane = g && paneOf(g);
|
|
91
|
+
if (!g || !pane || e.button != 0) return;
|
|
92
|
+
e.preventDefault();
|
|
93
|
+
g.setPointerCapture?.(e.pointerId);
|
|
94
|
+
drag = {
|
|
95
|
+
g,
|
|
96
|
+
pane,
|
|
97
|
+
x: e.clientX,
|
|
98
|
+
from: bounds(pane).width
|
|
99
|
+
};
|
|
100
|
+
doc.documentElement.dataset.resizing = '';
|
|
101
|
+
};
|
|
102
|
+
let move = (e)=>{
|
|
103
|
+
if (!drag) return;
|
|
104
|
+
let { min, max } = bounds(drag.pane);
|
|
105
|
+
let side = drag.g.dataset.resize;
|
|
106
|
+
put(drag.g, drag.pane, held(widened(side, drag.from, e.clientX - drag.x), min, max));
|
|
107
|
+
};
|
|
108
|
+
let up = ()=>{
|
|
109
|
+
let d = drag;
|
|
110
|
+
drag = undefined;
|
|
111
|
+
delete doc.documentElement.dataset.resizing;
|
|
112
|
+
// A press that moved nothing keeps nothing, so a double press resets.
|
|
113
|
+
let width = d && Math.round(bounds(d.pane).width);
|
|
114
|
+
if (d && width != Math.round(d.from)) keep(d.g, width);
|
|
115
|
+
};
|
|
116
|
+
let key = (e)=>{
|
|
117
|
+
let g = grip(e);
|
|
118
|
+
let pane = g && paneOf(g);
|
|
119
|
+
if (!g || !pane) return;
|
|
120
|
+
let { width, min, max } = bounds(pane);
|
|
121
|
+
let to = keyed(g.dataset.resize, e.key, e.shiftKey, width, min, max);
|
|
122
|
+
if (to == undefined) return;
|
|
123
|
+
e.preventDefault();
|
|
124
|
+
put(g, pane, to);
|
|
125
|
+
clearTimeout(timer);
|
|
126
|
+
timer = setTimeout(()=>keep(g, to), settle);
|
|
127
|
+
};
|
|
128
|
+
let focus = (e)=>{
|
|
129
|
+
let g = grip(e);
|
|
130
|
+
let pane = g && paneOf(g);
|
|
131
|
+
if (g && pane) tell(g, pane);
|
|
132
|
+
};
|
|
133
|
+
let reset = (e)=>{
|
|
134
|
+
let g = grip(e);
|
|
135
|
+
let pane = g && paneOf(g);
|
|
136
|
+
if (!g || !pane) return;
|
|
137
|
+
put(g, pane);
|
|
138
|
+
keep(g);
|
|
139
|
+
};
|
|
140
|
+
doc.addEventListener('focusin', focus);
|
|
141
|
+
doc.addEventListener('dblclick', reset);
|
|
142
|
+
doc.addEventListener('pointerdown', down);
|
|
143
|
+
doc.addEventListener('pointermove', move);
|
|
144
|
+
doc.addEventListener('pointerup', up);
|
|
145
|
+
doc.addEventListener('pointercancel', up);
|
|
146
|
+
doc.addEventListener('keydown', key);
|
|
147
|
+
return ()=>{
|
|
148
|
+
clearTimeout(timer);
|
|
149
|
+
doc.removeEventListener('focusin', focus);
|
|
150
|
+
doc.removeEventListener('dblclick', reset);
|
|
151
|
+
doc.removeEventListener('pointerdown', down);
|
|
152
|
+
doc.removeEventListener('pointermove', move);
|
|
153
|
+
doc.removeEventListener('pointerup', up);
|
|
154
|
+
doc.removeEventListener('pointercancel', up);
|
|
155
|
+
doc.removeEventListener('keydown', key);
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
if (typeof document != 'undefined') resize(document, globalThis);
|
|
159
|
+
//# sourceMappingURL=resize.js.map
|
package/resize.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resize.js","sources":["./resize.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8BC,GAED,2CAA2C,GAG3C,6DAA6D,GAC7D,OAAO,IAAI,OAAO,GAAE;AACpB,OAAO,IAAI,OAAO,GAAE;AACpB,uDAAuD,GACvD,OAAO,IAAI,SAAS,IAAG;AAEvB,0CAA0C,GAC1C,OAAO,IAAI,OAAO,CAAC,OAAe,KAAa,MAC7C,KAAK,KAAK,CAAC,KAAK,GAAG,CAAC,KAAK,GAAG,CAAC,OAAO,MAAM,MAAK;AAEjD;mDACmD,GACnD,OAAO,IAAI,UAAU,CAAC,MAAY,MAAc,KAC9C,QAAQ,SAAS,OAAO,KAAK,OAAO,GAAE;AAExC;yEACyE,GACzE,OAAO,IAAI,QAAQ,CACjB,MACA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AAO,UAAU"}
|
package/resize.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A pane the person resizes by its edge: a grip (`data-resize`, a focusable
|
|
3
|
+
* `separator`) stands on the edge of the pane it names (`aria-controls`), on
|
|
4
|
+
* the pane's `left` or `right` (`data-resize`'s value, the side the grip is
|
|
5
|
+
* on). Dragging the grip moves the edge with the pointer; with the grip
|
|
6
|
+
* focused, the arrow keys move it a step, a bigger one with Shift, and Home
|
|
7
|
+
* and End take it to its narrowest and widest. The pane's width is its
|
|
8
|
+
* `--shell-aside` (@yaks/ui `Shell`), held between the pane's own computed
|
|
9
|
+
* `min-width` and `max-width`, so its sheet says how narrow and wide it goes.
|
|
10
|
+
*
|
|
11
|
+
* Once a drag ends, or a moment after the last key, the width is kept: posted
|
|
12
|
+
* as the grip's `data-resize-name` to its `data-resize-action`, where the
|
|
13
|
+
* page keeps it for the person and draws it again on every page. A double
|
|
14
|
+
* press on the grip gives the pane back its sheet's width, posting none.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { held, keyed, widened } from '@yaks/ux/resize'
|
|
18
|
+
* import { equal } from '@yaks/testing'
|
|
19
|
+
*
|
|
20
|
+
* // A pane on the page's right, its grip on its left: moving left widens.
|
|
21
|
+
* equal(widened('left', 320, -40), 360)
|
|
22
|
+
* equal(widened('right', 320, -40), 280)
|
|
23
|
+
* equal(held(500, 256, 480), 480)
|
|
24
|
+
* equal(keyed('left', 'ArrowLeft', false, 320, 256, 640), 336)
|
|
25
|
+
* equal(keyed('left', 'ArrowRight', true, 320, 256, 640), 256)
|
|
26
|
+
* equal(keyed('left', 'End', false, 320, 256, 640), 640)
|
|
27
|
+
* equal(keyed('left', 'a', false, 320, 256, 640), undefined)
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @module
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/** The side of its pane a grip stands on. */
|
|
34
|
+
export type Side = 'left' | 'right'
|
|
35
|
+
|
|
36
|
+
/** How far an arrow key moves the edge, and with Shift, px. */
|
|
37
|
+
export let STEP = 16
|
|
38
|
+
export let LEAP = 64
|
|
39
|
+
/** How long after the last key the width is kept, ms. */
|
|
40
|
+
export let SETTLE = 400
|
|
41
|
+
|
|
42
|
+
/** `width` held between `min` and `max`. */
|
|
43
|
+
export let held = (width: number, min: number, max: number): number =>
|
|
44
|
+
Math.round(Math.min(Math.max(width, min), max))
|
|
45
|
+
|
|
46
|
+
/** The width of a pane `from` wide once its grip on `side` has moved `dx`:
|
|
47
|
+
* a grip on the left widens the pane moving left. */
|
|
48
|
+
export let widened = (side: Side, from: number, dx: number): number =>
|
|
49
|
+
side == 'left' ? from - dx : from + dx
|
|
50
|
+
|
|
51
|
+
/** The width a key on a grip on `side` takes a pane `width` wide to, held
|
|
52
|
+
* between `min` and `max`, or nothing for a key that is not the grip's. */
|
|
53
|
+
export let keyed = (
|
|
54
|
+
side: Side,
|
|
55
|
+
key: string,
|
|
56
|
+
shift: boolean,
|
|
57
|
+
width: number,
|
|
58
|
+
min: number,
|
|
59
|
+
max: number,
|
|
60
|
+
): number | undefined => {
|
|
61
|
+
if (key == 'Home') return min
|
|
62
|
+
if (key == 'End') return max
|
|
63
|
+
let way = key == 'ArrowLeft' ? -1 : key == 'ArrowRight' ? 1 : 0
|
|
64
|
+
if (!way) return undefined
|
|
65
|
+
return held(widened(side, width, way * (shift ? LEAP : STEP)), min, max)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** What the grips read of the window, and post the width with. */
|
|
69
|
+
export type Window = Pick<typeof globalThis, 'getComputedStyle' | 'fetch'>
|
|
70
|
+
|
|
71
|
+
let px = (s: string, or: number) => {
|
|
72
|
+
let n = parseFloat(s)
|
|
73
|
+
return Number.isFinite(n) ? n : or
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Resize `doc`'s panes by their grips, keeping a width `settle` ms after
|
|
77
|
+
* the last key. */
|
|
78
|
+
export let resize = (
|
|
79
|
+
doc: Document,
|
|
80
|
+
win: Window,
|
|
81
|
+
settle = SETTLE,
|
|
82
|
+
): () => void => {
|
|
83
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
84
|
+
let grip = (e: Event) =>
|
|
85
|
+
(e.target as Element | null)?.closest?.('[data-resize]') as
|
|
86
|
+
| HTMLElement
|
|
87
|
+
| null
|
|
88
|
+
let paneOf = (g: HTMLElement) =>
|
|
89
|
+
doc.getElementById(g.getAttribute('aria-controls') ?? '')
|
|
90
|
+
let bounds = (pane: HTMLElement) => {
|
|
91
|
+
let style = win.getComputedStyle(pane)
|
|
92
|
+
let width = pane.getBoundingClientRect().width
|
|
93
|
+
return {
|
|
94
|
+
width,
|
|
95
|
+
min: px(style.minWidth, 0),
|
|
96
|
+
max: px(style.maxWidth, Infinity),
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// The grip says how wide its pane is, and how narrow and wide it goes.
|
|
100
|
+
let tell = (g: HTMLElement, pane: HTMLElement) => {
|
|
101
|
+
let { width, min, max } = bounds(pane)
|
|
102
|
+
g.setAttribute('aria-valuenow', String(Math.round(width)))
|
|
103
|
+
g.setAttribute('aria-valuemin', String(Math.round(min)))
|
|
104
|
+
if (Number.isFinite(max)) {
|
|
105
|
+
g.setAttribute('aria-valuemax', String(Math.round(max)))
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
let put = (g: HTMLElement, pane: HTMLElement, width?: number) => {
|
|
109
|
+
if (width) pane.style.setProperty('--shell-aside', `${width}px`)
|
|
110
|
+
else pane.style.removeProperty('--shell-aside')
|
|
111
|
+
tell(g, pane)
|
|
112
|
+
}
|
|
113
|
+
let keep = (g: HTMLElement, width?: number) => {
|
|
114
|
+
let action = g.dataset.resizeAction
|
|
115
|
+
if (!action) return
|
|
116
|
+
let body = new FormData()
|
|
117
|
+
body.set(g.dataset.resizeName ?? 'width', width ? String(width) : '')
|
|
118
|
+
win.fetch(action, { method: 'POST', body }).catch(() => {})
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let drag:
|
|
122
|
+
| { g: HTMLElement; pane: HTMLElement; x: number; from: number }
|
|
123
|
+
| undefined
|
|
124
|
+
let down = (e: PointerEvent) => {
|
|
125
|
+
let g = grip(e)
|
|
126
|
+
let pane = g && paneOf(g)
|
|
127
|
+
if (!g || !pane || e.button != 0) return
|
|
128
|
+
e.preventDefault()
|
|
129
|
+
g.setPointerCapture?.(e.pointerId)
|
|
130
|
+
drag = { g, pane, x: e.clientX, from: bounds(pane).width }
|
|
131
|
+
doc.documentElement.dataset.resizing = ''
|
|
132
|
+
}
|
|
133
|
+
let move = (e: PointerEvent) => {
|
|
134
|
+
if (!drag) return
|
|
135
|
+
let { min, max } = bounds(drag.pane)
|
|
136
|
+
let side = drag.g.dataset.resize as Side
|
|
137
|
+
put(
|
|
138
|
+
drag.g,
|
|
139
|
+
drag.pane,
|
|
140
|
+
held(widened(side, drag.from, e.clientX - drag.x), min, max),
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
let up = () => {
|
|
144
|
+
let d = drag
|
|
145
|
+
drag = undefined
|
|
146
|
+
delete doc.documentElement.dataset.resizing
|
|
147
|
+
// A press that moved nothing keeps nothing, so a double press resets.
|
|
148
|
+
let width = d && Math.round(bounds(d.pane).width)
|
|
149
|
+
if (d && width != Math.round(d.from)) keep(d.g, width)
|
|
150
|
+
}
|
|
151
|
+
let key = (e: KeyboardEvent) => {
|
|
152
|
+
let g = grip(e)
|
|
153
|
+
let pane = g && paneOf(g)
|
|
154
|
+
if (!g || !pane) return
|
|
155
|
+
let { width, min, max } = bounds(pane)
|
|
156
|
+
let to = keyed(
|
|
157
|
+
g.dataset.resize as Side,
|
|
158
|
+
e.key,
|
|
159
|
+
e.shiftKey,
|
|
160
|
+
width,
|
|
161
|
+
min,
|
|
162
|
+
max,
|
|
163
|
+
)
|
|
164
|
+
if (to == undefined) return
|
|
165
|
+
e.preventDefault()
|
|
166
|
+
put(g, pane, to)
|
|
167
|
+
clearTimeout(timer)
|
|
168
|
+
timer = setTimeout(() => keep(g, to), settle)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let focus = (e: FocusEvent) => {
|
|
172
|
+
let g = grip(e)
|
|
173
|
+
let pane = g && paneOf(g)
|
|
174
|
+
if (g && pane) tell(g, pane)
|
|
175
|
+
}
|
|
176
|
+
let reset = (e: MouseEvent) => {
|
|
177
|
+
let g = grip(e)
|
|
178
|
+
let pane = g && paneOf(g)
|
|
179
|
+
if (!g || !pane) return
|
|
180
|
+
put(g, pane)
|
|
181
|
+
keep(g)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
doc.addEventListener('focusin', focus)
|
|
185
|
+
doc.addEventListener('dblclick', reset)
|
|
186
|
+
doc.addEventListener('pointerdown', down)
|
|
187
|
+
doc.addEventListener('pointermove', move)
|
|
188
|
+
doc.addEventListener('pointerup', up)
|
|
189
|
+
doc.addEventListener('pointercancel', up)
|
|
190
|
+
doc.addEventListener('keydown', key)
|
|
191
|
+
return () => {
|
|
192
|
+
clearTimeout(timer)
|
|
193
|
+
doc.removeEventListener('focusin', focus)
|
|
194
|
+
doc.removeEventListener('dblclick', reset)
|
|
195
|
+
doc.removeEventListener('pointerdown', down)
|
|
196
|
+
doc.removeEventListener('pointermove', move)
|
|
197
|
+
doc.removeEventListener('pointerup', up)
|
|
198
|
+
doc.removeEventListener('pointercancel', up)
|
|
199
|
+
doc.removeEventListener('keydown', key)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (typeof document != 'undefined') resize(document, globalThis)
|
package/rows.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rows a query answers in a page's graph, read live with hooks alone: a
|
|
3
|
+
* domain component asks for the rows it draws, and draws again as they
|
|
4
|
+
* change.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/ import { useStore } from "@yaks/ui/store";
|
|
8
|
+
/* @ts-types="npm:@types/react@19.3.0" */ import { useCallback, useEffect, useMemo } from "react";
|
|
9
|
+
/** Read a query live from a page graph or the app's store client: its rows,
|
|
10
|
+
* whether its first answer has arrived, and why it was refused, for `Answer`
|
|
11
|
+
* to draw. The hook owns the subscription for as long as the component needs
|
|
12
|
+
* it. A watch that says nothing of its readiness (a page's own graph) is
|
|
13
|
+
* ready. */ export let useAnswered = (front, query)=>{
|
|
14
|
+
let watch = useMemo(()=>front.watch(query), [
|
|
15
|
+
front,
|
|
16
|
+
query
|
|
17
|
+
]);
|
|
18
|
+
useEffect(()=>()=>watch.close?.(), [
|
|
19
|
+
watch
|
|
20
|
+
]);
|
|
21
|
+
let read = useMemo(()=>{
|
|
22
|
+
let last;
|
|
23
|
+
return ()=>{
|
|
24
|
+
let { value: rows, refused } = watch;
|
|
25
|
+
let ready = refused === undefined && (watch.ready ?? true);
|
|
26
|
+
if (last?.rows !== rows || last.ready !== ready || last.refused !== refused) last = {
|
|
27
|
+
rows,
|
|
28
|
+
ready,
|
|
29
|
+
refused
|
|
30
|
+
};
|
|
31
|
+
return last;
|
|
32
|
+
};
|
|
33
|
+
}, [
|
|
34
|
+
watch
|
|
35
|
+
]);
|
|
36
|
+
let subscribe = useCallback((wake)=>watch.subscribe(wake), [
|
|
37
|
+
watch
|
|
38
|
+
]);
|
|
39
|
+
return useStore(subscribe, read);
|
|
40
|
+
};
|
|
41
|
+
/** Read matching bundles live from a page graph or the app's store client.
|
|
42
|
+
* The hook owns the subscription for as long as the component needs it. */ export let useRows = (front, query)=>useAnswered(front, query).rows;
|
|
43
|
+
/** Read a watch already held by the host without taking over its lifetime. */ export let useWatch = (watch)=>{
|
|
44
|
+
let subscribe = useCallback((wake)=>watch.subscribe(wake), [
|
|
45
|
+
watch
|
|
46
|
+
]);
|
|
47
|
+
return useStore(subscribe, ()=>watch.value);
|
|
48
|
+
};
|
|
49
|
+
/** Read an observable snapshot and subscribe while the component is mounted. */ export let useObserved = (source)=>{
|
|
50
|
+
let subscribe = useCallback((changed)=>source.subscribe(changed), [
|
|
51
|
+
source
|
|
52
|
+
]);
|
|
53
|
+
return useStore(subscribe, ()=>source.peek());
|
|
54
|
+
};
|
|
55
|
+
/** Bind an entity's buttons or inputs to optimistic edits. Read `issue`
|
|
56
|
+
* beside the value so a refusal remains visible after the rollback. */ export let useStoreEdit = (app, eid)=>{
|
|
57
|
+
useEffect(()=>{
|
|
58
|
+
void app.prepareEdits?.();
|
|
59
|
+
}, [
|
|
60
|
+
app
|
|
61
|
+
]);
|
|
62
|
+
let issue = useStore(app.outbox.onPending, ()=>app.issue(eid));
|
|
63
|
+
return {
|
|
64
|
+
issue,
|
|
65
|
+
apply: (components)=>app.apply({
|
|
66
|
+
...components,
|
|
67
|
+
entity: {
|
|
68
|
+
eid
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
//# sourceMappingURL=rows.js.map
|
package/rows.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rows.js","sources":["./rows.ts"],"names":[],"mappings":"AAAA;;;;;;CAMC,GAED,SAAS,QAAQ,8BAAmC;AACpD,uCAAuC,GAAG,SAAS,WAAW,EAAE,SAAS,EAAE,OAAO,gBAA0B;AAK5G;;;;UAIU,GACV,OAAO,IAAI,cAAc,CACvB,OACA;EAEA,IAAI,QAAQ,QAAQ,IAAM,MAAM,KAAK,CAAC,QAAQ;IAAC;IAAO;GAAM;EAC5D,UAAU,IAAM,IAAM,MAAM,KAAK,MAAM;IAAC;GAAM;EAC9C,IAAI,OAAO,QAAQ;IACjB,IAAI;IACJ,OAAO;MACL,IAAI,EAAE,OAAO,IAAI,EAAE,OAAO,EAAE,GAAG;MAC/B,IAAI,QAAQ,YAAY,aAAa,CAAC,MAAM,KAAK,IAAI,IAAI;MACzD,IACE,MAAM,SAAS,QAAQ,KAAK,KAAK,KAAK,SACtC,KAAK,OAAO,KAAK,SACjB,OAAO;QAAE;QAAM;QAAO;MAAQ;MAChC,OAAO;IACT;EACF,GAAG;IAAC;GAAM;EACV,IAAI,YAAY,YACd,CAAC,OAAqB,MAAM,SAAS,CAAC,OACtC;IAAC;GAAM;EAET,OAAO,SAAS,WAAW;AAC7B,EAAC;AAED;yEACyE,GACzE,OAAO,IAAI,UAAU,CAAC,OAA6B,QACjD,YAAY,OAAO,OAAO,IAAI,CAAA;AAEhC,4EAA4E,GAC5E,OAAO,IAAI,WAAW,CAAI;EAIxB,IAAI,YAAY,YACd,CAAC,OAAqB,MAAM,SAAS,CAAC,OACtC;IAAC;GAAM;EAET,OAAO,SAAS,WAAW,IAAM,MAAM,KAAK;AAC9C,EAAC;AAED,8EAA8E,GAC9E,OAAO,IAAI,cAAc,CAAI;EAI3B,IAAI,YAAY,YACd,CAAC,UAAwB,OAAO,SAAS,CAAC,UAC1C;IAAC;GAAO;EAEV,OAAO,SAAS,WAAW,IAAM,OAAO,IAAI;AAC9C,EAAC;AAED;qEACqE,GACrE,OAAO,IAAI,eAAe,CACxB,KAMA;EAKA,UAAU;IACR,KAAK,IAAI,YAAY;EACvB,GAAG;IAAC;GAAI;EACR,IAAI,QAAQ,SAAS,IAAI,MAAM,CAAC,SAAS,EAAE,IAAM,IAAI,KAAK,CAAC;EAC3D,OAAO;IACL;IACA,OAAO,CAAC,aACN,IAAI,KAAK,CAAC;QAAE,GAAG,UAAU;QAAE,QAAQ;UAAE;QAAI;MAAE;EAC/C;AACF,EAAC"}
|
package/rows.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rows a query answers in a page's graph, read live with hooks alone: a
|
|
3
|
+
* domain component asks for the rows it draws, and draws again as they
|
|
4
|
+
* change.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { useStore } from '@yaks/ui/store'
|
|
10
|
+
/* @ts-types="npm:@types/react@19.3.0" */ import { useCallback, useEffect, useMemo } from 'react'
|
|
11
|
+
import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
|
|
12
|
+
import type { Front } from './host.ts'
|
|
13
|
+
import type { Answered } from './Answer.ts'
|
|
14
|
+
|
|
15
|
+
/** Read a query live from a page graph or the app's store client: its rows,
|
|
16
|
+
* whether its first answer has arrived, and why it was refused, for `Answer`
|
|
17
|
+
* to draw. The hook owns the subscription for as long as the component needs
|
|
18
|
+
* it. A watch that says nothing of its readiness (a page's own graph) is
|
|
19
|
+
* ready. */
|
|
20
|
+
export let useAnswered = (
|
|
21
|
+
front: Pick<Front, 'watch'>,
|
|
22
|
+
query: string,
|
|
23
|
+
): Answered => {
|
|
24
|
+
let watch = useMemo(() => front.watch(query), [front, query])
|
|
25
|
+
useEffect(() => () => watch.close?.(), [watch])
|
|
26
|
+
let read = useMemo(() => {
|
|
27
|
+
let last: Answered | undefined
|
|
28
|
+
return () => {
|
|
29
|
+
let { value: rows, refused } = watch
|
|
30
|
+
let ready = refused === undefined && (watch.ready ?? true)
|
|
31
|
+
if (
|
|
32
|
+
last?.rows !== rows || last.ready !== ready ||
|
|
33
|
+
last.refused !== refused
|
|
34
|
+
) last = { rows, ready, refused }
|
|
35
|
+
return last
|
|
36
|
+
}
|
|
37
|
+
}, [watch])
|
|
38
|
+
let subscribe = useCallback(
|
|
39
|
+
(wake: () => void) => watch.subscribe(wake),
|
|
40
|
+
[watch],
|
|
41
|
+
)
|
|
42
|
+
return useStore(subscribe, read)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Read matching bundles live from a page graph or the app's store client.
|
|
46
|
+
* The hook owns the subscription for as long as the component needs it. */
|
|
47
|
+
export let useRows = (front: Pick<Front, 'watch'>, query: string): Bundle[] =>
|
|
48
|
+
useAnswered(front, query).rows
|
|
49
|
+
|
|
50
|
+
/** Read a watch already held by the host without taking over its lifetime. */
|
|
51
|
+
export let useWatch = <T>(watch: {
|
|
52
|
+
readonly value: T
|
|
53
|
+
subscribe: (changed: () => void) => () => void
|
|
54
|
+
}): T => {
|
|
55
|
+
let subscribe = useCallback(
|
|
56
|
+
(wake: () => void) => watch.subscribe(wake),
|
|
57
|
+
[watch],
|
|
58
|
+
)
|
|
59
|
+
return useStore(subscribe, () => watch.value)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Read an observable snapshot and subscribe while the component is mounted. */
|
|
63
|
+
export let useObserved = <T>(source: {
|
|
64
|
+
peek: () => T
|
|
65
|
+
subscribe: (changed: () => void) => () => void
|
|
66
|
+
}): T => {
|
|
67
|
+
let subscribe = useCallback(
|
|
68
|
+
(changed: () => void) => source.subscribe(changed),
|
|
69
|
+
[source],
|
|
70
|
+
)
|
|
71
|
+
return useStore(subscribe, () => source.peek())
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Bind an entity's buttons or inputs to optimistic edits. Read `issue`
|
|
75
|
+
* beside the value so a refusal remains visible after the rollback. */
|
|
76
|
+
export let useStoreEdit = (
|
|
77
|
+
app: {
|
|
78
|
+
apply: (bundle: Bundle) => Promise<unknown>
|
|
79
|
+
issue: (eid: string) => string | undefined
|
|
80
|
+
prepareEdits?: () => unknown
|
|
81
|
+
outbox: { onPending: (changed: () => void) => () => void }
|
|
82
|
+
},
|
|
83
|
+
eid: string,
|
|
84
|
+
): {
|
|
85
|
+
issue: string | undefined
|
|
86
|
+
apply: (components: Omit<Bundle, 'entity'>) => Promise<unknown>
|
|
87
|
+
} => {
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
void app.prepareEdits?.()
|
|
90
|
+
}, [app])
|
|
91
|
+
let issue = useStore(app.outbox.onPending, () => app.issue(eid))
|
|
92
|
+
return {
|
|
93
|
+
issue,
|
|
94
|
+
apply: (components: Omit<Bundle, 'entity'>) =>
|
|
95
|
+
app.apply({ ...components, entity: { eid } }),
|
|
96
|
+
}
|
|
97
|
+
}
|