Skip to content

Commit

Permalink
Animations
Browse files Browse the repository at this point in the history
  • Loading branch information
rtcoder committed May 4, 2023
1 parent 79fb514 commit b88398f
Show file tree
Hide file tree
Showing 10 changed files with 456 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
.singleBlock {
position: relative;
box-sizing: border-box;
animation: none !important;
animation-delay: initial !important;
animation-direction: initial !important;
display: flex;
margin: 10px 0;

--animation: none;
--animation-delay: initial;
--animation-direction: initial;

&[style*="align-items: stretch"] {
.singleBlock {
width: 100%;
Expand All @@ -21,10 +14,13 @@
&:hover,
&.hovered {
outline: 2px solid #09c;
animation: none !important;
}

&.minimized {
//pointer-events: none;
filter: none !important;
animation: none !important;
height: 40px !important;
overflow: hidden;
border: none !important;
Expand Down
165 changes: 165 additions & 0 deletions src/components/Creator/LeftPanel/StylesPanel/Animations/Animations.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import styles from "@/components/Creator/LeftPanel/StylesPanel/StylesPanel.module.scss";
import {Option, Select} from "@/components/construction/Select";
import {useEffect, useState} from "react";
import {getInheritedStyleWith} from "@/helpers/block-styles";
import {useSelector} from "react-redux";
import Icon from "@/components/construction/Icon/Icon";
import SingleAnimation from "@/components/Creator/LeftPanel/StylesPanel/Animations/SingleAnimation/SingleAnimation";

type ChangeValue = {
animations: string[];
durations: string[];
delays: string[];
directions: string[];
iterations: string[];
}

interface Props {
onChange: (value: string | null, property: string) => void
}

const allAnimationsNames: [id: string, name: string][] = [
['none', 'Brak'],
['beat', 'Pulsowanie'],
['beat-fade', 'Pulsowanie z wygaszaniem'],
['bounce', 'Podskok'],
['fade', 'Wygaszanie'],
['flip', 'Odwracanie'],
['shake', 'Potrząsanie'],
['spin', 'Obrót'],
];
export default function (props: Props) {
const selectedBlock = useSelector((state: any) => state.structure.selectedBlock);
const rwd = useSelector((state: any) => state.structure.rwdMode);
const styleState = useSelector((state: any) => state.structure.styleState);
const [animations, setAnimations] = useState<string[]>([]);
const [durations, setDurations] = useState<string[]>([]);
const [delays, setDelays] = useState<string[]>([]);
const [directions, setDirections] = useState<string[]>([]);
const [iterations, setIterations] = useState<string[]>([]);

const changed = (value: ChangeValue) => {
setIterations([...value.iterations]);
setDirections([...value.directions]);
setDelays([...value.delays]);
setDurations([...value.durations]);
setAnimations([...value.animations]);
console.log({value})
if (value.animations.includes('none')) {
props.onChange('', 'animationIterationCount');
props.onChange('', 'animationDirection');
props.onChange('', 'animationDelay');
props.onChange('', 'animationDuration');
props.onChange('none', 'animationName');
return;
}
props.onChange(value.iterations.join(', '), 'animationIterationCount');
props.onChange(value.directions.join(', '), 'animationDirection');
props.onChange(value.delays.join(', '), 'animationDelay');
props.onChange(value.durations.join(', '), 'animationDuration');
props.onChange(value.animations.join(', '), 'animationName');
}
const splitValue = value => (value || '').split(',').map(val => val.trim()).filter(val => val.length > 0);
useEffect(() => {
const style = getInheritedStyleWith(
selectedBlock.styles,
rwd, styleState,
['animationName', 'animationDuration', 'animationDelay', 'animationDirection', 'animationIterationCount'],
) as any;
setIterations(splitValue(style.animationIterationCount));
setDirections(splitValue(style.animationDirection));
setDelays(splitValue(style.animationDelay));
setDurations(splitValue(style.animationDuration));
setAnimations(splitValue(style.animationName));
}, [selectedBlock, rwd, styleState])

const addAnimation = (animationId: string) => {
if (!animationId) {
return;
}
if (animationId === 'none') {
setIterations([]);
setDirections([]);
setDelays([]);
setDurations([]);
setAnimations(['none']);
changed({animations: ['none'], durations: [], delays: [], directions: [], iterations: []})
return;
}
iterations.push('infinite');
directions.push('normal');
delays.push('0s');
durations.push('1s');
if (animations.includes('none')) {
animations.length = 0;
}
animations.push(animationId);
changed({
animations,
durations,
delays,
directions,
iterations,
});
}
const removeFilter = index => {
iterations.splice(index, 1);
directions.splice(index, 1);
delays.splice(index, 1);
durations.splice(index, 1);
animations.splice(index, 1);
changed({
animations,
durations,
delays,
directions,
iterations,
});
}
const getAnimationName =animationId=>allAnimationsNames.find(([id,name])=>id===animationId)?.[1]||'';
const getAnimationsList = () => {
return animations.map((id, index) => {
return (<div className={styles.stylesFormRow} key={id}>
<SingleAnimation name={id}
duration={durations[index]}
delay={delays[index]}
direction={directions[index]}
iteration={iterations[index]}
label={getAnimationName(id)}
onChange={e => changedAnimation(index, e)}/>
<Icon type="material" name="close" onClick={e => removeFilter(index)}/>
</div>)
}
)
}

const changedAnimation = (index: number, [name, duration, delay, direction, iteration]) => {
iterations[index] = iteration;
directions[index] = direction;
delays[index] = delay;
durations[index] = duration;
changed({
animations,
durations,
delays,
directions,
iterations,
});
}

return (
<div className={styles.stylesFormGroup}>
<div className={styles.stylesFormRow}>
<Select onChange={addAnimation} label="Dodaj animacje">
<Option value="" selected={true}>Wybierz</Option>
{allAnimationsNames.map(([id, name]) =>
<Option key={id} value={id} disabled={animations.includes(id)}>{name}</Option>
)}
</Select>
</div>

{getAnimationsList()}

</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import styles from "@/components/Creator/LeftPanel/StylesPanel/StylesPanel.module.scss";
import InputWithUnits from "@/components/construction/InputWithUnits/InputWithUnits";
import {Units} from "@/types/units";
import {useState} from "react";
import {Option, Select} from "@/components/construction/Select";

type ValueType = [name: string, duration: string, delay: string, direction: string, iteration: string];

interface Props {
label: string;
name: string;
duration: string;
delay: string;
direction: string;
iteration: string;
onChange: (value: ValueType) => void
}

export default function (props: Props) {
const [duration, setDuration] = useState<string>(props.duration)
const [delay, setDelay] = useState<string>(props.delay)
const [direction, setDirection] = useState<string>(props.direction)
const [iteration, setIteration] = useState<string>(props.iteration)
const timeUnits: Units[] = ['s', 'ms'];

const changeDuration = (val: string) => {
setDuration(val);
props.onChange([props.name, val, delay, direction, iteration])
}
const changeDelay = (val: string) => {
setDelay(val);
props.onChange([props.name, duration, val, direction, iteration])
}
const changeDirection = (val: string) => {
setDirection(val);
props.onChange([props.name, duration, delay, val, iteration])
}
const changeIteration = (val: string) => {
setIteration(val);
props.onChange([props.name, duration, delay, direction, val])
}
return (
<div className={styles.stylesFormColumn}>
{props.label}
{props.name !== 'none'
? <>
<div className={styles.stylesFormRow}>
<div className={styles.stylesFormField}>
<InputWithUnits units={timeUnits} label="Czas trwania" value={duration}
onChange={changeDuration}
min={0}/>
</div>
<div className={styles.stylesFormField}>
<InputWithUnits units={timeUnits} label="Opóźnienie startu" value={delay}
onChange={changeDelay}
min={0}/>
</div>
</div>
<div className={styles.stylesFormRow}>
<div className={styles.stylesFormField}>
<Select label="Liczba iteracji" onChange={changeIteration}>
<Option value="infinite" selected={iteration === 'infinite'}>Nieskończona</Option>
<Option value="1" selected={iteration === '1'}>1</Option>
<Option value="2" selected={iteration === '2'}>2</Option>
<Option value="3" selected={iteration === '3'}>3</Option>
<Option value="4" selected={iteration === '4'}>4</Option>
<Option value="5" selected={iteration === '5'}>5</Option>
</Select>
</div>
<div className={styles.stylesFormField}>
<label htmlFor="animation-direction"></label>
<Select label="Kierunek animacji" onChange={changeDirection}>
<Option value="normal" selected={direction === 'normal'}>Do przodu</Option>
<Option value="reverse" selected={direction === 'reverse'}>Do tyłu</Option>
<Option value="alternate" selected={direction === 'alternate'}>Do przodu i do
tyłu</Option>
<Option value="alternate-reverse" selected={direction === 'alternate-reverse'}>Do tyłu i
do
przodu</Option>
</Select>
</div>
</div>
</> : ''}
</div>
)
}
11 changes: 7 additions & 4 deletions src/components/Creator/LeftPanel/StylesPanel/Filter/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ export default function (props: Props) {
}
const addFilterUnits = value => {
return [...value].map(([id, val]) => {
if(id === 'drop-shadow' ){
return [id, val.map((v,index)=>{
if(index===3){
if (id === 'drop-shadow') {
return [id, val.map((v, index) => {
if (index === 3) {
return v;
}
return `${parseInt(v)}px`
}).join(' ')]
}
const [, unit] = filterDefaultUnits.find(([f_id, _unit]) => f_id === id)!
return [id, `${val}${unit}`]
return [id, `${val}${unit}`]
});
}
const mapFiltersToArray = (value: string): SelectedFilter[] => {
Expand Down Expand Up @@ -110,6 +110,9 @@ export default function (props: Props) {
}, [selectedBlock, rwd, styleState])

const addFilter = (filterId: string) => {
if (!filterId) {
return;
}
const [, defaultValue] = filtersDefaultValues.find(([id, _]) => id === filterId);
const newFilter: SelectedFilter = [filterId, defaultValue];
setSelectedFilters([...selectedFilters, newFilter]);
Expand Down
3 changes: 2 additions & 1 deletion src/components/Creator/LeftPanel/StylesPanel/StylesPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Text from "@/components/Creator/LeftPanel/StylesPanel/Text/Text";
import TextShadow from "@/components/Creator/LeftPanel/StylesPanel/TextShadow/TextShadow";
import Background from "@/components/Creator/LeftPanel/StylesPanel/Background/Background";
import Filter from "@/components/Creator/LeftPanel/StylesPanel/Filter/Filter";
import Animations from "@/components/Creator/LeftPanel/StylesPanel/Animations/Animations";

export default function () {
const selectedBlock = useSelector((state: any) => state.structure.selectedBlock);
Expand Down Expand Up @@ -54,7 +55,7 @@ export default function () {
['margin', <AccordionItem key={'margin'} title="Marginesy zewnętrzne"><Margin onChange={styleChange}/></AccordionItem>],
['padding', <AccordionItem key={'padding'} title="Marginesy wewnętrzne"><Padding onChange={styleChange}/></AccordionItem>],
['filter', <AccordionItem key={'filter'} title="Filtry"><Filter onChange={styleChange}/></AccordionItem>],
// ['animations', <AccordionItem key={'animations'} title="Animacje"><Layout onChange={styleChange}/></AccordionItem>],
['animations', <AccordionItem key={'animations'} title="Animacje"><Animations onChange={styleChange}/></AccordionItem>],
]
const canShow = ([id,c]) => {
return shouldShowStyleForBlockType(selectedBlock.type, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface Props {
onChange: (value: string) => void;
}

const ALL_UNITS = ['px', 'pt', 'pc', 'in', 'mm', 'cm', '%', 'rem', 'em', 'vw', 'vh', 'vmin', 'vmax'];
const ALL_UNITS: Units[] = ['px', 'pt', 'pc', 'in', 'mm', 'cm', '%', 'rem', 'em', 'vw', 'vh', 'vmin', 'vmax', 'deg', 'ms', 's'];
export default function (props: Props) {
const propsValue = String(props.value || '');
const [inputValue, setInputValue] = useState('');
Expand Down
9 changes: 0 additions & 9 deletions src/helpers/block-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ export function getInheritedStyle(rwd: RWD_MODES, styleState: STYLE_STATE_NAMES,
}
}

if (finalStyles['animation']) {
finalStyles['--animation'] = finalStyles['animation'];
}
if (finalStyles['animationDelay']) {
finalStyles['--animation-delay'] = finalStyles['animationDelay'];
}
if (finalStyles['animationDirection']) {
finalStyles['--animation-direction'] = finalStyles['animationDirection'];
}
return finalStyles;
}

Expand Down
Loading

0 comments on commit b88398f

Please sign in to comment.