Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve: Set a separate fill color for text #421

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/react-filerobot-image-editor/src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import enableTextContentEdit, {
import setResize, { SET_RESIZE } from './setResize';
import setSaved, { SET_SAVED } from './setSaved';
import updateState, { UPDATE_STATE } from './updateState';
import setLatestColor, { SET_LATEST_COLOR } from './setLatestColor';
import setLatestColor, { SET_LATEST_COLOR, SET_LATEST_TEXT_COLOR, setLatestTextColor } from './setLatestColor';
import setShowTabsMenu, { SET_SHOWN_TABS_MENU } from './setShowTabsMenu';

export default {
Expand All @@ -55,6 +55,7 @@ export default {
[SET_SHOWN_IMAGE_DIMENSIONS]: setShownImageDimensions,
[ENABLE_TEXT_CONTENT_EDIT]: enableTextContentEdit,
[SET_LATEST_COLOR]: setLatestColor,
[SET_LATEST_TEXT_COLOR]: setLatestTextColor,
[SET_SHOWN_TABS_MENU]: setShowTabsMenu,
// Start of Design actions...
[ADD_FILTER]: addFilter,
Expand Down Expand Up @@ -90,6 +91,7 @@ export {
SET_SHOWN_IMAGE_DIMENSIONS,
ENABLE_TEXT_CONTENT_EDIT,
SET_LATEST_COLOR,
SET_LATEST_TEXT_COLOR,
SET_SHOWN_TABS_MENU,
// Start of Design actions...
ADD_FILTER,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const SET_LATEST_COLOR = 'SET_LATEST_COLOR';
export const SET_LATEST_TEXT_COLOR = 'SET_LATEST_TEXT_COLOR';

const setLatestColor = (state, payload) => ({
...state,
Expand All @@ -8,4 +9,12 @@ const setLatestColor = (state, payload) => ({
},
});

export const setLatestTextColor = (state, payload) => ({
...state,
latestTextColors: {
...state.latestTextColors,
...payload.latestTextColors,
},
})

export default setLatestColor;
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ const AnnotationOptions = ({
>
{!hideFillOption && (
<ColorInput
type={annotation.name}
color={annotation.fill}
onChange={changeAnnotationFill}
colorFor="fill"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,30 @@ import PropTypes from 'prop-types';

/** Internal Dependencies */
import { useStore } from 'hooks';
import { SET_LATEST_COLOR } from 'actions';
import { SET_LATEST_COLOR, SET_LATEST_TEXT_COLOR } from 'actions';
import ColorPickerModal from '../ColorPickerModal';
import { StyledPickerTrigger } from './ColorInput.styled';

const pinnedColorsKey = 'FIE_pinnedColors';

// colorFor is used to save the latest color for a specific purpose (e.g. fill/shadow/stroke)
const ColorInput = ({ onChange, color, colorFor }) => {
const ColorInput = ({ onChange, color, colorFor, type }) => {
const {
selectionsIds = [],
config: { annotationsCommon = {} },
dispatch,
latestColors = {},
latestTextColors = {},
} = useStore();
const latestColor = latestColors[colorFor];
let latestColor = latestColors[colorFor];
let latestTextColor = latestTextColors[colorFor]
const [anchorEl, setAnchorEl] = useState();
const [currentColor, setCurrentColor] = useState(
() => latestColor || color || annotationsCommon.fill,
);
const [currentTextColor, setCurrentTextColor] = useState(
() => latestTextColor || color || annotationsCommon.fill,
);
const [pinnedColors, setPinnedColors] = useState(
window?.localStorage
? JSON.parse(localStorage.getItem(pinnedColorsKey) || '[]')
Expand All @@ -47,19 +52,38 @@ const ColorInput = ({ onChange, color, colorFor }) => {
};

const changeColor = (_newColorHex, rgba, newPinnedColors) => {
setCurrentColor(rgba);

if (type === 'Text') {
setCurrentTextColor(rgba);
} else {
setCurrentColor(rgba);
}

onChange(rgba);
changePinnedColors(newPinnedColors);

if (latestColor !== rgba) {
dispatch({
type: SET_LATEST_COLOR,
payload: {
latestColors: {
[colorFor]: rgba,
if (type === 'Text') {
if (latestTextColor !== rgba) {
dispatch({
type: SET_LATEST_TEXT_COLOR,
payload: {
latestTextColors: {
[colorFor]: rgba,
},
},
});
}
} else {
if (latestColor !== rgba) {
dispatch({
type: SET_LATEST_COLOR,
payload: {
latestColors: {
[colorFor]: rgba,
},
},
},
});
});
}
}
};

Expand All @@ -68,23 +92,29 @@ const ColorInput = ({ onChange, color, colorFor }) => {
};

useEffect(() => {
const colorToSet = (selectionsIds.length === 0 && latestColor) || color;
setCurrentColor(colorToSet);
onChange(colorToSet);
if (type === 'Text') {
const colorToSet = (selectionsIds.length === 0 && latestTextColor) || color;
setCurrentTextColor(colorToSet);
onChange(colorToSet);
} else {
const colorToSet = (selectionsIds.length === 0 && latestColor) || color;
setCurrentColor(colorToSet);
onChange(colorToSet);
}
}, [color, selectionsIds]);

return (
<>
<StyledPickerTrigger
className="FIE_color-picker-triggerer"
onClick={togglePicker}
$color={currentColor}
$color={type === 'Text' ? currentTextColor : currentColor}
onChange={onChange}
/>
<ColorPickerModal
hideModalTitle
onChange={changeColor}
defaultColor={currentColor}
defaultColor={type === 'Text' ? currentTextColor : currentColor}
pinnedColors={pinnedColors}
open={Boolean(anchorEl)}
onClose={togglePicker}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const getInitialAppState = (config = {}) => {
isResetted: !hasLoadableDesignState ?? true,
haveNotSavedChanges: false,
latestColors: {},
latestTextColors: {},
showTabsMenu: false,
};
};
Expand Down