[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`
pull/1661/head
Steve Ruiz 2023-06-27 15:51:35 +01:00 zatwierdzone przez GitHub
rodzic 2d5b2bdc94
commit 7fd0ab75ed
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
5 zmienionych plików z 52 dodań i 3 usunięć

Wyświetl plik

@ -1,11 +1,12 @@
import { resizeBox } from '@tldraw/editor/src/lib/editor/shapes/shared/resizeBox'
import {
Box2d,
HTMLContainer,
ShapeUtil,
TLOnResizeHandler,
getDefaultColorTheme,
resizeBox,
} from '@tldraw/tldraw'
import { useState } from 'react'
import { ICardShape } from './card-shape-types'
// 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 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 (
<HTMLContainer
id={shape.id}
style={{
border: '1px solid black',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
pointerEvents: 'all',
backgroundColor: theme[shape.props.color].semi,
fontWeight: shape.props.weight,
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>
)
}

Wyświetl plik

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

Wyświetl plik

@ -1821,6 +1821,37 @@ export const REMOVE_SYMBOL: unique symbol;
// @public (undocumented)
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)
export const RICH_TYPES: Record<string, boolean>;

Wyświetl plik

@ -139,6 +139,7 @@ export { LineShape } from './lib/editor/shapes/line/LineShape'
export { LineShapeUtil, getSplineForLineShape } from './lib/editor/shapes/line/LineShapeUtil'
export { NoteShape } from './lib/editor/shapes/note/NoteShape'
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 { INDENT, TextShapeUtil } from './lib/editor/shapes/text/TextShapeUtil'
export { VideoShape } from './lib/editor/shapes/video/VideoShape'

Wyświetl plik

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