2023-09-06 10:07:18 +00:00
|
|
|
import { Editor } from '@tldraw/editor'
|
|
|
|
|
|
|
|
export function registerDefaultSideEffects(editor: Editor) {
|
2023-10-02 11:29:54 +00:00
|
|
|
return [
|
|
|
|
editor.sideEffects.registerAfterChangeHandler('instance', (prev, next) => {
|
|
|
|
if (prev.isFocused !== next.isFocused) {
|
|
|
|
if (next.isFocused) {
|
|
|
|
editor.getContainer().focus()
|
|
|
|
} else {
|
|
|
|
editor.complete() // stop any interaction
|
|
|
|
editor.getContainer().blur() // blur the container
|
2023-09-06 10:07:18 +00:00
|
|
|
}
|
2023-10-02 11:29:54 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
editor.sideEffects.registerAfterChangeHandler('instance_page_state', (prev, next) => {
|
|
|
|
if (prev.croppingShapeId !== next.croppingShapeId) {
|
|
|
|
const isInCroppingState = editor.isInAny(
|
|
|
|
'select.crop',
|
|
|
|
'select.pointing_crop_handle',
|
|
|
|
'select.cropping'
|
|
|
|
)
|
|
|
|
if (!prev.croppingShapeId && next.croppingShapeId) {
|
|
|
|
if (!isInCroppingState) {
|
|
|
|
editor.setCurrentTool('select.crop.idle')
|
|
|
|
}
|
|
|
|
} else if (prev.croppingShapeId && !next.croppingShapeId) {
|
|
|
|
if (isInCroppingState) {
|
|
|
|
editor.setCurrentTool('select.idle')
|
|
|
|
}
|
2023-09-06 10:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-02 11:29:54 +00:00
|
|
|
if (prev.editingShapeId !== next.editingShapeId) {
|
|
|
|
if (!prev.editingShapeId && next.editingShapeId) {
|
|
|
|
if (!editor.isIn('select.editing_shape')) {
|
|
|
|
editor.setCurrentTool('select.editing_shape')
|
|
|
|
}
|
|
|
|
} else if (prev.editingShapeId && !next.editingShapeId) {
|
|
|
|
if (editor.isIn('select.editing_shape')) {
|
|
|
|
editor.setCurrentTool('select.idle')
|
|
|
|
}
|
2023-09-06 10:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-02 11:29:54 +00:00
|
|
|
}),
|
|
|
|
]
|
2023-09-06 10:07:18 +00:00
|
|
|
}
|