edfbac9d7d
* feat: support unbinding text * fix unbound text * move the unbind option next to group action * use boundTextElement.id when unbinding * update original text so it takes same bounding box when unbind * Add spec * recompute measurements when unbinding
45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import { getNonDeletedElements } from "../element";
|
|
import { mutateElement } from "../element/mutateElement";
|
|
import { getBoundTextElement, measureText } from "../element/textElement";
|
|
import { ExcalidrawTextElement } from "../element/types";
|
|
import { getSelectedElements } from "../scene";
|
|
import { getFontString } from "../utils";
|
|
import { register } from "./register";
|
|
|
|
export const actionUnbindText = register({
|
|
name: "unbindText",
|
|
contextItemLabel: "labels.unbindText",
|
|
perform: (elements, appState) => {
|
|
const selectedElements = getSelectedElements(
|
|
getNonDeletedElements(elements),
|
|
appState,
|
|
);
|
|
selectedElements.forEach((element) => {
|
|
const boundTextElement = getBoundTextElement(element);
|
|
if (boundTextElement) {
|
|
const { width, height, baseline } = measureText(
|
|
boundTextElement.originalText,
|
|
getFontString(boundTextElement),
|
|
);
|
|
mutateElement(boundTextElement as ExcalidrawTextElement, {
|
|
containerId: null,
|
|
width,
|
|
height,
|
|
baseline,
|
|
text: boundTextElement.originalText,
|
|
});
|
|
mutateElement(element, {
|
|
boundElements: element.boundElements?.filter(
|
|
(ele) => ele.id !== boundTextElement.id,
|
|
),
|
|
});
|
|
}
|
|
});
|
|
return {
|
|
elements,
|
|
appState,
|
|
commitToHistory: true,
|
|
};
|
|
},
|
|
});
|