* 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)
| 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 |
no test coverage detected