actionCanvas.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import React from "react";
  2. import { ColorPicker } from "../components/ColorPicker";
  3. import { getDefaultAppState } from "../appState";
  4. import { trash, zoomIn, zoomOut, resetZoom } from "../components/icons";
  5. import { ToolButton } from "../components/ToolButton";
  6. import { t } from "../i18n";
  7. import { getNormalizedZoom } from "../scene";
  8. import { CODES, KEYS } from "../keys";
  9. import { getShortcutKey } from "../utils";
  10. import useIsMobile from "../is-mobile";
  11. import { register } from "./register";
  12. import { newElementWith } from "../element/mutateElement";
  13. import { AppState, NormalizedZoomValue } from "../types";
  14. import { getCommonBounds } from "../element";
  15. import { getNewZoom } from "../scene/zoom";
  16. import { centerScrollOn } from "../scene/scroll";
  17. import { EVENT_ACTION, EVENT_CHANGE, trackEvent } from "../analytics";
  18. import colors from "../colors";
  19. export const actionChangeViewBackgroundColor = register({
  20. name: "changeViewBackgroundColor",
  21. perform: (_, appState, value) => {
  22. if (value !== appState.viewBackgroundColor) {
  23. trackEvent(
  24. EVENT_CHANGE,
  25. "canvas color",
  26. colors.canvasBackground.includes(value)
  27. ? `${value} (picker ${colors.canvasBackground.indexOf(value)})`
  28. : value,
  29. );
  30. }
  31. return {
  32. appState: { ...appState, viewBackgroundColor: value },
  33. commitToHistory: true,
  34. };
  35. },
  36. PanelComponent: ({ appState, updateData }) => {
  37. return (
  38. <div style={{ position: "relative" }}>
  39. <ColorPicker
  40. label={t("labels.canvasBackground")}
  41. type="canvasBackground"
  42. color={appState.viewBackgroundColor}
  43. onChange={(color) => updateData(color)}
  44. />
  45. </div>
  46. );
  47. },
  48. });
  49. export const actionClearCanvas = register({
  50. name: "clearCanvas",
  51. perform: (elements, appState: AppState) => {
  52. trackEvent(EVENT_ACTION, "clear canvas");
  53. return {
  54. elements: elements.map((element) =>
  55. newElementWith(element, { isDeleted: true }),
  56. ),
  57. appState: {
  58. ...getDefaultAppState(),
  59. appearance: appState.appearance,
  60. elementLocked: appState.elementLocked,
  61. exportBackground: appState.exportBackground,
  62. exportEmbedScene: appState.exportEmbedScene,
  63. gridSize: appState.gridSize,
  64. shouldAddWatermark: appState.shouldAddWatermark,
  65. },
  66. commitToHistory: true,
  67. };
  68. },
  69. PanelComponent: ({ updateData }) => (
  70. <ToolButton
  71. type="button"
  72. icon={trash}
  73. title={t("buttons.clearReset")}
  74. aria-label={t("buttons.clearReset")}
  75. showAriaLabel={useIsMobile()}
  76. onClick={() => {
  77. if (window.confirm(t("alerts.clearReset"))) {
  78. updateData(null);
  79. }
  80. }}
  81. />
  82. ),
  83. });
  84. const ZOOM_STEP = 0.1;
  85. export const actionZoomIn = register({
  86. name: "zoomIn",
  87. perform: (_elements, appState) => {
  88. const zoom = getNewZoom(
  89. getNormalizedZoom(appState.zoom.value + ZOOM_STEP),
  90. appState.zoom,
  91. { x: appState.width / 2, y: appState.height / 2 },
  92. );
  93. trackEvent(EVENT_ACTION, "zoom", "in", zoom.value * 100);
  94. return {
  95. appState: {
  96. ...appState,
  97. zoom,
  98. },
  99. commitToHistory: false,
  100. };
  101. },
  102. PanelComponent: ({ updateData }) => (
  103. <ToolButton
  104. type="button"
  105. icon={zoomIn}
  106. title={`${t("buttons.zoomIn")} — ${getShortcutKey("CtrlOrCmd++")}`}
  107. aria-label={t("buttons.zoomIn")}
  108. onClick={() => {
  109. updateData(null);
  110. }}
  111. />
  112. ),
  113. keyTest: (event) =>
  114. (event.code === CODES.EQUAL || event.code === CODES.NUM_ADD) &&
  115. (event[KEYS.CTRL_OR_CMD] || event.shiftKey),
  116. });
  117. export const actionZoomOut = register({
  118. name: "zoomOut",
  119. perform: (_elements, appState) => {
  120. const zoom = getNewZoom(
  121. getNormalizedZoom(appState.zoom.value - ZOOM_STEP),
  122. appState.zoom,
  123. { x: appState.width / 2, y: appState.height / 2 },
  124. );
  125. trackEvent(EVENT_ACTION, "zoom", "out", zoom.value * 100);
  126. return {
  127. appState: {
  128. ...appState,
  129. zoom,
  130. },
  131. commitToHistory: false,
  132. };
  133. },
  134. PanelComponent: ({ updateData }) => (
  135. <ToolButton
  136. type="button"
  137. icon={zoomOut}
  138. title={`${t("buttons.zoomOut")} — ${getShortcutKey("CtrlOrCmd+-")}`}
  139. aria-label={t("buttons.zoomOut")}
  140. onClick={() => {
  141. updateData(null);
  142. }}
  143. />
  144. ),
  145. keyTest: (event) =>
  146. (event.code === CODES.MINUS || event.code === CODES.NUM_SUBTRACT) &&
  147. (event[KEYS.CTRL_OR_CMD] || event.shiftKey),
  148. });
  149. export const actionResetZoom = register({
  150. name: "resetZoom",
  151. perform: (_elements, appState) => {
  152. trackEvent(EVENT_ACTION, "zoom", "reset", 100);
  153. return {
  154. appState: {
  155. ...appState,
  156. zoom: getNewZoom(1 as NormalizedZoomValue, appState.zoom, {
  157. x: appState.width / 2,
  158. y: appState.height / 2,
  159. }),
  160. },
  161. commitToHistory: false,
  162. };
  163. },
  164. PanelComponent: ({ updateData }) => (
  165. <ToolButton
  166. type="button"
  167. icon={resetZoom}
  168. title={t("buttons.resetZoom")}
  169. aria-label={t("buttons.resetZoom")}
  170. onClick={() => {
  171. updateData(null);
  172. }}
  173. />
  174. ),
  175. keyTest: (event) =>
  176. (event.code === CODES.ZERO || event.code === CODES.NUM_ZERO) &&
  177. (event[KEYS.CTRL_OR_CMD] || event.shiftKey),
  178. });
  179. const zoomValueToFitBoundsOnViewport = (
  180. bounds: [number, number, number, number],
  181. viewportDimensions: { width: number; height: number },
  182. ) => {
  183. const [x1, y1, x2, y2] = bounds;
  184. const commonBoundsWidth = x2 - x1;
  185. const zoomValueForWidth = viewportDimensions.width / commonBoundsWidth;
  186. const commonBoundsHeight = y2 - y1;
  187. const zoomValueForHeight = viewportDimensions.height / commonBoundsHeight;
  188. const smallestZoomValue = Math.min(zoomValueForWidth, zoomValueForHeight);
  189. const zoomAdjustedToSteps =
  190. Math.floor(smallestZoomValue / ZOOM_STEP) * ZOOM_STEP;
  191. const clampedZoomValueToFitElements = Math.min(
  192. Math.max(zoomAdjustedToSteps, ZOOM_STEP),
  193. 1,
  194. );
  195. return clampedZoomValueToFitElements as NormalizedZoomValue;
  196. };
  197. export const actionZoomToFit = register({
  198. name: "zoomToFit",
  199. perform: (elements, appState) => {
  200. const nonDeletedElements = elements.filter((element) => !element.isDeleted);
  201. const commonBounds = getCommonBounds(nonDeletedElements);
  202. const zoomValue = zoomValueToFitBoundsOnViewport(commonBounds, {
  203. width: appState.width,
  204. height: appState.height,
  205. });
  206. const newZoom = getNewZoom(zoomValue, appState.zoom);
  207. const [x1, y1, x2, y2] = commonBounds;
  208. const centerX = (x1 + x2) / 2;
  209. const centerY = (y1 + y2) / 2;
  210. trackEvent(EVENT_ACTION, "zoom", "fit", newZoom.value * 100);
  211. return {
  212. appState: {
  213. ...appState,
  214. ...centerScrollOn({
  215. scenePoint: { x: centerX, y: centerY },
  216. viewportDimensions: {
  217. width: appState.width,
  218. height: appState.height,
  219. },
  220. zoom: newZoom,
  221. }),
  222. zoom: newZoom,
  223. },
  224. commitToHistory: false,
  225. };
  226. },
  227. keyTest: (event) =>
  228. event.code === CODES.ONE &&
  229. event.shiftKey &&
  230. !event.altKey &&
  231. !event[KEYS.CTRL_OR_CMD],
  232. });