Pārlūkot izejas kodu

move actionChangeFontSize as well

Aakansha Doshi 1 gadu atpakaļ
vecāks
revīzija
d710507283

+ 84 - 0
packages/excalidraw/actions/actionChangeFontSize.tsx

@@ -0,0 +1,84 @@
+import { ButtonIconSelect } from "../components/ButtonIconSelect";
+import {
+  FontSizeExtraLargeIcon,
+  FontSizeLargeIcon,
+  FontSizeMediumIcon,
+  FontSizeSmallIcon,
+} from "../components/icons";
+import { DEFAULT_FONT_SIZE } from "../constants";
+import { isTextElement } from "../element";
+import { getBoundTextElement } from "../element/textElement";
+import { t } from "../i18n";
+import { changeFontSize } from "./utils";
+import { getFormValue } from "./actionProperties";
+import { register } from "./register";
+
+export const actionChangeFontSize = register({
+  name: "changeFontSize",
+  trackEvent: false,
+  perform: (elements, appState, value, app) => {
+    return changeFontSize(elements, appState, app, () => value, value);
+  },
+  PanelComponent: ({ elements, appState, updateData, app }) => (
+    <fieldset>
+      <legend>{t("labels.fontSize")}</legend>
+      <ButtonIconSelect
+        group="font-size"
+        options={[
+          {
+            value: 16,
+            text: t("labels.small"),
+            icon: FontSizeSmallIcon,
+            testId: "fontSize-small",
+          },
+          {
+            value: 20,
+            text: t("labels.medium"),
+            icon: FontSizeMediumIcon,
+            testId: "fontSize-medium",
+          },
+          {
+            value: 28,
+            text: t("labels.large"),
+            icon: FontSizeLargeIcon,
+            testId: "fontSize-large",
+          },
+          {
+            value: 36,
+            text: t("labels.veryLarge"),
+            icon: FontSizeExtraLargeIcon,
+            testId: "fontSize-veryLarge",
+          },
+        ]}
+        value={getFormValue(
+          elements,
+          appState,
+          (element) => {
+            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)}
+      />
+    </fieldset>
+  ),
+});

+ 26 - 0
packages/excalidraw/actions/actionDecreaseFontSize.ts

@@ -0,0 +1,26 @@
+import { KEYS } from "../keys";
+
+import { register } from "./register";
+import { changeFontSize, FONT_SIZE_RELATIVE_INCREASE_STEP } from "./utils";
+
+export const actionDecreaseFontSize = register({
+  name: "decreaseFontSize",
+  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)
+    );
+  },
+});

+ 21 - 0
packages/excalidraw/actions/actionIncreaseFontSize.ts

@@ -0,0 +1,21 @@
+import { KEYS } from "../keys";
+import { register } from "./register";
+import { changeFontSize, FONT_SIZE_RELATIVE_INCREASE_STEP } from "./utils";
+
+export const actionIncreaseFontSize = register({
+  name: "increaseFontSize",
+  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)
+    );
+  },
+});

+ 0 - 70
packages/excalidraw/actions/actionProperties.tsx

@@ -525,76 +525,6 @@ export const actionChangeOpacity = register({
   ),
   ),
 });
 });
 
 
-export const actionChangeFontSize = register({
-  name: "changeFontSize",
-  trackEvent: false,
-  perform: (elements, appState, value, app) => {
-    return changeFontSize(elements, appState, app, () => value, value);
-  },
-  PanelComponent: ({ elements, appState, updateData, app }) => (
-    <fieldset>
-      <legend>{t("labels.fontSize")}</legend>
-      <ButtonIconSelect
-        group="font-size"
-        options={[
-          {
-            value: 16,
-            text: t("labels.small"),
-            icon: FontSizeSmallIcon,
-            testId: "fontSize-small",
-          },
-          {
-            value: 20,
-            text: t("labels.medium"),
-            icon: FontSizeMediumIcon,
-            testId: "fontSize-medium",
-          },
-          {
-            value: 28,
-            text: t("labels.large"),
-            icon: FontSizeLargeIcon,
-            testId: "fontSize-large",
-          },
-          {
-            value: 36,
-            text: t("labels.veryLarge"),
-            icon: FontSizeExtraLargeIcon,
-            testId: "fontSize-veryLarge",
-          },
-        ]}
-        value={getFormValue(
-          elements,
-          appState,
-          (element) => {
-            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)}
-      />
-    </fieldset>
-  ),
-});
-
 export const actionChangeFontFamily = register({
 export const actionChangeFontFamily = register({
   name: "changeFontFamily",
   name: "changeFontFamily",
   trackEvent: false,
   trackEvent: false,

+ 5 - 5
packages/excalidraw/actions/index.ts

@@ -14,16 +14,16 @@ export {
   actionChangeFillStyle,
   actionChangeFillStyle,
   actionChangeSloppiness,
   actionChangeSloppiness,
   actionChangeOpacity,
   actionChangeOpacity,
-  actionChangeFontSize,
   actionChangeFontFamily,
   actionChangeFontFamily,
   actionChangeTextAlign,
   actionChangeTextAlign,
   actionChangeVerticalAlign,
   actionChangeVerticalAlign,
 } from "./actionProperties";
 } from "./actionProperties";
 
 
-export {
-  actionDecreaseFontSize,
-  actionIncreaseFontSize,
-} from "./actionFontSize";
+export { actionDecreaseFontSize } from "./actionDecreaseFontSize";
+
+export { actionIncreaseFontSize } from "./actionIncreaseFontSize";
+
+export { actionChangeFontSize } from "./actionChangeFontSize";
 
 
 export {
 export {
   actionChangeViewBackgroundColor,
   actionChangeViewBackgroundColor,

+ 4 - 45
packages/excalidraw/actions/actionFontSize.ts → packages/excalidraw/actions/utils.ts

@@ -1,12 +1,11 @@
-import { newElementWith } from "..";
+import { mutateElement, newElementWith } from "..";
 import { isTextElement, redrawTextBoundingBox } from "../element";
 import { isTextElement, redrawTextBoundingBox } from "../element";
+import { isBoundToContainer } from "../element/typeChecks";
 import { ExcalidrawElement, ExcalidrawTextElement } from "../element/types";
 import { ExcalidrawElement, ExcalidrawTextElement } from "../element/types";
-import { KEYS } from "../keys";
 import { AppClassProperties, AppState } from "../types";
 import { AppClassProperties, AppState } from "../types";
 import { changeProperty } from "./actionProperties";
 import { changeProperty } from "./actionProperties";
-import { register } from "./register";
 
 
-const FONT_SIZE_RELATIVE_INCREASE_STEP = 0.1;
+export const FONT_SIZE_RELATIVE_INCREASE_STEP = 0.1;
 
 
 const offsetElementAfterFontResize = (
 const offsetElementAfterFontResize = (
   prevElement: ExcalidrawTextElement,
   prevElement: ExcalidrawTextElement,
@@ -32,7 +31,7 @@ const offsetElementAfterFontResize = (
   );
   );
 };
 };
 
 
-const changeFontSize = (
+export const changeFontSize = (
   elements: readonly ExcalidrawElement[],
   elements: readonly ExcalidrawElement[],
   appState: AppState,
   appState: AppState,
   app: AppClassProperties,
   app: AppClassProperties,
@@ -79,43 +78,3 @@ const changeFontSize = (
     commitToHistory: true,
     commitToHistory: true,
   };
   };
 };
 };
-
-export const actionDecreaseFontSize = register({
-  name: "decreaseFontSize",
-  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",
-  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)
-    );
-  },
-});

+ 2 - 4
packages/excalidraw/element/textWysiwyg.tsx

@@ -36,14 +36,12 @@ import {
   computeBoundTextPosition,
   computeBoundTextPosition,
   getBoundTextElement,
   getBoundTextElement,
 } from "./textElement";
 } from "./textElement";
-import {
-  actionDecreaseFontSize,
-  actionIncreaseFontSize,
-} from "../actions/actionFontSize";
+import { actionDecreaseFontSize } from "../actions/actionDecreaseFontSize";
 import { actionZoomIn, actionZoomOut } from "../actions/actionCanvas";
 import { actionZoomIn, actionZoomOut } from "../actions/actionCanvas";
 import App from "../components/App";
 import App from "../components/App";
 import { LinearElementEditor } from "./linearElementEditor";
 import { LinearElementEditor } from "./linearElementEditor";
 import { parseClipboard } from "../clipboard";
 import { parseClipboard } from "../clipboard";
+import { actionIncreaseFontSize } from "../actions";
 
 
 const getTransform = (
 const getTransform = (
   width: number,
   width: number,