MCPcopy
hub / github.com/tldraw/tldraw / setEditingShape

Method setEditingShape

packages/editor/src/lib/editor/Editor.ts:2731–2787  ·  view source on GitHub ↗

* Set the current editing shape. * * @example * ```ts * editor.setEditingShape(myShape) * editor.setEditingShape(myShape.id) * ``` * * @param shape - The shape (or shape id) to set as editing. * * @public

(shape: TLShapeId | TLShape | null)

Source from the content-addressed store, hash-verified

2729 * @public
2730 */
2731 setEditingShape(shape: TLShapeId | TLShape | null): this {
2732 const id = typeof shape === 'string' ? shape : (shape?.id ?? null)
2733
2734 if (!id) {
2735 // setting the editing shape to null
2736 this.run(
2737 () => {
2738 // Clean up the previous editing shape
2739 const prevEditingShapeId = this.getEditingShapeId()
2740 if (prevEditingShapeId) {
2741 const prevEditingShape = this.getShape(prevEditingShapeId)
2742 if (prevEditingShape) {
2743 this.getShapeUtil(prevEditingShape).onEditEnd?.(prevEditingShape)
2744 }
2745 }
2746
2747 // Clean up the editing shape state and rich text editor
2748 this._updateCurrentPageState({ editingShapeId: null })
2749 this._currentRichTextEditor.set(null)
2750 },
2751 { history: 'ignore' }
2752 )
2753
2754 return this
2755 }
2756
2757 // id was provided but the next editing shape was not editable or didn't exist, so do nothing
2758 if (!this.canEditShape(id)) return this
2759
2760 // id was provided and the next editing shape is editable, so set the rich text editor to null
2761 this.run(
2762 () => {
2763 // Clean up the previous editing shape
2764 const prevEditingShapeId = this.getEditingShapeId()
2765 if (prevEditingShapeId) {
2766 const prevEditingShape = this.getShape(prevEditingShapeId)
2767 if (prevEditingShape) {
2768 this.getShapeUtil(prevEditingShape).onEditEnd?.(prevEditingShape)
2769 }
2770 }
2771
2772 // Clean up the editing shape state and rich text editor
2773 this._updateCurrentPageState({ editingShapeId: null })
2774 this._currentRichTextEditor.set(null)
2775
2776 // Set the new editing shape
2777 this.select(id)
2778 this._updateCurrentPageState({ editingShapeId: id })
2779
2780 const nextEditingShape = this.getShape(id)! // shape should be there because canEditShape checked it. Possible small chance that onEditEnd deleted it?
2781 this.getShapeUtil(nextEditingShape).onEditStart?.(nextEditingShape)
2782 },
2783 { history: 'ignore' }
2784 )
2785
2786 return this
2787 }
2788

Callers 15

onExitMethod · 0.80
onExitMethod · 0.80
onPointerDownMethod · 0.80
onPointerUpMethod · 0.80
onPointerUpMethod · 0.80
startEditingShapeMethod · 0.80
Editor.test.tsxFile · 0.80
SelectTool.test.tsFile · 0.80
pinch.test.tsFile · 0.80

Calls 10

runMethod · 0.95
getEditingShapeIdMethod · 0.95
getShapeMethod · 0.95
getShapeUtilMethod · 0.95
canEditShapeMethod · 0.95
selectMethod · 0.95
onEditStartMethod · 0.80
setMethod · 0.65
onEditEndMethod · 0.45

Tested by

no test coverage detected