Let variables, fields, ports and list items be dragged into order (#310)
game-maker / gate (push) Successful in 3m10s
game-maker / suite (push) Failing after 19m34s

Variables of a behavior, the fields of a struct, the ports of a start
or end node and the items of a list could only be added and removed.
Changing their order meant removing and adding them again. Now each of
them is dragged by the mark in front of its row.

- sortBlocks (ui/blockDrag.ts) makes the rows of one column sortable.
  A block is every row it covers, so a struct moves with its fields and
  a list item with its nested list. The block is dimmed while it is
  carried, an accent line shows where it lands, and a scrolled panel
  follows the pointer at its edge. Less than 4 px stays a click.
- The arithmetic is pure and lives in ui/sortable.ts: blockGap,
  gapLineY, dropSlot and edgeScroll. dropIndex of the tile strip moved
  there too, so the reorder maths sits in one file.
- MoveVariableCommand, MoveStructFieldCommand and MovePortCommand
  build on the existing MoveCommand. Undo moves back.
- Lists need no command. InlineList replaces the list as a whole, as
  it does for every other edit of its items.
- The grip is the kind mark, for a list item its number. Moving only
  happens among siblings: a field stays in its struct.
- On a Struct asset the move runs through WithStructTypes. Typed
  variables, Pack/Expand ports and split outputs follow the new order.
- Values and edges hang on names, so only the order changes. Nodes
  running a behavior read their ports from the start node, the new
  order reaches them without a copy.
- Not sortable by design: the variables of a placed object, the fields
  of a typed struct (moved on the Struct asset) and the picked ports
  of Variables/Properties nodes.
- Test hook __gm.inspectorMark(label) finds the mark of a row.

Tests:
- Gate on melektaus-atx: 235 files, 3572 tests, CHECK EXIT 0 (+9).
- tests/core/reorder.test.ts: a variable, a field two levels deep, a
  Struct asset reaching a typed variable and Pack, and start ports
  reaching the calling node with edges and result kept.
- e2e 22: ALL PASSED, 39 checks (+4, sum 1739). 22f-iii drags a
  variable past a struct of several rows, then a field inside it.
- Counter-probes: dropSlot by top edge instead of middle and a field
  move that ignores its path fail 3 unit tests. With sortBlocks
  switched off, the three order checks of 22f-iii fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-03 21:45:04 +02:00
1 parent cf6043a066
commit 2c19c89878
18 files changed
+671 -83

No files matched your search

+25 -2
View File
@@ -23,8 +23,8 @@ import { isGraphAsset } from '../model/Asset';
import type { PropertyValue } from '../model/Properties';
import { kindLabel } from '../properties/kinds';
import type { Project } from '../model/Project';
import { type Removed, removeFirst, removeWhere, restoreAt } from '../util/list';
import { type Command, CommandError, RenameCommand } from './Command';
import { moveItem, type Removed, removeFirst, removeWhere, restoreAt } from '../util/list';
import { type Command, CommandError, MoveCommand, RenameCommand } from './Command';
import { nextFieldName } from './naming';
import { requireGraph, requireNode, requireNodeDef } from './require';
@@ -303,6 +303,29 @@ export class RenamePortCommand extends RenameCommand {
}
}
/**
* Edges hang on a port's name, so they stay connected; a running or calling node takes the order
* from here, as it takes the ports themselves (#310).
*/
export class MovePortCommand extends MoveCommand {
constructor(
private readonly assetId: string,
private readonly nodeId: string,
from: number,
to: number,
) {
super('Reorder ports', from, to);
}
protected move(project: Project, from: number, to: number): void {
const node = requireNode(project, this.assetId, this.nodeId);
const ports = [...(node.ports ?? [])];
if (!moveItem(ports, from, to))
throw new CommandError(`There is no port ${from + 1} to move to place ${to + 1}`);
node.ports = ports;
}
}
/** Renames one declared port on one node, taking its edges and its typed-in value along. */
function renamePortOn(graph: Graph, node: GraphNode, from: string, to: string): void {
const ports = node.ports ?? [];
+40 -2
View File
@@ -27,8 +27,8 @@ import {
variableDef,
} from '../properties/PropertySchema';
import { valueAt, withValueAt } from '../properties/valuePath';
import { type Removed, removeFirst, restoreAt } from '../util/list';
import { type Command, CommandError, RenameCommand } from './Command';
import { moveItem, type Removed, removeFirst, restoreAt } from '../util/list';
import { type Command, CommandError, MoveCommand, RenameCommand } from './Command';
import { renameVariablePorts } from './portCommands';
import { requireBehavior, requireVariableOwner } from './require';
@@ -183,6 +183,22 @@ export class RenameVariableCommand extends RenameCommand {
}
}
/** Only the order changes: values and the ports naming a variable go by its name (#310). */
export class MoveVariableCommand extends MoveCommand {
constructor(
private readonly assetId: string,
from: number,
to: number,
) {
super('Reorder variables', from, to);
}
protected move(project: Project, from: number, to: number): void {
if (!moveItem(requireVariableOwner(project, this.assetId).variables, from, to))
throw new CommandError(`There is no variable ${from + 1} to move to place ${to + 1}`);
}
}
/** The behavior, once it is known to declare `key`. */
function requireBehaviorVariable(project: Project, assetId: string, key: string): BehaviorAsset {
const asset = requireBehavior(project, assetId);
@@ -467,3 +483,25 @@ export class RenameStructFieldCommand extends RenameCommand {
site.write(withFieldRenamed(site.read(), from, to));
}
}
/** A struct value is read by field name, so only the declaration changes order (#310). */
export class MoveStructFieldCommand extends MoveCommand {
/** `path` names the struct whose fields are reordered: none for the variable itself. */
constructor(
private readonly assetId: string,
private readonly key: string,
from: number,
to: number,
private readonly path: readonly string[] = [],
) {
super('Reorder fields', from, to);
}
protected move(project: Project, from: number, to: number): void {
const owner = requireFieldOwner(project, this.assetId, this.key, this.path);
const fields = [...owner.fields()];
if (!moveItem(fields, from, to))
throw new CommandError(`There is no field ${from + 1} to move to place ${to + 1}`);
owner.setFields(fields);
}
}
+9 -2
View File
@@ -54,7 +54,9 @@ export function uiHooks({ app, state, editor }: HookContext) {
return control && { ...centreOf(control), left: control.getGlobalPosition().x };
};
/** The control right of the row label `label`, as `fieldOf` finds it. */
const controlOf = (label: string, last: boolean): Widget | null => {
const controlOf = (label: string, last: boolean): Widget | null => besideOf(label, last, 1);
/** The widget `step` places along from the row label `label`: +1 its control, -1 its mark. */
const besideOf = (label: string, last: boolean, step: number): Widget | null => {
let found: Text | null = null;
const walk = (node: Container): void => {
if ((found !== null && !last) || !node.visible) return;
@@ -70,7 +72,7 @@ export function uiHooks({ app, state, editor }: HookContext) {
while (owner && !(owner instanceof Widget)) owner = owner.parent;
const row = owner?.parent;
if (!row || !owner) return null;
const control = row.children[row.children.indexOf(owner) + 1];
const control = row.children[row.children.indexOf(owner) + step];
if (!(control instanceof Widget)) return null;
// Taller than the window since its groups have bands (#290): the row is scrolled into view.
inspector.reveal(control);
@@ -164,6 +166,11 @@ export function uiHooks({ app, state, editor }: HookContext) {
return (last ? hits.at(-1) : hits[0]) ?? null;
},
inspectorField: fieldOf,
/** The mark in front of the row named `label` – what a variable, a field or a port is dragged by (#310). */
inspectorMark: (label: string, last = false) => {
const mark = besideOf(label, last, -1);
return mark && centreOf(mark);
},
/** The eyedropper beside the object field of a row (#276): where to press it, and whether it is lit. */
inspectorEyedropper: (label: string, last = false) => {
const button = controlOf(label, last)?.children.at(-1);
+10 -4
View File
@@ -12,6 +12,8 @@ import { nextFieldName, nextVariableName } from '../../core/commands/naming';
import {
AddStructFieldCommand,
AddVariableCommand,
MoveStructFieldCommand,
MoveVariableCommand,
RemoveStructFieldCommand,
RemoveVariableCommand,
RenameStructFieldCommand,
@@ -51,7 +53,7 @@ import {
} from '../../core/properties/PropertySchema';
import { markedAssetSchema } from '../../core/properties/deviation';
import { isModified, resolveAssetProps } from '../../core/properties/resolve';
import { Button, Dropdown, Label, Menu, type MenuEntry } from '../../ui';
import { Button, Dropdown, Label, Menu, type MenuEntry, type SortBlock, sortBlocks } from '../../ui';
import { FontPreview } from '../font/FontPreview';
import { clearSaved, hasSaved } from '../saves';
import { SoundPreview } from '../sound/SoundPreview';
@@ -59,7 +61,7 @@ import type { VariablesOwner } from '../state';
import { aspectActions, sizeCommand } from './aspectAction';
import { buildCutRows, CUT_ROW_KEYS, refreshCutRows } from './cutSection';
import { actionIf, createFieldRow, type FieldHandlers } from './fieldFactory';
import { addNameField, addNote, addTitle, type InspectorHost, sectionHeader } from './host';
import { addedRows, addNameField, addNote, addTitle, type InspectorHost, sectionHeader } from './host';
import { buildShapeListSection, shapeListSignature } from './shapeSection';
import { buildVariable, kindMenu, type StructTyping, type VariableEditing } from './variableRows';
import { listSource, previewingHandlers, structFields } from './previewHandlers';
@@ -289,6 +291,7 @@ export function buildVariables(host: InspectorHost, asset: VariableOwnerAsset, i
const values = shownValues(host, asset);
const renamed = (path: readonly string[], from: string, to: string): FieldRename | undefined =>
isType ? { structType: asset.id, path, from, to } : undefined;
const blocks: SortBlock[] = [];
for (const def of asset.variables) {
const edit: VariableEditing = {
rename: (next) => run(new RenameVariableCommand(asset.id, def.key, next), renamed([], def.key, next)),
@@ -308,6 +311,7 @@ export function buildVariables(host: InspectorHost, asset: VariableOwnerAsset, i
path,
),
),
moveField: (path, from, to) => run(new MoveStructFieldCommand(asset.id, def.key, from, to, path)),
};
const behavior = asset.type === 'behavior';
const keep = behavior
@@ -324,11 +328,13 @@ export function buildVariables(host: InspectorHost, asset: VariableOwnerAsset, i
: undefined;
const value = values[def.key] ?? def.default;
const typing = structTyping(host, asset, def.key);
host.fields.set(
def.key,
const { rows, built } = addedRows(host, () =>
buildVariable(host, def, value, assetHandlers(host, asset.id, def), { edit, typing, keep, hidden }),
);
host.fields.set(def.key, built);
blocks.push({ grip: built.grip, rows });
}
sortBlocks(blocks, (from, to) => run(new MoveVariableCommand(asset.id, from, to)));
}
/**
+9
View File
@@ -8,6 +8,7 @@ import {
type TextFieldOptions,
Theme,
type VStack,
Widget,
} from '../../ui';
import { foldKey, isFolded, setFolded } from '../sectionFolds';
import type { EditorState } from '../state';
@@ -70,6 +71,14 @@ export function addNote(
host.body.add(new Label(text, { style }));
}
/** The rows `build` adds to the body – what one block of a sortable group covers (#310). */
export function addedRows<T>(host: InspectorHost, build: () => T): { rows: Widget[]; built: T } {
const before = host.body.children.length;
const built = build();
const rows = host.body.children.slice(before).filter((child): child is Widget => child instanceof Widget);
return { rows, built };
}
export function addGap(host: InspectorHost): void {
host.body.add(new Label(''));
}
+11 -3
View File
@@ -1,6 +1,7 @@
import {
AddPortCommand,
AddPortsCommand,
MovePortCommand,
nextPortName,
RemovePortCommand,
RenamePortCommand,
@@ -41,6 +42,8 @@ import {
Menu,
type MenuItem,
type SectionHeader,
type SortBlock,
sortBlocks,
Spacer,
TextField,
Theme,
@@ -261,7 +264,10 @@ export function buildPortSection(
: 'No outputs yet. Every output added here becomes a result of running this behavior; one of the kind Flow is a further way out.',
);
for (const port of ports) portRow(host, assetId, node, port, portNameField(host, assetId, node, port));
const blocks = ports.map((port) =>
portRow(host, assetId, node, port, portNameField(host, assetId, node, port)),
);
sortBlocks(blocks, (from, to) => host.state.execute(new MovePortCommand(assetId, node.id, from, to)));
}
const plural = (noun: string) => `${noun.toLowerCase()}s`;
@@ -286,15 +292,17 @@ function portRow(
port: DeclaredPort,
name: Widget,
extras: readonly Widget[] = [],
): void {
): SortBlock {
const row = new HStack({ gap: Theme.gap, align: 'center' });
row.minHeight = Theme.rowHeight;
row.add(markLabel(port.kind));
const mark = markLabel(port.kind);
row.add(mark);
row.add(name, 1);
for (const extra of extras) row.add(extra);
row.add(new Spacer());
row.add(removeButton(() => host.state.execute(new RemovePortCommand(assetId, node.id, port.key))));
host.body.add(row);
return { grip: mark, rows: [row] };
}
/**
+67 -44
View File
@@ -10,6 +10,8 @@ import {
Menu,
type MenuEntry,
type MenuItem,
type SortBlock,
sortBlocks,
Spacer,
TextField,
Theme,
@@ -33,7 +35,7 @@ import {
structFieldValue,
} from './fieldFactory';
import { KIND_ICON_SVG, VARIABLE_SWITCH_ICON_SVG } from '../panels/markIcons';
import { addNote, type InspectorHost } from './host';
import { addedRows, addNote, type InspectorHost } from './host';
/** The menu every "add a kind" button opens: one entry per kind, named as the kind table names it. */
export function kindMenu<K extends string>(kinds: readonly K[], pick: (kind: K) => void): MenuItem[] {
@@ -50,6 +52,8 @@ export interface VariableEditing {
renameField: (path: readonly string[], field: StructFieldDef, next: string) => void;
removeField: (path: readonly string[], field: StructFieldDef) => void;
addField: (path: readonly string[], kind: NestedKind) => void;
/** A field dragged to another place among the fields of its struct (#310). */
moveField: (path: readonly string[], from: number, to: number) => void;
}
/**
@@ -82,6 +86,11 @@ export interface VariableOptions {
actions?: RowActions;
}
/** A variable's first row, and its mark – where the asset lets it be dragged to another place (#310). */
export interface VariableRow extends FieldRow {
grip: Widget;
}
/**
* One variable, drawn the same on its asset and on a placed object – minus `edit` there, since the
* shape belongs to the asset. Adds its rows to the body itself and returns the first.
@@ -92,10 +101,11 @@ export function buildVariable(
value: PropertyValue,
handlers: FieldHandlers,
options: VariableOptions = {},
): FieldRow {
): VariableRow {
const { edit, typing, keep, hidden, actions } = options;
const name = nameCell(def.key, 0, edit && ((next) => edit.rename(next)));
const head = gridRow(0, markLabel(def.kind), name);
const grip = markLabel(def.kind);
const head = gridRow(0, grip, name);
let setValue: (next: PropertyValue) => void = () => {};
let below: Widget | undefined;
// A struct with a type has the type's fields: they are changed on the Struct asset, not here.
@@ -138,6 +148,7 @@ export function buildVariable(
return {
row: head,
grip,
setValue: (next) => setValue(next),
setModified: (modified) => {
if (name instanceof Label) name.setStyle(modified ? 'override' : 'muted');
@@ -175,7 +186,8 @@ function lockValue(field: Field, locked: boolean): void {
/**
* One row per field, each with its own value; a field that is a struct again is a head row with its
* fields indented below it (#239). `path` names the struct these fields belong to. Returns the
* setter that pushes a bundle into them.
* setter that pushes a bundle into them. Where the fields are edited, each one – its own fields
* and items included – is dragged by its mark among its siblings (#310).
*/
function structFieldRows(
host: InspectorHost,
@@ -188,58 +200,69 @@ function structFieldRows(
locked: boolean,
): (next: PropertyValue) => void {
const setters = new Map<string, (item: PropertyValue) => void>();
const blocks: SortBlock[] = [];
for (const field of fields) {
const row = fieldRow(field, path, edit);
const { row, mark } = fieldRow(field, path, edit);
const item = structFieldValue(value, field);
if (field.kind === 'struct') {
const inner = [...path, field.key];
const typed = typing?.typeName(field.structType) ?? null;
row.add(new Spacer(), 1);
if (edit && typed === null) row.add(addFieldMenu(edit, inner));
const { rows } = addedRows(host, () => {
if (field.kind === 'struct') {
const inner = [...path, field.key];
const typed = typing?.typeName(field.structType) ?? null;
row.add(new Spacer(), 1);
if (edit && typed === null) row.add(addFieldMenu(edit, inner));
if (edit) row.add(removeButton(() => edit.removeField(path, field)));
host.body.add(row);
if (typing)
host.body.add(typeRow(inner.length + 1, typed, () => typing.menu(inner, field.structType)));
const own = typed === null;
setters.set(
field.key,
structFieldRows(
host,
field.fields,
inner,
item,
handlers,
own ? edit : undefined,
own ? typing : undefined,
locked,
),
);
return;
}
const own = handlers.structFieldHandlers?.([...path, field.key], field) ?? READ_ONLY;
const sub = createField(host, fieldDef(field), item, own);
setters.set(field.key, addValue(row, sub));
lockValue(sub, locked);
if (edit) row.add(removeButton(() => edit.removeField(path, field)));
host.body.add(row);
if (typing) host.body.add(typeRow(inner.length + 1, typed, () => typing.menu(inner, field.structType)));
const own = typed === null;
setters.set(
field.key,
structFieldRows(
host,
field.fields,
inner,
item,
handlers,
own ? edit : undefined,
own ? typing : undefined,
locked,
),
);
continue;
}
const own = handlers.structFieldHandlers?.([...path, field.key], field) ?? READ_ONLY;
const sub = createField(host, fieldDef(field), item, own);
setters.set(field.key, addValue(row, sub));
lockValue(sub, locked);
if (edit) row.add(removeButton(() => edit.removeField(path, field)));
host.body.add(row);
if (field.kind === 'list' && typing) {
const inner = [...path, field.key];
host.body.add(
itemsRow(path.length + 2, typing.typeName(field.itemType), () =>
typing.listMenu(inner, field.itemType),
),
);
}
if (sub.below) host.body.add(indentedBy(sub.below, path.length + 2));
if (field.kind === 'list' && typing) {
const inner = [...path, field.key];
host.body.add(
itemsRow(path.length + 2, typing.typeName(field.itemType), () =>
typing.listMenu(inner, field.itemType),
),
);
}
if (sub.below) host.body.add(indentedBy(sub.below, path.length + 2));
});
blocks.push({ grip: mark, rows });
}
if (edit) sortBlocks(blocks, (from, to) => edit.moveField(path, from, to));
return (next) => {
for (const field of fields) setters.get(field.key)?.(structFieldValue(next, field));
};
}
function fieldRow(field: StructFieldDef, path: readonly string[], edit: VariableEditing | undefined): HStack {
function fieldRow(
field: StructFieldDef,
path: readonly string[],
edit: VariableEditing | undefined,
): { row: HStack; mark: Widget } {
const depth = path.length + 1;
const name = nameCell(field.key, depth, edit && ((next) => edit.renameField(path, field, next)));
return gridRow(depth, markLabel(field.kind), name);
const mark = markLabel(field.kind);
return { row: gridRow(depth, mark, name), mark };
}
/** Which Struct asset a struct follows, as a row of its own under the struct's head. */
+19 -3
View File
@@ -17,6 +17,8 @@ import {
Menu,
type MenuEntry,
ScrollContainer,
type SortBlock,
sortBlocks,
Spacer,
TextField,
Theme,
@@ -40,6 +42,7 @@ import {
import type { Project } from '../../core/model/Project';
import { structAssetOf, typedItem, typeFields } from '../../core/model/structTypes';
import { nextFieldName } from '../../core/commands/naming';
import { moveItem } from '../../core/util/list';
import { type FieldKind, NESTED_KINDS, type NestedKind } from '../../core/properties/kinds';
import {
type ListItem,
@@ -312,7 +315,8 @@ export class InlineList extends VStack {
this.more = null;
if (access) {
const { shown, more } = shownItems(access.value.length, this.options.limit);
access.value.slice(0, shown).forEach((item, index) => this.itemRow(access, item, index));
const blocks = access.value.slice(0, shown).map((item, index) => this.itemRow(access, item, index));
sortBlocks(blocks, (from, to) => this.moveItem(from, to));
if (more > 0) this.add(this.moreLine(access, more));
}
this.relayout();
@@ -332,7 +336,16 @@ export class InlineList extends VStack {
return line;
}
private itemRow(access: ListAccess, item: ListValue[number], index: number): void {
/** An item dragged by its number to another place (#310): the order is part of the value. */
private moveItem(from: number, to: number): void {
const access = this.access();
if (!access) return;
const next = [...access.value];
if (moveItem(next, from, to)) access.replace(next);
}
/** The item's rows, its number being where it is dragged from. */
private itemRow(access: ListAccess, item: ListValue[number], index: number): SortBlock {
const kind = itemKindOf(item);
// A struct item is a stack of field rows: its row grows with it and aligns to the top.
const tall = kind === 'struct';
@@ -365,7 +378,10 @@ export class InlineList extends VStack {
const widgets = this.rowWidgets[index];
if (widgets) widgets.remove = remove;
this.add(row);
if (below) this.add(indented(below, INDEX_WIDTH + Theme.gap));
if (!below) return { grip: label, rows: [row] };
const items = indented(below, INDEX_WIDTH + Theme.gap);
this.add(items);
return { grip: label, rows: [row, items] };
}
/** A list inside this one: its head goes where a value would, its items below, one step further in. */
+1 -18
View File
@@ -8,6 +8,7 @@ import type { TileAsset } from '../../core/model/Asset';
import { pictureChoices, textureRefLabel } from '../../core/properties/textureRef';
import {
Button,
dropIndex,
type DropSpot,
HStack,
Label,
@@ -22,24 +23,6 @@ import type { EditorState } from '../state';
const WIDTH = 210;
/**
* Where a dragged row lands, counted in the list it has *left* – which is what the move command
* takes. A drop on the middle of a row takes that row's place: the list is flat, so there is no
* "inside" for it to mean, and without this a drag downwards onto the next row would cancel itself
* out. The gaps above and below a row are insert points, and one below the dragged row shifts up
* by one once the row is out. `-1` is "nothing to do".
*/
export function dropIndex(from: number, spot: DropSpot, length: number): number {
const last = length - 1;
if (from < 0 || from > last) return -1;
if (spot.at === 'end') return last;
const target = Number(spot.targetId);
if (!Number.isInteger(target) || target < 0 || target > last) return -1;
if (spot.at === 'onto') return target;
const insert = target + (spot.at === 'after' ? 1 : 0);
return Math.min(last, insert > from ? insert - 1 : insert);
}
/**
* The picture list beside the board (#118): one row per animation step, in the order they run.
* Rows are addressed by their place rather than by the picture they name – the same block may be
+152
View File
@@ -0,0 +1,152 @@
import { type Container, type FederatedPointerEvent, Graphics, Point } from 'pixi.js';
import { cancelFrameWork, onFrame } from './render';
import { blockGap, type BlockSpan, dropSlot, edgeScroll, gapLineY } from './sortable';
import { Theme } from './Theme';
import type { Widget } from './Widget';
import { ScrollContainer } from './widgets/ScrollContainer';
/** One thing to reorder: where it is taken hold of, and the rows it fills in its column. */
export interface SortBlock {
grip: Widget;
/** Siblings in one column, top to bottom. */
rows: readonly Widget[];
}
/** How far the pointer goes before a press on a grip becomes a drag; less stays a click. */
const DRAG_THRESHOLD = 4;
const FRAME_KEY = 'block-drag';
const DIMMED = 0.4;
/**
* Lets the blocks of one column be reordered by dragging their grips (#310). Unlike a
* `SortableList` the rows are ordinary widgets of any height – a struct is its head with its fields
* below – so a block is told by the rows it covers. While dragging, the block is dimmed, a line
* shows where it lands, and a scrolled column follows the pointer to its edge. `move` gets the
* indices the model takes, the block already taken out.
*/
export function sortBlocks(blocks: readonly SortBlock[], move: (from: number, to: number) => void): void {
if (blocks.length < 2) return;
blocks.forEach((block, index) => {
block.grip.eventMode = 'static';
block.grip.cursor = 'grab';
block.grip.on('pointerdown', (e: FederatedPointerEvent) => {
if (e.button === 0) new BlockDrag(blocks, index, e.global.y, move);
});
});
}
class BlockDrag {
private readonly column: Container | null;
private readonly scroll: ScrollContainer | null;
private readonly line = new Graphics();
private readonly alphas: number[] = [];
private readonly pointer = new Point();
private active = false;
private readonly unbind: () => void;
constructor(
private readonly blocks: readonly SortBlock[],
private readonly from: number,
private readonly startY: number,
private readonly move: (from: number, to: number) => void,
) {
const block = blocks[from]!;
this.column = block.rows[0]?.parent ?? null;
this.scroll = scrollOf(this.column);
const grip = block.grip;
const onMove = (e: FederatedPointerEvent) => this.onMove(e);
const onUp = () => this.finish(true);
const onGone = () => this.finish(false);
grip.on('globalpointermove', onMove);
grip.on('pointerup', onUp);
grip.on('pointerupoutside', onUp);
// A rebuild of the rows mid-drag takes the grip away – and with it the release.
grip.once('destroyed', onGone);
this.unbind = () => {
grip.off('globalpointermove', onMove);
grip.off('pointerup', onUp);
grip.off('pointerupoutside', onUp);
grip.off('destroyed', onGone);
};
}
private get rows(): readonly Widget[] {
return this.blocks[this.from]!.rows;
}
private onMove(e: FederatedPointerEvent): void {
this.pointer.copyFrom(e.global);
if (!this.active && Math.abs(e.global.y - this.startY) < DRAG_THRESHOLD) return;
if (!this.column) return;
if (!this.active) {
this.active = true;
for (const row of this.rows) {
this.alphas.push(row.alpha);
row.alpha = DIMMED;
}
this.column.addChild(this.line);
}
this.drawLine();
this.followEdge();
}
/** The blocks' extents in the column, read now: rows may have moved since the drag began. */
private spans(): BlockSpan[] {
return this.blocks.map(({ rows }) => ({
top: Math.min(...rows.map((row) => row.y)),
bottom: Math.max(...rows.map((row) => row.y + row.h)),
}));
}
private localY(): number {
return this.column ? this.column.toLocal(this.pointer).y : 0;
}
/** Only where a drop would move the block: a line where it already stands promises nothing. */
private drawLine(): void {
const column = this.column;
this.line.clear();
if (!column) return;
const spans = this.spans();
const y = this.localY();
if (dropSlot(y, spans, this.from) === null) return;
// From the grip's own indent, so a field's line shows it stays inside its struct.
const left = column.toLocal(this.blocks[this.from]!.grip.getGlobalPosition()).x;
const right = Math.max(...this.rows.map((row) => row.x + row.w));
const at = Math.round(gapLineY(blockGap(y, spans), spans));
this.line.rect(left, at - 1, Math.max(0, right - left), 2).fill(Theme.colors.accent);
}
/** Scrolls a frame's worth while the pointer stays at the column's edge, and asks for the next. */
private followEdge(): void {
const scroll = this.scroll;
if (!scroll || !this.active) return;
onFrame(FRAME_KEY, () => {
if (!this.active) return;
const before = scroll.offset;
scroll.scrollTo(before + edgeScroll(this.pointer.y, scroll.getGlobalPosition().y, scroll.h));
if (scroll.offset === before) return;
this.drawLine();
this.followEdge();
});
}
private finish(drop: boolean): void {
this.unbind();
cancelFrameWork(FRAME_KEY);
if (!this.active) return;
this.active = false;
const to = drop ? dropSlot(this.localY(), this.spans(), this.from) : null;
this.rows.forEach((row, i) => {
if (!row.destroyed) row.alpha = this.alphas[i] ?? 1;
});
if (!this.line.destroyed) this.line.destroy();
if (to !== null) this.move(this.from, to);
}
}
function scrollOf(node: Container | null): ScrollContainer | null {
for (let parent = node; parent; parent = parent.parent)
if (parent instanceof ScrollContainer) return parent;
return null;
}
+2 -1
View File
@@ -65,7 +65,8 @@ export {
type SortableValue,
type ThumbClip,
} from './widgets/SortableList';
export { dropGap, dropMove, rowOrder } from './sortable';
export { dropGap, dropIndex, dropMove, rowOrder } from './sortable';
export { type SortBlock, sortBlocks } from './blockDrag';
export { Popup, type PopupOptions } from './widgets/Popup';
export {
currentMenu,
+59 -1
View File
@@ -1,10 +1,12 @@
import type { DropSpot } from './widgets/ListView';
/**
* How a sortable list maps between what is on screen and what its owner holds. A layer list shows the
* drawing order upside down – the item drawn last is the topmost row – so every index has two
* readings, and dropping a dragged row has a third: the place it lands in is counted with the row
* still in the list, while the model takes it out first.
*
* Kept apart from the widget so the arithmetic can be measured without a canvas.
* Kept apart from the widgets so the arithmetic can be measured without a canvas.
*/
/** The ids as the list shows them, top first. `reversed` puts the model's last item on top. */
@@ -36,3 +38,59 @@ export function dropMove(
const last = count - 1;
return { from: last - fromRow, to: last - toRow };
}
/**
* Where a dragged row lands, counted in the list it has *left* – which is what the move command
* takes. A drop on the middle of a row takes that row's place: the list is flat, so there is no
* "inside" for it to mean, and without this a drag downwards onto the next row would cancel itself
* out. The gaps above and below a row are insert points, and one below the dragged row shifts up
* by one once the row is out. `-1` is "nothing to do".
*/
export function dropIndex(from: number, spot: DropSpot, length: number): number {
const last = length - 1;
if (from < 0 || from > last) return -1;
if (spot.at === 'end') return last;
const target = Number(spot.targetId);
if (!Number.isInteger(target) || target < 0 || target > last) return -1;
if (spot.at === 'onto') return target;
const insert = target + (spot.at === 'after' ? 1 : 0);
return Math.min(last, insert > from ? insert - 1 : insert);
}
/** The rows of one block, top to bottom – a struct with its fields, an item with its nested list. */
export interface BlockSpan {
top: number;
bottom: number;
}
/**
* The gap a block dropped at `y` falls into, 0…count: past every block whose middle is above `y`.
* Blocks differ in height, so the middle of each is what the pointer has to cross.
*/
export function blockGap(y: number, spans: readonly BlockSpan[]): number {
return spans.filter((span) => (span.top + span.bottom) / 2 < y).length;
}
/** Where the line for `gap` is drawn: halfway between the blocks around it, on the edge at either end. */
export function gapLineY(gap: number, spans: readonly BlockSpan[]): number {
const above = spans[gap - 1];
const below = spans[gap];
if (above && below) return (above.bottom + below.top) / 2;
return above?.bottom ?? below?.top ?? 0;
}
/** The place block `from` moves to when dropped at `y`, or null where it would stay. */
export function dropSlot(y: number, spans: readonly BlockSpan[], from: number): number | null {
return dropMove(spans.length, from, blockGap(y, spans), false)?.to ?? null;
}
/**
* How far a scrolled column moves this frame while a drag holds the pointer near its top or bottom:
* the deeper into the `edge` band, the faster, up to `step`. Positive scrolls down.
*/
export function edgeScroll(y: number, top: number, height: number, edge = 24, step = 12): number {
const into = (depth: number) => Math.ceil((Math.min(edge, depth) / edge) * step);
if (y < top + edge) return -into(top + edge - y);
if (y > top + height - edge) return into(y - (top + height - edge));
return 0;
}