From 3bbcb9cbdc1bb061dc513f26787d0f7e985760ce Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Sun, 5 Jan 2020 19:57:50 -0800 Subject: [PATCH] Improve Color Picker *2 (#195) --- src/index.tsx | 279 +++++++++++++----------------------------------- src/styles.scss | 3 +- 2 files changed, 78 insertions(+), 204 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 82df8fd5..ace037e3 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -2,7 +2,7 @@ import React from "react"; import ReactDOM from "react-dom"; import rough from "roughjs/bin/wrappers/rough"; import { RoughCanvas } from "roughjs/bin/canvas"; -import { SketchPicker } from "react-color"; +import { TwitterPicker } from "react-color"; import { moveOneLeft, moveAllLeft, moveOneRight, moveAllRight } from "./zindex"; import { roundRect } from "./roundRect"; @@ -980,16 +980,9 @@ function restore( } } -enum ColorPicker { - CANVAS_BACKGROUND, - SHAPE_STROKE, - SHAPE_BACKGROUND -} - type AppState = { draggingElement: ExcalidrawElement | null; resizingElement: ExcalidrawElement | null; - currentColorPicker: ColorPicker | null; elementType: string; exportBackground: boolean; currentItemStrokeColor: string; @@ -1124,70 +1117,17 @@ const hasStroke = () => element.type === "arrow") ); -function getSelectedFillStyles() { - const fillStyles = Array.from( +function getSelectedAttribute( + getAttribute: (element: ExcalidrawElement) => T +): T | null { + const attributes = Array.from( new Set( elements .filter(element => element.isSelected) - .map(element => element.fillStyle) + .map(element => getAttribute(element)) ) ); - return fillStyles.length === 1 ? fillStyles[0] : null; -} - -function getSelectedStrokeWidth() { - const strokeWidth = Array.from( - new Set( - elements - .filter(element => element.isSelected) - .map(element => element.strokeWidth) - ) - ); - return strokeWidth.length === 1 ? strokeWidth[0] : null; -} - -function getSelectedRoughness() { - const roughness = Array.from( - new Set( - elements - .filter(element => element.isSelected) - .map(element => element.roughness) - ) - ); - return roughness.length === 1 ? roughness[0] : null; -} - -function getSelectedOpacity() { - const opacity = Array.from( - new Set( - elements - .filter(element => element.isSelected) - .map(element => element.opacity) - ) - ); - return opacity.length === 1 ? opacity[0] : null; -} - -function getSelectedStrokeColor() { - const strokeColors = Array.from( - new Set( - elements - .filter(element => element.isSelected) - .map(element => element.strokeColor) - ) - ); - return strokeColors.length === 1 ? strokeColors[0] : null; -} - -function getSelectedBackgroundColor() { - const backgroundColors = Array.from( - new Set( - elements - .filter(element => element.isSelected) - .map(element => element.backgroundColor) - ) - ); - return backgroundColors.length === 1 ? backgroundColors[0] : null; + return attributes.length === 1 ? attributes[0] : null; } function addTextElement(element: ExcalidrawTextElement) { @@ -1254,6 +1194,56 @@ function ButtonSelect({ ); } +function ColorPicker({ + color, + onChange +}: { + color: string | null; + onChange: (color: string) => void; +}) { + const [isActive, setActive] = React.useState(false); + return ( +
+
Stroke Color
-
-