import { AppClassProperties, AppState, Primitive } from "../types"; import { DEFAULT_ELEMENT_BACKGROUND_COLOR_PALETTE, DEFAULT_ELEMENT_BACKGROUND_PICKS, DEFAULT_ELEMENT_STROKE_COLOR_PALETTE, DEFAULT_ELEMENT_STROKE_PICKS, } from "../colors"; import { trackEvent } from "../analytics"; import { ButtonIconSelect } from "../components/ButtonIconSelect"; import { ColorPicker } from "../components/ColorPicker/ColorPicker"; import { IconPicker } from "../components/IconPicker"; // TODO barnabasmolnar/editor-redesign // TextAlignTopIcon, TextAlignBottomIcon,TextAlignMiddleIcon, // ArrowHead icons import { ArrowheadArrowIcon, ArrowheadBarIcon, ArrowheadCircleIcon, ArrowheadTriangleIcon, ArrowheadNoneIcon, StrokeStyleDashedIcon, StrokeStyleDottedIcon, TextAlignTopIcon, TextAlignBottomIcon, TextAlignMiddleIcon, FillHachureIcon, FillCrossHatchIcon, FillSolidIcon, SloppinessArchitectIcon, SloppinessArtistIcon, SloppinessCartoonistIcon, StrokeWidthBaseIcon, StrokeWidthBoldIcon, StrokeWidthExtraBoldIcon, FontSizeSmallIcon, FontSizeMediumIcon, FontSizeLargeIcon, FontSizeExtraLargeIcon, EdgeSharpIcon, EdgeRoundIcon, FreedrawIcon, FontFamilyNormalIcon, FontFamilyCodeIcon, TextAlignLeftIcon, TextAlignCenterIcon, TextAlignRightIcon, FillZigZagIcon, ArrowheadTriangleOutlineIcon, ArrowheadCircleOutlineIcon, ArrowheadDiamondIcon, ArrowheadDiamondOutlineIcon, fontSizeIcon, } from "../components/icons"; import { DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE, FONT_FAMILY, ROUNDNESS, STROKE_WIDTH, VERTICAL_ALIGN, } from "../constants"; import { getNonDeletedElements, isTextElement, redrawTextBoundingBox, } from "../element"; import { mutateElement, newElementWith } from "../element/mutateElement"; import { getBoundTextElement, getDefaultLineHeight, } from "../element/textElement"; import { isBoundToContainer, isLinearElement, isUsingAdaptiveRadius, } from "../element/typeChecks"; import { Arrowhead, ExcalidrawElement, ExcalidrawLinearElement, ExcalidrawTextElement, FontFamilyValues, TextAlign, VerticalAlign, } from "../element/types"; import { getLanguage, t } from "../i18n"; import { KEYS } from "../keys"; import { randomInteger } from "../random"; import { canHaveArrowheads, getCommonAttributeOfSelectedElements, getSelectedElements, getTargetElements, isSomeElementSelected, } from "../scene"; import { hasStrokeColor } from "../scene/comparisons"; import { arrayToMap, getShortcutKey } from "../utils"; import { register } from "./register"; const FONT_SIZE_RELATIVE_INCREASE_STEP = 0.1; export const changeProperty = ( elements: readonly ExcalidrawElement[], appState: AppState, callback: (element: ExcalidrawElement) => ExcalidrawElement, includeBoundText = false, ) => { const selectedElementIds = arrayToMap( getSelectedElements(elements, appState, { includeBoundTextElement: includeBoundText, }), ); return elements.map((element) => { if ( selectedElementIds.get(element.id) || element.id === appState.editingElement?.id ) { return callback(element); } return element; }); }; export const getFormValue = function ( elements: readonly ExcalidrawElement[], appState: AppState, getAttribute: (element: ExcalidrawElement) => T, isRelevantElement: true | ((element: ExcalidrawElement) => boolean), defaultValue: T | ((isSomeElementSelected: boolean) => T), ): T { const editingElement = appState.editingElement; const nonDeletedElements = getNonDeletedElements(elements); let ret: T | null = null; if (editingElement) { ret = getAttribute(editingElement); } if (!ret) { const hasSelection = isSomeElementSelected(nonDeletedElements, appState); if (hasSelection) { ret = getCommonAttributeOfSelectedElements( isRelevantElement === true ? nonDeletedElements : nonDeletedElements.filter((el) => isRelevantElement(el)), appState, getAttribute, ) ?? (typeof defaultValue === "function" ? defaultValue(true) : defaultValue); } else { ret = typeof defaultValue === "function" ? defaultValue(false) : defaultValue; } } return ret; }; const offsetElementAfterFontResize = ( prevElement: ExcalidrawTextElement, nextElement: ExcalidrawTextElement, ) => { if (isBoundToContainer(nextElement)) { return nextElement; } return mutateElement( nextElement, { x: prevElement.textAlign === "left" ? prevElement.x : prevElement.x + (prevElement.width - nextElement.width) / (prevElement.textAlign === "center" ? 2 : 1), // centering vertically is non-standard, but for Excalidraw I think // it makes sense y: prevElement.y + (prevElement.height - nextElement.height) / 2, }, false, ); }; const changeFontSize = ( elements: readonly ExcalidrawElement[], appState: AppState, app: AppClassProperties, getNewFontSize: (element: ExcalidrawTextElement) => number, fallbackValue?: ExcalidrawTextElement["fontSize"], ) => { const newFontSizes = new Set(); return { elements: changeProperty( elements, appState, (oldElement) => { if (isTextElement(oldElement)) { const newFontSize = getNewFontSize(oldElement); newFontSizes.add(newFontSize); let newElement: ExcalidrawTextElement = newElementWith(oldElement, { fontSize: newFontSize, }); redrawTextBoundingBox( newElement, app.scene.getContainerElement(oldElement), app.scene.getNonDeletedElementsMap(), ); newElement = offsetElementAfterFontResize(oldElement, newElement); return newElement; } return oldElement; }, true, ), appState: { ...appState, // update state only if we've set all select text elements to // the same font size currentItemFontSize: newFontSizes.size === 1 ? [...newFontSizes][0] : fallbackValue ?? appState.currentItemFontSize, }, commitToHistory: true, }; }; // ----------------------------------------------------------------------------- export const actionChangeStrokeColor = register({ name: "changeStrokeColor", label: "labels.stroke", trackEvent: false, perform: (elements, appState, value) => { return { ...(value.currentItemStrokeColor && { elements: changeProperty( elements, appState, (el) => { return hasStrokeColor(el.type) ? newElementWith(el, { strokeColor: value.currentItemStrokeColor, }) : el; }, true, ), }), appState: { ...appState, ...value, }, commitToHistory: !!value.currentItemStrokeColor, }; }, PanelComponent: ({ elements, appState, updateData, appProps }) => ( <> element.strokeColor, true, appState.currentItemStrokeColor, )} onChange={(color) => updateData({ currentItemStrokeColor: color })} elements={elements} appState={appState} updateData={updateData} /> ), }); export const actionChangeBackgroundColor = register({ name: "changeBackgroundColor", label: "labels.changeBackground", trackEvent: false, perform: (elements, appState, value) => { return { ...(value.currentItemBackgroundColor && { elements: changeProperty(elements, appState, (el) => newElementWith(el, { backgroundColor: value.currentItemBackgroundColor, }), ), }), appState: { ...appState, ...value, }, commitToHistory: !!value.currentItemBackgroundColor, }; }, PanelComponent: ({ elements, appState, updateData, appProps }) => ( <> element.backgroundColor, true, appState.currentItemBackgroundColor, )} onChange={(color) => updateData({ currentItemBackgroundColor: color })} elements={elements} appState={appState} updateData={updateData} /> ), }); export const actionChangeFillStyle = register({ name: "changeFillStyle", label: "labels.fill", trackEvent: false, perform: (elements, appState, value, app) => { trackEvent( "element", "changeFillStyle", `${value} (${app.device.editor.isMobile ? "mobile" : "desktop"})`, ); return { elements: changeProperty(elements, appState, (el) => newElementWith(el, { fillStyle: value, }), ), appState: { ...appState, currentItemFillStyle: value }, commitToHistory: true, }; }, PanelComponent: ({ elements, appState, updateData }) => { const selectedElements = getSelectedElements(elements, appState); const allElementsZigZag = selectedElements.length > 0 && selectedElements.every((el) => el.fillStyle === "zigzag"); return (
{t("labels.fill")} element.fillStyle, (element) => element.hasOwnProperty("fillStyle"), (hasSelection) => hasSelection ? null : appState.currentItemFillStyle, )} onClick={(value, event) => { const nextValue = event.altKey && value === "hachure" && selectedElements.every((el) => el.fillStyle === "hachure") ? "zigzag" : value; updateData(nextValue); }} />
); }, }); export const actionChangeStrokeWidth = register({ name: "changeStrokeWidth", label: "labels.strokeWidth", trackEvent: false, perform: (elements, appState, value) => { return { elements: changeProperty(elements, appState, (el) => newElementWith(el, { strokeWidth: value, }), ), appState: { ...appState, currentItemStrokeWidth: value }, commitToHistory: true, }; }, PanelComponent: ({ elements, appState, updateData }) => (
{t("labels.strokeWidth")} element.strokeWidth, (element) => element.hasOwnProperty("strokeWidth"), (hasSelection) => hasSelection ? null : appState.currentItemStrokeWidth, )} onChange={(value) => updateData(value)} />
), }); export const actionChangeSloppiness = register({ name: "changeSloppiness", label: "labels.sloppiness", trackEvent: false, perform: (elements, appState, value) => { return { elements: changeProperty(elements, appState, (el) => newElementWith(el, { seed: randomInteger(), roughness: value, }), ), appState: { ...appState, currentItemRoughness: value }, commitToHistory: true, }; }, PanelComponent: ({ elements, appState, updateData }) => (
{t("labels.sloppiness")} element.roughness, (element) => element.hasOwnProperty("roughness"), (hasSelection) => hasSelection ? null : appState.currentItemRoughness, )} onChange={(value) => updateData(value)} />
), }); export const actionChangeStrokeStyle = register({ name: "changeStrokeStyle", label: "labels.strokeStyle", trackEvent: false, perform: (elements, appState, value) => { return { elements: changeProperty(elements, appState, (el) => newElementWith(el, { strokeStyle: value, }), ), appState: { ...appState, currentItemStrokeStyle: value }, commitToHistory: true, }; }, PanelComponent: ({ elements, appState, updateData }) => (
{t("labels.strokeStyle")} element.strokeStyle, (element) => element.hasOwnProperty("strokeStyle"), (hasSelection) => hasSelection ? null : appState.currentItemStrokeStyle, )} onChange={(value) => updateData(value)} />
), }); export const actionChangeOpacity = register({ name: "changeOpacity", label: "labels.opacity", trackEvent: false, perform: (elements, appState, value) => { return { elements: changeProperty( elements, appState, (el) => newElementWith(el, { opacity: value, }), true, ), appState: { ...appState, currentItemOpacity: value }, commitToHistory: true, }; }, PanelComponent: ({ elements, appState, updateData }) => ( ), }); export const actionChangeFontSize = register({ name: "changeFontSize", label: "labels.fontSize", trackEvent: false, perform: (elements, appState, value, app) => { return changeFontSize(elements, appState, app, () => value, value); }, PanelComponent: ({ elements, appState, updateData, app }) => (
{t("labels.fontSize")} { if (isTextElement(element)) { return element.fontSize; } const boundTextElement = getBoundTextElement( element, app.scene.getNonDeletedElementsMap(), ); if (boundTextElement) { return boundTextElement.fontSize; } return null; }, (element) => isTextElement(element) || getBoundTextElement( element, app.scene.getNonDeletedElementsMap(), ) !== null, (hasSelection) => hasSelection ? null : appState.currentItemFontSize || DEFAULT_FONT_SIZE, )} onChange={(value) => updateData(value)} />
), }); export const actionDecreaseFontSize = register({ name: "decreaseFontSize", label: "labels.decreaseFontSize", icon: fontSizeIcon, trackEvent: false, perform: (elements, appState, value, app) => { return changeFontSize(elements, appState, app, (element) => Math.round( // get previous value before relative increase (doesn't work fully // due to rounding and float precision issues) (1 / (1 + FONT_SIZE_RELATIVE_INCREASE_STEP)) * element.fontSize, ), ); }, keyTest: (event) => { return ( event[KEYS.CTRL_OR_CMD] && event.shiftKey && // KEYS.COMMA needed for MacOS (event.key === KEYS.CHEVRON_LEFT || event.key === KEYS.COMMA) ); }, }); export const actionIncreaseFontSize = register({ name: "increaseFontSize", label: "labels.increaseFontSize", icon: fontSizeIcon, trackEvent: false, perform: (elements, appState, value, app) => { return changeFontSize(elements, appState, app, (element) => Math.round(element.fontSize * (1 + FONT_SIZE_RELATIVE_INCREASE_STEP)), ); }, keyTest: (event) => { return ( event[KEYS.CTRL_OR_CMD] && event.shiftKey && // KEYS.PERIOD needed for MacOS (event.key === KEYS.CHEVRON_RIGHT || event.key === KEYS.PERIOD) ); }, }); export const actionChangeFontFamily = register({ name: "changeFontFamily", label: "labels.fontFamily", trackEvent: false, perform: (elements, appState, value, app) => { return { elements: changeProperty( elements, appState, (oldElement) => { if (isTextElement(oldElement)) { const newElement: ExcalidrawTextElement = newElementWith( oldElement, { fontFamily: value, lineHeight: getDefaultLineHeight(value), }, ); redrawTextBoundingBox( newElement, app.scene.getContainerElement(oldElement), app.scene.getNonDeletedElementsMap(), ); return newElement; } return oldElement; }, true, ), appState: { ...appState, currentItemFontFamily: value, }, commitToHistory: true, }; }, PanelComponent: ({ elements, appState, updateData, app }) => { const options: { value: FontFamilyValues; text: string; icon: JSX.Element; testId: string; }[] = [ { value: FONT_FAMILY.Virgil, text: t("labels.handDrawn"), icon: FreedrawIcon, testId: "font-family-virgil", }, { value: FONT_FAMILY.Helvetica, text: t("labels.normal"), icon: FontFamilyNormalIcon, testId: "font-family-normal", }, { value: FONT_FAMILY.Cascadia, text: t("labels.code"), icon: FontFamilyCodeIcon, testId: "font-family-code", }, ]; return (
{t("labels.fontFamily")} group="font-family" options={options} value={getFormValue( elements, appState, (element) => { if (isTextElement(element)) { return element.fontFamily; } const boundTextElement = getBoundTextElement( element, app.scene.getNonDeletedElementsMap(), ); if (boundTextElement) { return boundTextElement.fontFamily; } return null; }, (element) => isTextElement(element) || getBoundTextElement( element, app.scene.getNonDeletedElementsMap(), ) !== null, (hasSelection) => hasSelection ? null : appState.currentItemFontFamily || DEFAULT_FONT_FAMILY, )} onChange={(value) => updateData(value)} />
); }, }); export const actionChangeTextAlign = register({ name: "changeTextAlign", label: "Change text alignment", trackEvent: false, perform: (elements, appState, value, app) => { return { elements: changeProperty( elements, appState, (oldElement) => { if (isTextElement(oldElement)) { const newElement: ExcalidrawTextElement = newElementWith( oldElement, { textAlign: value }, ); redrawTextBoundingBox( newElement, app.scene.getContainerElement(oldElement), app.scene.getNonDeletedElementsMap(), ); return newElement; } return oldElement; }, true, ), appState: { ...appState, currentItemTextAlign: value, }, commitToHistory: true, }; }, PanelComponent: ({ elements, appState, updateData, app }) => { const elementsMap = app.scene.getNonDeletedElementsMap(); return (
{t("labels.textAlign")} group="text-align" options={[ { value: "left", text: t("labels.left"), icon: TextAlignLeftIcon, testId: "align-left", }, { value: "center", text: t("labels.center"), icon: TextAlignCenterIcon, testId: "align-horizontal-center", }, { value: "right", text: t("labels.right"), icon: TextAlignRightIcon, testId: "align-right", }, ]} value={getFormValue( elements, appState, (element) => { if (isTextElement(element)) { return element.textAlign; } const boundTextElement = getBoundTextElement( element, elementsMap, ); if (boundTextElement) { return boundTextElement.textAlign; } return null; }, (element) => isTextElement(element) || getBoundTextElement(element, elementsMap) !== null, (hasSelection) => hasSelection ? null : appState.currentItemTextAlign, )} onChange={(value) => updateData(value)} />
); }, }); export const actionChangeVerticalAlign = register({ name: "changeVerticalAlign", label: "Change vertical alignment", trackEvent: { category: "element" }, perform: (elements, appState, value, app) => { return { elements: changeProperty( elements, appState, (oldElement) => { if (isTextElement(oldElement)) { const newElement: ExcalidrawTextElement = newElementWith( oldElement, { verticalAlign: value }, ); redrawTextBoundingBox( newElement, app.scene.getContainerElement(oldElement), app.scene.getNonDeletedElementsMap(), ); return newElement; } return oldElement; }, true, ), appState: { ...appState, }, commitToHistory: true, }; }, PanelComponent: ({ elements, appState, updateData, app }) => { return (
group="text-align" options={[ { value: VERTICAL_ALIGN.TOP, text: t("labels.alignTop"), icon: , testId: "align-top", }, { value: VERTICAL_ALIGN.MIDDLE, text: t("labels.centerVertically"), icon: , testId: "align-middle", }, { value: VERTICAL_ALIGN.BOTTOM, text: t("labels.alignBottom"), icon: , testId: "align-bottom", }, ]} value={getFormValue( elements, appState, (element) => { if (isTextElement(element) && element.containerId) { return element.verticalAlign; } const boundTextElement = getBoundTextElement( element, app.scene.getNonDeletedElementsMap(), ); if (boundTextElement) { return boundTextElement.verticalAlign; } return null; }, (element) => isTextElement(element) || getBoundTextElement( element, app.scene.getNonDeletedElementsMap(), ) !== null, (hasSelection) => (hasSelection ? null : VERTICAL_ALIGN.MIDDLE), )} onChange={(value) => updateData(value)} />
); }, }); export const actionChangeRoundness = register({ name: "changeRoundness", label: "Change edge roundness", trackEvent: false, perform: (elements, appState, value) => { return { elements: changeProperty(elements, appState, (el) => newElementWith(el, { roundness: value === "round" ? { type: isUsingAdaptiveRadius(el.type) ? ROUNDNESS.ADAPTIVE_RADIUS : ROUNDNESS.PROPORTIONAL_RADIUS, } : null, }), ), appState: { ...appState, currentItemRoundness: value, }, commitToHistory: true, }; }, PanelComponent: ({ elements, appState, updateData }) => { const targetElements = getTargetElements( getNonDeletedElements(elements), appState, ); const hasLegacyRoundness = targetElements.some( (el) => el.roundness?.type === ROUNDNESS.LEGACY, ); return (
{t("labels.edges")} hasLegacyRoundness ? null : element.roundness ? "round" : "sharp", (element) => element.hasOwnProperty("roundness"), (hasSelection) => hasSelection ? null : appState.currentItemRoundness, )} onChange={(value) => updateData(value)} />
); }, }); const getArrowheadOptions = (flip: boolean) => { return [ { value: null, text: t("labels.arrowhead_none"), keyBinding: "q", icon: ArrowheadNoneIcon, }, { value: "arrow", text: t("labels.arrowhead_arrow"), keyBinding: "w", icon: , }, { value: "bar", text: t("labels.arrowhead_bar"), keyBinding: "e", icon: , }, { value: "dot", text: t("labels.arrowhead_circle"), keyBinding: null, icon: , showInPicker: false, }, { value: "circle", text: t("labels.arrowhead_circle"), keyBinding: "r", icon: , showInPicker: false, }, { value: "circle_outline", text: t("labels.arrowhead_circle_outline"), keyBinding: null, icon: , showInPicker: false, }, { value: "triangle", text: t("labels.arrowhead_triangle"), icon: , keyBinding: "t", }, { value: "triangle_outline", text: t("labels.arrowhead_triangle_outline"), icon: , keyBinding: null, showInPicker: false, }, { value: "diamond", text: t("labels.arrowhead_diamond"), icon: , keyBinding: null, showInPicker: false, }, { value: "diamond_outline", text: t("labels.arrowhead_diamond_outline"), icon: , keyBinding: null, showInPicker: false, }, ] as const; }; export const actionChangeArrowhead = register({ name: "changeArrowhead", label: "Change arrowheads", trackEvent: false, perform: ( elements, appState, value: { position: "start" | "end"; type: Arrowhead }, ) => { return { elements: changeProperty(elements, appState, (el) => { if (isLinearElement(el)) { const { position, type } = value; if (position === "start") { const element: ExcalidrawLinearElement = newElementWith(el, { startArrowhead: type, }); return element; } else if (position === "end") { const element: ExcalidrawLinearElement = newElementWith(el, { endArrowhead: type, }); return element; } } return el; }), appState: { ...appState, [value.position === "start" ? "currentItemStartArrowhead" : "currentItemEndArrowhead"]: value.type, }, commitToHistory: true, }; }, PanelComponent: ({ elements, appState, updateData }) => { const isRTL = getLanguage().rtl; return (
{t("labels.arrowheads")}
( elements, appState, (element) => isLinearElement(element) && canHaveArrowheads(element.type) ? element.startArrowhead : appState.currentItemStartArrowhead, true, appState.currentItemStartArrowhead, )} onChange={(value) => updateData({ position: "start", type: value })} /> ( elements, appState, (element) => isLinearElement(element) && canHaveArrowheads(element.type) ? element.endArrowhead : appState.currentItemEndArrowhead, true, appState.currentItemEndArrowhead, )} onChange={(value) => updateData({ position: "end", type: value })} />
); }, });