-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
829 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/** | ||
* Used some code from https://tldraw.dev/examples/shapes/tools/screenshot-tool | ||
*/ | ||
import * as React from "react"; | ||
import BoundsSelectorTool, { | ||
BoundsDraggingState, | ||
BOUNDS_BOX, | ||
BOUNDS_SHAPES_BOX, | ||
BOUNDS_USING_ASPECT_RATIO, | ||
BOUNDS_ASPECT_RATIO, | ||
BOUNDS_CURRENT_BOX, | ||
BOUNDS_SELECTOR_INITIALIZED | ||
} from "src/tldraw/tools/bounds-selector-tool"; | ||
import { Box, Editor, useEditor, useValue } from "tldraw"; | ||
|
||
function calculateBoundingBoxInEditor(box: Box, editor: Editor) { | ||
const zoomLevel = editor.getZoomLevel() | ||
const { x, y } = editor.pageToViewport({ x: box.x, y: box.y }) | ||
return new Box(x, y, box.w * zoomLevel, box.h * zoomLevel) | ||
} | ||
|
||
export default function BoundsTool() { | ||
const editor = useEditor(); | ||
|
||
const toolInitialized = useValue(BOUNDS_SELECTOR_INITIALIZED, () => ( | ||
editor.getStateDescendant<BoundsSelectorTool>(BoundsSelectorTool.id)?.boundsSelectorInitialized.get() | ||
), [editor]); | ||
|
||
React.useEffect( | ||
() => { | ||
if (toolInitialized === undefined) { | ||
return; | ||
} | ||
if (!toolInitialized) { | ||
editor.getStateDescendant<BoundsSelectorTool>(BoundsSelectorTool.id)?.init(); | ||
} | ||
}, | ||
[toolInitialized] | ||
); | ||
|
||
const currentBox = useValue(BOUNDS_CURRENT_BOX, | ||
() => { | ||
const selectorTool = editor.getStateDescendant<BoundsSelectorTool>(BoundsSelectorTool.id); | ||
const box = selectorTool?.currentBounds.get() | ||
|
||
if (!box) return; | ||
|
||
return calculateBoundingBoxInEditor(box, editor); | ||
}, [editor], | ||
); | ||
|
||
const boundsBox = useValue(BOUNDS_BOX, | ||
() => { | ||
if (editor.getPath() !== BoundsSelectorTool.draggingStatePath) return null; | ||
|
||
const draggingState = editor.getStateDescendant<BoundsDraggingState>(BoundsSelectorTool.draggingStatePath)!; | ||
const box = draggingState.boundsBox.get() | ||
|
||
return calculateBoundingBoxInEditor(box, editor); | ||
}, [editor], | ||
); | ||
|
||
const shapesBox = useValue(BOUNDS_SHAPES_BOX, | ||
() => { | ||
if (editor.getPath() !== BoundsSelectorTool.draggingStatePath) return null; | ||
|
||
const draggingState = editor.getStateDescendant<BoundsDraggingState>(BoundsSelectorTool.draggingStatePath)!; | ||
const box = draggingState.shapesBox.get() | ||
|
||
if (!box) return null; | ||
|
||
return calculateBoundingBoxInEditor(box, editor); | ||
}, [editor], | ||
); | ||
|
||
const boundsUsingAspectRatio = useValue(BOUNDS_USING_ASPECT_RATIO, () => ( | ||
editor.getStateDescendant<BoundsDraggingState>(BoundsSelectorTool.draggingStatePath)?.boundsUsingAspectRatio.get() ?? false | ||
), [editor]); | ||
|
||
const boundsAspectRatio = useValue(BOUNDS_ASPECT_RATIO, () => ( | ||
editor.getStateDescendant<BoundsSelectorTool>(BoundsSelectorTool.id)?.aspectRatio.get() | ||
), [editor]); | ||
|
||
return ( | ||
<> | ||
{ | ||
!currentBox ? <></> : ( | ||
<div | ||
className="ptl-embed-bounds-selection" | ||
style={{ | ||
pointerEvents: 'none', | ||
transform: `translate(${currentBox.x}px, ${currentBox.y}px)`, | ||
width: currentBox.w, | ||
height: currentBox.h, | ||
}} | ||
data-target-bounds={!boundsBox} | ||
/> | ||
) | ||
} | ||
{ | ||
!boundsBox ? <></> : ( | ||
<div | ||
className="ptl-embed-bounds-selection" | ||
style={{ | ||
transform: `translate(${boundsBox.x}px, ${boundsBox.y}px)`, | ||
width: boundsBox.w, | ||
height: boundsBox.h, | ||
}} | ||
data-shade-bg={!shapesBox} | ||
data-target-bounds={!shapesBox} | ||
> | ||
Hold Ctrl to use the bounds within the shapes. | ||
<br /> | ||
Hold Shift to use an aspect ratio. | ||
<br /> | ||
Press Alt to cycle through aspect ratios. | ||
{ | ||
!boundsAspectRatio || !boundsUsingAspectRatio ? <></> : ( | ||
<> | ||
<br /> | ||
Aspect ratio: {boundsAspectRatio.w}:{boundsAspectRatio.h} | ||
</> | ||
) | ||
} | ||
</div> | ||
) | ||
} | ||
{ | ||
!shapesBox ? <></> : ( | ||
<div | ||
className="ptl-embed-bounds-selection" | ||
style={{ | ||
transform: `translate(${shapesBox.x}px, ${shapesBox.y}px)`, | ||
width: shapesBox.w, | ||
height: shapesBox.h, | ||
}} | ||
data-shade-bg={true} | ||
data-target-bounds={true} | ||
/> | ||
) | ||
} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* https://tldraw.dev/examples/editor-api/indicators-logic | ||
*/ | ||
import * as React from "react" | ||
import BoundsSelectorTool, { BOUNDS_SELECTED_SHAPES, BoundsDraggingState } from "src/tldraw/tools/bounds-selector-tool" | ||
import { useEditor, useEditorComponents, useValue } from "tldraw" | ||
|
||
export default function BoundsToolSelectedShapeIndicator() { | ||
const editor = useEditor() | ||
|
||
const boundsSelectedShapes = useValue(BOUNDS_SELECTED_SHAPES, | ||
() => { | ||
if (editor.getPath() !== BoundsSelectorTool.draggingStatePath) return null; | ||
|
||
const draggingState = editor.getStateDescendant<BoundsDraggingState>(BoundsSelectorTool.draggingStatePath)!; | ||
return draggingState.selectedShapes.get(); | ||
}, [editor], | ||
); | ||
|
||
const { ShapeIndicator } = useEditorComponents() | ||
if (!ShapeIndicator || !boundsSelectedShapes || !boundsSelectedShapes.length) return null | ||
|
||
return ( | ||
<div style={{ position: 'absolute', top: 0, left: 0, zIndex: 9999 }}> | ||
{boundsSelectedShapes.map(({ id }) => ( | ||
<ShapeIndicator key={id + '_indicator'} shapeId={id} /> | ||
))} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as React from "react"; | ||
import BoundsSelectorTool from "src/tldraw/tools/bounds-selector-tool"; | ||
import { DefaultToolbar, DefaultToolbarContent, TldrawUiMenuItem, useIsToolSelected, useTools } from "tldraw"; | ||
|
||
export default function EmbedTldrawToolBar() { | ||
const tools = useTools(); | ||
const boundsSelectorTool = tools[BoundsSelectorTool.id]; | ||
const isBoundsSelectorSelected = useIsToolSelected(boundsSelectorTool); | ||
return ( | ||
<DefaultToolbar> | ||
<TldrawUiMenuItem isSelected={isBoundsSelectorSelected} {...boundsSelectorTool} /> | ||
<DefaultToolbarContent /> | ||
</DefaultToolbar> | ||
) | ||
} |
Oops, something went wrong.