[improvement] custom shapes example (#1660)

This PR fixes an import in the custom shapes example. It also tweaks the
example to show how buttons and other interactive content should work.

### Change Type

- [x] `documentation`
This commit is contained in:
Steve Ruiz 2023-06-27 15:51:35 +01:00 committed by GitHub
parent 2d5b2bdc94
commit 7fd0ab75ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 3 deletions

View file

@ -1,11 +1,12 @@
import { resizeBox } from '@tldraw/editor/src/lib/editor/shapes/shared/resizeBox'
import { import {
Box2d, Box2d,
HTMLContainer, HTMLContainer,
ShapeUtil, ShapeUtil,
TLOnResizeHandler, TLOnResizeHandler,
getDefaultColorTheme, getDefaultColorTheme,
resizeBox,
} from '@tldraw/tldraw' } from '@tldraw/tldraw'
import { useState } from 'react'
import { ICardShape } from './card-shape-types' import { ICardShape } from './card-shape-types'
// A utility class for the card shape. This is where you define // A utility class for the card shape. This is where you define
@ -38,20 +39,34 @@ export class CardShapeUtil extends ShapeUtil<ICardShape> {
const bounds = this.editor.getBounds(shape) const bounds = this.editor.getBounds(shape)
const theme = getDefaultColorTheme(this.editor) const theme = getDefaultColorTheme(this.editor)
// Unfortunately eslint will think this is a class components
// eslint-disable-next-line react-hooks/rules-of-hooks
const [count, setCount] = useState(0)
return ( return (
<HTMLContainer <HTMLContainer
id={shape.id} id={shape.id}
style={{ style={{
border: '1px solid black', border: '1px solid black',
display: 'flex', display: 'flex',
flexDirection: 'column',
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',
pointerEvents: 'all', pointerEvents: 'all',
backgroundColor: theme[shape.props.color].semi,
fontWeight: shape.props.weight, fontWeight: shape.props.weight,
color: theme[shape.props.color].solid, color: theme[shape.props.color].solid,
}} }}
> >
{bounds.w.toFixed()}x{bounds.h.toFixed()} <h2>Clicks: {count}</h2>
<button
onClick={() => setCount((count) => count + 1)}
// You need to stop the pointer down event on buttons
// that should prevent shape selection or click and drag
onPointerDown={(e) => e.stopPropagation()}
>
{bounds.w.toFixed()}x{bounds.h.toFixed()}
</button>
</HTMLContainer> </HTMLContainer>
) )
} }

View file

@ -1,3 +1,3 @@
import { CardShape } from '../16-custom-styles/CardShape' import { CardShape } from './CardShape/CardShape'
export const customShapes = [CardShape] export const customShapes = [CardShape]

View file

@ -1821,6 +1821,37 @@ export const REMOVE_SYMBOL: unique symbol;
// @public (undocumented) // @public (undocumented)
export type RequiredKeys<T, K extends keyof T> = Pick<T, K> & Partial<T>; export type RequiredKeys<T, K extends keyof T> = Pick<T, K> & Partial<T>;
// @public (undocumented)
export function resizeBox(shape: TLBaseBoxShape, info: {
newPoint: Vec2dModel;
handle: TLResizeHandle;
mode: TLResizeMode;
scaleX: number;
scaleY: number;
initialBounds: Box2d;
initialShape: TLBaseBoxShape;
}, opts?: Partial<{
minWidth: number;
maxWidth: number;
minHeight: number;
maxHeight: number;
}>): {
x: number;
y: number;
props: {
w: number;
h: number;
};
};
// @public (undocumented)
export type ResizeBoxOptions = Partial<{
minWidth: number;
maxWidth: number;
minHeight: number;
maxHeight: number;
}>;
// @internal (undocumented) // @internal (undocumented)
export const RICH_TYPES: Record<string, boolean>; export const RICH_TYPES: Record<string, boolean>;

View file

@ -139,6 +139,7 @@ export { LineShape } from './lib/editor/shapes/line/LineShape'
export { LineShapeUtil, getSplineForLineShape } from './lib/editor/shapes/line/LineShapeUtil' export { LineShapeUtil, getSplineForLineShape } from './lib/editor/shapes/line/LineShapeUtil'
export { NoteShape } from './lib/editor/shapes/note/NoteShape' export { NoteShape } from './lib/editor/shapes/note/NoteShape'
export { NoteShapeUtil } from './lib/editor/shapes/note/NoteShapeUtil' export { NoteShapeUtil } from './lib/editor/shapes/note/NoteShapeUtil'
export { resizeBox, type ResizeBoxOptions } from './lib/editor/shapes/shared/resizeBox'
export { TextShape } from './lib/editor/shapes/text/TextShape' export { TextShape } from './lib/editor/shapes/text/TextShape'
export { INDENT, TextShapeUtil } from './lib/editor/shapes/text/TextShapeUtil' export { INDENT, TextShapeUtil } from './lib/editor/shapes/text/TextShapeUtil'
export { VideoShape } from './lib/editor/shapes/video/VideoShape' export { VideoShape } from './lib/editor/shapes/video/VideoShape'

View file

@ -4,6 +4,7 @@ import { TLResizeHandle } from '../../types/selection-types'
import { TLBaseBoxShape } from '../BaseBoxShapeUtil' import { TLBaseBoxShape } from '../BaseBoxShapeUtil'
import { TLResizeMode } from '../ShapeUtil' import { TLResizeMode } from '../ShapeUtil'
/** @public */
export type ResizeBoxOptions = Partial<{ export type ResizeBoxOptions = Partial<{
minWidth: number minWidth: number
maxWidth: number maxWidth: number
@ -11,6 +12,7 @@ export type ResizeBoxOptions = Partial<{
maxHeight: number maxHeight: number
}> }>
/** @public */
export function resizeBox( export function resizeBox(
shape: TLBaseBoxShape, shape: TLBaseBoxShape,
info: { info: {