UNPKG

tldraw

Version:

A tiny little drawing editor.

8 lines (7 loc) 5.41 kB
{ "version": 3, "sources": ["../../../../src/lib/tools/selection-logic/selectOnCanvasPointerUp.ts"], "sourcesContent": ["import { Editor, TLClickEventInfo, TLPointerEventInfo, TLShape, isShapeId } from '@tldraw/editor'\n\nexport function selectOnCanvasPointerUp(\n\teditor: Editor,\n\tinfo: TLPointerEventInfo | TLClickEventInfo\n) {\n\tconst selectedShapeIds = editor.getSelectedShapeIds()\n\tconst currentPagePoint = editor.inputs.getCurrentPagePoint()\n\tconst { shiftKey, altKey, accelKey } = info\n\tconst additiveSelectionKey = shiftKey || accelKey\n\n\tconst hitShape = editor.getShapeAtPoint(currentPagePoint, {\n\t\thitInside: false,\n\t\tmargin: editor.options.hitTestMargin / editor.getZoomLevel(),\n\t\thitLabels: true,\n\t\trenderingOnly: true,\n\t\tfilter: (shape) => !shape.isLocked,\n\t})\n\n\t// Note at the start: if we select a shape that is inside of a group,\n\t// the editor will automatically adjust the selection to the outermost\n\t// selectable shape (the group)\n\n\t// If the shape's outermost selected id (e.g. the group that contains\n\t// the shape) is not the same as the editor's only selected shape, then\n\t// we want to select the outermost selected shape instead of the shape\n\n\tif (hitShape) {\n\t\tconst outermostSelectableShape = editor.getOutermostSelectableShape(hitShape)\n\t\t// If the user is holding shift, they're either adding to or removing from\n\t\t// their selection.\n\t\tif (additiveSelectionKey && !altKey) {\n\t\t\teditor.cancelDoubleClick() // fuckin eh\n\n\t\t\tif (selectedShapeIds.includes(outermostSelectableShape.id)) {\n\t\t\t\t// Remove it from selected shapes\n\t\t\t\teditor.markHistoryStoppingPoint('deselecting shape')\n\t\t\t\teditor.deselect(outermostSelectableShape)\n\t\t\t} else {\n\t\t\t\t// Add it to selected shapes\n\t\t\t\teditor.markHistoryStoppingPoint('shift selecting shape')\n\t\t\t\teditor.setSelectedShapes([...selectedShapeIds, outermostSelectableShape.id])\n\t\t\t}\n\t\t} else {\n\t\t\tlet shapeToSelect: TLShape | undefined = undefined\n\n\t\t\tif (outermostSelectableShape === hitShape) {\n\t\t\t\t// There's no group around the shape, so we can select it.\n\t\t\t\tshapeToSelect = hitShape\n\t\t\t} else {\n\t\t\t\t// There's a group around the hit shape.\n\t\t\t\t// If the group is the current focus layer, OR if the group is\n\t\t\t\t// already selected, then we can select the shape inside the group.\n\t\t\t\t// Otherwise, if the group isn't selected and isn't our current\n\t\t\t\t// focus layer, then we need to select the group instead.\n\t\t\t\tif (\n\t\t\t\t\toutermostSelectableShape.id === editor.getFocusedGroupId() ||\n\t\t\t\t\tselectedShapeIds.includes(outermostSelectableShape.id)\n\t\t\t\t) {\n\t\t\t\t\tshapeToSelect = hitShape\n\t\t\t\t} else {\n\t\t\t\t\tshapeToSelect = outermostSelectableShape\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (shapeToSelect && !selectedShapeIds.includes(shapeToSelect.id)) {\n\t\t\t\teditor.markHistoryStoppingPoint('selecting shape')\n\t\t\t\teditor.select(shapeToSelect.id)\n\t\t\t}\n\t\t}\n\t} else {\n\t\t// We didn't hit anything...\n\t\tif (additiveSelectionKey) {\n\t\t\t// If we were holding shift, then it's a noop. We keep the\n\t\t\t// current selection because we didn't add anything to it\n\t\t\treturn\n\t\t} else {\n\t\t\t// Otherwise, we clear the selction because the user selected\n\t\t\t// nothing instead of their current selection.\n\n\t\t\tif (selectedShapeIds.length > 0) {\n\t\t\t\teditor.markHistoryStoppingPoint('selecting none')\n\t\t\t\teditor.selectNone()\n\t\t\t}\n\n\t\t\t// If the click was inside of the current focused group, then\n\t\t\t// we keep that focused group; otherwise we clear the focused\n\t\t\t// group (reset it to the page)\n\t\t\tconst focusedGroupId = editor.getFocusedGroupId()\n\n\t\t\tif (isShapeId(focusedGroupId)) {\n\t\t\t\tconst groupShape = editor.getShape(focusedGroupId)!\n\t\t\t\tif (!editor.isPointInShape(groupShape, currentPagePoint, { margin: 0, hitInside: true })) {\n\t\t\t\t\teditor.setFocusedGroup(null)\n\t\t\t\t}\n\t\t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