Primitives / Interaction
Marquee: a lasso over marked items
PointerDrag starts only from a marked element, so a press on a canvas's empty
space is nobody's. Marquee is that press: it is a Mount beside PointerDrag
on the same container (Behavior.attach both; a press on a marked descendant
is the drag's, a press on anything else is the lasso's). It owns no state and
draws nothing. A primary press on empty space that moves DRAG_THRESHOLD
starts MarqueeStarted { additive } (additive when Shift, Ctrl or Meta was
held), then MarqueeChanged { rect, ids, additive } as the pointer moves or
edgeScroll scrolls the container under it, then MarqueeEnded with the same fields, or
MarqueeCancelled on Escape or a cancelled pointer. rect is { x, y, width,
height } in the container's content coordinates (its padding box's top left,
scrolled with it, and clipped to what the container holds), so a view draws the
band in a Slot of its own at left: x; top: y, and ids are the marked
descendants the rectangle overlaps, in document order. A press is not empty
space when it is on a marked descendant, a link, button, field, label,
summary or editable text, or on the container's own scrollbar, and a finger
never starts one (it scrolls). The click a lasso ends with is swallowed, as a
drop's is. The container must be a box (not display: contents). Each change
measures every marked descendant (a querySelectorAll and a box each), so a
container of thousands draws only the rows that show, or marks fewer.
It selects nothing. The ids are for update to hand to Selection, whose
replace(ids) helper sets the selection, so there is one way to select;
Marquee.selected(before, fact) is the pure combination, the ids alone, or
after before when the press was additive:
import { Match, Option, Schema } from 'effect'
import type { HtmlBuilder } from 'foldkit/html'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Bundle } from 'foldkit-bundle'
import { Behavior, Behaviors, Capability, Slot, SlotView, Slots } from 'foldkit-mixins'
import { Marquee, PointerDrag, Selection } from 'foldkit-primitives/interaction'
const Picks = Bundle.declare(Selection.bundle, 'picks')
const Model = Schema.Struct({
...Picks.fields,
ids: Schema.Array(Schema.String),
// What was selected when the press began, for an additive lasso and for Escape.
before: Schema.Array(Schema.String),
band: Schema.OptionFromNullOr(Marquee.MarqueeRect),
})
type Model = typeof Model.Type
const Message = defineMessageUnion({
...Picks.cases,
LassoStarted: {},
LassoChanged: {
rect: Marquee.MarqueeRect,
ids: Schema.Array(Schema.String),
additive: Schema.Boolean,
},
LassoEnded: {},
LassoCancelled: {},
})
type Message = typeof Message.Type
const pickArgs = { mode: 'multiple', allowEmpty: true } as const
const Parent = Bundle.parent({ Model, Message })
const Picked = Parent.at(Picks, { args: pickArgs })
const assembly = Parent.assemble(Picked)
const update = assembly.update((model, message) =>
Match.valueTags(message, {
LassoStarted: () => ({
model: modifyFields(model, { before: () => model.picks.selected }),
}),
LassoChanged: ({ rect, ids, additive }) =>
Picked.helpers.replace(Marquee.selected(model.before, { ids, additive }))(
modifyFields(model, { band: () => Option.some(rect) }),
),
LassoEnded: () => ({ model: modifyFields(model, { band: () => Option.none() }) }),
LassoCancelled: () =>
Picked.helpers.replace(model.before)(
modifyFields(model, { band: () => Option.none() }),
),
}),
)
const CanvasSlots = Slots.define({
canvas: Slot.make({ capability: Capability.Container }),
node: Slot.make({ capability: Capability.Interactive }),
band: Slot.make({ capability: Capability.Base }),
})
const Canvas = SlotView.over(CanvasSlots)<Model, Message>()
const items = (model: Model) => Behaviors.Collection.of(model.ids, { id: id => id })
const view = Canvas.define((model, slots, h: HtmlBuilder<Message>) =>
h.div(
slots.canvas.attrs([h.Style({ position: 'relative', height: '240px' })]),
[
...model.ids.map((id, index) =>
h.div(slots.node.attrs([h.Key(id), h.DataAttribute('node', id)], { index, id }), [id]),
),
...Option.match(model.band, {
onNone: () => [],
onSome: rect => [
h.div(
slots.band.attrs([
h.Style({
position: 'absolute',
left: `${rect.x}px`,
top: `${rect.y}px`,
width: `${rect.width}px`,
height: `${rect.height}px`,
'pointer-events': 'none',
}),
]),
[],
),
],
}),
],
),
).pipe(
Behavior.attach(
Marquee.behavior(Canvas)({
container: 'canvas',
attribute: 'data-node',
edgeScroll: {},
toMessage: fact =>
Match.valueTags(fact, {
MarqueeStarted: () => Message.LassoStarted(),
MarqueeChanged: ({ rect, ids, additive }) =>
Message.LassoChanged({ rect, ids, additive }),
MarqueeEnded: () => Message.LassoEnded(),
MarqueeCancelled: () => Message.LassoCancelled(),
}),
}),
),
Behavior.attach(
Selection.behavior(Picks, pickArgs)(Canvas)({ container: 'canvas', item: 'node', items }),
),
)
void update
void viewNodes are dragged by PointerDrag on the same container and keep their own
click through Selection. The keyboard's way to select more than one is not a
lasso: it is Shift-extending from the roving item (Selection's Ranged,
fed by Press's Pressed { modifiers }) or replace(ids) from a "select all"
chord the parent routes. A lasso is a pointer gesture only, as a drag is
without Reorder.