2020-01-12 02:22:03 +04:00
|
|
|
import React from "react";
|
|
|
|
import { Action } from "./types";
|
|
|
|
import { ExcalidrawElement, ExcalidrawTextElement } from "../element/types";
|
2020-01-21 00:16:22 +01:00
|
|
|
import { getCommonAttributeOfSelectedElements } from "../scene";
|
2020-01-12 02:22:03 +04:00
|
|
|
import { ButtonSelect } from "../components/ButtonSelect";
|
|
|
|
import { isTextElement, redrawTextBoundingBox } from "../element";
|
2020-01-15 20:42:02 +05:00
|
|
|
import { ColorPicker } from "../components/ColorPicker";
|
2020-01-21 00:16:22 +01:00
|
|
|
import { AppState } from "../../src/types";
|
2020-01-31 21:06:06 +00:00
|
|
|
import { t } from "../i18n";
|
2020-01-12 02:22:03 +04:00
|
|
|
|
|
|
|
const changeProperty = (
|
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-01-24 12:04:54 +02:00
|
|
|
callback: (element: ExcalidrawElement) => ExcalidrawElement,
|
2020-01-12 02:22:03 +04:00
|
|
|
) => {
|
|
|
|
return elements.map(element => {
|
|
|
|
if (element.isSelected) {
|
|
|
|
return callback(element);
|
|
|
|
}
|
|
|
|
return element;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-01-21 00:16:22 +01:00
|
|
|
const getFormValue = function<T>(
|
|
|
|
editingElement: AppState["editingElement"],
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
getAttribute: (element: ExcalidrawElement) => T,
|
2020-01-24 12:04:54 +02:00
|
|
|
defaultValue?: T,
|
2020-01-21 00:16:22 +01:00
|
|
|
): T | null {
|
|
|
|
return (
|
2020-01-22 20:51:56 +01:00
|
|
|
(editingElement && getAttribute(editingElement)) ??
|
2020-01-26 04:12:47 -08:00
|
|
|
(elements.some(element => element.isSelected)
|
|
|
|
? getCommonAttributeOfSelectedElements(elements, getAttribute)
|
|
|
|
: defaultValue) ??
|
2020-01-21 00:16:22 +01:00
|
|
|
null
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-01-12 02:22:03 +04:00
|
|
|
export const actionChangeStrokeColor: Action = {
|
|
|
|
name: "changeStrokeColor",
|
|
|
|
perform: (elements, appState, value) => {
|
|
|
|
return {
|
|
|
|
elements: changeProperty(elements, el => ({
|
|
|
|
...el,
|
2020-01-12 04:00:00 +04:00
|
|
|
shape: null,
|
2020-01-24 12:04:54 +02:00
|
|
|
strokeColor: value,
|
2020-01-12 02:22:03 +04:00
|
|
|
})),
|
2020-01-24 12:04:54 +02:00
|
|
|
appState: { ...appState, currentItemStrokeColor: value },
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
},
|
2020-01-31 21:06:06 +00:00
|
|
|
PanelComponent: ({ elements, appState, updateData }) => (
|
2020-01-21 00:16:22 +01:00
|
|
|
<>
|
2020-01-25 14:52:03 -03:00
|
|
|
<h3 aria-hidden="true">{t("labels.stroke")}</h3>
|
2020-01-21 00:16:22 +01:00
|
|
|
<ColorPicker
|
|
|
|
type="elementStroke"
|
2020-01-25 14:52:03 -03:00
|
|
|
label={t("labels.stroke")}
|
2020-01-21 00:16:22 +01:00
|
|
|
color={getFormValue(
|
|
|
|
appState.editingElement,
|
|
|
|
elements,
|
|
|
|
element => element.strokeColor,
|
2020-01-24 12:04:54 +02:00
|
|
|
appState.currentItemStrokeColor,
|
2020-01-21 00:16:22 +01:00
|
|
|
)}
|
|
|
|
onChange={updateData}
|
|
|
|
/>
|
|
|
|
</>
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const actionChangeBackgroundColor: Action = {
|
|
|
|
name: "changeBackgroundColor",
|
|
|
|
perform: (elements, appState, value) => {
|
|
|
|
return {
|
|
|
|
elements: changeProperty(elements, el => ({
|
|
|
|
...el,
|
2020-01-12 04:00:00 +04:00
|
|
|
shape: null,
|
2020-01-24 12:04:54 +02:00
|
|
|
backgroundColor: value,
|
2020-01-12 02:22:03 +04:00
|
|
|
})),
|
2020-01-24 12:04:54 +02:00
|
|
|
appState: { ...appState, currentItemBackgroundColor: value },
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
},
|
2020-01-31 21:06:06 +00:00
|
|
|
PanelComponent: ({ elements, appState, updateData }) => (
|
2020-01-15 20:42:02 +05:00
|
|
|
<>
|
2020-01-25 14:52:03 -03:00
|
|
|
<h3 aria-hidden="true">{t("labels.background")}</h3>
|
2020-01-15 20:42:02 +05:00
|
|
|
<ColorPicker
|
|
|
|
type="elementBackground"
|
2020-01-25 14:52:03 -03:00
|
|
|
label={t("labels.background")}
|
2020-01-21 00:16:22 +01:00
|
|
|
color={getFormValue(
|
|
|
|
appState.editingElement,
|
|
|
|
elements,
|
|
|
|
element => element.backgroundColor,
|
2020-01-24 12:04:54 +02:00
|
|
|
appState.currentItemBackgroundColor,
|
2020-01-21 00:16:22 +01:00
|
|
|
)}
|
2020-01-15 20:42:02 +05:00
|
|
|
onChange={updateData}
|
|
|
|
/>
|
|
|
|
</>
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const actionChangeFillStyle: Action = {
|
|
|
|
name: "changeFillStyle",
|
|
|
|
perform: (elements, appState, value) => {
|
|
|
|
return {
|
|
|
|
elements: changeProperty(elements, el => ({
|
|
|
|
...el,
|
2020-01-12 04:00:00 +04:00
|
|
|
shape: null,
|
2020-01-24 12:04:54 +02:00
|
|
|
fillStyle: value,
|
|
|
|
})),
|
2020-01-25 18:58:57 +01:00
|
|
|
appState: { ...appState, currentItemFillStyle: value },
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
},
|
2020-01-31 21:06:06 +00:00
|
|
|
PanelComponent: ({ elements, appState, updateData }) => (
|
2020-01-25 14:52:03 -03:00
|
|
|
<fieldset>
|
|
|
|
<legend>{t("labels.fill")}</legend>
|
2020-01-12 02:22:03 +04:00
|
|
|
<ButtonSelect
|
|
|
|
options={[
|
2020-01-22 16:25:04 +02:00
|
|
|
{ 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") },
|
2020-01-12 02:22:03 +04:00
|
|
|
]}
|
2020-01-25 14:52:03 -03:00
|
|
|
group="fill"
|
2020-01-21 00:16:22 +01:00
|
|
|
value={getFormValue(
|
|
|
|
appState.editingElement,
|
|
|
|
elements,
|
2020-01-24 12:04:54 +02:00
|
|
|
element => element.fillStyle,
|
2020-01-26 19:09:11 +00:00
|
|
|
appState.currentItemFillStyle,
|
2020-01-21 00:16:22 +01:00
|
|
|
)}
|
2020-01-12 02:22:03 +04:00
|
|
|
onChange={value => {
|
|
|
|
updateData(value);
|
|
|
|
}}
|
|
|
|
/>
|
2020-01-25 14:52:03 -03:00
|
|
|
</fieldset>
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const actionChangeStrokeWidth: Action = {
|
|
|
|
name: "changeStrokeWidth",
|
|
|
|
perform: (elements, appState, value) => {
|
|
|
|
return {
|
|
|
|
elements: changeProperty(elements, el => ({
|
|
|
|
...el,
|
2020-01-12 04:00:00 +04:00
|
|
|
shape: null,
|
2020-01-24 12:04:54 +02:00
|
|
|
strokeWidth: value,
|
|
|
|
})),
|
2020-01-25 18:58:57 +01:00
|
|
|
appState: { ...appState, currentItemStrokeWidth: value },
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
},
|
2020-01-31 21:06:06 +00:00
|
|
|
PanelComponent: ({ elements, appState, updateData }) => (
|
2020-01-25 14:52:03 -03:00
|
|
|
<fieldset>
|
|
|
|
<legend>{t("labels.strokeWidth")}</legend>
|
2020-01-12 02:22:03 +04:00
|
|
|
<ButtonSelect
|
2020-01-25 14:52:03 -03:00
|
|
|
group="stroke-width"
|
2020-01-12 02:22:03 +04:00
|
|
|
options={[
|
2020-01-22 16:25:04 +02:00
|
|
|
{ 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") },
|
2020-01-12 02:22:03 +04:00
|
|
|
]}
|
2020-01-21 00:16:22 +01:00
|
|
|
value={getFormValue(
|
|
|
|
appState.editingElement,
|
|
|
|
elements,
|
2020-01-24 12:04:54 +02:00
|
|
|
element => element.strokeWidth,
|
2020-01-26 19:09:11 +00:00
|
|
|
appState.currentItemStrokeWidth,
|
2020-01-21 00:16:22 +01:00
|
|
|
)}
|
2020-01-12 02:22:03 +04:00
|
|
|
onChange={value => updateData(value)}
|
|
|
|
/>
|
2020-01-25 14:52:03 -03:00
|
|
|
</fieldset>
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const actionChangeSloppiness: Action = {
|
|
|
|
name: "changeSloppiness",
|
|
|
|
perform: (elements, appState, value) => {
|
|
|
|
return {
|
|
|
|
elements: changeProperty(elements, el => ({
|
|
|
|
...el,
|
2020-01-12 04:00:00 +04:00
|
|
|
shape: null,
|
2020-01-24 12:04:54 +02:00
|
|
|
roughness: value,
|
|
|
|
})),
|
2020-01-25 18:58:57 +01:00
|
|
|
appState: { ...appState, currentItemRoughness: value },
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
},
|
2020-01-31 21:06:06 +00:00
|
|
|
PanelComponent: ({ elements, appState, updateData }) => (
|
2020-01-25 14:52:03 -03:00
|
|
|
<fieldset>
|
|
|
|
<legend>{t("labels.sloppiness")}</legend>
|
2020-01-12 02:22:03 +04:00
|
|
|
<ButtonSelect
|
2020-01-25 14:52:03 -03:00
|
|
|
group="sloppiness"
|
2020-01-12 02:22:03 +04:00
|
|
|
options={[
|
2020-01-22 16:25:04 +02:00
|
|
|
{ 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") },
|
2020-01-12 02:22:03 +04:00
|
|
|
]}
|
2020-01-21 00:16:22 +01:00
|
|
|
value={getFormValue(
|
|
|
|
appState.editingElement,
|
|
|
|
elements,
|
2020-01-24 12:04:54 +02:00
|
|
|
element => element.roughness,
|
2020-01-26 19:09:11 +00:00
|
|
|
appState.currentItemRoughness,
|
2020-01-21 00:16:22 +01:00
|
|
|
)}
|
2020-01-12 02:22:03 +04:00
|
|
|
onChange={value => updateData(value)}
|
|
|
|
/>
|
2020-01-25 14:52:03 -03:00
|
|
|
</fieldset>
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const actionChangeOpacity: Action = {
|
|
|
|
name: "changeOpacity",
|
|
|
|
perform: (elements, appState, value) => {
|
|
|
|
return {
|
|
|
|
elements: changeProperty(elements, el => ({
|
|
|
|
...el,
|
2020-01-12 04:00:00 +04:00
|
|
|
shape: null,
|
2020-01-24 12:04:54 +02:00
|
|
|
opacity: value,
|
|
|
|
})),
|
2020-01-25 18:58:57 +01:00
|
|
|
appState: { ...appState, currentItemOpacity: value },
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
},
|
2020-01-31 21:06:06 +00:00
|
|
|
PanelComponent: ({ elements, appState, updateData }) => (
|
2020-01-25 14:52:03 -03:00
|
|
|
<label className="control-label">
|
|
|
|
{t("labels.opacity")}
|
2020-01-12 02:22:03 +04:00
|
|
|
<input
|
|
|
|
type="range"
|
|
|
|
min="0"
|
|
|
|
max="100"
|
2020-02-02 17:09:50 -08:00
|
|
|
step="10"
|
2020-01-12 02:22:03 +04:00
|
|
|
onChange={e => updateData(+e.target.value)}
|
|
|
|
value={
|
2020-01-21 00:16:22 +01:00
|
|
|
getFormValue(
|
|
|
|
appState.editingElement,
|
|
|
|
elements,
|
|
|
|
element => element.opacity,
|
2020-01-26 19:09:11 +00:00
|
|
|
appState.currentItemOpacity,
|
2020-01-22 20:51:56 +01:00
|
|
|
) ?? undefined
|
2020-01-12 02:22:03 +04:00
|
|
|
}
|
|
|
|
/>
|
2020-01-25 14:52:03 -03:00
|
|
|
</label>
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const actionChangeFontSize: Action = {
|
|
|
|
name: "changeFontSize",
|
|
|
|
perform: (elements, appState, value) => {
|
|
|
|
return {
|
|
|
|
elements: changeProperty(elements, el => {
|
|
|
|
if (isTextElement(el)) {
|
|
|
|
const element: ExcalidrawTextElement = {
|
|
|
|
...el,
|
2020-01-12 04:00:00 +04:00
|
|
|
shape: null,
|
2020-01-24 12:04:54 +02:00
|
|
|
font: `${value}px ${el.font.split("px ")[1]}`,
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
redrawTextBoundingBox(element);
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
return el;
|
2020-01-24 12:04:54 +02:00
|
|
|
}),
|
2020-01-25 18:58:57 +01:00
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
currentItemFont: `${value}px ${
|
|
|
|
appState.currentItemFont.split("px ")[1]
|
|
|
|
}`,
|
|
|
|
},
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
},
|
2020-01-31 21:06:06 +00:00
|
|
|
PanelComponent: ({ elements, appState, updateData }) => (
|
2020-01-25 14:52:03 -03:00
|
|
|
<fieldset>
|
|
|
|
<legend>{t("labels.fontSize")}</legend>
|
2020-01-12 02:22:03 +04:00
|
|
|
<ButtonSelect
|
2020-01-25 14:52:03 -03:00
|
|
|
group="font-size"
|
2020-01-12 02:22:03 +04:00
|
|
|
options={[
|
2020-01-22 16:25:04 +02:00
|
|
|
{ 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") },
|
2020-01-12 02:22:03 +04:00
|
|
|
]}
|
2020-01-21 00:16:22 +01:00
|
|
|
value={getFormValue(
|
|
|
|
appState.editingElement,
|
2020-01-12 02:22:03 +04:00
|
|
|
elements,
|
2020-01-24 12:04:54 +02:00
|
|
|
element => isTextElement(element) && +element.font.split("px ")[0],
|
2020-01-26 19:09:11 +00:00
|
|
|
+(appState.currentItemFont || "20px Virgil").split("px ")[0],
|
2020-01-12 02:22:03 +04:00
|
|
|
)}
|
|
|
|
onChange={value => updateData(value)}
|
|
|
|
/>
|
2020-01-25 14:52:03 -03:00
|
|
|
</fieldset>
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const actionChangeFontFamily: Action = {
|
|
|
|
name: "changeFontFamily",
|
|
|
|
perform: (elements, appState, value) => {
|
|
|
|
return {
|
|
|
|
elements: changeProperty(elements, el => {
|
|
|
|
if (isTextElement(el)) {
|
|
|
|
const element: ExcalidrawTextElement = {
|
|
|
|
...el,
|
2020-01-12 04:00:00 +04:00
|
|
|
shape: null,
|
2020-01-24 12:04:54 +02:00
|
|
|
font: `${el.font.split("px ")[0]}px ${value}`,
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
redrawTextBoundingBox(element);
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
return el;
|
2020-01-24 12:04:54 +02:00
|
|
|
}),
|
2020-01-25 18:58:57 +01:00
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
currentItemFont: `${
|
|
|
|
appState.currentItemFont.split("px ")[0]
|
|
|
|
}px ${value}`,
|
|
|
|
},
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
},
|
2020-01-31 21:06:06 +00:00
|
|
|
PanelComponent: ({ elements, appState, updateData }) => (
|
2020-01-25 14:52:03 -03:00
|
|
|
<fieldset>
|
|
|
|
<legend>{t("labels.fontFamily")}</legend>
|
2020-01-12 02:22:03 +04:00
|
|
|
<ButtonSelect
|
2020-01-25 14:52:03 -03:00
|
|
|
group="font-family"
|
2020-01-12 02:22:03 +04:00
|
|
|
options={[
|
2020-01-21 01:14:10 +02:00
|
|
|
{ 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") },
|
2020-01-12 02:22:03 +04:00
|
|
|
]}
|
2020-01-21 00:16:22 +01:00
|
|
|
value={getFormValue(
|
|
|
|
appState.editingElement,
|
2020-01-12 02:22:03 +04:00
|
|
|
elements,
|
2020-01-24 12:04:54 +02:00
|
|
|
element => isTextElement(element) && element.font.split("px ")[1],
|
2020-01-26 19:09:11 +00:00
|
|
|
(appState.currentItemFont || "20px Virgil").split("px ")[1],
|
2020-01-12 02:22:03 +04:00
|
|
|
)}
|
|
|
|
onChange={value => updateData(value)}
|
|
|
|
/>
|
2020-01-25 14:52:03 -03:00
|
|
|
</fieldset>
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|