import React from "react"; import { Action } from "./types"; import { ExcalidrawElement, ExcalidrawTextElement } from "../element/types"; import { getSelectedAttribute } from "../scene"; import { ButtonSelect } from "../components/ButtonSelect"; import { PanelColor } from "../components/panels/PanelColor"; import { isTextElement, redrawTextBoundingBox } from "../element"; const changeProperty = ( elements: readonly ExcalidrawElement[], callback: (element: ExcalidrawElement) => ExcalidrawElement ) => { return elements.map(element => { if (element.isSelected) { return callback(element); } return element; }); }; export const actionChangeStrokeColor: Action = { name: "changeStrokeColor", perform: (elements, appState, value) => { return { elements: changeProperty(elements, el => ({ ...el, strokeColor: value })), appState: { ...appState, currentItemStrokeColor: value } }; }, PanelComponent: ({ elements, appState, updateData }) => ( { updateData(color); }} colorValue={getSelectedAttribute( elements, element => element.strokeColor )} /> ) }; export const actionChangeBackgroundColor: Action = { name: "changeBackgroundColor", perform: (elements, appState, value) => { return { elements: changeProperty(elements, el => ({ ...el, backgroundColor: value })), appState: { ...appState, currentItemBackgroundColor: value } }; }, PanelComponent: ({ elements, updateData }) => ( { updateData(color); }} colorValue={getSelectedAttribute( elements, element => element.backgroundColor )} /> ) }; export const actionChangeFillStyle: Action = { name: "changeFillStyle", perform: (elements, appState, value) => { return { elements: changeProperty(elements, el => ({ ...el, fillStyle: value })) }; }, PanelComponent: ({ elements, updateData }) => ( <>
Fill
element.fillStyle)} onChange={value => { updateData(value); }} /> ) }; export const actionChangeStrokeWidth: Action = { name: "changeStrokeWidth", perform: (elements, appState, value) => { return { elements: changeProperty(elements, el => ({ ...el, strokeWidth: value })) }; }, PanelComponent: ({ elements, appState, updateData }) => ( <>
Stroke Width
element.strokeWidth)} onChange={value => updateData(value)} /> ) }; export const actionChangeSloppiness: Action = { name: "changeSloppiness", perform: (elements, appState, value) => { return { elements: changeProperty(elements, el => ({ ...el, roughness: value })) }; }, PanelComponent: ({ elements, appState, updateData }) => ( <>
Sloppiness
element.roughness)} onChange={value => updateData(value)} /> ) }; export const actionChangeOpacity: Action = { name: "changeOpacity", perform: (elements, appState, value) => { return { elements: changeProperty(elements, el => ({ ...el, opacity: value })) }; }, PanelComponent: ({ elements, updateData }) => ( <>
Opacity
updateData(+e.target.value)} value={ getSelectedAttribute(elements, element => element.opacity) || 0 /* Put the opacity at 0 if there are two conflicting ones */ } /> ) }; export const actionChangeFontSize: Action = { name: "changeFontSize", perform: (elements, appState, value) => { return { elements: changeProperty(elements, el => { if (isTextElement(el)) { const element: ExcalidrawTextElement = { ...el, font: `${value}px ${el.font.split("px ")[1]}` }; redrawTextBoundingBox(element); return element; } return el; }) }; }, PanelComponent: ({ elements, updateData }) => ( <>
Font size
isTextElement(element) && +element.font.split("px ")[0] )} onChange={value => updateData(value)} /> ) }; export const actionChangeFontFamily: Action = { name: "changeFontFamily", perform: (elements, appState, value) => { return { elements: changeProperty(elements, el => { if (isTextElement(el)) { const element: ExcalidrawTextElement = { ...el, font: `${el.font.split("px ")[0]}px ${value}` }; redrawTextBoundingBox(element); return element; } return el; }) }; }, PanelComponent: ({ elements, updateData }) => ( <>
Font family
isTextElement(element) && element.font.split("px ")[1] )} onChange={value => updateData(value)} /> ) };