@cntrl-site/sdk-nextjs 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cntrl-site-sdk-nextjs-0.4.0.tgz +0 -0
- package/lib/common/useCurrentLayout.js +42 -0
- package/lib/common/useKeyframeValue.js +66 -0
- package/lib/components/Article.js +6 -1
- package/lib/components/Item.js +21 -15
- package/lib/components/Page.js +6 -2
- package/lib/components/items/ImageItem.js +7 -4
- package/lib/components/items/RectangleItem.js +8 -5
- package/lib/components/items/VideoItem.js +7 -4
- package/lib/components/items/VimeoEmbed.js +14 -15
- package/lib/components/items/YoutubeEmbed.js +14 -15
- package/lib/components/items/useEmbedVideoItem.js +16 -0
- package/lib/components/items/useFileItem.js +22 -0
- package/lib/components/items/useRectangleItem.js +29 -0
- package/lib/components/useItemAngle.js +9 -0
- package/lib/components/useItemDimensions.js +19 -0
- package/lib/components/useItemPosition.js +34 -0
- package/lib/provider/ArticleRectContext.js +5 -0
- package/lib/provider/CntrlSdkContext.js +9 -3
- package/lib/provider/Keyframes.js +12 -0
- package/lib/provider/KeyframesContext.js +6 -0
- package/lib/utils/Animator/Animator.js +159 -0
- package/lib/utils/ArticleRectManager/ArticleRectObserver.js +61 -0
- package/lib/utils/ArticleRectManager/useArticleRectObserver.js +18 -0
- package/lib/utils/EventEmitter.js +35 -0
- package/lib/utils/binSearchInsertAt.js +36 -0
- package/package.json +5 -3
- package/src/common/useCurrentLayout.ts +47 -0
- package/src/common/useKeyframeValue.ts +74 -0
- package/src/components/Article.tsx +9 -4
- package/src/components/Item.tsx +27 -17
- package/src/components/Page.tsx +10 -4
- package/src/components/items/ImageItem.tsx +9 -4
- package/src/components/items/RectangleItem.tsx +10 -5
- package/src/components/items/VideoItem.tsx +9 -4
- package/src/components/items/VimeoEmbed.tsx +17 -17
- package/src/components/items/YoutubeEmbed.tsx +16 -16
- package/src/components/items/useEmbedVideoItem.ts +19 -0
- package/src/components/items/useFileItem.ts +29 -0
- package/src/components/items/useRectangleItem.ts +40 -0
- package/src/components/useItemAngle.ts +12 -0
- package/src/components/useItemDimensions.ts +23 -0
- package/src/components/useItemPosition.ts +36 -0
- package/src/provider/ArticleRectContext.ts +4 -0
- package/src/provider/CntrlSdkContext.ts +14 -5
- package/src/provider/Keyframes.ts +11 -0
- package/src/provider/KeyframesContext.ts +5 -0
- package/src/utils/Animator/Animator.ts +206 -0
- package/src/utils/ArticleRectManager/ArticleRectObserver.ts +69 -0
- package/src/utils/ArticleRectManager/useArticleRectObserver.ts +17 -0
- package/src/utils/EventEmitter.ts +41 -0
- package/src/utils/binSearchInsertAt.ts +33 -0
- package/.idea/inspectionProfiles/Project_Default.xml +0 -15
|
@@ -3,15 +3,23 @@ import { TRectangleItem, getLayoutStyles } from '@cntrl-site/sdk';
|
|
|
3
3
|
import { ItemProps } from '../Item';
|
|
4
4
|
import { LinkWrapper } from '../LinkWrapper';
|
|
5
5
|
import { useCntrlContext } from '../../provider/useCntrlContext';
|
|
6
|
+
import { useRectangleItem } from './useRectangleItem';
|
|
6
7
|
|
|
7
8
|
export const RectangleItem: FC<ItemProps<TRectangleItem>> = ({ item }) => {
|
|
8
9
|
const { layouts } = useCntrlContext();
|
|
10
|
+
const { fillColor, radius, strokeWidth } = useRectangleItem(item);
|
|
9
11
|
return (
|
|
10
12
|
<LinkWrapper url={item.link?.url}>
|
|
11
13
|
<>
|
|
12
|
-
<div className={`rectangle-${item.id}`}
|
|
14
|
+
<div className={`rectangle-${item.id}`}
|
|
15
|
+
style={{
|
|
16
|
+
backgroundColor: `${fillColor}`,
|
|
17
|
+
borderRadius: `${radius * 100}vw`,
|
|
18
|
+
borderWidth: `${strokeWidth * 100}vw`
|
|
19
|
+
}}
|
|
20
|
+
/>
|
|
13
21
|
<style jsx>{`
|
|
14
|
-
${getLayoutStyles(layouts, [item.layoutParams], ([{ strokeColor
|
|
22
|
+
${getLayoutStyles(layouts, [item.layoutParams], ([{ strokeColor }]) => (`
|
|
15
23
|
.rectangle-${item.id} {
|
|
16
24
|
position: absolute;
|
|
17
25
|
width: 100%;
|
|
@@ -19,9 +27,6 @@ export const RectangleItem: FC<ItemProps<TRectangleItem>> = ({ item }) => {
|
|
|
19
27
|
border-style: solid;
|
|
20
28
|
box-sizing: border-box;
|
|
21
29
|
border-color: ${strokeColor};
|
|
22
|
-
background-color: ${fillColor};
|
|
23
|
-
border-radius: ${radius * 100}vw;
|
|
24
|
-
border-width: ${strokeWidth * 100}vw;
|
|
25
30
|
}`
|
|
26
31
|
))}
|
|
27
32
|
`}</style>
|
|
@@ -4,18 +4,25 @@ import { ItemProps } from '../Item';
|
|
|
4
4
|
import { LinkWrapper } from '../LinkWrapper';
|
|
5
5
|
import { getLayoutStyles } from '@cntrl-site/sdk';
|
|
6
6
|
import { useCntrlContext } from '../../provider/useCntrlContext';
|
|
7
|
+
import { useFileItem } from './useFileItem';
|
|
7
8
|
|
|
8
9
|
export const VideoItem: FC<ItemProps<TVideoItem>> = ({ item }) => {
|
|
9
10
|
const { layouts } = useCntrlContext();
|
|
11
|
+
const { radius, strokeWidth } = useFileItem(item);
|
|
10
12
|
return (
|
|
11
13
|
<LinkWrapper url={item.link?.url}>
|
|
12
|
-
<div className={`video-wrapper-${item.id}`}
|
|
14
|
+
<div className={`video-wrapper-${item.id}`}
|
|
15
|
+
style={{
|
|
16
|
+
borderRadius: `${radius * 100}vw`,
|
|
17
|
+
borderWidth: `${strokeWidth * 100}vw`
|
|
18
|
+
}}
|
|
19
|
+
>
|
|
13
20
|
<video autoPlay muted loop playsInline className="video">
|
|
14
21
|
<source src={item.commonParams.url} />
|
|
15
22
|
</video>
|
|
16
23
|
</div>
|
|
17
24
|
<style jsx>{`
|
|
18
|
-
${getLayoutStyles(layouts, [item.layoutParams], ([{ strokeColor,
|
|
25
|
+
${getLayoutStyles(layouts, [item.layoutParams], ([{ strokeColor, opacity }]) => (`
|
|
19
26
|
.video-wrapper-${item.id} {
|
|
20
27
|
position: absolute;
|
|
21
28
|
overflow: hidden;
|
|
@@ -24,8 +31,6 @@ export const VideoItem: FC<ItemProps<TVideoItem>> = ({ item }) => {
|
|
|
24
31
|
border-style: solid;
|
|
25
32
|
box-sizing: border-box;
|
|
26
33
|
border-color: ${strokeColor};
|
|
27
|
-
border-radius: ${radius * 100}vw;
|
|
28
|
-
border-width: ${strokeWidth * 100}vw;
|
|
29
34
|
opacity: ${opacity};
|
|
30
35
|
}`
|
|
31
36
|
))
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { FC } from 'react';
|
|
2
|
-
import { getLayoutStyles } from '@cntrl-site/sdk';
|
|
3
2
|
import { TVimeoEmbedItem } from '@cntrl-site/core';
|
|
4
3
|
import { ItemProps } from '../Item';
|
|
5
4
|
import { LinkWrapper } from '../LinkWrapper';
|
|
6
|
-
import {
|
|
5
|
+
import { useEmbedVideoItem } from './useEmbedVideoItem';
|
|
7
6
|
|
|
8
7
|
export const VimeoEmbedItem: FC<ItemProps<TVimeoEmbedItem>> = ({ item }) => {
|
|
9
|
-
const {
|
|
10
|
-
|
|
8
|
+
const { radius } = useEmbedVideoItem(item);
|
|
9
|
+
const { autoplay, controls, loop, muted, pictureInPicture, url } = item.commonParams;
|
|
11
10
|
const getValidVimeoUrl = (url: string): string => {
|
|
12
11
|
const validURL = new URL(url);
|
|
13
12
|
validURL.searchParams.append('controls', String(controls));
|
|
@@ -26,7 +25,11 @@ export const VimeoEmbedItem: FC<ItemProps<TVimeoEmbedItem>> = ({ item }) => {
|
|
|
26
25
|
|
|
27
26
|
return (
|
|
28
27
|
<LinkWrapper url={item.link?.url}>
|
|
29
|
-
<div className={`embed-video-wrapper-${item.id}`}
|
|
28
|
+
<div className={`embed-video-wrapper-${item.id}`}
|
|
29
|
+
style={{
|
|
30
|
+
borderRadius: `${radius * 100}vw`
|
|
31
|
+
}}
|
|
32
|
+
>
|
|
30
33
|
<iframe
|
|
31
34
|
className="embedVideo"
|
|
32
35
|
src={validUrl || ''}
|
|
@@ -35,18 +38,15 @@ export const VimeoEmbedItem: FC<ItemProps<TVimeoEmbedItem>> = ({ item }) => {
|
|
|
35
38
|
/>
|
|
36
39
|
</div>
|
|
37
40
|
<style jsx>{`
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
border-radius: ${radius * 100}vw;
|
|
48
|
-
}`
|
|
49
|
-
))}
|
|
41
|
+
.embed-video-wrapper-${item.id} {
|
|
42
|
+
position: absolute;
|
|
43
|
+
overflow: hidden;
|
|
44
|
+
width: 100%;
|
|
45
|
+
height: 100%;
|
|
46
|
+
top: 50%;
|
|
47
|
+
left: 50%;
|
|
48
|
+
transform: translate(-50%, -50%);
|
|
49
|
+
}
|
|
50
50
|
.embedVideo {
|
|
51
51
|
width: 100%;
|
|
52
52
|
height: 100%;
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { FC } from 'react';
|
|
2
|
-
import { getLayoutStyles } from '@cntrl-site/sdk';
|
|
3
2
|
import { TYoutubeEmbedItem } from '@cntrl-site/core';
|
|
4
3
|
import { ItemProps } from '../Item';
|
|
5
4
|
import { LinkWrapper } from '../LinkWrapper';
|
|
6
5
|
import { getYoutubeId } from '../../utils/getValidYoutubeUrl';
|
|
7
|
-
import {
|
|
6
|
+
import { useEmbedVideoItem } from './useEmbedVideoItem';
|
|
8
7
|
|
|
9
8
|
export const YoutubeEmbedItem: FC<ItemProps<TYoutubeEmbedItem>> = ({ item }) => {
|
|
10
|
-
const { layouts } = useCntrlContext();
|
|
11
9
|
const { autoplay, controls, url } = item.commonParams;
|
|
10
|
+
const { radius } = useEmbedVideoItem(item);
|
|
12
11
|
|
|
13
12
|
const getValidYoutubeUrl = (url: string): string => {
|
|
14
13
|
const newUrl = new URL(url);
|
|
@@ -24,7 +23,11 @@ export const YoutubeEmbedItem: FC<ItemProps<TYoutubeEmbedItem>> = ({ item }) =>
|
|
|
24
23
|
|
|
25
24
|
return (
|
|
26
25
|
<LinkWrapper url={item.link?.url}>
|
|
27
|
-
<div className={`embed-youtube-video-wrapper-${item.id}`}
|
|
26
|
+
<div className={`embed-youtube-video-wrapper-${item.id}`}
|
|
27
|
+
style={{
|
|
28
|
+
borderRadius: `${radius * 100}vw`
|
|
29
|
+
}}
|
|
30
|
+
>
|
|
28
31
|
<iframe
|
|
29
32
|
className="embedYoutubeVideo"
|
|
30
33
|
src={validUrl || ''}
|
|
@@ -33,18 +36,15 @@ export const YoutubeEmbedItem: FC<ItemProps<TYoutubeEmbedItem>> = ({ item }) =>
|
|
|
33
36
|
/>
|
|
34
37
|
</div>
|
|
35
38
|
<style jsx>{`
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
border-radius: ${radius * 100}vw;
|
|
46
|
-
}`
|
|
47
|
-
))}
|
|
39
|
+
.embed-youtube-video-wrapper-${item.id} {
|
|
40
|
+
position: absolute;
|
|
41
|
+
overflow: hidden;
|
|
42
|
+
width: 100%;
|
|
43
|
+
height: 100%;
|
|
44
|
+
top: 50%;
|
|
45
|
+
left: 50%;
|
|
46
|
+
transform: translate(-50%, -50%);
|
|
47
|
+
}
|
|
48
48
|
.embedYoutubeVideo {
|
|
49
49
|
width: 100%;
|
|
50
50
|
height: 100%;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { TVimeoEmbedItem, TYoutubeEmbedItem } from '@cntrl-site/core';
|
|
2
|
+
import { useCurrentLayout } from '../../common/useCurrentLayout';
|
|
3
|
+
import { useKeyframeValue } from '../../common/useKeyframeValue';
|
|
4
|
+
|
|
5
|
+
export const useEmbedVideoItem = (item: TVimeoEmbedItem | TYoutubeEmbedItem) => {
|
|
6
|
+
const layoutId = useCurrentLayout();
|
|
7
|
+
const radius = useKeyframeValue(
|
|
8
|
+
item,
|
|
9
|
+
(item, layoutId) => {
|
|
10
|
+
if (!layoutId) return 0;
|
|
11
|
+
const layoutParams = item.layoutParams[layoutId];
|
|
12
|
+
return 'radius' in layoutParams ? layoutParams.radius : 0;
|
|
13
|
+
},
|
|
14
|
+
(animator, scroll, value) => animator.getRadius({ radius: value }, scroll).radius,
|
|
15
|
+
[layoutId]
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
return { radius };
|
|
19
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { TImageItem, TVideoItem } from '@cntrl-site/sdk';
|
|
2
|
+
import { useKeyframeValue } from '../../common/useKeyframeValue';
|
|
3
|
+
import { useCurrentLayout } from '../../common/useCurrentLayout';
|
|
4
|
+
|
|
5
|
+
export const useFileItem = (item: TImageItem | TVideoItem) => {
|
|
6
|
+
const layoutId = useCurrentLayout();
|
|
7
|
+
const radius = useKeyframeValue(
|
|
8
|
+
item,
|
|
9
|
+
(item, layoutId) => {
|
|
10
|
+
if (!layoutId) return 0;
|
|
11
|
+
const layoutParams = item.layoutParams[layoutId];
|
|
12
|
+
return 'radius' in layoutParams ? layoutParams.radius : 0;
|
|
13
|
+
},
|
|
14
|
+
(animator, scroll, value) => animator.getRadius({ radius: value }, scroll).radius,
|
|
15
|
+
[layoutId]
|
|
16
|
+
);
|
|
17
|
+
const strokeWidth = useKeyframeValue(
|
|
18
|
+
item,
|
|
19
|
+
(item, layoutId) => {
|
|
20
|
+
if (!layoutId) return 0;
|
|
21
|
+
const layoutParams = item.layoutParams[layoutId];
|
|
22
|
+
return 'strokeWidth' in layoutParams ? layoutParams.strokeWidth : 0;
|
|
23
|
+
},
|
|
24
|
+
(animator, scroll, value) => animator.getBorderWidth({ borderWidth: value }, scroll).borderWidth,
|
|
25
|
+
[layoutId]
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
return { radius, strokeWidth };
|
|
29
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { TRectangleItem } from '@cntrl-site/sdk';
|
|
2
|
+
import { useKeyframeValue } from '../../common/useKeyframeValue';
|
|
3
|
+
import { useCurrentLayout } from '../../common/useCurrentLayout';
|
|
4
|
+
|
|
5
|
+
const defaultColor = 'rgba(0, 0, 0, 1)';
|
|
6
|
+
|
|
7
|
+
export const useRectangleItem = (item: TRectangleItem) => {
|
|
8
|
+
const layoutId = useCurrentLayout();
|
|
9
|
+
const radius = useKeyframeValue(
|
|
10
|
+
item,
|
|
11
|
+
(item, layoutId) => {
|
|
12
|
+
if (!layoutId) return 0;
|
|
13
|
+
const layoutParams = item.layoutParams[layoutId];
|
|
14
|
+
return 'radius' in layoutParams ? layoutParams.radius : 0;
|
|
15
|
+
},
|
|
16
|
+
(animator, scroll, value) => animator.getRadius({ radius: value }, scroll).radius,
|
|
17
|
+
[layoutId]
|
|
18
|
+
);
|
|
19
|
+
const strokeWidth = useKeyframeValue(
|
|
20
|
+
item,
|
|
21
|
+
(item, layoutId) => {
|
|
22
|
+
if (!layoutId) return 0;
|
|
23
|
+
const layoutParams = item.layoutParams[layoutId];
|
|
24
|
+
return 'strokeWidth' in layoutParams ? layoutParams.strokeWidth : 0;
|
|
25
|
+
},
|
|
26
|
+
(animator, scroll, value) => animator.getBorderWidth({ borderWidth: value }, scroll).borderWidth,
|
|
27
|
+
[layoutId]
|
|
28
|
+
);
|
|
29
|
+
const fillColor = useKeyframeValue(
|
|
30
|
+
item,
|
|
31
|
+
(item, layoutId) => {
|
|
32
|
+
if (!layoutId) return defaultColor;
|
|
33
|
+
const layoutParams = item.layoutParams[layoutId];
|
|
34
|
+
return 'fillColor' in layoutParams ? layoutParams.fillColor : defaultColor;
|
|
35
|
+
},
|
|
36
|
+
(animator, scroll, value) => animator.getColor({ color: value }, scroll).color
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
return { fillColor, strokeWidth, radius };
|
|
40
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TArticleItemAny } from '@cntrl-site/sdk';
|
|
2
|
+
import { useKeyframeValue } from '../common/useKeyframeValue';
|
|
3
|
+
|
|
4
|
+
export const useItemAngle = (item: TArticleItemAny) => {
|
|
5
|
+
const { angle } = useKeyframeValue(
|
|
6
|
+
item,
|
|
7
|
+
(item, layoutId) => ({ angle: layoutId ? item.area[layoutId].angle : 0 }),
|
|
8
|
+
(animator, scroll, value) => animator.getRotation(value, scroll)
|
|
9
|
+
);
|
|
10
|
+
return angle;
|
|
11
|
+
};
|
|
12
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { TArticleItemAny } from '@cntrl-site/sdk';
|
|
2
|
+
import { useCurrentLayout } from '../common/useCurrentLayout';
|
|
3
|
+
import { useKeyframeValue } from '../common/useKeyframeValue';
|
|
4
|
+
|
|
5
|
+
const defaultArea = {
|
|
6
|
+
left: 0,
|
|
7
|
+
top: 0,
|
|
8
|
+
width: 0,
|
|
9
|
+
height: 0,
|
|
10
|
+
angle: 0,
|
|
11
|
+
zIndex: 0
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const useItemDimensions = (item: TArticleItemAny) => {
|
|
15
|
+
const layoutId = useCurrentLayout();
|
|
16
|
+
const { width, height } = useKeyframeValue<{ width: number; height: number }>(
|
|
17
|
+
item,
|
|
18
|
+
(item, layoutId) => layoutId ? item.area[layoutId] : defaultArea,
|
|
19
|
+
(animator, scroll, value) => animator.getDimensions(value, scroll),
|
|
20
|
+
[layoutId]
|
|
21
|
+
);
|
|
22
|
+
return { width, height };
|
|
23
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { AnchorSide, TArticleItemAny } from '@cntrl-site/sdk';
|
|
2
|
+
import { useKeyframeValue } from '../common/useKeyframeValue';
|
|
3
|
+
import { useCurrentLayout } from '../common/useCurrentLayout';
|
|
4
|
+
|
|
5
|
+
const defaultArea = {
|
|
6
|
+
left: 0,
|
|
7
|
+
top: 0,
|
|
8
|
+
width: 0,
|
|
9
|
+
height: 0,
|
|
10
|
+
angle: 0,
|
|
11
|
+
zIndex: 0
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const useItemPosition = (item: TArticleItemAny) => {
|
|
15
|
+
const layoutId = useCurrentLayout()
|
|
16
|
+
const { top, left } = useKeyframeValue<{ top: number; left: number }>(
|
|
17
|
+
item,
|
|
18
|
+
(item, layoutId) => item.area[layoutId],
|
|
19
|
+
(animator, scroll, value) => animator.getPositions(value, scroll),
|
|
20
|
+
[layoutId]
|
|
21
|
+
);
|
|
22
|
+
return { top: getItemTopStyle(top, layoutId ? item.area[layoutId].anchorSide : AnchorSide.Top), left };
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export function getItemTopStyle(top: number, anchorSide?: AnchorSide) {
|
|
26
|
+
const defaultValue = `${top * 100}vw`;
|
|
27
|
+
if (!anchorSide) return defaultValue;
|
|
28
|
+
switch (anchorSide) {
|
|
29
|
+
case AnchorSide.Top:
|
|
30
|
+
return defaultValue;
|
|
31
|
+
case AnchorSide.Center:
|
|
32
|
+
return `calc(50% + ${top * 100}vw)`;
|
|
33
|
+
case AnchorSide.Bottom:
|
|
34
|
+
return `calc(100% + ${top * 100}vw)`;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
import { CustomItemRegistry } from './CustomItemRegistry';
|
|
2
|
-
import { TLayout, TTypePresets
|
|
2
|
+
import { TLayout, TTypePresets, } from '@cntrl-site/sdk';
|
|
3
|
+
|
|
3
4
|
|
|
4
5
|
export class CntrlSdkContext {
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
private _typePresets?: TTypePresets;
|
|
7
|
+
private _layouts: TLayout[] = [];
|
|
7
8
|
constructor(
|
|
8
9
|
public readonly customItems: CustomItemRegistry,
|
|
9
10
|
) {}
|
|
10
11
|
|
|
11
12
|
setTypePresets(typePresets: TTypePresets) {
|
|
12
|
-
this.
|
|
13
|
+
this._typePresets = typePresets;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
setLayouts(layouts: TLayout[]) {
|
|
16
|
-
this.
|
|
17
|
+
this._layouts = layouts;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get layouts(): TLayout[] {
|
|
21
|
+
return this._layouts;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get typePresets(): TTypePresets | undefined {
|
|
25
|
+
return this._typePresets;
|
|
17
26
|
}
|
|
18
27
|
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { KeyframeType, TKeyframeValueMap } from '@cntrl-site/sdk';
|
|
2
|
+
import { binSearchInsertAt, createInsert } from '../binSearchInsertAt';
|
|
3
|
+
|
|
4
|
+
export interface AnimationData<T extends KeyframeType> {
|
|
5
|
+
position: number;
|
|
6
|
+
value: TKeyframeValueMap[T];
|
|
7
|
+
type: T;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface PositionKeyframes<T extends KeyframeType> {
|
|
11
|
+
start: AnimationData<T>;
|
|
12
|
+
end: AnimationData<T>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type KeyframesMap = {
|
|
16
|
+
[T in KeyframeType]: AnimationData<T>[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const compare = (lhs: { position: number }, rhs: { position: number }) => lhs.position - rhs.position;
|
|
20
|
+
const insertBin = createInsert(binSearchInsertAt, compare);
|
|
21
|
+
|
|
22
|
+
export class Animator {
|
|
23
|
+
private static pushKeyframeToMap<T extends KeyframeType>(keyframe: AnimationData<T>, map: KeyframesMap): void {
|
|
24
|
+
insertBin(map[keyframe.type], keyframe);
|
|
25
|
+
}
|
|
26
|
+
private keyframesMap: KeyframesMap = createKeyframesMap();
|
|
27
|
+
constructor(
|
|
28
|
+
private keyframes: AnimationData<KeyframeType>[]
|
|
29
|
+
) {
|
|
30
|
+
this.fillKeyframesMap();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
getDimensions(
|
|
34
|
+
values: TKeyframeValueMap[KeyframeType.Dimensions],
|
|
35
|
+
pos: number
|
|
36
|
+
): TKeyframeValueMap[KeyframeType.Dimensions] {
|
|
37
|
+
const keyframes = this.keyframesMap[KeyframeType.Dimensions];
|
|
38
|
+
if (!keyframes || !keyframes.length) return values;
|
|
39
|
+
if (keyframes.length === 1) {
|
|
40
|
+
const [keyframe] = keyframes;
|
|
41
|
+
return {
|
|
42
|
+
width: keyframe.value.width,
|
|
43
|
+
height: keyframe.value.height
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const { start, end } = this.getStartEnd<KeyframeType.Dimensions>(pos, keyframes);
|
|
47
|
+
return {
|
|
48
|
+
width: rangeMap(pos, start.position, end.position, start.value.width, end.value.width, true),
|
|
49
|
+
height: rangeMap(pos, start.position, end.position, start.value.height, end.value.height, true)
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getPositions(
|
|
54
|
+
values: TKeyframeValueMap[KeyframeType.Position],
|
|
55
|
+
pos: number
|
|
56
|
+
): TKeyframeValueMap[KeyframeType.Position] {
|
|
57
|
+
const keyframes = this.keyframesMap[KeyframeType.Position];
|
|
58
|
+
if (!keyframes || !keyframes.length) return values;
|
|
59
|
+
if (keyframes.length === 1) {
|
|
60
|
+
const [keyframe] = keyframes;
|
|
61
|
+
return {
|
|
62
|
+
left: keyframe.value.left,
|
|
63
|
+
top: keyframe.value.top
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const { start, end } = this.getStartEnd<KeyframeType.Position>(pos, keyframes);
|
|
67
|
+
return {
|
|
68
|
+
left: rangeMap(pos, start.position, end.position, start.value.left, end.value.left, true),
|
|
69
|
+
top: rangeMap(pos, start.position, end.position, start.value.top, end.value.top, true)
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
getColor(
|
|
74
|
+
values: TKeyframeValueMap[KeyframeType.Color],
|
|
75
|
+
pos: number
|
|
76
|
+
): TKeyframeValueMap[KeyframeType.Color] {
|
|
77
|
+
const keyframes = this.keyframesMap[KeyframeType.Color];
|
|
78
|
+
if (!keyframes || !keyframes.length) return values;
|
|
79
|
+
if (keyframes.length === 1) {
|
|
80
|
+
const [keyframe] = keyframes;
|
|
81
|
+
return {
|
|
82
|
+
color: keyframe.value.color
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const { start, end } = this.getStartEnd<KeyframeType.Color>(pos, keyframes);
|
|
86
|
+
return {
|
|
87
|
+
color: this.getRgba(start, end, pos)
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
getRadius(
|
|
92
|
+
values: TKeyframeValueMap[KeyframeType.BorderRadius],
|
|
93
|
+
pos: number
|
|
94
|
+
): TKeyframeValueMap[KeyframeType.BorderRadius] {
|
|
95
|
+
const keyframes = this.keyframesMap[KeyframeType.BorderRadius];
|
|
96
|
+
if (!keyframes || !keyframes.length) return values;
|
|
97
|
+
if (keyframes.length === 1) {
|
|
98
|
+
const [keyframe] = keyframes;
|
|
99
|
+
return {
|
|
100
|
+
radius: keyframe.value.radius
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
const { start, end } = this.getStartEnd<KeyframeType.BorderRadius>(pos, keyframes);
|
|
104
|
+
return {
|
|
105
|
+
radius: rangeMap(pos, start.position, end.position, start.value.radius, end.value.radius, true)
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
getBorderWidth(
|
|
110
|
+
values: TKeyframeValueMap[KeyframeType.BorderWidth],
|
|
111
|
+
pos: number
|
|
112
|
+
): TKeyframeValueMap[KeyframeType.BorderWidth] {
|
|
113
|
+
const keyframes = this.keyframesMap[KeyframeType.BorderWidth];
|
|
114
|
+
if (!keyframes || !keyframes.length) return values;
|
|
115
|
+
if (keyframes.length === 1) {
|
|
116
|
+
const [keyframe] = keyframes;
|
|
117
|
+
return {
|
|
118
|
+
borderWidth: keyframe.value.borderWidth
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const { start, end } = this.getStartEnd<KeyframeType.BorderWidth>(pos, keyframes);
|
|
122
|
+
return {
|
|
123
|
+
borderWidth: rangeMap(pos, start.position, end.position, start.value.borderWidth, end.value.borderWidth, true)
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
getRotation(
|
|
128
|
+
values: TKeyframeValueMap[KeyframeType.Rotation],
|
|
129
|
+
pos: number
|
|
130
|
+
): TKeyframeValueMap[KeyframeType.Rotation] {
|
|
131
|
+
const keyframes = this.keyframesMap[KeyframeType.Rotation];
|
|
132
|
+
if (!keyframes || !keyframes.length) return values;
|
|
133
|
+
if (keyframes.length === 1) {
|
|
134
|
+
const [keyframe] = keyframes;
|
|
135
|
+
return {
|
|
136
|
+
angle: keyframe.value.angle
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
const { start, end } = this.getStartEnd<KeyframeType.Rotation>(pos, keyframes);
|
|
140
|
+
return {
|
|
141
|
+
angle: rangeMap(pos, start.position, end.position, start.value.angle, end.value.angle, true)
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
getStartEnd<T extends KeyframeType>(position: number, keyframes: AnimationData<T>[]): PositionKeyframes<T> {
|
|
146
|
+
const index = binSearchInsertAt(keyframes, { position }, compare);
|
|
147
|
+
const end = index === keyframes.length ? index - 1 : index;
|
|
148
|
+
const start = end === 0 ? end : end - 1;
|
|
149
|
+
return { start: keyframes[start], end: keyframes[end] };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private fillKeyframesMap() {
|
|
153
|
+
this.keyframesMap = createKeyframesMap();
|
|
154
|
+
for (const keyframe of this.keyframes) {
|
|
155
|
+
Animator.pushKeyframeToMap(keyframe, this.keyframesMap);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private getRgba(
|
|
160
|
+
start: AnimationData<KeyframeType.Color>,
|
|
161
|
+
end: AnimationData<KeyframeType.Color>,
|
|
162
|
+
position: number
|
|
163
|
+
): string {
|
|
164
|
+
const [startR, startG, startB, startA] = parseRgba(start.value.color);
|
|
165
|
+
const [endR, endG, endB, endA] = parseRgba(end.value.color);
|
|
166
|
+
const r = rangeMap(position, start.position, end.position, startR, endR, true);
|
|
167
|
+
const g = rangeMap(position, start.position, end.position, startG, endG, true);
|
|
168
|
+
const b = rangeMap(position, start.position, end.position, startB, endB, true);
|
|
169
|
+
const a = rangeMap(position, start.position, end.position, startA, endA, true);
|
|
170
|
+
return `rgba(${r},${g},${b},${a})`;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const reRgba = /^rgba?\( *(\d+) *, *(\d+) *, *(\d+)(?: *, *(\d+(?:\.\d+)?))? *\)$/i;
|
|
175
|
+
|
|
176
|
+
function parseRgba(color: string): [r: number, g: number, b: number, a: number] {
|
|
177
|
+
const match = reRgba.exec(color);
|
|
178
|
+
if (!match) return [0, 0, 0, 1];
|
|
179
|
+
const [, r, g, b, a] = match;
|
|
180
|
+
return [parseInt(r, 10), parseInt(g, 10), parseInt(b, 10), a ? parseFloat(a) : NaN];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function createKeyframesMap(): KeyframesMap {
|
|
184
|
+
return {
|
|
185
|
+
[KeyframeType.Dimensions]: [],
|
|
186
|
+
[KeyframeType.Position]: [],
|
|
187
|
+
[KeyframeType.BorderWidth]: [],
|
|
188
|
+
[KeyframeType.BorderRadius]: [],
|
|
189
|
+
[KeyframeType.Color]: [],
|
|
190
|
+
[KeyframeType.Rotation]: []
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function rangeMap(
|
|
195
|
+
n: number,
|
|
196
|
+
start1: number,
|
|
197
|
+
stop1: number,
|
|
198
|
+
start2: number,
|
|
199
|
+
stop2: number,
|
|
200
|
+
withinBounds: boolean = false
|
|
201
|
+
): number {
|
|
202
|
+
const mapped = (n - start1) / (stop1 - start1) * (stop2 - start2) + start2;
|
|
203
|
+
if (withinBounds && n < start1) return start2;
|
|
204
|
+
if (withinBounds && n > stop1) return stop2;
|
|
205
|
+
return mapped;
|
|
206
|
+
}
|