excalidraw/src/actions/actionProperties.tsx

329 lines
8.9 KiB
TypeScript
Raw Normal View History

import React from "react";
import { Action } from "./types";
import { ExcalidrawElement, ExcalidrawTextElement } from "../element/types";
import { getCommonAttributeOfSelectedElements } from "../scene";
import { ButtonSelect } from "../components/ButtonSelect";
import { isTextElement, redrawTextBoundingBox } from "../element";
import { ColorPicker } from "../components/ColorPicker";
import { AppState } from "../../src/types";
import { t } from "../i18n";
const changeProperty = (
elements: readonly ExcalidrawElement[],
2020-01-24 12:04:54 +02:00
callback: (element: ExcalidrawElement) => ExcalidrawElement,
) => {
return elements.map(element => {
if (element.isSelected) {
return callback(element);
}
return element;
});
};
const getFormValue = function<T>(
editingElement: AppState["editingElement"],
elements: readonly ExcalidrawElement[],
getAttribute: (element: ExcalidrawElement) => T,
2020-01-24 12:04:54 +02:00
defaultValue?: T,
): T | null {
return (
(editingElement && getAttribute(editingElement)) ??
(elements.some(element => element.isSelected)
? getCommonAttributeOfSelectedElements(elements, getAttribute)
: defaultValue) ??
null
);
};
export const actionChangeStrokeColor: Action = {
name: "changeStrokeColor",
perform: (elements, appState, value) => {
return {
elements: changeProperty(elements, el => ({
...el,
shape: null,
2020-01-24 12:04:54 +02:00
strokeColor: value,
})),
2020-01-24 12:04:54 +02:00
appState: { ...appState, currentItemStrokeColor: value },
};
},
PanelComponent: ({ elements, appState, updateData }) => (
<>
<h3 aria-hidden="true">{t("labels.stroke")}</h3>
<ColorPicker
type="elementStroke"
label={t("labels.stroke")}
color={getFormValue(
appState.editingElement,
elements,
element => element.strokeColor,
2020-01-24 12:04:54 +02:00
appState.currentItemStrokeColor,
)}
onChange={updateData}
/>
</>
2020-01-24 12:04:54 +02:00
),
};
export const actionChangeBackgroundColor: Action = {
name: "changeBackgroundColor",
perform: (elements, appState, value) => {
return {
elements: changeProperty(elements, el => ({
...el,
shape: null,
2020-01-24 12:04:54 +02:00
backgroundColor: value,
})),
2020-01-24 12:04:54 +02:00
appState: { ...appState, currentItemBackgroundColor: value },
};
},
PanelComponent: ({ elements, appState, updateData }) => (
<>
<h3 aria-hidden="true">{t("labels.background")}</h3>
<ColorPicker
type="elementBackground"
label={t("labels.background")}
color={getFormValue(
appState.editingElement,
elements,
element => element.backgroundColor,
2020-01-24 12:04:54 +02:00
appState.currentItemBackgroundColor,
)}
onChange={updateData}
/>
</>
2020-01-24 12:04:54 +02:00
),
};
export const actionChangeFillStyle: Action = {
name: "changeFillStyle",
perform: (elements, appState, value) => {
return {
elements: changeProperty(elements, el => ({
...el,
shape: null,
2020-01-24 12:04:54 +02:00
fillStyle: value,
})),
appState: { ...appState, currentItemFillStyle: value },
};
},
PanelComponent: ({ elements, appState, updateData }) => (
<fieldset>
<legend>{t("labels.fill")}</legend>
<ButtonSelect
options={[
{ value: "solid", text: t("labels.solid") },
{ value: "hachure", text: t("labels.hachure") },
2020-01-24 12:04:54 +02:00
{ value: "cross-hatch", text: t("labels.crossHatch") },
]}
group="fill"
value={getFormValue(
appState.editingElement,
elements,
2020-01-24 12:04:54 +02:00
element => element.fillStyle,
appState.currentItemFillStyle,
)}
onChange={value => {
updateData(value);
}}
/>
</fieldset>
2020-01-24 12:04:54 +02:00
),
};
export const actionChangeStrokeWidth: Action = {
name: "changeStrokeWidth",
perform: (elements, appState, value) => {
return {
elements: changeProperty(elements, el => ({
...el,
shape: null,
2020-01-24 12:04:54 +02:00
strokeWidth: value,
})),
appState: { ...appState, currentItemStrokeWidth: value },
};
},
PanelComponent: ({ elements, appState, updateData }) => (
<fieldset>
<legend>{t("labels.strokeWidth")}</legend>
<ButtonSelect
group="stroke-width"
options={[
{ value: 1, text: t("labels.thin") },
{ value: 2, text: t("labels.bold") },
2020-01-24 12:04:54 +02:00
{ value: 4, text: t("labels.extraBold") },
]}
value={getFormValue(
appState.editingElement,
elements,
2020-01-24 12:04:54 +02:00
element => element.strokeWidth,
appState.currentItemStrokeWidth,
)}
onChange={value => updateData(value)}
/>
</fieldset>
2020-01-24 12:04:54 +02:00
),
};
export const actionChangeSloppiness: Action = {
name: "changeSloppiness",
perform: (elements, appState, value) => {
return {
elements: changeProperty(elements, el => ({
...el,
shape: null,
2020-01-24 12:04:54 +02:00
roughness: value,
})),
appState: { ...appState, currentItemRoughness: value },
};
},
PanelComponent: ({ elements, appState, updateData }) => (
<fieldset>
<legend>{t("labels.sloppiness")}</legend>
<ButtonSelect
group="sloppiness"
options={[
{ value: 0, text: t("labels.architect") },
{ value: 1, text: t("labels.artist") },
2020-01-26 20:16:33 +01:00
{ value: 2, text: t("labels.cartoonist") },
]}
value={getFormValue(
appState.editingElement,
elements,
2020-01-24 12:04:54 +02:00
element => element.roughness,
appState.currentItemRoughness,
)}
onChange={value => updateData(value)}
/>
</fieldset>
2020-01-24 12:04:54 +02:00
),
};
export const actionChangeOpacity: Action = {
name: "changeOpacity",
perform: (elements, appState, value) => {
return {
elements: changeProperty(elements, el => ({
...el,
shape: null,
2020-01-24 12:04:54 +02:00
opacity: value,
})),
appState: { ...appState, currentItemOpacity: value },
};
},
PanelComponent: ({ elements, appState, updateData }) => (
<label className="control-label">
{t("labels.opacity")}
<input
type="range"
min="0"
max="100"
onChange={e => updateData(+e.target.value)}
value={
getFormValue(
appState.editingElement,
elements,
element => element.opacity,
appState.currentItemOpacity,
) ?? undefined
}
/>
</label>
2020-01-24 12:04:54 +02:00
),
};
export const actionChangeFontSize: Action = {
name: "changeFontSize",
perform: (elements, appState, value) => {
return {
elements: changeProperty(elements, el => {
if (isTextElement(el)) {
const element: ExcalidrawTextElement = {
...el,
shape: null,
2020-01-24 12:04:54 +02:00
font: `${value}px ${el.font.split("px ")[1]}`,
};
redrawTextBoundingBox(element);
return element;
}
return el;
2020-01-24 12:04:54 +02:00
}),
appState: {
...appState,
currentItemFont: `${value}px ${
appState.currentItemFont.split("px ")[1]
}`,
},
};
},
PanelComponent: ({ elements, appState, updateData }) => (
<fieldset>
<legend>{t("labels.fontSize")}</legend>
<ButtonSelect
group="font-size"
options={[
{ value: 16, text: t("labels.small") },
{ value: 20, text: t("labels.medium") },
{ value: 28, text: t("labels.large") },
2020-01-24 12:04:54 +02:00
{ value: 36, text: t("labels.veryLarge") },
]}
value={getFormValue(
appState.editingElement,
elements,
2020-01-24 12:04:54 +02:00
element => isTextElement(element) && +element.font.split("px ")[0],
+(appState.currentItemFont || "20px Virgil").split("px ")[0],
)}
onChange={value => updateData(value)}
/>
</fieldset>
2020-01-24 12:04:54 +02:00
),
};
export const actionChangeFontFamily: Action = {
name: "changeFontFamily",
perform: (elements, appState, value) => {
return {
elements: changeProperty(elements, el => {
if (isTextElement(el)) {
const element: ExcalidrawTextElement = {
...el,
shape: null,
2020-01-24 12:04:54 +02:00
font: `${el.font.split("px ")[0]}px ${value}`,
};
redrawTextBoundingBox(element);
return element;
}
return el;
2020-01-24 12:04:54 +02:00
}),
appState: {
...appState,
currentItemFont: `${
appState.currentItemFont.split("px ")[0]
}px ${value}`,
},
};
},
PanelComponent: ({ elements, appState, updateData }) => (
<fieldset>
<legend>{t("labels.fontFamily")}</legend>
<ButtonSelect
group="font-family"
options={[
{ value: "Virgil", text: t("labels.handDrawn") },
{ value: "Helvetica", text: t("labels.normal") },
2020-01-24 12:04:54 +02:00
{ value: "Cascadia", text: t("labels.code") },
]}
value={getFormValue(
appState.editingElement,
elements,
2020-01-24 12:04:54 +02:00
element => isTextElement(element) && element.font.split("px ")[1],
(appState.currentItemFont || "20px Virgil").split("px ")[1],
)}
onChange={value => updateData(value)}
/>
</fieldset>
2020-01-24 12:04:54 +02:00
),
};