Skip to content

feat: group and ungroup the selected objects - #217

Open
tbolis wants to merge 1 commit into
release/1.2.0from
feature/group-ungroup
Open

tbolis wants to merge 1 commit into
release/1.2.0from
feature/group-ungroup

Conversation

@tbolis

@tbolis tbolis commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Refs #42 (not closed by this: the issue can be answered once it is released).

Change

The issue asks for fabric's group: select some objects and group them.

  • group() makes one object of the selected objects (two or more) and selects it. ungroup() takes the selected group apart and selects its objects, which stay where they are on the canvas, with the move, the scale and the turn which the group gave them.
  • canGroup() and canUngroup() tell whether they will do something, for example to enable a button.
  • A group is its own class, SketchGroup, saved with its type and loaded as one by value, defaultValue and fromJSON(). The group of the arrow tool is a plain group, so it is never taken apart.
  • Each is one undo step. The history puts the group back as the same object (an emptied group is filled again, a new one is not made), so the modifications made on the group can still be undone after an ungroup and its undo, or after an undo and a redo.
  • The group takes the place of the topmost of its objects, so what is above it stays above it. Undo puts each object back in its own place.
  • The text box of a rectangle label goes into the group with its rectangle.
  • Like undo and redo, they call onChange and not onObjectAdded.
  • Showcase: Group and Ungroup buttons in the selection panel, with Ctrl G and Ctrl Shift G, also in the shortcuts dialog. The action buttons of the panel are small now: with five of them in a row, a panel which scrolls (a short window, with scrollbars which take room) got a horizontal scrollbar.
  • README (a "Groups" section and the method table), the onChange note in types.ts, and the CHANGELOG.

Verification

  • 23 new tests in history/grouping.spec.tsx: can group and ungroup, the group (selected, objects kept where they are, place among the objects, order, undo, redo, no-ops, onChange), a group which is changed (move, undo and redo of it; ungroup, its undo and the undo of the move; redo of all of it), ungroup (position, move, scale and turn of the group, undo and redo, a group in a group, onChange), a group among the objects (saved and loaded as a SketchGroup, copied, removed and put back), and a rectangle with a label. Three deliberate breakages of the code (the place of the group, the text box of the label, a new group instead of the same one) each fail some of them.
  • Whole suite: 543 passed on React 19 and on React 18. Format, lint, typecheck and yarn build pass.
  • In headless Chrome on the built showcase, 16 checks pass: marquee-select two objects, group (nothing moves), a click on one selects the whole group, move, undo and redo, ungroup (nothing moves, then the circle moves alone), undo of the ungroup brings the group back, and the buttons are enabled and disabled as canGroup() and canUngroup() say. The horizontal scrollbar of the panel was reproduced with a short window and 15 pixel scrollbars, and is gone.

Notes

  • A group is one object to the select tool: its objects cannot be selected or changed until it is taken apart. Entering a group by a double click is not part of this.
  • ungroup() takes apart one selected group, not several at once.
  • The eraser cuts a group as a whole (the objects are erasable, not "deep"), so taking the group apart gives back what was erased from it. This is in the README, and it has no test.
  • Undo and redo of a group or an ungroup leave nothing selected.
  • feat: add the bucket fill tool #214 also changes SketchHistory.ts (the entries of the history), CHANGELOG.md and the showcase store and actions, so the second of the two to merge needs a conflict resolution.

group() makes one object of the selected objects, and ungroup() takes it apart.
canGroup() and canUngroup() tell whether they can be used. A group is a class of
its own (SketchGroup), so that it is saved and loaded as a group, and the group
of an arrow is not taken apart.

Both are one undo step. The history puts the group back as the same object, so
the modifications made on it can still be undone. The group takes the place of
the topmost of its objects, and the text box of a rectangle label goes into the
group with its rectangle.

The showcase has Group and Ungroup buttons in the selection panel, with Ctrl G
and Ctrl Shift G. The buttons of the panel are small, so that the row of five
fits in the panel when it scrolls.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant