From 8466eb0eefe2f27e65557878d0540f36fc002530 Mon Sep 17 00:00:00 2001 From: Ryan Di Date: Sat, 10 Dec 2022 20:12:09 +0800 Subject: [PATCH] fix: apply the right type of roundness when pasting styles (#5979) * fix: only paste roundness when target and source elements are of the same type * apply roundness when pasting across different types * simplify Co-authored-by: dwelle --- src/actions/actionStyles.ts | 15 +++++++++-- src/element/typeChecks.ts | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/actions/actionStyles.ts b/src/actions/actionStyles.ts index ab2db9a1d..b2be3853d 100644 --- a/src/actions/actionStyles.ts +++ b/src/actions/actionStyles.ts @@ -13,7 +13,11 @@ import { DEFAULT_TEXT_ALIGN, } from "../constants"; import { getBoundTextElement } from "../element/textElement"; -import { hasBoundTextElement } from "../element/typeChecks"; +import { + hasBoundTextElement, + canApplyRoundnessTypeToElement, + getDefaultRoundnessTypeForElement, +} from "../element/typeChecks"; import { getSelectedElements } from "../scene"; // `copiedStyles` is exported only for tests. @@ -77,7 +81,14 @@ export const actionPasteStyles = register({ fillStyle: elementStylesToCopyFrom?.fillStyle, opacity: elementStylesToCopyFrom?.opacity, roughness: elementStylesToCopyFrom?.roughness, - roundness: elementStylesToCopyFrom?.roundness, + roundness: elementStylesToCopyFrom.roundness + ? canApplyRoundnessTypeToElement( + elementStylesToCopyFrom.roundness.type, + element, + ) + ? elementStylesToCopyFrom.roundness + : getDefaultRoundnessTypeForElement(element) + : null, }); if (isTextElement(newElement)) { diff --git a/src/element/typeChecks.ts b/src/element/typeChecks.ts index 86aa23907..72088e727 100644 --- a/src/element/typeChecks.ts +++ b/src/element/typeChecks.ts @@ -1,3 +1,4 @@ +import { ROUNDNESS } from "../constants"; import { AppState } from "../types"; import { ExcalidrawElement, @@ -10,6 +11,7 @@ import { ExcalidrawImageElement, ExcalidrawTextElementWithContainer, ExcalidrawTextContainer, + RoundnessType, } from "./types"; export const isGenericElement = ( @@ -154,3 +156,51 @@ export const isBoundToContainer = ( }; export const isUsingAdaptiveRadius = (type: string) => type === "rectangle"; + +export const isUsingProportionalRadius = (type: string) => + type === "line" || type === "arrow" || type === "diamond"; + +export const canApplyRoundnessTypeToElement = ( + roundnessType: RoundnessType, + element: ExcalidrawElement, +) => { + if ( + (roundnessType === ROUNDNESS.ADAPTIVE_RADIUS || + // if legacy roundness, it can be applied to elements that currently + // use adaptive radius + roundnessType === ROUNDNESS.LEGACY) && + isUsingAdaptiveRadius(element.type) + ) { + return true; + } + if ( + roundnessType === ROUNDNESS.PROPORTIONAL_RADIUS && + isUsingProportionalRadius(element.type) + ) { + return true; + } + + return false; +}; + +export const getDefaultRoundnessTypeForElement = ( + element: ExcalidrawElement, +) => { + if ( + element.type === "arrow" || + element.type === "line" || + element.type === "diamond" + ) { + return { + type: ROUNDNESS.PROPORTIONAL_RADIUS, + }; + } + + if (element.type === "rectangle") { + return { + type: ROUNDNESS.ADAPTIVE_RADIUS, + }; + } + + return null; +};