tldraw 5.4.0-canary.e10ff229bb14 → 5.4.0-next.09eca5dee30d

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/DOCS.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # tldraw Documentation
2
2
 
3
- Version: `5.4.0-canary.e10ff229bb14`
3
+ Version: `5.4.0-next.09eca5dee30d`
4
4
 
5
5
  This file is generated during package publishing from the tldraw docs content for this exact package version.
6
6
 
package/RELEASE_NOTES.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Releases
2
2
 
3
- Version: `5.4.0-canary.e10ff229bb14`
3
+ Version: `5.4.0-next.09eca5dee30d`
4
4
 
5
5
  This file is generated during package publishing from the tldraw release notes content for this exact package version.
6
6
 
package/dist-cjs/index.js CHANGED
@@ -653,7 +653,7 @@ var import_buildFromV1Document = require("./lib/utils/tldr/buildFromV1Document")
653
653
  var import_file = require("./lib/utils/tldr/file");
654
654
  (0, import_editor.registerTldrawLibraryVersion)(
655
655
  "tldraw",
656
- "5.4.0-canary.e10ff229bb14",
656
+ "5.4.0-next.09eca5dee30d",
657
657
  "cjs"
658
658
  );
659
659
  //# sourceMappingURL=index.js.map
@@ -41,6 +41,8 @@ const DefaultActionsMenu = (0, import_react.memo)(function DefaultActionsMenu2({
41
41
  const breakpoint = (0, import_breakpoints.useBreakpoint)();
42
42
  const isReadonlyMode = (0, import_useReadonly.useReadonly)();
43
43
  const { orientation } = (0, import_layout.useTldrawUiOrientation)();
44
+ const ref = (0, import_react.useRef)(null);
45
+ (0, import_editor.usePassThroughWheelEvents)(ref);
44
46
  const editor = (0, import_editor.useEditor)();
45
47
  const isInAcceptableReadonlyState = (0, import_editor.useValue)(
46
48
  "should display quick actions when in readonly",
@@ -73,6 +75,7 @@ const DefaultActionsMenu = (0, import_react.memo)(function DefaultActionsMenu2({
73
75
  children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
74
76
  import_TldrawUiToolbar.TldrawUiToolbar,
75
77
  {
78
+ ref,
76
79
  label: msg("actions-menu.title"),
77
80
  className: "tlui-actions-menu",
78
81
  "data-testid": "actions-menu.content",
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../src/lib/ui/components/ActionsMenu/DefaultActionsMenu.tsx"],
4
- "sourcesContent": ["import { useEditor, useValue } from '@tldraw/editor'\nimport { ReactNode, memo } from 'react'\nimport { PORTRAIT_BREAKPOINT } from '../../constants'\nimport { useBreakpoint } from '../../context/breakpoints'\nimport { useReadonly } from '../../hooks/useReadonly'\nimport { useTranslation } from '../../hooks/useTranslation/useTranslation'\nimport { TldrawUiButtonIcon } from '../primitives/Button/TldrawUiButtonIcon'\nimport { useTldrawUiOrientation } from '../primitives/layout'\nimport { TldrawUiMenuContextProvider } from '../primitives/menus/TldrawUiMenuContext'\nimport {\n\tTldrawUiPopover,\n\tTldrawUiPopoverContent,\n\tTldrawUiPopoverTrigger,\n} from '../primitives/TldrawUiPopover'\nimport { TldrawUiToolbar, TldrawUiToolbarButton } from '../primitives/TldrawUiToolbar'\nimport { DefaultActionsMenuContent } from './DefaultActionsMenuContent'\n\n/** @public */\nexport interface TLUiActionsMenuProps {\n\tchildren?: ReactNode\n}\n\n/** @public @react */\nexport const DefaultActionsMenu = memo(function DefaultActionsMenu({\n\tchildren,\n}: TLUiActionsMenuProps) {\n\tconst msg = useTranslation()\n\tconst breakpoint = useBreakpoint()\n\tconst isReadonlyMode = useReadonly()\n\tconst { orientation } = useTldrawUiOrientation()\n\n\tconst editor = useEditor()\n\tconst isInAcceptableReadonlyState = useValue(\n\t\t'should display quick actions when in readonly',\n\t\t() => editor.isInAny('hand', 'zoom'),\n\t\t[editor]\n\t)\n\n\t// Get the actions menu content, either the default component or the user's\n\t// override. If there's no menu content, then the user has set it to null,\n\t// so skip rendering the menu.\n\n\tconst content = children ?? <DefaultActionsMenuContent />\n\tif (isReadonlyMode && !isInAcceptableReadonlyState) return\n\n\treturn (\n\t\t<TldrawUiPopover id=\"actions-menu\">\n\t\t\t<TldrawUiPopoverTrigger>\n\t\t\t\t<TldrawUiToolbarButton\n\t\t\t\t\ttype=\"icon\"\n\t\t\t\t\tdata-testid=\"actions-menu.button\"\n\t\t\t\t\ttitle={msg('actions-menu.title')}\n\t\t\t\t>\n\t\t\t\t\t<TldrawUiButtonIcon\n\t\t\t\t\t\ticon={orientation === 'horizontal' ? 'dots-vertical' : 'dots-horizontal'}\n\t\t\t\t\t\tsmall\n\t\t\t\t\t/>\n\t\t\t\t</TldrawUiToolbarButton>\n\t\t\t</TldrawUiPopoverTrigger>\n\t\t\t<TldrawUiPopoverContent\n\t\t\t\tside={\n\t\t\t\t\torientation === 'horizontal'\n\t\t\t\t\t\t? breakpoint >= PORTRAIT_BREAKPOINT.TABLET\n\t\t\t\t\t\t\t? 'bottom'\n\t\t\t\t\t\t\t: 'top'\n\t\t\t\t\t\t: 'right'\n\t\t\t\t}\n\t\t\t\tsideOffset={6}\n\t\t\t>\n\t\t\t\t<TldrawUiToolbar\n\t\t\t\t\tlabel={msg('actions-menu.title')}\n\t\t\t\t\tclassName=\"tlui-actions-menu\"\n\t\t\t\t\tdata-testid=\"actions-menu.content\"\n\t\t\t\t\torientation=\"grid\"\n\t\t\t\t>\n\t\t\t\t\t<TldrawUiMenuContextProvider type=\"icons\" sourceId=\"actions-menu\">\n\t\t\t\t\t\t{content}\n\t\t\t\t\t</TldrawUiMenuContextProvider>\n\t\t\t\t</TldrawUiToolbar>\n\t\t\t</TldrawUiPopoverContent>\n\t\t</TldrawUiPopover>\n\t)\n})\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA0C6B;AA1C7B,oBAAoC;AACpC,mBAAgC;AAChC,uBAAoC;AACpC,yBAA8B;AAC9B,yBAA4B;AAC5B,4BAA+B;AAC/B,gCAAmC;AACnC,oBAAuC;AACvC,iCAA4C;AAC5C,6BAIO;AACP,6BAAuD;AACvD,uCAA0C;AAQnC,MAAM,yBAAqB,mBAAK,SAASA,oBAAmB;AAAA,EAClE;AACD,GAAyB;AACxB,QAAM,UAAM,sCAAe;AAC3B,QAAM,iBAAa,kCAAc;AACjC,QAAM,qBAAiB,gCAAY;AACnC,QAAM,EAAE,YAAY,QAAI,sCAAuB;AAE/C,QAAM,aAAS,yBAAU;AACzB,QAAM,kCAA8B;AAAA,IACnC;AAAA,IACA,MAAM,OAAO,QAAQ,QAAQ,MAAM;AAAA,IACnC,CAAC,MAAM;AAAA,EACR;AAMA,QAAM,UAAU,YAAY,4CAAC,8DAA0B;AACvD,MAAI,kBAAkB,CAAC,4BAA6B;AAEpD,SACC,6CAAC,0CAAgB,IAAG,gBACnB;AAAA,gDAAC,iDACA;AAAA,MAAC;AAAA;AAAA,QACA,MAAK;AAAA,QACL,eAAY;AAAA,QACZ,OAAO,IAAI,oBAAoB;AAAA,QAE/B;AAAA,UAAC;AAAA;AAAA,YACA,MAAM,gBAAgB,eAAe,kBAAkB;AAAA,YACvD,OAAK;AAAA;AAAA,QACN;AAAA;AAAA,IACD,GACD;AAAA,IACA;AAAA,MAAC;AAAA;AAAA,QACA,MACC,gBAAgB,eACb,cAAc,qCAAoB,SACjC,WACA,QACD;AAAA,QAEJ,YAAY;AAAA,QAEZ;AAAA,UAAC;AAAA;AAAA,YACA,OAAO,IAAI,oBAAoB;AAAA,YAC/B,WAAU;AAAA,YACV,eAAY;AAAA,YACZ,aAAY;AAAA,YAEZ,sDAAC,0DAA4B,MAAK,SAAQ,UAAS,gBACjD,mBACF;AAAA;AAAA,QACD;AAAA;AAAA,IACD;AAAA,KACD;AAEF,CAAC;",
4
+ "sourcesContent": ["import { useEditor, usePassThroughWheelEvents, useValue } from '@tldraw/editor'\nimport { ReactNode, memo, useRef } from 'react'\nimport { PORTRAIT_BREAKPOINT } from '../../constants'\nimport { useBreakpoint } from '../../context/breakpoints'\nimport { useReadonly } from '../../hooks/useReadonly'\nimport { useTranslation } from '../../hooks/useTranslation/useTranslation'\nimport { TldrawUiButtonIcon } from '../primitives/Button/TldrawUiButtonIcon'\nimport { useTldrawUiOrientation } from '../primitives/layout'\nimport { TldrawUiMenuContextProvider } from '../primitives/menus/TldrawUiMenuContext'\nimport {\n\tTldrawUiPopover,\n\tTldrawUiPopoverContent,\n\tTldrawUiPopoverTrigger,\n} from '../primitives/TldrawUiPopover'\nimport { TldrawUiToolbar, TldrawUiToolbarButton } from '../primitives/TldrawUiToolbar'\nimport { DefaultActionsMenuContent } from './DefaultActionsMenuContent'\n\n/** @public */\nexport interface TLUiActionsMenuProps {\n\tchildren?: ReactNode\n}\n\n/** @public @react */\nexport const DefaultActionsMenu = memo(function DefaultActionsMenu({\n\tchildren,\n}: TLUiActionsMenuProps) {\n\tconst msg = useTranslation()\n\tconst breakpoint = useBreakpoint()\n\tconst isReadonlyMode = useReadonly()\n\tconst { orientation } = useTldrawUiOrientation()\n\n\tconst ref = useRef<HTMLDivElement>(null)\n\tusePassThroughWheelEvents(ref)\n\n\tconst editor = useEditor()\n\tconst isInAcceptableReadonlyState = useValue(\n\t\t'should display quick actions when in readonly',\n\t\t() => editor.isInAny('hand', 'zoom'),\n\t\t[editor]\n\t)\n\n\t// Get the actions menu content, either the default component or the user's\n\t// override. If there's no menu content, then the user has set it to null,\n\t// so skip rendering the menu.\n\n\tconst content = children ?? <DefaultActionsMenuContent />\n\tif (isReadonlyMode && !isInAcceptableReadonlyState) return\n\n\treturn (\n\t\t<TldrawUiPopover id=\"actions-menu\">\n\t\t\t<TldrawUiPopoverTrigger>\n\t\t\t\t<TldrawUiToolbarButton\n\t\t\t\t\ttype=\"icon\"\n\t\t\t\t\tdata-testid=\"actions-menu.button\"\n\t\t\t\t\ttitle={msg('actions-menu.title')}\n\t\t\t\t>\n\t\t\t\t\t<TldrawUiButtonIcon\n\t\t\t\t\t\ticon={orientation === 'horizontal' ? 'dots-vertical' : 'dots-horizontal'}\n\t\t\t\t\t\tsmall\n\t\t\t\t\t/>\n\t\t\t\t</TldrawUiToolbarButton>\n\t\t\t</TldrawUiPopoverTrigger>\n\t\t\t<TldrawUiPopoverContent\n\t\t\t\tside={\n\t\t\t\t\torientation === 'horizontal'\n\t\t\t\t\t\t? breakpoint >= PORTRAIT_BREAKPOINT.TABLET\n\t\t\t\t\t\t\t? 'bottom'\n\t\t\t\t\t\t\t: 'top'\n\t\t\t\t\t\t: 'right'\n\t\t\t\t}\n\t\t\t\tsideOffset={6}\n\t\t\t>\n\t\t\t\t<TldrawUiToolbar\n\t\t\t\t\tref={ref}\n\t\t\t\t\tlabel={msg('actions-menu.title')}\n\t\t\t\t\tclassName=\"tlui-actions-menu\"\n\t\t\t\t\tdata-testid=\"actions-menu.content\"\n\t\t\t\t\torientation=\"grid\"\n\t\t\t\t>\n\t\t\t\t\t<TldrawUiMenuContextProvider type=\"icons\" sourceId=\"actions-menu\">\n\t\t\t\t\t\t{content}\n\t\t\t\t\t</TldrawUiMenuContextProvider>\n\t\t\t\t</TldrawUiToolbar>\n\t\t\t</TldrawUiPopoverContent>\n\t\t</TldrawUiPopover>\n\t)\n})\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA6C6B;AA7C7B,oBAA+D;AAC/D,mBAAwC;AACxC,uBAAoC;AACpC,yBAA8B;AAC9B,yBAA4B;AAC5B,4BAA+B;AAC/B,gCAAmC;AACnC,oBAAuC;AACvC,iCAA4C;AAC5C,6BAIO;AACP,6BAAuD;AACvD,uCAA0C;AAQnC,MAAM,yBAAqB,mBAAK,SAASA,oBAAmB;AAAA,EAClE;AACD,GAAyB;AACxB,QAAM,UAAM,sCAAe;AAC3B,QAAM,iBAAa,kCAAc;AACjC,QAAM,qBAAiB,gCAAY;AACnC,QAAM,EAAE,YAAY,QAAI,sCAAuB;AAE/C,QAAM,UAAM,qBAAuB,IAAI;AACvC,+CAA0B,GAAG;AAE7B,QAAM,aAAS,yBAAU;AACzB,QAAM,kCAA8B;AAAA,IACnC;AAAA,IACA,MAAM,OAAO,QAAQ,QAAQ,MAAM;AAAA,IACnC,CAAC,MAAM;AAAA,EACR;AAMA,QAAM,UAAU,YAAY,4CAAC,8DAA0B;AACvD,MAAI,kBAAkB,CAAC,4BAA6B;AAEpD,SACC,6CAAC,0CAAgB,IAAG,gBACnB;AAAA,gDAAC,iDACA;AAAA,MAAC;AAAA;AAAA,QACA,MAAK;AAAA,QACL,eAAY;AAAA,QACZ,OAAO,IAAI,oBAAoB;AAAA,QAE/B;AAAA,UAAC;AAAA;AAAA,YACA,MAAM,gBAAgB,eAAe,kBAAkB;AAAA,YACvD,OAAK;AAAA;AAAA,QACN;AAAA;AAAA,IACD,GACD;AAAA,IACA;AAAA,MAAC;AAAA;AAAA,QACA,MACC,gBAAgB,eACb,cAAc,qCAAoB,SACjC,WACA,QACD;AAAA,QAEJ,YAAY;AAAA,QAEZ;AAAA,UAAC;AAAA;AAAA,YACA;AAAA,YACA,OAAO,IAAI,oBAAoB;AAAA,YAC/B,WAAU;AAAA,YACV,eAAY;AAAA,YACZ,aAAY;AAAA,YAEZ,sDAAC,0DAA4B,MAAK,SAAQ,UAAS,gBACjD,mBACF;AAAA;AAAA,QACD;AAAA;AAAA,IACD;AAAA,KACD;AAEF,CAAC;",
6
6
  "names": ["DefaultActionsMenu"]
7
7
  }
@@ -48,53 +48,31 @@ async function flattenShapesToImages(editor, shapeIds, flattenImageBoundsExpand)
48
48
  bounds: editor.getShapeMaskedPageBounds(shape).clone().expandBy(flattenImageBoundsExpand)
49
49
  };
50
50
  });
51
- const parents = expandedBounds.map((_, i) => i);
52
- const find = (i) => {
53
- let root = i;
54
- while (parents[root] !== root) root = parents[root];
55
- while (parents[i] !== root) {
56
- const next = parents[i];
57
- parents[i] = root;
58
- i = next;
51
+ for (let i = 0; i < expandedBounds.length; i++) {
52
+ const item = expandedBounds[i];
53
+ if (i === 0) {
54
+ groups[0] = {
55
+ shapes: [item.shape],
56
+ bounds: item.bounds
57
+ };
58
+ continue;
59
59
  }
60
- return root;
61
- };
62
- const union = (i, j) => {
63
- const ri = find(i);
64
- const rj = find(j);
65
- if (ri !== rj) parents[ri] = rj;
66
- };
67
- const order = expandedBounds.map((_, i) => i).sort((a, b) => {
68
- return expandedBounds[a].bounds.minX - expandedBounds[b].bounds.minX;
69
- });
70
- for (let a = 0; a < order.length; a++) {
71
- const i = order[a];
72
- const boundsI = expandedBounds[i].bounds;
73
- for (let b = a + 1; b < order.length; b++) {
74
- const j = order[b];
75
- if (expandedBounds[j].bounds.minX > boundsI.maxX) break;
76
- if (find(i) === find(j)) continue;
77
- if (boundsI.includes(expandedBounds[j].bounds)) {
78
- union(i, j);
60
+ let didLand = false;
61
+ for (const group of groups) {
62
+ if (group.bounds.includes(item.bounds)) {
63
+ group.shapes.push(item.shape);
64
+ group.bounds.expand(item.bounds);
65
+ didLand = true;
66
+ break;
79
67
  }
80
68
  }
81
- }
82
- const groupsByRoot = /* @__PURE__ */ new Map();
83
- for (let i = 0; i < expandedBounds.length; i++) {
84
- const root = find(i);
85
- const item = expandedBounds[i];
86
- const existing = groupsByRoot.get(root);
87
- if (existing) {
88
- existing.shapes.push(item.shape);
89
- existing.bounds.expand(item.bounds);
90
- } else {
91
- groupsByRoot.set(root, {
69
+ if (!didLand) {
70
+ groups.push({
92
71
  shapes: [item.shape],
93
- bounds: item.bounds.clone()
72
+ bounds: item.bounds
94
73
  });
95
74
  }
96
75
  }
97
- groups.push(...groupsByRoot.values());
98
76
  } else {
99
77
  const bounds = import_editor.Box.Common(shapes.map((shape) => editor.getShapeMaskedPageBounds(shape)));
100
78
  groups.push({
@@ -128,8 +106,11 @@ async function flattenShapesToImages(editor, shapeIds, flattenImageBoundsExpand)
128
106
  if (!commonAncestorId) continue;
129
107
  let index = "a1";
130
108
  for (const shape of shapes2) {
131
- if (shape.parentId === commonAncestorId && shape.index > index) {
132
- index = shape.index;
109
+ if (shape.parentId === commonAncestorId) {
110
+ if (shape.index > index) {
111
+ index = shape.index;
112
+ }
113
+ break;
133
114
  }
134
115
  }
135
116
  let x;
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/lib/ui/hooks/useFlatten.ts"],
4
- "sourcesContent": ["import {\n\tBox,\n\tEditor,\n\tIndexKey,\n\tTLImageAsset,\n\tTLShape,\n\tTLShapeId,\n\tVec,\n\tcompact,\n\tcreateShapeId,\n\tisShapeId,\n\ttransact,\n\tuseEditor,\n} from '@tldraw/editor'\nimport { useCallback } from 'react'\n\nexport async function flattenShapesToImages(\n\teditor: Editor,\n\tshapeIds: TLShapeId[],\n\tflattenImageBoundsExpand?: number\n) {\n\tconst shapes = compact(\n\t\tshapeIds.map((id) => {\n\t\t\tconst shape = editor.getShape(id)\n\t\t\tif (!shape) return\n\t\t\tconst util = editor.getShapeUtil(shape.type)\n\t\t\t// skip shapes that don't have a toSvg method\n\t\t\tif (util.toSvg === undefined) return\n\t\t\treturn shape\n\t\t})\n\t)\n\n\tif (shapes.length === 0) return\n\n\t// Don't flatten if it's just one image\n\tif (shapes.length === 1) {\n\t\tconst shape = shapes[0]\n\t\tif (!shape) return\n\t\tif (editor.isShapeOfType(shape, 'image')) return\n\t}\n\n\tconst groups: { shapes: TLShape[]; bounds: Box; asset?: TLImageAsset }[] = []\n\n\tif (flattenImageBoundsExpand !== undefined) {\n\t\tconst expandedBounds = shapes.map((shape) => {\n\t\t\treturn {\n\t\t\t\tshape,\n\t\t\t\tbounds: editor.getShapeMaskedPageBounds(shape)!.clone().expandBy(flattenImageBoundsExpand),\n\t\t\t}\n\t\t})\n\n\t\t// Cluster shapes by connected components: any two shapes whose expanded\n\t\t// bounds intersect end up in the same group. Using union-find here\n\t\t// instead of a greedy first-fit pass means the result is independent of\n\t\t// the order shapes appear in `shapeIds`, and a shape that bridges two\n\t\t// otherwise-disjoint clusters merges them in one pass.\n\t\tconst parents = expandedBounds.map((_, i) => i)\n\t\tconst find = (i: number): number => {\n\t\t\tlet root = i\n\t\t\twhile (parents[root] !== root) root = parents[root]\n\t\t\twhile (parents[i] !== root) {\n\t\t\t\tconst next = parents[i]\n\t\t\t\tparents[i] = root\n\t\t\t\ti = next\n\t\t\t}\n\t\t\treturn root\n\t\t}\n\t\tconst union = (i: number, j: number) => {\n\t\t\tconst ri = find(i)\n\t\t\tconst rj = find(j)\n\t\t\tif (ri !== rj) parents[ri] = rj\n\t\t}\n\n\t\t// Broad-phase sweep: sort by minX and, for each box, only compare against\n\t\t// later boxes until one starts past the current box's maxX. Boxes that\n\t\t// don't overlap on the x-axis can't intersect, so this skips the bulk of\n\t\t// the pairwise checks for spread-out selections while still finding every\n\t\t// intersecting pair (Box.includes is symmetric, so each pair is tested once).\n\t\tconst order = expandedBounds\n\t\t\t.map((_, i) => i)\n\t\t\t.sort((a, b) => {\n\t\t\t\treturn expandedBounds[a].bounds.minX - expandedBounds[b].bounds.minX\n\t\t\t})\n\n\t\tfor (let a = 0; a < order.length; a++) {\n\t\t\tconst i = order[a]\n\t\t\tconst boundsI = expandedBounds[i].bounds\n\t\t\tfor (let b = a + 1; b < order.length; b++) {\n\t\t\t\tconst j = order[b]\n\t\t\t\t// sorted by minX, so once a box starts past i's maxX no later box can overlap i\n\t\t\t\tif (expandedBounds[j].bounds.minX > boundsI.maxX) break\n\t\t\t\tif (find(i) === find(j)) continue\n\t\t\t\tif (boundsI.includes(expandedBounds[j].bounds)) {\n\t\t\t\t\tunion(i, j)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst groupsByRoot = new Map<number, { shapes: TLShape[]; bounds: Box }>()\n\t\tfor (let i = 0; i < expandedBounds.length; i++) {\n\t\t\tconst root = find(i)\n\t\t\tconst item = expandedBounds[i]\n\t\t\tconst existing = groupsByRoot.get(root)\n\t\t\tif (existing) {\n\t\t\t\texisting.shapes.push(item.shape)\n\t\t\t\texisting.bounds.expand(item.bounds)\n\t\t\t} else {\n\t\t\t\tgroupsByRoot.set(root, {\n\t\t\t\t\tshapes: [item.shape],\n\t\t\t\t\tbounds: item.bounds.clone(),\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\n\t\tgroups.push(...groupsByRoot.values())\n\t} else {\n\t\tconst bounds = Box.Common(shapes.map((shape) => editor.getShapeMaskedPageBounds(shape)!))\n\t\tgroups.push({\n\t\t\tshapes,\n\t\t\tbounds,\n\t\t})\n\t}\n\n\tconst padding = editor.options.flattenImageBoundsPadding\n\n\tfor (const group of groups) {\n\t\tif (flattenImageBoundsExpand !== undefined) {\n\t\t\t// shrink the bounds again, removing the expanded area\n\t\t\tgroup.bounds.expandBy(-flattenImageBoundsExpand)\n\t\t}\n\n\t\t// get an image for the shapes\n\t\tconst svgResult = await editor.getSvgString(group.shapes, {\n\t\t\tpadding,\n\t\t\tbackground: false,\n\t\t})\n\t\tif (!svgResult?.svg) continue\n\n\t\t// get an image asset for the image\n\t\tconst asset = (await editor.getAssetForExternalContent({\n\t\t\ttype: 'file',\n\t\t\tfile: new File([svgResult.svg], 'asset.svg', { type: 'image/svg+xml' }),\n\t\t})) as TLImageAsset\n\t\tif (!asset) continue\n\n\t\t// add it to the group\n\t\tgroup.asset = asset\n\t}\n\n\tconst createdShapeIds: TLShapeId[] = []\n\n\ttransact(() => {\n\t\tfor (const group of groups) {\n\t\t\tconst { asset, bounds, shapes } = group\n\t\t\tif (!asset) continue\n\n\t\t\tconst commonAncestorId = editor.findCommonAncestor(shapes) ?? editor.getCurrentPageId()\n\t\t\tif (!commonAncestorId) continue\n\n\t\t\tlet index: IndexKey = 'a1' as IndexKey\n\t\t\tfor (const shape of shapes) {\n\t\t\t\tif (shape.parentId === commonAncestorId && shape.index > index) {\n\t\t\t\t\tindex = shape.index\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet x: number\n\t\t\tlet y: number\n\t\t\tlet rotation: number\n\n\t\t\tif (isShapeId(commonAncestorId)) {\n\t\t\t\tconst commonAncestor = editor.getShape(commonAncestorId)\n\t\t\t\tif (!commonAncestor) continue\n\t\t\t\t// put the point in the parent's space\n\t\t\t\tconst point = editor.getPointInShapeSpace(commonAncestor, {\n\t\t\t\t\tx: bounds.x,\n\t\t\t\t\ty: bounds.y,\n\t\t\t\t})\n\t\t\t\t// get the parent's rotation\n\t\t\t\trotation = editor.getShapePageTransform(commonAncestorId).rotation()\n\t\t\t\t// rotate the point against the parent's rotation\n\t\t\t\tpoint.sub(new Vec(padding, padding).rot(-rotation))\n\t\t\t\tx = point.x\n\t\t\t\ty = point.y\n\t\t\t} else {\n\t\t\t\t// if the common ancestor is the page, then just adjust for the padding\n\t\t\t\tx = bounds.x - padding\n\t\t\t\ty = bounds.y - padding\n\t\t\t\trotation = 0\n\t\t\t}\n\n\t\t\t// delete the shapes\n\t\t\teditor.deleteShapes(shapes)\n\n\t\t\t// create the asset\n\t\t\teditor.createAssets([{ ...asset, id: asset.id }])\n\n\t\t\tconst shapeId = createShapeId()\n\n\t\t\t// create an image shape in the same place as the shapes\n\t\t\teditor.createShape({\n\t\t\t\tid: shapeId,\n\t\t\t\ttype: 'image',\n\t\t\t\tindex,\n\t\t\t\tparentId: commonAncestorId,\n\t\t\t\tx,\n\t\t\t\ty,\n\t\t\t\trotation: -rotation,\n\t\t\t\tprops: {\n\t\t\t\t\tassetId: asset.id,\n\t\t\t\t\tw: bounds.w + padding * 2,\n\t\t\t\t\th: bounds.h + padding * 2,\n\t\t\t\t},\n\t\t\t})\n\n\t\t\tcreatedShapeIds.push(shapeId)\n\t\t}\n\t})\n\n\treturn createdShapeIds\n}\n\nexport function useFlatten() {\n\tconst editor = useEditor()\n\treturn useCallback(\n\t\t(ids: TLShapeId[]) => {\n\t\t\treturn flattenShapesToImages(editor, ids)\n\t\t},\n\t\t[editor]\n\t)\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAaO;AACP,mBAA4B;AAE5B,eAAsB,sBACrB,QACA,UACA,0BACC;AACD,QAAM,aAAS;AAAA,IACd,SAAS,IAAI,CAAC,OAAO;AACpB,YAAM,QA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AM,cAAU,6BAAc;AAG9B,aAAO,YAAY;AAAA,QAClB,IAAI;AAAA,QACJ,MAAM;AAAA,QACN;AAAA,QACA,UAAU;AAAA,QACV;AAAA,QACA;AAAA,QACA,UAAU,CAAC;AAAA,QACX,OAAO;AAAA,UACN,SAAS,MAAM;AAAA,UACf,GAAG,OAAO,IAAI,UAAU;AAAA,UACxB,GAAG,OAAO,IAAI,UAAU;AAAA,QACzB;AAAA,MACD,CAAC;AAED,sBAAgB,KAAK,OAAO;AAAA,IAC7B;AAAA,EACD,CAAC;AAED,SAAO;AACR;AAEO,SAAS,aAAa;AAC5B,QAAM,aAAS,yBAAU;AACzB,aAAO;AAAA,IACN,CAAC,QAAqB;AACrB,aAAO,sBAAsB,QAAQ,GAAG;AAAA,IACzC;AAAA,IACA,CAAC,MAAM;AAAA,EACR;AACD;",
4
+ "sourcesContent": ["import {\n\tBox,\n\tEditor,\n\tIndexKey,\n\tTLImageAsset,\n\tTLShape,\n\tTLShapeId,\n\tVec,\n\tcompact,\n\tcreateShapeId,\n\tisShapeId,\n\ttransact,\n\tuseEditor,\n} from '@tldraw/editor'\nimport { useCallback } from 'react'\n\nexport async function flattenShapesToImages(\n\teditor: Editor,\n\tshapeIds: TLShapeId[],\n\tflattenImageBoundsExpand?: number\n) {\n\tconst shapes = compact(\n\t\tshapeIds.map((id) => {\n\t\t\tconst shape = editor.getShape(id)\n\t\t\tif (!shape) return\n\t\t\tconst util = editor.getShapeUtil(shape.type)\n\t\t\t// skip shapes that don't have a toSvg method\n\t\t\tif (util.toSvg === undefined) return\n\t\t\treturn shape\n\t\t})\n\t)\n\n\tif (shapes.length === 0) return\n\n\t// Don't flatten if it's just one image\n\tif (shapes.length === 1) {\n\t\tconst shape = shapes[0]\n\t\tif (!shape) return\n\t\tif (editor.isShapeOfType(shape, 'image')) return\n\t}\n\n\tconst groups: { shapes: TLShape[]; bounds: Box; asset?: TLImageAsset }[] = []\n\n\tif (flattenImageBoundsExpand !== undefined) {\n\t\tconst expandedBounds = shapes.map((shape) => {\n\t\t\treturn {\n\t\t\t\tshape,\n\t\t\t\tbounds: editor.getShapeMaskedPageBounds(shape)!.clone().expandBy(flattenImageBoundsExpand),\n\t\t\t}\n\t\t})\n\n\t\tfor (let i = 0; i < expandedBounds.length; i++) {\n\t\t\tconst item = expandedBounds[i]\n\t\t\tif (i === 0) {\n\t\t\t\tgroups[0] = {\n\t\t\t\t\tshapes: [item.shape],\n\t\t\t\t\tbounds: item.bounds,\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\tlet didLand = false\n\n\t\t\tfor (const group of groups) {\n\t\t\t\tif (group.bounds.includes(item.bounds)) {\n\t\t\t\t\tgroup.shapes.push(item.shape)\n\t\t\t\t\tgroup.bounds.expand(item.bounds)\n\t\t\t\t\tdidLand = true\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!didLand) {\n\t\t\t\tgroups.push({\n\t\t\t\t\tshapes: [item.shape],\n\t\t\t\t\tbounds: item.bounds,\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\t} else {\n\t\tconst bounds = Box.Common(shapes.map((shape) => editor.getShapeMaskedPageBounds(shape)!))\n\t\tgroups.push({\n\t\t\tshapes,\n\t\t\tbounds,\n\t\t})\n\t}\n\n\tconst padding = editor.options.flattenImageBoundsPadding\n\n\tfor (const group of groups) {\n\t\tif (flattenImageBoundsExpand !== undefined) {\n\t\t\t// shrink the bounds again, removing the expanded area\n\t\t\tgroup.bounds.expandBy(-flattenImageBoundsExpand)\n\t\t}\n\n\t\t// get an image for the shapes\n\t\tconst svgResult = await editor.getSvgString(group.shapes, {\n\t\t\tpadding,\n\t\t\tbackground: false,\n\t\t})\n\t\tif (!svgResult?.svg) continue\n\n\t\t// get an image asset for the image\n\t\tconst asset = (await editor.getAssetForExternalContent({\n\t\t\ttype: 'file',\n\t\t\tfile: new File([svgResult.svg], 'asset.svg', { type: 'image/svg+xml' }),\n\t\t})) as TLImageAsset\n\t\tif (!asset) continue\n\n\t\t// add it to the group\n\t\tgroup.asset = asset\n\t}\n\n\tconst createdShapeIds: TLShapeId[] = []\n\n\ttransact(() => {\n\t\tfor (const group of groups) {\n\t\t\tconst { asset, bounds, shapes } = group\n\t\t\tif (!asset) continue\n\n\t\t\tconst commonAncestorId = editor.findCommonAncestor(shapes) ?? editor.getCurrentPageId()\n\t\t\tif (!commonAncestorId) continue\n\n\t\t\tlet index: IndexKey = 'a1' as IndexKey\n\t\t\tfor (const shape of shapes) {\n\t\t\t\tif (shape.parentId === commonAncestorId) {\n\t\t\t\t\tif (shape.index > index) {\n\t\t\t\t\t\tindex = shape.index\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet x: number\n\t\t\tlet y: number\n\t\t\tlet rotation: number\n\n\t\t\tif (isShapeId(commonAncestorId)) {\n\t\t\t\tconst commonAncestor = editor.getShape(commonAncestorId)\n\t\t\t\tif (!commonAncestor) continue\n\t\t\t\t// put the point in the parent's space\n\t\t\t\tconst point = editor.getPointInShapeSpace(commonAncestor, {\n\t\t\t\t\tx: bounds.x,\n\t\t\t\t\ty: bounds.y,\n\t\t\t\t})\n\t\t\t\t// get the parent's rotation\n\t\t\t\trotation = editor.getShapePageTransform(commonAncestorId).rotation()\n\t\t\t\t// rotate the point against the parent's rotation\n\t\t\t\tpoint.sub(new Vec(padding, padding).rot(-rotation))\n\t\t\t\tx = point.x\n\t\t\t\ty = point.y\n\t\t\t} else {\n\t\t\t\t// if the common ancestor is the page, then just adjust for the padding\n\t\t\t\tx = bounds.x - padding\n\t\t\t\ty = bounds.y - padding\n\t\t\t\trotation = 0\n\t\t\t}\n\n\t\t\t// delete the shapes\n\t\t\teditor.deleteShapes(shapes)\n\n\t\t\t// create the asset\n\t\t\teditor.createAssets([{ ...asset, id: asset.id }])\n\n\t\t\tconst shapeId = createShapeId()\n\n\t\t\t// create an image shape in the same place as the shapes\n\t\t\teditor.createShape({\n\t\t\t\tid: shapeId,\n\t\t\t\ttype: 'image',\n\t\t\t\tindex,\n\t\t\t\tparentId: commonAncestorId,\n\t\t\t\tx,\n\t\t\t\ty,\n\t\t\t\trotation: -rotation,\n\t\t\t\tprops: {\n\t\t\t\t\tassetId: asset.id,\n\t\t\t\t\tw: bounds.w + padding * 2,\n\t\t\t\t\th: bounds.h + padding * 2,\n\t\t\t\t},\n\t\t\t})\n\n\t\t\tcreatedShapeIds.push(shapeId)\n\t\t}\n\t})\n\n\treturn createdShapeIds\n}\n\nexport function useFlatten() {\n\tconst editor = useEditor()\n\treturn useCallback(\n\t\t(ids: TLShapeId[]) => {\n\t\t\treturn flattenShapesToImages(editor, ids)\n\t\t},\n\t\t[editor]\n\t)\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAaO;AACP,mBAA4B;AAE5B,eAAsB,sBACrB,QACA,UACA,0BACC;AACD,QAAM,aAAS;AAAA,IACd,SAAS,IAAI,CAAC,OAAO;AACpB,YAAM,QAAQ,OAAO,SAAS,EAAE;AAChC,UAAI,CAAC,MAAO;AACZ,YAAM,OAAO,OAAO,aAAa,MAAM,IAAI;AAE3C,UAAI,KAAK,UAAU,OAAW;AAC9B,aAAO;AAAA,IACR,CAAC;AAAA,EACF;AAEA,MAAI,OAAO,WAAW,EAAG;AAGzB,MAAI,OAAO,WAAW,GAAG;AACxB,UAAM,QAAQ,OAAO,CAAC;AACtB,QAAI,CAAC,MAAO;AACZ,QAAI,OAAO,cAAc,OAAO,OAAO,EAAG;AAAA,EAC3C;AAEA,QAAM,SAAqE,CAAC;AAE5E,MAAI,6BAA6B,QAAW;AAC3C,UAAM,iBAAiB,OAAO,IAAI,CAAC,UAAU;AAC5C,aAAO;AAAA,QACN;AAAA,QACA,QAAQ,OAAO,yBAAyB,KAAK,EAAG,MAAM,EAAE,SAAS,wBAAwB;AAAA,MAC1F;AAAA,IACD,CAAC;AAED,aAAS,IA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A,QACzB;AAAA,MACD,CAAC;AAED,sBAAgB,KAAK,OAAO;AAAA,IAC7B;AAAA,EACD,CAAC;AAED,SAAO;AACR;AAEO,SAAS,aAAa;AAC5B,QAAM,aAAS,yBAAU;AACzB,aAAO;AAAA,IACN,CAAC,QAAqB;AACrB,aAAO,sBAAsB,QAAQ,GAAG;AAAA,IACzC;AAAA,IACA,CAAC,MAAM;AAAA,EACR;AACD;",
6
6
  "names": ["shapes"]
7
7
  }
@@ -22,10 +22,10 @@ __export(version_exports, {
22
22
  version: () => version
23
23
  });
24
24
  module.exports = __toCommonJS(version_exports);
25
- const version = "5.4.0-canary.e10ff229bb14";
25
+ const version = "5.4.0-next.09eca5dee30d";
26
26
  const publishDates = {
27
27
  major: "2026-05-06T16:28:18.473Z",
28
- minor: "2026-08-07T15:30:34.999Z",
29
- patch: "2026-08-07T15:30:34.999Z"
28
+ minor: "2026-08-10T10:12:59.188Z",
29
+ patch: "2026-08-10T10:12:59.188Z"
30
30
  };
31
31
  //# sourceMappingURL=version.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/lib/ui/version.ts"],
4
- "sourcesContent": ["// This file is automatically generated by internal/scripts/refresh-assets.ts.\n// Do not edit manually. Or do, I'm a comment, not a cop.\n\nexport const version = '5.4.0-canary.e10ff229bb14'\nexport const publishDates = {\n\tmajor: '2026-05-06T16:28:18.473Z',\n\tminor: '2026-08-07T15:30:34.999Z',\n\tpatch: '2026-08-07T15:30:34.999Z',\n}\n"],
4
+ "sourcesContent": ["// This file is automatically generated by internal/scripts/refresh-assets.ts.\n// Do not edit manually. Or do, I'm a comment, not a cop.\n\nexport const version = '5.4.0-next.09eca5dee30d'\nexport const publishDates = {\n\tmajor: '2026-05-06T16:28:18.473Z',\n\tminor: '2026-08-10T10:12:59.188Z',\n\tpatch: '2026-08-10T10:12:59.188Z',\n}\n"],
5
5
  "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,MAAM,UAAU;AAChB,MAAM,eAAe;AAAA,EAC3B,OAAO;AAAA,EACP,OAAO;AAAA,EACP,OAAO;AACR;",
6
6
  "names": []
7
7
  }
@@ -637,7 +637,7 @@ import {
637
637
  } from "./lib/utils/tldr/file.mjs";
638
638
  registerTldrawLibraryVersion(
639
639
  "tldraw",
640
- "5.4.0-canary.e10ff229bb14",
640
+ "5.4.0-next.09eca5dee30d",
641
641
  "esm"
642
642
  );
643
643
  export {
@@ -1,6 +1,6 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
- import { useEditor, useValue } from "@tldraw/editor";
3
- import { memo } from "react";
2
+ import { useEditor, usePassThroughWheelEvents, useValue } from "@tldraw/editor";
3
+ import { memo, useRef } from "react";
4
4
  import { PORTRAIT_BREAKPOINT } from "../../constants.mjs";
5
5
  import { useBreakpoint } from "../../context/breakpoints.mjs";
6
6
  import { useReadonly } from "../../hooks/useReadonly.mjs";
@@ -22,6 +22,8 @@ const DefaultActionsMenu = memo(function DefaultActionsMenu2({
22
22
  const breakpoint = useBreakpoint();
23
23
  const isReadonlyMode = useReadonly();
24
24
  const { orientation } = useTldrawUiOrientation();
25
+ const ref = useRef(null);
26
+ usePassThroughWheelEvents(ref);
25
27
  const editor = useEditor();
26
28
  const isInAcceptableReadonlyState = useValue(
27
29
  "should display quick actions when in readonly",
@@ -54,6 +56,7 @@ const DefaultActionsMenu = memo(function DefaultActionsMenu2({
54
56
  children: /* @__PURE__ */ jsx(
55
57
  TldrawUiToolbar,
56
58
  {
59
+ ref,
57
60
  label: msg("actions-menu.title"),
58
61
  className: "tlui-actions-menu",
59
62
  "data-testid": "actions-menu.content",
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../src/lib/ui/components/ActionsMenu/DefaultActionsMenu.tsx"],
4
- "sourcesContent": ["import { useEditor, useValue } from '@tldraw/editor'\nimport { ReactNode, memo } from 'react'\nimport { PORTRAIT_BREAKPOINT } from '../../constants'\nimport { useBreakpoint } from '../../context/breakpoints'\nimport { useReadonly } from '../../hooks/useReadonly'\nimport { useTranslation } from '../../hooks/useTranslation/useTranslation'\nimport { TldrawUiButtonIcon } from '../primitives/Button/TldrawUiButtonIcon'\nimport { useTldrawUiOrientation } from '../primitives/layout'\nimport { TldrawUiMenuContextProvider } from '../primitives/menus/TldrawUiMenuContext'\nimport {\n\tTldrawUiPopover,\n\tTldrawUiPopoverContent,\n\tTldrawUiPopoverTrigger,\n} from '../primitives/TldrawUiPopover'\nimport { TldrawUiToolbar, TldrawUiToolbarButton } from '../primitives/TldrawUiToolbar'\nimport { DefaultActionsMenuContent } from './DefaultActionsMenuContent'\n\n/** @public */\nexport interface TLUiActionsMenuProps {\n\tchildren?: ReactNode\n}\n\n/** @public @react */\nexport const DefaultActionsMenu = memo(function DefaultActionsMenu({\n\tchildren,\n}: TLUiActionsMenuProps) {\n\tconst msg = useTranslation()\n\tconst breakpoint = useBreakpoint()\n\tconst isReadonlyMode = useReadonly()\n\tconst { orientation } = useTldrawUiOrientation()\n\n\tconst editor = useEditor()\n\tconst isInAcceptableReadonlyState = useValue(\n\t\t'should display quick actions when in readonly',\n\t\t() => editor.isInAny('hand', 'zoom'),\n\t\t[editor]\n\t)\n\n\t// Get the actions menu content, either the default component or the user's\n\t// override. If there's no menu content, then the user has set it to null,\n\t// so skip rendering the menu.\n\n\tconst content = children ?? <DefaultActionsMenuContent />\n\tif (isReadonlyMode && !isInAcceptableReadonlyState) return\n\n\treturn (\n\t\t<TldrawUiPopover id=\"actions-menu\">\n\t\t\t<TldrawUiPopoverTrigger>\n\t\t\t\t<TldrawUiToolbarButton\n\t\t\t\t\ttype=\"icon\"\n\t\t\t\t\tdata-testid=\"actions-menu.button\"\n\t\t\t\t\ttitle={msg('actions-menu.title')}\n\t\t\t\t>\n\t\t\t\t\t<TldrawUiButtonIcon\n\t\t\t\t\t\ticon={orientation === 'horizontal' ? 'dots-vertical' : 'dots-horizontal'}\n\t\t\t\t\t\tsmall\n\t\t\t\t\t/>\n\t\t\t\t</TldrawUiToolbarButton>\n\t\t\t</TldrawUiPopoverTrigger>\n\t\t\t<TldrawUiPopoverContent\n\t\t\t\tside={\n\t\t\t\t\torientation === 'horizontal'\n\t\t\t\t\t\t? breakpoint >= PORTRAIT_BREAKPOINT.TABLET\n\t\t\t\t\t\t\t? 'bottom'\n\t\t\t\t\t\t\t: 'top'\n\t\t\t\t\t\t: 'right'\n\t\t\t\t}\n\t\t\t\tsideOffset={6}\n\t\t\t>\n\t\t\t\t<TldrawUiToolbar\n\t\t\t\t\tlabel={msg('actions-menu.title')}\n\t\t\t\t\tclassName=\"tlui-actions-menu\"\n\t\t\t\t\tdata-testid=\"actions-menu.content\"\n\t\t\t\t\torientation=\"grid\"\n\t\t\t\t>\n\t\t\t\t\t<TldrawUiMenuContextProvider type=\"icons\" sourceId=\"actions-menu\">\n\t\t\t\t\t\t{content}\n\t\t\t\t\t</TldrawUiMenuContextProvider>\n\t\t\t\t</TldrawUiToolbar>\n\t\t\t</TldrawUiPopoverContent>\n\t\t</TldrawUiPopover>\n\t)\n})\n"],
5
- "mappings": "AA0C6B,cAI3B,YAJ2B;AA1C7B,SAAS,WAAW,gBAAgB;AACpC,SAAoB,YAAY;AAChC,SAAS,2BAA2B;AACpC,SAAS,qBAAqB;AAC9B,SAAS,mBAAmB;AAC5B,SAAS,sBAAsB;AAC/B,SAAS,0BAA0B;AACnC,SAAS,8BAA8B;AACvC,SAAS,mCAAmC;AAC5C;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,iBAAiB,6BAA6B;AACvD,SAAS,iCAAiC;AAQnC,MAAM,qBAAqB,KAAK,SAASA,oBAAmB;AAAA,EAClE;AACD,GAAyB;AACxB,QAAM,MAAM,eAAe;AAC3B,QAAM,aAAa,cAAc;AACjC,QAAM,iBAAiB,YAAY;AACnC,QAAM,EAAE,YAAY,IAAI,uBAAuB;AAE/C,QAAM,SAAS,UAAU;AACzB,QAAM,8BAA8B;AAAA,IACnC;AAAA,IACA,MAAM,OAAO,QAAQ,QAAQ,MAAM;AAAA,IACnC,CAAC,MAAM;AAAA,EACR;AAMA,QAAM,UAAU,YAAY,oBAAC,6BAA0B;AACvD,MAAI,kBAAkB,CAAC,4BAA6B;AAEpD,SACC,qBAAC,mBAAgB,IAAG,gBACnB;AAAA,wBAAC,0BACA;AAAA,MAAC;AAAA;AAAA,QACA,MAAK;AAAA,QACL,eAAY;AAAA,QACZ,OAAO,IAAI,oBAAoB;AAAA,QAE/B;AAAA,UAAC;AAAA;AAAA,YACA,MAAM,gBAAgB,eAAe,kBAAkB;AAAA,YACvD,OAAK;AAAA;AAAA,QACN;AAAA;AAAA,IACD,GACD;AAAA,IACA;AAAA,MAAC;AAAA;AAAA,QACA,MACC,gBAAgB,eACb,cAAc,oBAAoB,SACjC,WACA,QACD;AAAA,QAEJ,YAAY;AAAA,QAEZ;AAAA,UAAC;AAAA;AAAA,YACA,OAAO,IAAI,oBAAoB;AAAA,YAC/B,WAAU;AAAA,YACV,eAAY;AAAA,YACZ,aAAY;AAAA,YAEZ,8BAAC,+BAA4B,MAAK,SAAQ,UAAS,gBACjD,mBACF;AAAA;AAAA,QACD;AAAA;AAAA,IACD;AAAA,KACD;AAEF,CAAC;",
4
+ "sourcesContent": ["import { useEditor, usePassThroughWheelEvents, useValue } from '@tldraw/editor'\nimport { ReactNode, memo, useRef } from 'react'\nimport { PORTRAIT_BREAKPOINT } from '../../constants'\nimport { useBreakpoint } from '../../context/breakpoints'\nimport { useReadonly } from '../../hooks/useReadonly'\nimport { useTranslation } from '../../hooks/useTranslation/useTranslation'\nimport { TldrawUiButtonIcon } from '../primitives/Button/TldrawUiButtonIcon'\nimport { useTldrawUiOrientation } from '../primitives/layout'\nimport { TldrawUiMenuContextProvider } from '../primitives/menus/TldrawUiMenuContext'\nimport {\n\tTldrawUiPopover,\n\tTldrawUiPopoverContent,\n\tTldrawUiPopoverTrigger,\n} from '../primitives/TldrawUiPopover'\nimport { TldrawUiToolbar, TldrawUiToolbarButton } from '../primitives/TldrawUiToolbar'\nimport { DefaultActionsMenuContent } from './DefaultActionsMenuContent'\n\n/** @public */\nexport interface TLUiActionsMenuProps {\n\tchildren?: ReactNode\n}\n\n/** @public @react */\nexport const DefaultActionsMenu = memo(function DefaultActionsMenu({\n\tchildren,\n}: TLUiActionsMenuProps) {\n\tconst msg = useTranslation()\n\tconst breakpoint = useBreakpoint()\n\tconst isReadonlyMode = useReadonly()\n\tconst { orientation } = useTldrawUiOrientation()\n\n\tconst ref = useRef<HTMLDivElement>(null)\n\tusePassThroughWheelEvents(ref)\n\n\tconst editor = useEditor()\n\tconst isInAcceptableReadonlyState = useValue(\n\t\t'should display quick actions when in readonly',\n\t\t() => editor.isInAny('hand', 'zoom'),\n\t\t[editor]\n\t)\n\n\t// Get the actions menu content, either the default component or the user's\n\t// override. If there's no menu content, then the user has set it to null,\n\t// so skip rendering the menu.\n\n\tconst content = children ?? <DefaultActionsMenuContent />\n\tif (isReadonlyMode && !isInAcceptableReadonlyState) return\n\n\treturn (\n\t\t<TldrawUiPopover id=\"actions-menu\">\n\t\t\t<TldrawUiPopoverTrigger>\n\t\t\t\t<TldrawUiToolbarButton\n\t\t\t\t\ttype=\"icon\"\n\t\t\t\t\tdata-testid=\"actions-menu.button\"\n\t\t\t\t\ttitle={msg('actions-menu.title')}\n\t\t\t\t>\n\t\t\t\t\t<TldrawUiButtonIcon\n\t\t\t\t\t\ticon={orientation === 'horizontal' ? 'dots-vertical' : 'dots-horizontal'}\n\t\t\t\t\t\tsmall\n\t\t\t\t\t/>\n\t\t\t\t</TldrawUiToolbarButton>\n\t\t\t</TldrawUiPopoverTrigger>\n\t\t\t<TldrawUiPopoverContent\n\t\t\t\tside={\n\t\t\t\t\torientation === 'horizontal'\n\t\t\t\t\t\t? breakpoint >= PORTRAIT_BREAKPOINT.TABLET\n\t\t\t\t\t\t\t? 'bottom'\n\t\t\t\t\t\t\t: 'top'\n\t\t\t\t\t\t: 'right'\n\t\t\t\t}\n\t\t\t\tsideOffset={6}\n\t\t\t>\n\t\t\t\t<TldrawUiToolbar\n\t\t\t\t\tref={ref}\n\t\t\t\t\tlabel={msg('actions-menu.title')}\n\t\t\t\t\tclassName=\"tlui-actions-menu\"\n\t\t\t\t\tdata-testid=\"actions-menu.content\"\n\t\t\t\t\torientation=\"grid\"\n\t\t\t\t>\n\t\t\t\t\t<TldrawUiMenuContextProvider type=\"icons\" sourceId=\"actions-menu\">\n\t\t\t\t\t\t{content}\n\t\t\t\t\t</TldrawUiMenuContextProvider>\n\t\t\t\t</TldrawUiToolbar>\n\t\t\t</TldrawUiPopoverContent>\n\t\t</TldrawUiPopover>\n\t)\n})\n"],
5
+ "mappings": "AA6C6B,cAI3B,YAJ2B;AA7C7B,SAAS,WAAW,2BAA2B,gBAAgB;AAC/D,SAAoB,MAAM,cAAc;AACxC,SAAS,2BAA2B;AACpC,SAAS,qBAAqB;AAC9B,SAAS,mBAAmB;AAC5B,SAAS,sBAAsB;AAC/B,SAAS,0BAA0B;AACnC,SAAS,8BAA8B;AACvC,SAAS,mCAAmC;AAC5C;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,iBAAiB,6BAA6B;AACvD,SAAS,iCAAiC;AAQnC,MAAM,qBAAqB,KAAK,SAASA,oBAAmB;AAAA,EAClE;AACD,GAAyB;AACxB,QAAM,MAAM,eAAe;AAC3B,QAAM,aAAa,cAAc;AACjC,QAAM,iBAAiB,YAAY;AACnC,QAAM,EAAE,YAAY,IAAI,uBAAuB;AAE/C,QAAM,MAAM,OAAuB,IAAI;AACvC,4BAA0B,GAAG;AAE7B,QAAM,SAAS,UAAU;AACzB,QAAM,8BAA8B;AAAA,IACnC;AAAA,IACA,MAAM,OAAO,QAAQ,QAAQ,MAAM;AAAA,IACnC,CAAC,MAAM;AAAA,EACR;AAMA,QAAM,UAAU,YAAY,oBAAC,6BAA0B;AACvD,MAAI,kBAAkB,CAAC,4BAA6B;AAEpD,SACC,qBAAC,mBAAgB,IAAG,gBACnB;AAAA,wBAAC,0BACA;AAAA,MAAC;AAAA;AAAA,QACA,MAAK;AAAA,QACL,eAAY;AAAA,QACZ,OAAO,IAAI,oBAAoB;AAAA,QAE/B;AAAA,UAAC;AAAA;AAAA,YACA,MAAM,gBAAgB,eAAe,kBAAkB;AAAA,YACvD,OAAK;AAAA;AAAA,QACN;AAAA;AAAA,IACD,GACD;AAAA,IACA;AAAA,MAAC;AAAA;AAAA,QACA,MACC,gBAAgB,eACb,cAAc,oBAAoB,SACjC,WACA,QACD;AAAA,QAEJ,YAAY;AAAA,QAEZ;AAAA,UAAC;AAAA;AAAA,YACA;AAAA,YACA,OAAO,IAAI,oBAAoB;AAAA,YAC/B,WAAU;AAAA,YACV,eAAY;AAAA,YACZ,aAAY;AAAA,YAEZ,8BAAC,+BAA4B,MAAK,SAAQ,UAAS,gBACjD,mBACF;AAAA;AAAA,QACD;AAAA;AAAA,IACD;AAAA,KACD;AAEF,CAAC;",
6
6
  "names": ["DefaultActionsMenu"]
7
7
  }
@@ -32,53 +32,31 @@ async function flattenShapesToImages(editor, shapeIds, flattenImageBoundsExpand)
32
32
  bounds: editor.getShapeMaskedPageBounds(shape).clone().expandBy(flattenImageBoundsExpand)
33
33
  };
34
34
  });
35
- const parents = expandedBounds.map((_, i) => i);
36
- const find = (i) => {
37
- let root = i;
38
- while (parents[root] !== root) root = parents[root];
39
- while (parents[i] !== root) {
40
- const next = parents[i];
41
- parents[i] = root;
42
- i = next;
35
+ for (let i = 0; i < expandedBounds.length; i++) {
36
+ const item = expandedBounds[i];
37
+ if (i === 0) {
38
+ groups[0] = {
39
+ shapes: [item.shape],
40
+ bounds: item.bounds
41
+ };
42
+ continue;
43
43
  }
44
- return root;
45
- };
46
- const union = (i, j) => {
47
- const ri = find(i);
48
- const rj = find(j);
49
- if (ri !== rj) parents[ri] = rj;
50
- };
51
- const order = expandedBounds.map((_, i) => i).sort((a, b) => {
52
- return expandedBounds[a].bounds.minX - expandedBounds[b].bounds.minX;
53
- });
54
- for (let a = 0; a < order.length; a++) {
55
- const i = order[a];
56
- const boundsI = expandedBounds[i].bounds;
57
- for (let b = a + 1; b < order.length; b++) {
58
- const j = order[b];
59
- if (expandedBounds[j].bounds.minX > boundsI.maxX) break;
60
- if (find(i) === find(j)) continue;
61
- if (boundsI.includes(expandedBounds[j].bounds)) {
62
- union(i, j);
44
+ let didLand = false;
45
+ for (const group of groups) {
46
+ if (group.bounds.includes(item.bounds)) {
47
+ group.shapes.push(item.shape);
48
+ group.bounds.expand(item.bounds);
49
+ didLand = true;
50
+ break;
63
51
  }
64
52
  }
65
- }
66
- const groupsByRoot = /* @__PURE__ */ new Map();
67
- for (let i = 0; i < expandedBounds.length; i++) {
68
- const root = find(i);
69
- const item = expandedBounds[i];
70
- const existing = groupsByRoot.get(root);
71
- if (existing) {
72
- existing.shapes.push(item.shape);
73
- existing.bounds.expand(item.bounds);
74
- } else {
75
- groupsByRoot.set(root, {
53
+ if (!didLand) {
54
+ groups.push({
76
55
  shapes: [item.shape],
77
- bounds: item.bounds.clone()
56
+ bounds: item.bounds
78
57
  });
79
58
  }
80
59
  }
81
- groups.push(...groupsByRoot.values());
82
60
  } else {
83
61
  const bounds = Box.Common(shapes.map((shape) => editor.getShapeMaskedPageBounds(shape)));
84
62
  groups.push({
@@ -112,8 +90,11 @@ async function flattenShapesToImages(editor, shapeIds, flattenImageBoundsExpand)
112
90
  if (!commonAncestorId) continue;
113
91
  let index = "a1";
114
92
  for (const shape of shapes2) {
115
- if (shape.parentId === commonAncestorId && shape.index > index) {
116
- index = shape.index;
93
+ if (shape.parentId === commonAncestorId) {
94
+ if (shape.index > index) {
95
+ index = shape.index;
96
+ }
97
+ break;
117
98
  }
118
99
  }
119
100
  let x;
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/lib/ui/hooks/useFlatten.ts"],
4
- "sourcesContent": ["import {\n\tBox,\n\tEditor,\n\tIndexKey,\n\tTLImageAsset,\n\tTLShape,\n\tTLShapeId,\n\tVec,\n\tcompact,\n\tcreateShapeId,\n\tisShapeId,\n\ttransact,\n\tuseEditor,\n} from '@tldraw/editor'\nimport { useCallback } from 'react'\n\nexport async function flattenShapesToImages(\n\teditor: Editor,\n\tshapeIds: TLShapeId[],\n\tflattenImageBoundsExpand?: number\n) {\n\tconst shapes = compact(\n\t\tshapeIds.map((id) => {\n\t\t\tconst shape = editor.getShape(id)\n\t\t\tif (!shape) return\n\t\t\tconst util = editor.getShapeUtil(shape.type)\n\t\t\t// skip shapes that don't have a toSvg method\n\t\t\tif (util.toSvg === undefined) return\n\t\t\treturn shape\n\t\t})\n\t)\n\n\tif (shapes.length === 0) return\n\n\t// Don't flatten if it's just one image\n\tif (shapes.length === 1) {\n\t\tconst shape = shapes[0]\n\t\tif (!shape) return\n\t\tif (editor.isShapeOfType(shape, 'image')) return\n\t}\n\n\tconst groups: { shapes: TLShape[]; bounds: Box; asset?: TLImageAsset }[] = []\n\n\tif (flattenImageBoundsExpand !== undefined) {\n\t\tconst expandedBounds = shapes.map((shape) => {\n\t\t\treturn {\n\t\t\t\tshape,\n\t\t\t\tbounds: editor.getShapeMaskedPageBounds(shape)!.clone().expandBy(flattenImageBoundsExpand),\n\t\t\t}\n\t\t})\n\n\t\t// Cluster shapes by connected components: any two shapes whose expanded\n\t\t// bounds intersect end up in the same group. Using union-find here\n\t\t// instead of a greedy first-fit pass means the result is independent of\n\t\t// the order shapes appear in `shapeIds`, and a shape that bridges two\n\t\t// otherwise-disjoint clusters merges them in one pass.\n\t\tconst parents = expandedBounds.map((_, i) => i)\n\t\tconst find = (i: number): number => {\n\t\t\tlet root = i\n\t\t\twhile (parents[root] !== root) root = parents[root]\n\t\t\twhile (parents[i] !== root) {\n\t\t\t\tconst next = parents[i]\n\t\t\t\tparents[i] = root\n\t\t\t\ti = next\n\t\t\t}\n\t\t\treturn root\n\t\t}\n\t\tconst union = (i: number, j: number) => {\n\t\t\tconst ri = find(i)\n\t\t\tconst rj = find(j)\n\t\t\tif (ri !== rj) parents[ri] = rj\n\t\t}\n\n\t\t// Broad-phase sweep: sort by minX and, for each box, only compare against\n\t\t// later boxes until one starts past the current box's maxX. Boxes that\n\t\t// don't overlap on the x-axis can't intersect, so this skips the bulk of\n\t\t// the pairwise checks for spread-out selections while still finding every\n\t\t// intersecting pair (Box.includes is symmetric, so each pair is tested once).\n\t\tconst order = expandedBounds\n\t\t\t.map((_, i) => i)\n\t\t\t.sort((a, b) => {\n\t\t\t\treturn expandedBounds[a].bounds.minX - expandedBounds[b].bounds.minX\n\t\t\t})\n\n\t\tfor (let a = 0; a < order.length; a++) {\n\t\t\tconst i = order[a]\n\t\t\tconst boundsI = expandedBounds[i].bounds\n\t\t\tfor (let b = a + 1; b < order.length; b++) {\n\t\t\t\tconst j = order[b]\n\t\t\t\t// sorted by minX, so once a box starts past i's maxX no later box can overlap i\n\t\t\t\tif (expandedBounds[j].bounds.minX > boundsI.maxX) break\n\t\t\t\tif (find(i) === find(j)) continue\n\t\t\t\tif (boundsI.includes(expandedBounds[j].bounds)) {\n\t\t\t\t\tunion(i, j)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst groupsByRoot = new Map<number, { shapes: TLShape[]; bounds: Box }>()\n\t\tfor (let i = 0; i < expandedBounds.length; i++) {\n\t\t\tconst root = find(i)\n\t\t\tconst item = expandedBounds[i]\n\t\t\tconst existing = groupsByRoot.get(root)\n\t\t\tif (existing) {\n\t\t\t\texisting.shapes.push(item.shape)\n\t\t\t\texisting.bounds.expand(item.bounds)\n\t\t\t} else {\n\t\t\t\tgroupsByRoot.set(root, {\n\t\t\t\t\tshapes: [item.shape],\n\t\t\t\t\tbounds: item.bounds.clone(),\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\n\t\tgroups.push(...groupsByRoot.values())\n\t} else {\n\t\tconst bounds = Box.Common(shapes.map((shape) => editor.getShapeMaskedPageBounds(shape)!))\n\t\tgroups.push({\n\t\t\tshapes,\n\t\t\tbounds,\n\t\t})\n\t}\n\n\tconst padding = editor.options.flattenImageBoundsPadding\n\n\tfor (const group of groups) {\n\t\tif (flattenImageBoundsExpand !== undefined) {\n\t\t\t// shrink the bounds again, removing the expanded area\n\t\t\tgroup.bounds.expandBy(-flattenImageBoundsExpand)\n\t\t}\n\n\t\t// get an image for the shapes\n\t\tconst svgResult = await editor.getSvgString(group.shapes, {\n\t\t\tpadding,\n\t\t\tbackground: false,\n\t\t})\n\t\tif (!svgResult?.svg) continue\n\n\t\t// get an image asset for the image\n\t\tconst asset = (await editor.getAssetForExternalContent({\n\t\t\ttype: 'file',\n\t\t\tfile: new File([svgResult.svg], 'asset.svg', { type: 'image/svg+xml' }),\n\t\t})) as TLImageAsset\n\t\tif (!asset) continue\n\n\t\t// add it to the group\n\t\tgroup.asset = asset\n\t}\n\n\tconst createdShapeIds: TLShapeId[] = []\n\n\ttransact(() => {\n\t\tfor (const group of groups) {\n\t\t\tconst { asset, bounds, shapes } = group\n\t\t\tif (!asset) continue\n\n\t\t\tconst commonAncestorId = editor.findCommonAncestor(shapes) ?? editor.getCurrentPageId()\n\t\t\tif (!commonAncestorId) continue\n\n\t\t\tlet index: IndexKey = 'a1' as IndexKey\n\t\t\tfor (const shape of shapes) {\n\t\t\t\tif (shape.parentId === commonAncestorId && shape.index > index) {\n\t\t\t\t\tindex = shape.index\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet x: number\n\t\t\tlet y: number\n\t\t\tlet rotation: number\n\n\t\t\tif (isShapeId(commonAncestorId)) {\n\t\t\t\tconst commonAncestor = editor.getShape(commonAncestorId)\n\t\t\t\tif (!commonAncestor) continue\n\t\t\t\t// put the point in the parent's space\n\t\t\t\tconst point = editor.getPointInShapeSpace(commonAncestor, {\n\t\t\t\t\tx: bounds.x,\n\t\t\t\t\ty: bounds.y,\n\t\t\t\t})\n\t\t\t\t// get the parent's rotation\n\t\t\t\trotation = editor.getShapePageTransform(commonAncestorId).rotation()\n\t\t\t\t// rotate the point against the parent's rotation\n\t\t\t\tpoint.sub(new Vec(padding, padding).rot(-rotation))\n\t\t\t\tx = point.x\n\t\t\t\ty = point.y\n\t\t\t} else {\n\t\t\t\t// if the common ancestor is the page, then just adjust for the padding\n\t\t\t\tx = bounds.x - padding\n\t\t\t\ty = bounds.y - padding\n\t\t\t\trotation = 0\n\t\t\t}\n\n\t\t\t// delete the shapes\n\t\t\teditor.deleteShapes(shapes)\n\n\t\t\t// create the asset\n\t\t\teditor.createAssets([{ ...asset, id: asset.id }])\n\n\t\t\tconst shapeId = createShapeId()\n\n\t\t\t// create an image shape in the same place as the shapes\n\t\t\teditor.createShape({\n\t\t\t\tid: shapeId,\n\t\t\t\ttype: 'image',\n\t\t\t\tindex,\n\t\t\t\tparentId: commonAncestorId,\n\t\t\t\tx,\n\t\t\t\ty,\n\t\t\t\trotation: -rotation,\n\t\t\t\tprops: {\n\t\t\t\t\tassetId: asset.id,\n\t\t\t\t\tw: bounds.w + padding * 2,\n\t\t\t\t\th: bounds.h + padding * 2,\n\t\t\t\t},\n\t\t\t})\n\n\t\t\tcreatedShapeIds.push(shapeId)\n\t\t}\n\t})\n\n\treturn createdShapeIds\n}\n\nexport function useFlatten() {\n\tconst editor = useEditor()\n\treturn useCallback(\n\t\t(ids: TLShapeId[]) => {\n\t\t\treturn flattenShapesToImages(editor, ids)\n\t\t},\n\t\t[editor]\n\t)\n}\n"],
5
- "mappings": "AAAA;AAAA,EACC;AAAA,EAMA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,mBAAmB;AAE5B,eAAsB,sBACrB,QACA,UACA,0BACC;AACD,QAAM,SAAS;AAAA,IACd,SAAS,IAAI,CAAC,OAAO;AACpB,YAAM,QAAQ,OAAO,SAAS,EAAE;AAChC,UAAI,CAAC,MAAO;AACZ,YAAM,OAAO,OAAO,aAAa,MAAM,IAAI;AAE3C,UAAI,KAAK,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A,QACA;AAAA,QACA,UAAU,CAAC;AAAA,QACX,OAAO;AAAA,UACN,SAAS,MAAM;AAAA,UACf,GAAG,OAAO,IAAI,UAAU;AAAA,UACxB,GAAG,OAAO,IAAI,UAAU;AAAA,QACzB;AAAA,MACD,CAAC;AAED,sBAAgB,KAAK,OAAO;AAAA,IAC7B;AAAA,EACD,CAAC;AAED,SAAO;AACR;AAEO,SAAS,aAAa;AAC5B,QAAM,SAAS,UAAU;AACzB,SAAO;AAAA,IACN,CAAC,QAAqB;AACrB,aAAO,sBAAsB,QAAQ,GAAG;AAAA,IACzC;AAAA,IACA,CAAC,MAAM;AAAA,EACR;AACD;",
4
+ "sourcesContent": ["import {\n\tBox,\n\tEditor,\n\tIndexKey,\n\tTLImageAsset,\n\tTLShape,\n\tTLShapeId,\n\tVec,\n\tcompact,\n\tcreateShapeId,\n\tisShapeId,\n\ttransact,\n\tuseEditor,\n} from '@tldraw/editor'\nimport { useCallback } from 'react'\n\nexport async function flattenShapesToImages(\n\teditor: Editor,\n\tshapeIds: TLShapeId[],\n\tflattenImageBoundsExpand?: number\n) {\n\tconst shapes = compact(\n\t\tshapeIds.map((id) => {\n\t\t\tconst shape = editor.getShape(id)\n\t\t\tif (!shape) return\n\t\t\tconst util = editor.getShapeUtil(shape.type)\n\t\t\t// skip shapes that don't have a toSvg method\n\t\t\tif (util.toSvg === undefined) return\n\t\t\treturn shape\n\t\t})\n\t)\n\n\tif (shapes.length === 0) return\n\n\t// Don't flatten if it's just one image\n\tif (shapes.length === 1) {\n\t\tconst shape = shapes[0]\n\t\tif (!shape) return\n\t\tif (editor.isShapeOfType(shape, 'image')) return\n\t}\n\n\tconst groups: { shapes: TLShape[]; bounds: Box; asset?: TLImageAsset }[] = []\n\n\tif (flattenImageBoundsExpand !== undefined) {\n\t\tconst expandedBounds = shapes.map((shape) => {\n\t\t\treturn {\n\t\t\t\tshape,\n\t\t\t\tbounds: editor.getShapeMaskedPageBounds(shape)!.clone().expandBy(flattenImageBoundsExpand),\n\t\t\t}\n\t\t})\n\n\t\tfor (let i = 0; i < expandedBounds.length; i++) {\n\t\t\tconst item = expandedBounds[i]\n\t\t\tif (i === 0) {\n\t\t\t\tgroups[0] = {\n\t\t\t\t\tshapes: [item.shape],\n\t\t\t\t\tbounds: item.bounds,\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\tlet didLand = false\n\n\t\t\tfor (const group of groups) {\n\t\t\t\tif (group.bounds.includes(item.bounds)) {\n\t\t\t\t\tgroup.shapes.push(item.shape)\n\t\t\t\t\tgroup.bounds.expand(item.bounds)\n\t\t\t\t\tdidLand = true\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!didLand) {\n\t\t\t\tgroups.push({\n\t\t\t\t\tshapes: [item.shape],\n\t\t\t\t\tbounds: item.bounds,\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\t} else {\n\t\tconst bounds = Box.Common(shapes.map((shape) => editor.getShapeMaskedPageBounds(shape)!))\n\t\tgroups.push({\n\t\t\tshapes,\n\t\t\tbounds,\n\t\t})\n\t}\n\n\tconst padding = editor.options.flattenImageBoundsPadding\n\n\tfor (const group of groups) {\n\t\tif (flattenImageBoundsExpand !== undefined) {\n\t\t\t// shrink the bounds again, removing the expanded area\n\t\t\tgroup.bounds.expandBy(-flattenImageBoundsExpand)\n\t\t}\n\n\t\t// get an image for the shapes\n\t\tconst svgResult = await editor.getSvgString(group.shapes, {\n\t\t\tpadding,\n\t\t\tbackground: false,\n\t\t})\n\t\tif (!svgResult?.svg) continue\n\n\t\t// get an image asset for the image\n\t\tconst asset = (await editor.getAssetForExternalContent({\n\t\t\ttype: 'file',\n\t\t\tfile: new File([svgResult.svg], 'asset.svg', { type: 'image/svg+xml' }),\n\t\t})) as TLImageAsset\n\t\tif (!asset) continue\n\n\t\t// add it to the group\n\t\tgroup.asset = asset\n\t}\n\n\tconst createdShapeIds: TLShapeId[] = []\n\n\ttransact(() => {\n\t\tfor (const group of groups) {\n\t\t\tconst { asset, bounds, shapes } = group\n\t\t\tif (!asset) continue\n\n\t\t\tconst commonAncestorId = editor.findCommonAncestor(shapes) ?? editor.getCurrentPageId()\n\t\t\tif (!commonAncestorId) continue\n\n\t\t\tlet index: IndexKey = 'a1' as IndexKey\n\t\t\tfor (const shape of shapes) {\n\t\t\t\tif (shape.parentId === commonAncestorId) {\n\t\t\t\t\tif (shape.index > index) {\n\t\t\t\t\t\tindex = shape.index\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet x: number\n\t\t\tlet y: number\n\t\t\tlet rotation: number\n\n\t\t\tif (isShapeId(commonAncestorId)) {\n\t\t\t\tconst commonAncestor = editor.getShape(commonAncestorId)\n\t\t\t\tif (!commonAncestor) continue\n\t\t\t\t// put the point in the parent's space\n\t\t\t\tconst point = editor.getPointInShapeSpace(commonAncestor, {\n\t\t\t\t\tx: bounds.x,\n\t\t\t\t\ty: bounds.y,\n\t\t\t\t})\n\t\t\t\t// get the parent's rotation\n\t\t\t\trotation = editor.getShapePageTransform(commonAncestorId).rotation()\n\t\t\t\t// rotate the point against the parent's rotation\n\t\t\t\tpoint.sub(new Vec(padding, padding).rot(-rotation))\n\t\t\t\tx = point.x\n\t\t\t\ty = point.y\n\t\t\t} else {\n\t\t\t\t// if the common ancestor is the page, then just adjust for the padding\n\t\t\t\tx = bounds.x - padding\n\t\t\t\ty = bounds.y - padding\n\t\t\t\trotation = 0\n\t\t\t}\n\n\t\t\t// delete the shapes\n\t\t\teditor.deleteShapes(shapes)\n\n\t\t\t// create the asset\n\t\t\teditor.createAssets([{ ...asset, id: asset.id }])\n\n\t\t\tconst shapeId = createShapeId()\n\n\t\t\t// create an image shape in the same place as the shapes\n\t\t\teditor.createShape({\n\t\t\t\tid: shapeId,\n\t\t\t\ttype: 'image',\n\t\t\t\tindex,\n\t\t\t\tparentId: commonAncestorId,\n\t\t\t\tx,\n\t\t\t\ty,\n\t\t\t\trotation: -rotation,\n\t\t\t\tprops: {\n\t\t\t\t\tassetId: asset.id,\n\t\t\t\t\tw: bounds.w + padding * 2,\n\t\t\t\t\th: bounds.h + padding * 2,\n\t\t\t\t},\n\t\t\t})\n\n\t\t\tcreatedShapeIds.push(shapeId)\n\t\t}\n\t})\n\n\treturn createdShapeIds\n}\n\nexport function useFlatten() {\n\tconst editor = useEditor()\n\treturn useCallback(\n\t\t(ids: TLShapeId[]) => {\n\t\t\treturn flattenShapesToImages(editor, ids)\n\t\t},\n\t\t[editor]\n\t)\n}\n"],
5
+ "mappings": "AAAA;AAAA,EACC;AAAA,EAMA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,mBAAmB;AAE5B,eAAsB,sBACrB,QACA,UACA,0BACC;AACD,QAAM,SAAS;AAAA,IACd,SAAS,IAAI,CAAC,OAAO;AACpB,YAAM,QAAQ,OAAO,SAAS,EAAE;AAChC,UAAI,CAAC,MAAO;AACZ,YAAM,OAAO,OAAO,aAAa,MAAM,IAAI;AAE3C,UAAI,KAAK,UAAU,OAAW;AAC9B,aAAO;AAAA,IACR,CAAC;AAAA,EACF;AAEA,MAAI,OAAO,WAAW,EAAG;AAGzB,MAAI,OAAO,WAAW,GAAG;AACxB,UAAM,QAAQ,OAAO,CAAC;AACtB,QAAI,CAAC,MAAO;AACZ,QAAI,OAAO,cAAc,OAAO,OAAO,EAAG;AAAA,EAC3C;AAEA,QAAM,SAAqE,CAAC;AAE5E,MAAI,6BAA6B,QAAW;AAC3C,UAAM,iBAAiB,OAAO,IAAI,CAAC,UAAU;AAC5C,aAAO;AAAA,QACN;AAAA,QACA,QAAQ,OAAO,yBAAyB,KAAK,EAAG,MAAM,EAAE,SAAS,wBAAwB;AAAA,MAC1F;AAAA,IACD,CAAC;AAED,aAAS,IAAI,GAAG,IAAI,eAAe,QAAQ,KAAK;AAC/C,YAAM,OAAO,eAAe,CAAC;AAC7B,UAAI,MAAM,G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ACR;AAEO,SAAS,aAAa;AAC5B,QAAM,SAAS,UAAU;AACzB,SAAO;AAAA,IACN,CAAC,QAAqB;AACrB,aAAO,sBAAsB,QAAQ,GAAG;AAAA,IACzC;AAAA,IACA,CAAC,MAAM;AAAA,EACR;AACD;",
6
6
  "names": ["shapes"]
7
7
  }
@@ -1,8 +1,8 @@
1
- const version = "5.4.0-canary.e10ff229bb14";
1
+ const version = "5.4.0-next.09eca5dee30d";
2
2
  const publishDates = {
3
3
  major: "2026-05-06T16:28:18.473Z",
4
- minor: "2026-08-07T15:30:34.999Z",
5
- patch: "2026-08-07T15:30:34.999Z"
4
+ minor: "2026-08-10T10:12:59.188Z",
5
+ patch: "2026-08-10T10:12:59.188Z"
6
6
  };
7
7
  export {
8
8
  publishDates,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/lib/ui/version.ts"],
4
- "sourcesContent": ["// This file is automatically generated by internal/scripts/refresh-assets.ts.\n// Do not edit manually. Or do, I'm a comment, not a cop.\n\nexport const version = '5.4.0-canary.e10ff229bb14'\nexport const publishDates = {\n\tmajor: '2026-05-06T16:28:18.473Z',\n\tminor: '2026-08-07T15:30:34.999Z',\n\tpatch: '2026-08-07T15:30:34.999Z',\n}\n"],
4
+ "sourcesContent": ["// This file is automatically generated by internal/scripts/refresh-assets.ts.\n// Do not edit manually. Or do, I'm a comment, not a cop.\n\nexport const version = '5.4.0-next.09eca5dee30d'\nexport const publishDates = {\n\tmajor: '2026-05-06T16:28:18.473Z',\n\tminor: '2026-08-10T10:12:59.188Z',\n\tpatch: '2026-08-10T10:12:59.188Z',\n}\n"],
5
5
  "mappings": "AAGO,MAAM,UAAU;AAChB,MAAM,eAAe;AAAA,EAC3B,OAAO;AAAA,EACP,OAAO;AAAA,EACP,OAAO;AACR;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tldraw",
3
3
  "description": "A tiny little drawing editor.",
4
- "version": "5.4.0-canary.e10ff229bb14",
4
+ "version": "5.4.0-next.09eca5dee30d",
5
5
  "author": {
6
6
  "name": "tldraw Inc.",
7
7
  "email": "hello@tldraw.com"
@@ -63,11 +63,11 @@
63
63
  "@tiptap/pm": "^3.12.1",
64
64
  "@tiptap/react": "^3.12.1",
65
65
  "@tiptap/starter-kit": "^3.12.1",
66
- "@tldraw/driver": "5.4.0-canary.e10ff229bb14",
67
- "@tldraw/editor": "5.4.0-canary.e10ff229bb14",
68
- "@tldraw/state": "5.4.0-canary.e10ff229bb14",
69
- "@tldraw/store": "5.4.0-canary.e10ff229bb14",
70
- "@tldraw/tlschema": "5.4.0-canary.e10ff229bb14",
66
+ "@tldraw/driver": "5.4.0-next.09eca5dee30d",
67
+ "@tldraw/editor": "5.4.0-next.09eca5dee30d",
68
+ "@tldraw/state": "5.4.0-next.09eca5dee30d",
69
+ "@tldraw/store": "5.4.0-next.09eca5dee30d",
70
+ "@tldraw/tlschema": "5.4.0-next.09eca5dee30d",
71
71
  "classnames": "^2.5.1",
72
72
  "idb": "^7.1.1",
73
73
  "lz-string": "^1.5.0",
@@ -1,5 +1,5 @@
1
- import { useEditor, useValue } from '@tldraw/editor'
2
- import { ReactNode, memo } from 'react'
1
+ import { useEditor, usePassThroughWheelEvents, useValue } from '@tldraw/editor'
2
+ import { ReactNode, memo, useRef } from 'react'
3
3
  import { PORTRAIT_BREAKPOINT } from '../../constants'
4
4
  import { useBreakpoint } from '../../context/breakpoints'
5
5
  import { useReadonly } from '../../hooks/useReadonly'
@@ -29,6 +29,9 @@ export const DefaultActionsMenu = memo(function DefaultActionsMenu({
29
29
  const isReadonlyMode = useReadonly()
30
30
  const { orientation } = useTldrawUiOrientation()
31
31
 
32
+ const ref = useRef<HTMLDivElement>(null)
33
+ usePassThroughWheelEvents(ref)
34
+
32
35
  const editor = useEditor()
33
36
  const isInAcceptableReadonlyState = useValue(
34
37
  'should display quick actions when in readonly',
@@ -68,6 +71,7 @@ export const DefaultActionsMenu = memo(function DefaultActionsMenu({
68
71
  sideOffset={6}
69
72
  >
70
73
  <TldrawUiToolbar
74
+ ref={ref}
71
75
  label={msg('actions-menu.title')}
72
76
  className="tlui-actions-menu"
73
77
  data-testid="actions-menu.content"
@@ -49,70 +49,34 @@ export async function flattenShapesToImages(
49
49
  }
50
50
  })
51
51
 
52
- // Cluster shapes by connected components: any two shapes whose expanded
53
- // bounds intersect end up in the same group. Using union-find here
54
- // instead of a greedy first-fit pass means the result is independent of
55
- // the order shapes appear in `shapeIds`, and a shape that bridges two
56
- // otherwise-disjoint clusters merges them in one pass.
57
- const parents = expandedBounds.map((_, i) => i)
58
- const find = (i: number): number => {
59
- let root = i
60
- while (parents[root] !== root) root = parents[root]
61
- while (parents[i] !== root) {
62
- const next = parents[i]
63
- parents[i] = root
64
- i = next
52
+ for (let i = 0; i < expandedBounds.length; i++) {
53
+ const item = expandedBounds[i]
54
+ if (i === 0) {
55
+ groups[0] = {
56
+ shapes: [item.shape],
57
+ bounds: item.bounds,
58
+ }
59
+ continue
65
60
  }
66
- return root
67
- }
68
- const union = (i: number, j: number) => {
69
- const ri = find(i)
70
- const rj = find(j)
71
- if (ri !== rj) parents[ri] = rj
72
- }
73
61
 
74
- // Broad-phase sweep: sort by minX and, for each box, only compare against
75
- // later boxes until one starts past the current box's maxX. Boxes that
76
- // don't overlap on the x-axis can't intersect, so this skips the bulk of
77
- // the pairwise checks for spread-out selections while still finding every
78
- // intersecting pair (Box.includes is symmetric, so each pair is tested once).
79
- const order = expandedBounds
80
- .map((_, i) => i)
81
- .sort((a, b) => {
82
- return expandedBounds[a].bounds.minX - expandedBounds[b].bounds.minX
83
- })
62
+ let didLand = false
84
63
 
85
- for (let a = 0; a < order.length; a++) {
86
- const i = order[a]
87
- const boundsI = expandedBounds[i].bounds
88
- for (let b = a + 1; b < order.length; b++) {
89
- const j = order[b]
90
- // sorted by minX, so once a box starts past i's maxX no later box can overlap i
91
- if (expandedBounds[j].bounds.minX > boundsI.maxX) break
92
- if (find(i) === find(j)) continue
93
- if (boundsI.includes(expandedBounds[j].bounds)) {
94
- union(i, j)
64
+ for (const group of groups) {
65
+ if (group.bounds.includes(item.bounds)) {
66
+ group.shapes.push(item.shape)
67
+ group.bounds.expand(item.bounds)
68
+ didLand = true
69
+ break
95
70
  }
96
71
  }
97
- }
98
72
 
99
- const groupsByRoot = new Map<number, { shapes: TLShape[]; bounds: Box }>()
100
- for (let i = 0; i < expandedBounds.length; i++) {
101
- const root = find(i)
102
- const item = expandedBounds[i]
103
- const existing = groupsByRoot.get(root)
104
- if (existing) {
105
- existing.shapes.push(item.shape)
106
- existing.bounds.expand(item.bounds)
107
- } else {
108
- groupsByRoot.set(root, {
73
+ if (!didLand) {
74
+ groups.push({
109
75
  shapes: [item.shape],
110
- bounds: item.bounds.clone(),
76
+ bounds: item.bounds,
111
77
  })
112
78
  }
113
79
  }
114
-
115
- groups.push(...groupsByRoot.values())
116
80
  } else {
117
81
  const bounds = Box.Common(shapes.map((shape) => editor.getShapeMaskedPageBounds(shape)!))
118
82
  groups.push({
@@ -159,8 +123,11 @@ export async function flattenShapesToImages(
159
123
 
160
124
  let index: IndexKey = 'a1' as IndexKey
161
125
  for (const shape of shapes) {
162
- if (shape.parentId === commonAncestorId && shape.index > index) {
163
- index = shape.index
126
+ if (shape.parentId === commonAncestorId) {
127
+ if (shape.index > index) {
128
+ index = shape.index
129
+ }
130
+ break
164
131
  }
165
132
  }
166
133
 
@@ -1,9 +1,9 @@
1
1
  // This file is automatically generated by internal/scripts/refresh-assets.ts.
2
2
  // Do not edit manually. Or do, I'm a comment, not a cop.
3
3
 
4
- export const version = '5.4.0-canary.e10ff229bb14'
4
+ export const version = '5.4.0-next.09eca5dee30d'
5
5
  export const publishDates = {
6
6
  major: '2026-05-06T16:28:18.473Z',
7
- minor: '2026-08-07T15:30:34.999Z',
8
- patch: '2026-08-07T15:30:34.999Z',
7
+ minor: '2026-08-10T10:12:59.188Z',
8
+ patch: '2026-08-10T10:12:59.188Z',
9
9
  }
@@ -1,135 +0,0 @@
1
- import {
2
- AssetRecordType,
3
- TLImageAsset,
4
- TLShapeId,
5
- TLShapePartial,
6
- createShapeId,
7
- } from '@tldraw/editor'
8
- import { vi } from 'vitest'
9
- import { flattenShapesToImages } from '../lib/ui/hooks/useFlatten'
10
- import { TestEditor } from './TestEditor'
11
-
12
- let editor: TestEditor
13
- let assetCount = 0
14
-
15
- function installFlattenMocks(target: TestEditor) {
16
- // flattenShapesToImages calls editor.getSvgString and
17
- // editor.getAssetForExternalContent. Mock both to bypass the real export
18
- // pipeline (jsdom can't render SVGs and we don't need real pixels — these
19
- // tests only care about which shapes end up in which group).
20
- vi.spyOn(target, 'getSvgString').mockResolvedValue({
21
- svg: '<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"></svg>',
22
- width: 10,
23
- height: 10,
24
- trimPadding: 0,
25
- })
26
- vi.spyOn(target, 'getAssetForExternalContent').mockImplementation(async () => {
27
- const asset: TLImageAsset = {
28
- id: AssetRecordType.createId(`flat-${assetCount++}`),
29
- typeName: 'asset',
30
- type: 'image',
31
- props: {
32
- w: 10,
33
- h: 10,
34
- name: 'asset.svg',
35
- isAnimated: false,
36
- mimeType: 'image/svg+xml',
37
- src: 'data:image/svg+xml;base64,',
38
- },
39
- meta: {},
40
- }
41
- return asset
42
- })
43
- }
44
-
45
- beforeEach(() => {
46
- editor = new TestEditor()
47
- assetCount = 0
48
- installFlattenMocks(editor)
49
- })
50
-
51
- afterEach(() => {
52
- editor?.dispose()
53
- })
54
-
55
- const box = (id: TLShapeId, x: number, y: number, w = 50, h = 50): TLShapePartial => ({
56
- id,
57
- type: 'geo',
58
- x,
59
- y,
60
- props: { w, h, fill: 'solid' },
61
- })
62
-
63
- function getImageShapeCount() {
64
- return editor.getCurrentPageShapes().filter((s) => s.type === 'image').length
65
- }
66
-
67
- describe('flattenShapesToImages clustering', () => {
68
- const expand = 64
69
-
70
- it('groups shapes within the expand threshold into one image', async () => {
71
- const a = createShapeId('a')
72
- const b = createShapeId('b')
73
- // gap of 30 < 2 * 64 expand, so the expanded boxes intersect
74
- editor.createShapes([box(a, 0, 0), box(b, 80, 0)])
75
-
76
- await flattenShapesToImages(editor, [a, b], expand)
77
-
78
- expect(getImageShapeCount()).toBe(1)
79
- })
80
-
81
- it('keeps shapes in separate images when their gap exceeds the expand threshold', async () => {
82
- const a = createShapeId('a')
83
- const b = createShapeId('b')
84
- // gap of 200 > 2 * 64 expand, so the expanded boxes do not intersect
85
- editor.createShapes([box(a, 0, 0), box(b, 250, 0)])
86
-
87
- await flattenShapesToImages(editor, [a, b], expand)
88
-
89
- expect(getImageShapeCount()).toBe(2)
90
- })
91
-
92
- it('merges all shapes into one group when a bridging shape connects them, independent of input order', async () => {
93
- // Layout: A — gap — bridge — gap — C. A and C alone do not intersect
94
- // each other's expanded bounds, but `bridge` sits in the middle and
95
- // should pull them all into a single group regardless of where it
96
- // appears in the list. The previous greedy pass would only merge
97
- // `bridge` into the first group it touched, leaving C as a separate
98
- // cluster when `bridge` came after C in the list — which made the
99
- // result depend on selection order.
100
- const a = createShapeId('a')
101
- const bridge = createShapeId('bridge')
102
- const c = createShapeId('c')
103
-
104
- const orders: TLShapeId[][] = [
105
- [a, bridge, c],
106
- [a, c, bridge],
107
- [bridge, a, c],
108
- [c, bridge, a],
109
- ]
110
-
111
- for (const order of orders) {
112
- editor.dispose()
113
- editor = new TestEditor()
114
- assetCount = 0
115
- installFlattenMocks(editor)
116
- editor.createShapes([box(a, 0, 0), box(bridge, 100, 0), box(c, 200, 0)])
117
-
118
- await flattenShapesToImages(editor, order, expand)
119
-
120
- expect(getImageShapeCount(), `order ${order.join(',')}`).toBe(1)
121
- }
122
- })
123
-
124
- it('uses a single Box.Common group when no expand value is provided', async () => {
125
- // Two shapes far apart should still flatten to one image when called
126
- // via the no-expand path (the default useFlatten() hook).
127
- const a = createShapeId('a')
128
- const b = createShapeId('b')
129
- editor.createShapes([box(a, 0, 0), box(b, 1000, 0)])
130
-
131
- await flattenShapesToImages(editor, [a, b])
132
-
133
- expect(getImageShapeCount()).toBe(1)
134
- })
135
- })