@cntrl-site/sdk-nextjs 0.2.13 → 0.4.0
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 +7 -2
- package/lib/components/Item.js +24 -16
- package/lib/components/Page.js +9 -2
- package/lib/components/Section.js +3 -1
- package/lib/components/items/ImageItem.js +11 -4
- package/lib/components/items/RectangleItem.js +12 -5
- package/lib/components/items/RichTextItem.js +12 -4
- package/lib/components/items/VideoItem.js +29 -22
- package/lib/components/items/VimeoEmbed.js +15 -14
- package/lib/components/items/YoutubeEmbed.js +15 -14
- 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/index.js +4 -4
- package/lib/provider/ArticleRectContext.js +5 -0
- package/lib/provider/CntrlSdkContext.js +13 -0
- 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/{RichTextConverter.js → RichTextConverter/RichTextConverter.js} +3 -3
- package/lib/utils/binSearchInsertAt.js +36 -0
- package/lib/utils/generateTypePresetStyles/generateTypePresetStyles.js +27 -0
- package/package.json +8 -3
- package/src/common/useCurrentLayout.ts +47 -0
- package/src/common/useKeyframeValue.ts +74 -0
- package/src/components/Article.tsx +13 -9
- package/src/components/Item.tsx +31 -21
- package/src/components/Page.tsx +16 -4
- package/src/components/Section.tsx +4 -3
- package/src/components/items/ImageItem.tsx +23 -14
- package/src/components/items/RectangleItem.tsx +21 -12
- package/src/components/items/RichTextItem.tsx +22 -11
- package/src/components/items/VideoItem.tsx +37 -30
- package/src/components/items/VimeoEmbed.tsx +19 -17
- package/src/components/items/YoutubeEmbed.tsx +17 -15
- 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/index.ts +2 -3
- package/src/provider/ArticleRectContext.ts +4 -0
- package/src/provider/CntrlSdkContext.ts +22 -2
- 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/{RichTextConverter.tsx → RichTextConverter/RichTextConverter.tsx} +7 -6
- package/src/utils/binSearchInsertAt.ts +33 -0
- package/src/utils/generateTypePresetStyles/__mock__/layoutsMock.ts +37 -0
- package/src/utils/generateTypePresetStyles/__mock__/presetMock.ts +49 -0
- package/src/utils/generateTypePresetStyles/generateTypePresetStyles.test.ts +49 -0
- package/src/utils/generateTypePresetStyles/generateTypePresetStyles.ts +25 -0
- package/src/utils/getInvertedRanges.ts +43 -0
- package/.idea/workspace.xml +0 -293
- package/cntrl-site-sdk-nextjs-0.2.13.tgz +0 -0
|
@@ -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
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
export { cntrlSdkContext } from './provider/defaultContext';
|
|
2
2
|
|
|
3
3
|
export * from '@cntrl-site/sdk';
|
|
4
4
|
|
|
5
|
-
export { RichTextConverter } from './utils/RichTextConverter';
|
|
5
|
+
export { RichTextConverter } from './utils/RichTextConverter/RichTextConverter';
|
|
6
6
|
export { Page } from './components/Page';
|
|
7
7
|
export { CNTRLHead as Head } from './components/Head';
|
|
8
8
|
export { Article } from './components/Article';
|
|
@@ -19,4 +19,3 @@ export { YoutubeEmbedItem } from './components/items/YoutubeEmbed';
|
|
|
19
19
|
// custom items
|
|
20
20
|
export { CntrlProvider } from './provider/CntrlProvider';
|
|
21
21
|
export type { CustomItemComponent } from './provider/CustomItemTypes';
|
|
22
|
-
export const CustomItems = cntrlSdkContext.customItems;
|
|
@@ -1,7 +1,27 @@
|
|
|
1
1
|
import { CustomItemRegistry } from './CustomItemRegistry';
|
|
2
|
+
import { TLayout, TTypePresets, } from '@cntrl-site/sdk';
|
|
3
|
+
|
|
2
4
|
|
|
3
5
|
export class CntrlSdkContext {
|
|
6
|
+
private _typePresets?: TTypePresets;
|
|
7
|
+
private _layouts: TLayout[] = [];
|
|
4
8
|
constructor(
|
|
5
|
-
public readonly customItems: CustomItemRegistry
|
|
6
|
-
) {
|
|
9
|
+
public readonly customItems: CustomItemRegistry,
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
setTypePresets(typePresets: TTypePresets) {
|
|
13
|
+
this._typePresets = typePresets;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
setLayouts(layouts: TLayout[]) {
|
|
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;
|
|
26
|
+
}
|
|
7
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
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { EventEmitter } from '../EventEmitter';
|
|
2
|
+
|
|
3
|
+
interface EventMap {
|
|
4
|
+
'scroll': undefined;
|
|
5
|
+
'resize': undefined;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class ArticleRectObserver extends EventEmitter<EventMap> {
|
|
9
|
+
private resizeObserver: ResizeObserver;
|
|
10
|
+
private articleWidth: number = 0;
|
|
11
|
+
private scrollPos: number = window.scrollY;
|
|
12
|
+
private animationFrame: number = NaN;
|
|
13
|
+
|
|
14
|
+
constructor() {
|
|
15
|
+
super();
|
|
16
|
+
this.resizeObserver = new ResizeObserver(this.handleResize);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get scroll(): number {
|
|
20
|
+
return this.scrollPos;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get width(): number {
|
|
24
|
+
return this.articleWidth;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private setScroll(scroll: number) {
|
|
28
|
+
this.scrollPos = scroll;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
start(el: HTMLElement) {
|
|
32
|
+
this.resizeObserver.observe(el);
|
|
33
|
+
const onScroll = () => {
|
|
34
|
+
this.handleScroll(window.scrollY);
|
|
35
|
+
if (!isNaN(this.animationFrame)) return;
|
|
36
|
+
this.animationFrame = window.requestAnimationFrame(() => {
|
|
37
|
+
this.animationFrame = NaN;
|
|
38
|
+
this.emit('scroll', undefined);
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
window.addEventListener('scroll', onScroll);
|
|
42
|
+
return () => {
|
|
43
|
+
this.resizeObserver.unobserve(el);
|
|
44
|
+
window.removeEventListener('scroll', onScroll);
|
|
45
|
+
if (!isNaN(this.animationFrame)) {
|
|
46
|
+
window.cancelAnimationFrame(this.animationFrame);
|
|
47
|
+
this.animationFrame = NaN;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private handleScroll = (scroll: number) => {
|
|
53
|
+
this.setScroll(scroll / this.articleWidth);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
private handleResize: ResizeObserverCallback = (entries: ResizeObserverEntry[]) => {
|
|
57
|
+
const [entry] = entries;
|
|
58
|
+
if (!entry) return;
|
|
59
|
+
const element = entry.target;
|
|
60
|
+
if (!(element instanceof HTMLElement)) return;
|
|
61
|
+
this.notify(element);
|
|
62
|
+
this.emit('resize', undefined);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
private notify(el: HTMLElement) {
|
|
66
|
+
const elBoundary = el.getBoundingClientRect();
|
|
67
|
+
this.articleWidth = elBoundary.width;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { ArticleRectObserver } from './ArticleRectObserver';
|
|
3
|
+
|
|
4
|
+
export const useArticleRectObserver = (el?: HTMLElement | null) => {
|
|
5
|
+
const [articleRectObserver, setArticleRectObserver] = useState<ArticleRectObserver | null>(null);
|
|
6
|
+
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
setArticleRectObserver(new ArticleRectObserver());
|
|
9
|
+
}, []);
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
if (!el || !articleRectObserver) return;
|
|
13
|
+
return articleRectObserver.start(el);
|
|
14
|
+
}, [el, articleRectObserver]);
|
|
15
|
+
|
|
16
|
+
return articleRectObserver;
|
|
17
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export type Listener<P> = (payload: P) => void;
|
|
2
|
+
type Listeners<EventMap> = {
|
|
3
|
+
[E in keyof (EventMap)]?: Listener<EventMap[E]>[];
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export abstract class EventEmitter<EventMap> {
|
|
7
|
+
protected listeners: Listeners<EventMap> = {};
|
|
8
|
+
|
|
9
|
+
on<E extends (keyof EventMap)>(
|
|
10
|
+
event: E,
|
|
11
|
+
listener: Listener<EventMap[E]>
|
|
12
|
+
): () => void {
|
|
13
|
+
if (!Array.isArray(this.listeners[event])) {
|
|
14
|
+
this.listeners[event] = [];
|
|
15
|
+
}
|
|
16
|
+
this.listeners[event]?.push(listener);
|
|
17
|
+
return () => {
|
|
18
|
+
this.off(event, listener);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
off<E extends (keyof EventMap)>(
|
|
23
|
+
event: E,
|
|
24
|
+
listener: Listener<EventMap[E]>
|
|
25
|
+
): void {
|
|
26
|
+
const filtered = this.listeners[event]?.filter(l => l !== listener);
|
|
27
|
+
this.listeners[event] = filtered && filtered.length > 0 ? filtered : undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected emit<E extends (keyof EventMap)>(event: E, payload: EventMap[E]): void {
|
|
31
|
+
const listeners = this.listeners[event];
|
|
32
|
+
if (!Array.isArray(listeners)) return;
|
|
33
|
+
for (const listener of listeners) {
|
|
34
|
+
try {
|
|
35
|
+
listener(payload);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
console.error(error instanceof Error ? error.stack : error);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -7,12 +7,12 @@ import {
|
|
|
7
7
|
TRichTextItem,
|
|
8
8
|
VerticalAlign
|
|
9
9
|
} from '@cntrl-site/sdk';
|
|
10
|
-
import { LinkWrapper } from '
|
|
10
|
+
import { LinkWrapper } from '../../components/LinkWrapper';
|
|
11
11
|
|
|
12
12
|
interface StyleGroup {
|
|
13
13
|
start: number;
|
|
14
14
|
end: number;
|
|
15
|
-
styles:
|
|
15
|
+
styles: Style[];
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
interface EntitiesGroup {
|
|
@@ -22,7 +22,7 @@ interface EntitiesGroup {
|
|
|
22
22
|
end: number;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
interface
|
|
25
|
+
interface Style {
|
|
26
26
|
name: string;
|
|
27
27
|
value?: string;
|
|
28
28
|
}
|
|
@@ -36,7 +36,8 @@ export const FontStyles: Record<string, Record<string, string>> = {
|
|
|
36
36
|
export class RichTextConverter {
|
|
37
37
|
toHtml(
|
|
38
38
|
richText: TRichTextItem,
|
|
39
|
-
layouts: TLayout[]
|
|
39
|
+
layouts: TLayout[],
|
|
40
|
+
hasPreset: boolean
|
|
40
41
|
): [ReactNode[], string] {
|
|
41
42
|
const { text: rawText, blocks = [] } = richText.commonParams;
|
|
42
43
|
const text = Array.from(rawText); // because of emoji
|
|
@@ -86,7 +87,7 @@ export class RichTextConverter {
|
|
|
86
87
|
.${blockClass} {
|
|
87
88
|
display: ${group.some(g => g.layout === l.id) ? 'block' : 'none'};
|
|
88
89
|
text-align: ${ta};
|
|
89
|
-
line-height: 0;
|
|
90
|
+
${hasPreset ? '' : 'line-height: 0;'}
|
|
90
91
|
white-space: normal;
|
|
91
92
|
overflow-wrap: break-word;
|
|
92
93
|
}
|
|
@@ -215,7 +216,7 @@ export class RichTextConverter {
|
|
|
215
216
|
return entitiesGroups;
|
|
216
217
|
}
|
|
217
218
|
|
|
218
|
-
private static fromDraftToInline(draftStyle:
|
|
219
|
+
private static fromDraftToInline(draftStyle: Style): string {
|
|
219
220
|
const { value, name } = draftStyle;
|
|
220
221
|
const map: Record<string, Record<string, string | undefined>> = {
|
|
221
222
|
'COLOR': { 'color': value },
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
type CompareFn<L, R> = (lhs: L, rhs: R) => number;
|
|
2
|
+
type InsertStrategy<T> = (list: T[], item: T, compare: CompareFn<T, T>) => number;
|
|
3
|
+
|
|
4
|
+
export function binSearchInsertAt<T, K>(list: T[], item: K, compare: CompareFn<K, T>): number {
|
|
5
|
+
switch (list.length) {
|
|
6
|
+
case 0: return 0;
|
|
7
|
+
case 1: return compare(item, list[0]) < 0 ? 0 : 1;
|
|
8
|
+
}
|
|
9
|
+
let start = 0;
|
|
10
|
+
let end = list.length;
|
|
11
|
+
let pivot: number;
|
|
12
|
+
let cmpResult: number;
|
|
13
|
+
while (start < end) {
|
|
14
|
+
pivot = start + Math.floor((end - start) / 2);
|
|
15
|
+
cmpResult = compare(item, list[pivot]);
|
|
16
|
+
if (cmpResult === 0) return pivot + 1;
|
|
17
|
+
if (cmpResult < 0) {
|
|
18
|
+
end = pivot;
|
|
19
|
+
}
|
|
20
|
+
if (cmpResult > 0) {
|
|
21
|
+
start = pivot + 1;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (start >= list.length) return list.length;
|
|
25
|
+
return compare(item, list[start]) < 0 ? start : start + 1;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function createInsert<T>(start: InsertStrategy<T>, compare: CompareFn<T, T>): (list: T[], item: T) => void {
|
|
29
|
+
return (list: T[], item: T): void => {
|
|
30
|
+
const pos = start(list, item, compare);
|
|
31
|
+
list.splice(pos, 0, item);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { TLayout } from '@cntrl-site/sdk';
|
|
2
|
+
|
|
3
|
+
const sampleGrid = {
|
|
4
|
+
columnsAmount: 0,
|
|
5
|
+
beatHeight: 0,
|
|
6
|
+
beatMultiplier: 1,
|
|
7
|
+
maxWidth: 0,
|
|
8
|
+
columnWidth: 0.01,
|
|
9
|
+
gutterWidth: 0.001
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const layoutsMock: TLayout[] = [
|
|
13
|
+
{
|
|
14
|
+
id: 'tablet',
|
|
15
|
+
exemplary: 768,
|
|
16
|
+
startsWith: 768,
|
|
17
|
+
title: 'Tablet',
|
|
18
|
+
icon: 'any',
|
|
19
|
+
grid: sampleGrid
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
id: 'desktop',
|
|
23
|
+
exemplary: 1440,
|
|
24
|
+
startsWith: 1024,
|
|
25
|
+
title: 'Desktop',
|
|
26
|
+
icon: 'any',
|
|
27
|
+
grid: sampleGrid
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
id: 'mobile',
|
|
31
|
+
exemplary: 375,
|
|
32
|
+
startsWith: 0,
|
|
33
|
+
title: 'Mobile',
|
|
34
|
+
icon: 'any',
|
|
35
|
+
grid: sampleGrid
|
|
36
|
+
}
|
|
37
|
+
];
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { AllowedTags, TTypePresets } from '@cntrl-site/sdk';
|
|
2
|
+
|
|
3
|
+
const layoutParams = {
|
|
4
|
+
'desktop': {
|
|
5
|
+
fontSize: 0.011111,
|
|
6
|
+
lineHeight: 0.013888,
|
|
7
|
+
wordSpacing: 0.0006944,
|
|
8
|
+
letterSpacing: 0.0006944,
|
|
9
|
+
color: 'rgba(0, 0, 0, 1)'
|
|
10
|
+
},
|
|
11
|
+
'tablet': {
|
|
12
|
+
fontSize: 0.02604,
|
|
13
|
+
lineHeight: 0.03125,
|
|
14
|
+
wordSpacing: 0,
|
|
15
|
+
letterSpacing: 0,
|
|
16
|
+
color: 'rgba(0, 0, 0, 1)'
|
|
17
|
+
},
|
|
18
|
+
'mobile': {
|
|
19
|
+
fontSize: 0.053333,
|
|
20
|
+
lineHeight: 0.064,
|
|
21
|
+
wordSpacing: 0.008,
|
|
22
|
+
letterSpacing: 0.008,
|
|
23
|
+
color: 'rgba(0, 0, 0, 1)'
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const presetMock: TTypePresets = {
|
|
28
|
+
id: 'presetId',
|
|
29
|
+
presets: [
|
|
30
|
+
{
|
|
31
|
+
id: 'heading01',
|
|
32
|
+
fontFamily: 'Aeonik',
|
|
33
|
+
fontWeight: '400',
|
|
34
|
+
fontStyle: '',
|
|
35
|
+
name: 'Heading',
|
|
36
|
+
tag: AllowedTags.h1,
|
|
37
|
+
layoutParams
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
id: 'heading02',
|
|
41
|
+
fontFamily: 'Aeonik',
|
|
42
|
+
fontWeight: '400',
|
|
43
|
+
fontStyle: 'italic',
|
|
44
|
+
name: 'Heading',
|
|
45
|
+
tag: AllowedTags.h1,
|
|
46
|
+
layoutParams
|
|
47
|
+
}
|
|
48
|
+
]
|
|
49
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { generateTypePresetStyles } from './generateTypePresetStyles';
|
|
2
|
+
import { presetMock } from './__mock__/presetMock';
|
|
3
|
+
import { layoutsMock } from './__mock__/layoutsMock';
|
|
4
|
+
|
|
5
|
+
describe('generateTypePresetStyles', () => {
|
|
6
|
+
it('generates blocks of styles', () => {
|
|
7
|
+
const styles = generateTypePresetStyles(presetMock, layoutsMock);
|
|
8
|
+
const expectedMedia = (id: string) =>
|
|
9
|
+
`@media (min-width: 0px) and (max-width: 767px) {
|
|
10
|
+
.cntrl-preset-${id} {
|
|
11
|
+
font-size: 5.3332999999999995vw;
|
|
12
|
+
line-height: 6.4vw;
|
|
13
|
+
letter-spacing: 0.8vw;
|
|
14
|
+
word-spacing: 0.8vw;
|
|
15
|
+
color: rgba(0, 0, 0, 1);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
@media (min-width: 768px) and (max-width: 1023px) {
|
|
19
|
+
.cntrl-preset-${id} {
|
|
20
|
+
font-size: 2.604vw;
|
|
21
|
+
line-height: 3.125vw;
|
|
22
|
+
letter-spacing: 0vw;
|
|
23
|
+
word-spacing: 0vw;
|
|
24
|
+
color: rgba(0, 0, 0, 1);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
@media (min-width: 1024px) {
|
|
28
|
+
.cntrl-preset-${id} {
|
|
29
|
+
font-size: 1.1111vw;
|
|
30
|
+
line-height: 1.3888vw;
|
|
31
|
+
letter-spacing: 0.06944vw;
|
|
32
|
+
word-spacing: 0.06944vw;
|
|
33
|
+
color: rgba(0, 0, 0, 1);
|
|
34
|
+
}
|
|
35
|
+
}`;
|
|
36
|
+
expect(styles).toBe(
|
|
37
|
+
`.cntrl-preset-heading01 {
|
|
38
|
+
font-family: Aeonik;
|
|
39
|
+
font-weight: 400;
|
|
40
|
+
}
|
|
41
|
+
${expectedMedia('heading01')}
|
|
42
|
+
.cntrl-preset-heading02 {
|
|
43
|
+
font-family: Aeonik;
|
|
44
|
+
font-style: italic;
|
|
45
|
+
font-weight: 400;
|
|
46
|
+
}
|
|
47
|
+
${expectedMedia('heading02')}`);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { getLayoutMediaQuery, TLayout, TTypePresets } from '@cntrl-site/sdk';
|
|
2
|
+
|
|
3
|
+
const reEmptyLines = /^\s*\n/gm;
|
|
4
|
+
|
|
5
|
+
export function generateTypePresetStyles(preset: TTypePresets, layouts: TLayout[]): string {
|
|
6
|
+
const sorted = layouts.slice().sort((a, b) => a.startsWith - b.startsWith);
|
|
7
|
+
const stylesStr = (`
|
|
8
|
+
${preset.presets.map(p => (`
|
|
9
|
+
.cntrl-preset-${p.id} {
|
|
10
|
+
font-family: ${p.fontFamily};
|
|
11
|
+
${p.fontStyle.length > 0 ? `font-style: ${p.fontStyle};` : ''}
|
|
12
|
+
font-weight: ${p.fontWeight};
|
|
13
|
+
}
|
|
14
|
+
${sorted.map(l => (`
|
|
15
|
+
${getLayoutMediaQuery(l.id, sorted)} {
|
|
16
|
+
.cntrl-preset-${p.id} {
|
|
17
|
+
font-size: ${p.layoutParams[l.id].fontSize * 100}vw;
|
|
18
|
+
line-height: ${p.layoutParams[l.id].lineHeight * 100}vw;
|
|
19
|
+
letter-spacing: ${p.layoutParams[l.id].letterSpacing * 100}vw;
|
|
20
|
+
word-spacing: ${p.layoutParams[l.id].wordSpacing * 100}vw;
|
|
21
|
+
color: ${p.layoutParams[l.id].color};
|
|
22
|
+
}
|
|
23
|
+
}`)).join('\n')}`)).join('\n')}`);
|
|
24
|
+
return stylesStr.replace(reEmptyLines, '');
|
|
25
|
+
}
|