2024-03-26 18:38:19 +00:00
|
|
|
import { Editor, Tldraw, createShapeId } from 'tldraw'
|
|
|
|
|
|
|
|
export default function BeforeDeleteShapeExample() {
|
|
|
|
return (
|
|
|
|
<div className="tldraw__editor">
|
|
|
|
<Tldraw
|
|
|
|
onMount={(editor) => {
|
|
|
|
// register a handler to run before any shape is deleted:
|
|
|
|
editor.sideEffects.registerBeforeDeleteHandler('shape', (shape) => {
|
|
|
|
// if the shape is red, prevent the deletion:
|
|
|
|
if ('color' in shape.props && shape.props.color === 'red') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
createDemoShapes(editor)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create some shapes to demonstrate the side-effect we added
|
|
|
|
function createDemoShapes(editor: Editor) {
|
2024-04-26 12:32:41 +00:00
|
|
|
const result = editor.createShapes([
|
|
|
|
{
|
|
|
|
id: createShapeId(),
|
|
|
|
type: 'text',
|
|
|
|
props: {
|
|
|
|
text: "Red shapes can't be deleted",
|
|
|
|
color: 'red',
|
2024-03-26 18:38:19 +00:00
|
|
|
},
|
2024-04-26 12:32:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: createShapeId(),
|
|
|
|
type: 'text',
|
|
|
|
y: 30,
|
|
|
|
props: {
|
|
|
|
text: 'but other shapes can',
|
|
|
|
color: 'black',
|
2024-03-26 18:38:19 +00:00
|
|
|
},
|
2024-04-26 12:32:41 +00:00
|
|
|
},
|
|
|
|
])
|
|
|
|
if (result.ok) {
|
|
|
|
editor.zoomToContent({ duration: 0 })
|
|
|
|
}
|
2024-03-26 18:38:19 +00:00
|
|
|
}
|