Files
wekan/models/lib/workspacesTree.js
T
Lauri Ojansivu 73a136bedd Workspaces nest: drop into one to make it a sub-workspace, and fold the tree.
A workspace held boards and, in the data, other workspaces - but a drag could
only ever say one thing about them, and the fold was not there at all.

WHERE IN A ROW it is dropped is now what the drop MEANS:

  the top quarter    - it becomes that row's previous sibling
  the bottom quarter - it becomes that row's next sibling
  the middle half    - it becomes that row's LAST CHILD, a sub-workspace

The middle is the biggest target on purpose: reordering can also be reached by
aiming at the neighbouring row's far edge, but nesting has only this one. Coming
back up is a drop like any other - a child dragged onto a root row's edge is a
root again - because nesting has to be undoable or a workspace put one level too
deep is stuck there.

THE PLACEHOLDER IS A SLOT, NOT A LINE. While the pointer is over a row an empty
slot a row high opens above it, below it, or - for "into this one" - INDENTED
underneath it, which is exactly where the workspace will appear. A line between
two rows is a target that has to be aimed at; a slot is a place to drop into.
The slot is a pseudo-element of the row, so the rows below shift down and the
row being aimed at stays where the pointer put it.

A workspace may not be dropped into itself or into its own descendant: the
subtree would be cut off from the root, taking every workspace under it. It is
refused by NOT calling preventDefault(), which is how HTML5 drag and drop says
no, so the cursor refuses while the workspace is still in the air rather than
the drop landing and quietly doing nothing.

AND THE DROP THAT DID NOTHING. With the handles off the drag starts on an
ANCHOR, and a browser puts that anchor's own text into text/plain. The drop read
text/plain first and took the workspace for a BOARD, so it assigned nothing and
the row snapped back - which is why the same drop worked with handles on, where
the drag starts from a span that carries no text. The drag clearData()s before
setting its own type, and the drop decides on the workspace id FIRST.

A CARET FOLDS a workspace's own workspaces away, at the start of the row -
before the drag handle, so the carets of a tree line up whatever the handles
toggle says. It is the control a list has on a board and the one the left menu
itself has, down to the two words in its tooltip, and it answers Enter and Space
because a tree that only opens with a mouse is a tree half the readers cannot
open. A workspace with nothing under it keeps a spacer of the same width, so a
row does not shift sideways the moment it gains its first child.

Open is the default and only the FOLDED ones are stored: a missing key is an
open workspace, so fifty workspaces with two folded is two keys. Remembered in
the same three layers as the rest of this menu - a Session value, then
profile.collapsedWorkspaces, then a cookie for a reader who is not signed in.
The server method takes the id into a DOTTED FIELD PATH and the tree is written
by the client, so the id is checked against ^[A-Za-z0-9_-]{1,64}$ first: one
carrying a `.` or a `$` would address a different field.

The depth is UNLIMITED because nothing counts it: the template draws itself for
a workspace's children, each level indenting by one caret's width with a logical
property, so a right-to-left tree indents from the right by itself.

What a drag does to the tree is a pure module with its own tests -
dropPosition() and moveWorkspace(), the guards included - so the rules can be
proved without a browser, and the page is pinned to calling them rather than
re-deriving them.

Thanks to xet7 !
2026-08-01 23:51:56 +03:00

201 lines
7.3 KiB
JavaScript

'use strict';
// The workspaces tree of the All Boards left menu: what a drag does to it.
//
// A workspace holds boards, and it holds other workspaces - to any depth. The
// tree is a plain array of `{ id, name, children }` on the user document
// (`profile.boardWorkspacesTree`), so every operation here is "given this tree,
// give me the tree it becomes", with no Meteor, no DOM and no collection.
//
// A drag of one workspace onto another used to mean exactly one thing: put it
// after the target, as a sibling. That is one of the three things a reader can
// mean, and the other two had no way to be said:
//
// before - dropped on the TOP edge of a row: becomes its previous sibling
// after - dropped on the BOTTOM edge: becomes its next sibling
// inside - dropped on the MIDDLE of a row: becomes its LAST CHILD, which
// is how a sub-workspace is made
//
// `dropPosition()` turns a pointer's height within a row into one of those, so
// the rule is one line of arithmetic that can be tested rather than three
// branches in an event handler.
//
// Two moves are refused, and both would corrupt the tree rather than merely
// look odd:
// * onto itself - nothing to do;
// * into its own descendant - the subtree would be cut off from the root and
// become unreachable, taking every workspace under it with it.
//
// Nothing here mutates its input: a move returns a new tree, so a caller can
// compare, discard, or hand it to the server without having already changed
// what is on screen.
//
// Design: docs/Design/Page/Workspaces.md
const BEFORE = 'before';
const AFTER = 'after';
const INSIDE = 'inside';
const POSITIONS = [BEFORE, INSIDE, AFTER];
// How much of a row's height, at each end, means "between the rows" rather than
// "into this one". A quarter each way leaves the middle HALF meaning `inside`,
// which is the gesture that needs the biggest target: reordering can also be
// done by aiming at the neighbouring gap, but nesting has only this one.
const EDGE_FRACTION = 0.25;
// Where in a row the pointer is, as one of the three positions.
//
// `offsetY` is measured from the TOP of the row and `height` is the row's own
// height. A row with no height yet - measured before layout - answers `inside`
// rather than dividing by zero.
function dropPosition(offsetY, height) {
// `== null` catches both null and undefined BEFORE Number() does: `Number(null)`
// is 0, which is a perfectly good offset meaning the very top of the row - so
// a missing offset would have read as a deliberate "before".
if (height == null || offsetY == null) return INSIDE;
const h = Number(height);
if (!Number.isFinite(h) || h <= 0) return INSIDE;
const y = Number(offsetY);
if (!Number.isFinite(y)) return INSIDE;
const edge = h * EDGE_FRACTION;
if (y < edge) return BEFORE;
if (y > h - edge) return AFTER;
return INSIDE;
}
function asNodes(nodes) {
return Array.isArray(nodes) ? nodes : [];
}
// The node with this id, anywhere in the tree.
function findNode(nodes, id) {
if (!id) return null;
for (const node of asNodes(nodes)) {
if (!node) continue;
if (node.id === id) return node;
const deeper = findNode(node.children, id);
if (deeper) return deeper;
}
return null;
}
// Is `id` the node itself, or anywhere below it? This is the guard that keeps a
// subtree attached to the root: dropping a workspace into its own child would
// leave both of them - and everything under them - in a ring that the tree no
// longer reaches.
function isSelfOrDescendant(node, id) {
if (!node || !id) return false;
if (node.id === id) return true;
return asNodes(node.children).some(child => isSelfOrDescendant(child, id));
}
// Every id in the tree, in the order they are drawn. Used by the callers that
// have to forget the state of workspaces that no longer exist.
function collectIds(nodes, out = []) {
for (const node of asNodes(nodes)) {
if (!node || !node.id) continue;
out.push(node.id);
collectIds(node.children, out);
}
return out;
}
function hasChildren(node) {
return asNodes(node && node.children).length > 0;
}
// Take the node out of the tree it is in, wherever it is. Returns the node, or
// null when the id names nothing; the tree passed in is modified, so callers
// clone first.
function detach(nodes, id) {
const list = asNodes(nodes);
for (let i = 0; i < list.length; i += 1) {
const node = list[i];
if (!node) continue;
if (node.id === id) return list.splice(i, 1)[0];
const removed = detach(node.children, id);
if (removed) return removed;
}
return null;
}
// Put `node` next to `targetId`, on the side `position` names. Returns true
// when the target was found.
function attachBeside(nodes, targetId, node, position) {
const list = asNodes(nodes);
for (let i = 0; i < list.length; i += 1) {
const current = list[i];
if (!current) continue;
if (current.id === targetId) {
list.splice(position === BEFORE ? i : i + 1, 0, node);
return true;
}
if (attachBeside(current.children, targetId, node, position)) return true;
}
return false;
}
// Put `node` inside `targetId`, as its LAST child - the end of the list, where a
// thing you have just added is looked for. A target with no `children` yet gets
// one; this is what makes a plain workspace into a parent.
function attachInside(nodes, targetId, node) {
const target = findNode(nodes, targetId);
if (!target) return false;
if (!Array.isArray(target.children)) target.children = [];
target.children.push(node);
return true;
}
// The tree after dragging `draggedId` onto `targetId` at `position`, or NULL
// when the move is refused or changes nothing. Null rather than the tree it was
// given, so a caller can tell "nothing to save" from "here is the new tree"
// without comparing two trees.
function moveWorkspace(tree, draggedId, targetId, position) {
const nodes = asNodes(tree);
if (!draggedId || !targetId || draggedId === targetId) return null;
if (!POSITIONS.includes(position)) return null;
const dragged = findNode(nodes, draggedId);
const target = findNode(nodes, targetId);
if (!dragged || !target) return null;
// Into itself or its own descendant: refused. See isSelfOrDescendant.
if (isSelfOrDescendant(dragged, targetId)) return null;
// Deep copy, so the tree on screen is untouched until the caller says so.
// JSON round-trip rather than a library: these nodes are plain data - ids,
// names, colours, children - which is exactly what it preserves.
const next = JSON.parse(JSON.stringify(nodes));
const moved = detach(next, draggedId);
if (!moved) return null;
const placed = position === INSIDE
? attachInside(next, targetId, moved)
: attachBeside(next, targetId, moved, position);
if (!placed) return null;
return next;
}
// Would this move change anything? A drop that puts a workspace back exactly
// where it already was is a no-op, and saving it would write the same tree to
// the server and re-render for nothing.
function isNoOpMove(tree, draggedId, targetId, position) {
const next = moveWorkspace(tree, draggedId, targetId, position);
if (!next) return true;
return JSON.stringify(next) === JSON.stringify(asNodes(tree));
}
module.exports = {
BEFORE,
AFTER,
INSIDE,
POSITIONS,
EDGE_FRACTION,
dropPosition,
findNode,
isSelfOrDescendant,
collectIds,
hasChildren,
moveWorkspace,
isNoOpMove,
};