@cntrl-site/sdk-nextjs 0.15.1 → 0.15.2-beta.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.
Binary file
Binary file
@@ -15,6 +15,7 @@ const useCurrentLayout = () => {
15
15
  ...acc,
16
16
  {
17
17
  layoutId: layout.id,
18
+ exemplary: layout.exemplary,
18
19
  start: layout.startsWith,
19
20
  end: next ? next.startsWith : Number.MAX_SAFE_INTEGER
20
21
  }
@@ -22,17 +23,21 @@ const useCurrentLayout = () => {
22
23
  }, []);
23
24
  }, [layouts]);
24
25
  const getCurrentLayout = (0, react_1.useCallback)((articleWidth) => {
25
- return layoutRanges.find(l => articleWidth >= l.start && articleWidth < l.end).layoutId;
26
+ const range = layoutRanges.find(l => articleWidth >= l.start && articleWidth < l.end);
27
+ return range;
26
28
  }, [layoutRanges]);
27
29
  const [layoutId, setLayoutId] = (0, react_1.useState)(undefined);
30
+ const [deviation, setDeviation] = (0, react_1.useState)(1);
28
31
  (0, react_1.useEffect)(() => {
29
32
  if (!articleRectObserver)
30
33
  return;
31
34
  return articleRectObserver.on('resize', () => {
32
35
  const articleWidth = articleRectObserver.width;
33
- setLayoutId(getCurrentLayout(articleWidth));
36
+ const { layoutId, exemplary } = getCurrentLayout(articleWidth);
37
+ setLayoutId(layoutId);
38
+ setDeviation(articleWidth / exemplary);
34
39
  });
35
40
  }, [articleRectObserver, getCurrentLayout]);
36
- return layoutId;
41
+ return { layoutId, layoutDeviation: deviation };
37
42
  };
38
43
  exports.useCurrentLayout = useCurrentLayout;
@@ -12,7 +12,7 @@ const LayoutContext_1 = require("../provider/LayoutContext");
12
12
  const ArticleWrapper = ({ children }) => {
13
13
  const id = (0, react_1.useId)();
14
14
  const [isPageLoaded, setIsPageLoaded] = (0, react_1.useState)(false);
15
- const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
15
+ const { layoutId, layoutDeviation } = (0, useCurrentLayout_1.useCurrentLayout)();
16
16
  (0, react_1.useEffect)(() => {
17
17
  const onPageLoad = () => {
18
18
  setIsPageLoaded(true);
@@ -25,7 +25,11 @@ const ArticleWrapper = ({ children }) => {
25
25
  return () => window.removeEventListener('load', onPageLoad);
26
26
  }
27
27
  }, []);
28
- return ((0, jsx_runtime_1.jsxs)(LayoutContext_1.LayoutContext.Provider, { value: layoutId, children: [(0, jsx_runtime_1.jsx)("div", { className: "article-wrapper", style: { opacity: layoutId && isPageLoaded ? 1 : 0 }, children: children }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
28
+ const layoutDeviationStyle = { '--layout-deviation': layoutDeviation };
29
+ return ((0, jsx_runtime_1.jsxs)(LayoutContext_1.LayoutContext.Provider, { value: layoutId, children: [(0, jsx_runtime_1.jsx)("div", { className: "article-wrapper", style: {
30
+ opacity: layoutId && isPageLoaded ? 1 : 0,
31
+ ...layoutDeviationStyle
32
+ }, children: children }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
29
33
  .article-wrapper {
30
34
  opacity: 1;
31
35
  transition: opacity 0.2s ease;
@@ -40,6 +40,7 @@ const Item = ({ item, sectionId }) => {
40
40
  const id = (0, react_1.useId)();
41
41
  const { layouts } = (0, useCntrlContext_1.useCntrlContext)();
42
42
  const layout = (0, useLayoutContext_1.useLayoutContext)();
43
+ const exemplary = layouts.find(l => l.id === layout)?.exemplary ?? 1;
43
44
  const [wrapperHeight, setWrapperHeight] = (0, react_1.useState)(undefined);
44
45
  const { scale, scaleAnchor } = (0, useItemScale_1.useItemScale)(item, sectionId);
45
46
  const { top, left } = (0, useItemPosition_1.useItemPosition)(item, sectionId);
@@ -72,10 +73,15 @@ const Item = ({ item, sectionId }) => {
72
73
  (0, react_1.useEffect)(() => {
73
74
  isInitialRef.current = false;
74
75
  }, []);
76
+ const isRichText = isItemType(item, sdk_1.ArticleItemType.RichText);
75
77
  const styles = {
76
78
  transform: `scale(${scale})`,
77
79
  transformOrigin: ScaleAnchorMap_1.ScaleAnchorMap[scaleAnchor],
78
- width: `${sizingAxis.x === sdk_1.ArticleItemSizingType.Manual ? `${width * 100}vw` : 'max-content'}`,
80
+ width: `${sizingAxis.x === sdk_1.ArticleItemSizingType.Manual
81
+ ? isRichText
82
+ ? `${width * exemplary}px`
83
+ : `${width * 100}vw`
84
+ : 'max-content'}`,
79
85
  height: `${sizingAxis.y === sdk_1.ArticleItemSizingType.Manual ? `${height * 100}vw` : 'unset'}`,
80
86
  top: stickyTop
81
87
  };
@@ -34,7 +34,10 @@ const RichTextItem = ({ item, sectionId, onResize }) => {
34
34
  return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { ref: setRef, className: `${className} rich-text-wrapper-${item.id}`, style: {
35
35
  transform: `rotate(${angle}deg)`,
36
36
  filter: `blur(${blur * 100}vw)`
37
- }, children: content }), (0, jsx_runtime_1.jsxs)(style_1.default, { id: id, children: [styles, `${(0, sdk_1.getLayoutStyles)(layouts, [item.state.hover], ([hoverParams]) => {
37
+ }, children: (0, jsx_runtime_1.jsx)("div", { className: "rich-text-scale", style: {
38
+ transformOrigin: 'top left',
39
+ transform: 'scale(var(--layout-deviation))'
40
+ }, children: content }) }), (0, jsx_runtime_1.jsxs)(style_1.default, { id: id, children: [styles, `${(0, sdk_1.getLayoutStyles)(layouts, [item.state.hover], ([hoverParams]) => {
38
41
  return (`
39
42
  .rich-text-wrapper-${item.id} {
40
43
  transition: ${(0, HoverStyles_1.getTransitions)(['angle', 'blur'], hoverParams)};
@@ -12,7 +12,7 @@ const useRichTextItem = (item) => {
12
12
  const preset = presetId
13
13
  ? typePresets?.presets.find(p => p.id === presetId) ?? null
14
14
  : null;
15
- const [content, styles] = richTextConv.toHtml(item, layouts, !!preset);
15
+ const [content, styles] = richTextConv.toHtml(item, layouts);
16
16
  return [content, styles, preset];
17
17
  };
18
18
  exports.useRichTextItem = useRichTextItem;
@@ -10,7 +10,7 @@ exports.FontStyles = {
10
10
  'italic': { 'font-style': 'italic' }
11
11
  };
12
12
  class RichTextConverter {
13
- toHtml(richText, layouts, hasPreset) {
13
+ toHtml(richText, layouts) {
14
14
  const { text, blocks = [] } = richText.commonParams;
15
15
  const root = [];
16
16
  const styleRules = layouts.reduce((rec, layout) => {
@@ -32,7 +32,7 @@ class RichTextConverter {
32
32
  const lh = RichTextConverter.fromDraftToInline({
33
33
  name: 'LINEHEIGHT',
34
34
  value: currentLineHeight[l.id]
35
- });
35
+ }, l.exemplary);
36
36
  styleRules[l.id].push(`.rt_${richText.id}_br_${blockIndex} {${lh}}`);
37
37
  });
38
38
  continue;
@@ -60,7 +60,6 @@ class RichTextConverter {
60
60
  text-align: ${ta};
61
61
  white-space: ${whiteSpace};
62
62
  overflow-wrap: break-word;
63
- ${!hasPreset ? 'line-height: 0;' : ''}
64
63
  }
65
64
  `);
66
65
  });
@@ -94,6 +93,7 @@ class RichTextConverter {
94
93
  kids.push(sliceSymbols(content, offset));
95
94
  }
96
95
  for (const item of group) {
96
+ const { exemplary } = layouts.find(l => l.id === item.layout);
97
97
  const entitiesGroups = this.groupEntities(entities, item.styles) ?? [];
98
98
  for (const entitiesGroup of entitiesGroups) {
99
99
  if (!entitiesGroup.stylesGroup)
@@ -106,7 +106,7 @@ class RichTextConverter {
106
106
  }
107
107
  styleRules[item.layout].push(`
108
108
  .${blockClass} .s-${styleGroup.start}-${styleGroup.end} {
109
- ${styleGroup.styles.map(s => RichTextConverter.fromDraftToInline(s)).join('\n')}
109
+ ${styleGroup.styles.map(s => RichTextConverter.fromDraftToInline(s, exemplary)).join('\n')}
110
110
  }
111
111
  `);
112
112
  if (color) {
@@ -194,17 +194,17 @@ class RichTextConverter {
194
194
  }
195
195
  return entitiesGroups;
196
196
  }
197
- static fromDraftToInline(draftStyle) {
197
+ static fromDraftToInline(draftStyle, exemplary) {
198
198
  const { value, name } = draftStyle;
199
199
  const map = {
200
200
  'COLOR': { 'color': getResolvedValue(value, name) },
201
201
  'TYPEFACE': { 'font-family': `${value}` },
202
202
  'FONTSTYLE': value ? { ...exports.FontStyles[value] } : {},
203
203
  'FONTWEIGHT': { 'font-weight': value },
204
- 'FONTSIZE': { 'font-size': `${parseFloat(value) * 100}vw` },
205
- 'LINEHEIGHT': { 'line-height': `${parseFloat(value) * 100}vw` },
206
- 'LETTERSPACING': { 'letter-spacing': `${parseFloat(value) * 100}vw` },
207
- 'WORDSPACING': { 'word-spacing': `${parseFloat(value) * 100}vw` },
204
+ 'FONTSIZE': { 'font-size': `${parseFloat(value) * exemplary}px` },
205
+ 'LINEHEIGHT': { 'line-height': `${parseFloat(value) * exemplary}px` },
206
+ 'LETTERSPACING': { 'letter-spacing': `${parseFloat(value) * exemplary}px` },
207
+ 'WORDSPACING': { 'word-spacing': `${parseFloat(value) * exemplary}px` },
208
208
  'TEXTTRANSFORM': value ? { 'text-transform': value } : { 'text-transform': sdk_1.TextTransform.None },
209
209
  'VERTICALALIGN': value ? { 'vertical-align': value } : { 'vertical-align': sdk_1.VerticalAlign.Unset },
210
210
  'TEXTDECORATION': { 'text-decoration': value }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/sdk-nextjs",
3
- "version": "0.15.1",
3
+ "version": "0.15.2-beta.0",
4
4
  "description": "SDK for Next.js",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/index.ts",
@@ -4,11 +4,17 @@ import { ArticleRectContext } from '../provider/ArticleRectContext';
4
4
 
5
5
  interface LayoutData {
6
6
  layoutId: string;
7
+ exemplary: number;
7
8
  start: number;
8
9
  end: number;
9
10
  }
10
11
 
11
- export const useCurrentLayout = (): string | undefined => {
12
+ type UseCurrentLayoutReturn = {
13
+ layoutId: string | undefined;
14
+ layoutDeviation: number;
15
+ };
16
+
17
+ export const useCurrentLayout = (): UseCurrentLayoutReturn => {
12
18
  const { layouts } = useCntrlContext();
13
19
  const articleRectObserver = useContext(ArticleRectContext);
14
20
  const layoutRanges = useMemo(() => {
@@ -19,6 +25,7 @@ export const useCurrentLayout = (): string | undefined => {
19
25
  ...acc,
20
26
  {
21
27
  layoutId: layout.id,
28
+ exemplary: layout.exemplary,
22
29
  start: layout.startsWith,
23
30
  end: next ? next.startsWith : Number.MAX_SAFE_INTEGER
24
31
  }
@@ -26,16 +33,20 @@ export const useCurrentLayout = (): string | undefined => {
26
33
  }, []);
27
34
  }, [layouts]);
28
35
  const getCurrentLayout = useCallback((articleWidth: number) => {
29
- return layoutRanges.find(l => articleWidth >= l.start && articleWidth < l.end)!.layoutId;
36
+ const range = layoutRanges.find(l => articleWidth >= l.start && articleWidth < l.end);
37
+ return range;
30
38
  }, [layoutRanges]);
31
39
  const [layoutId, setLayoutId] = useState<string | undefined>(undefined);
40
+ const [deviation, setDeviation] = useState(1);
32
41
  useEffect(() => {
33
42
  if (!articleRectObserver) return;
34
43
  return articleRectObserver.on('resize', () => {
35
44
  const articleWidth = articleRectObserver.width;
36
- setLayoutId(getCurrentLayout(articleWidth));
45
+ const { layoutId, exemplary } = getCurrentLayout(articleWidth)!;
46
+ setLayoutId(layoutId);
47
+ setDeviation(articleWidth / exemplary);
37
48
  });
38
49
  }, [articleRectObserver, getCurrentLayout]);
39
50
 
40
- return layoutId;
51
+ return { layoutId, layoutDeviation: deviation };
41
52
  };
@@ -1,4 +1,4 @@
1
- import React, { FC, PropsWithChildren, useEffect, useId, useState } from 'react';
1
+ import React, { CSSProperties, FC, PropsWithChildren, useEffect, useId, useState } from 'react';
2
2
  import JSXStyle from 'styled-jsx/style';
3
3
  import { useCurrentLayout } from '../common/useCurrentLayout';
4
4
  import { LayoutContext } from '../provider/LayoutContext';
@@ -6,7 +6,7 @@ import { LayoutContext } from '../provider/LayoutContext';
6
6
  export const ArticleWrapper: FC<PropsWithChildren<{}>> = ({ children }) => {
7
7
  const id = useId();
8
8
  const [isPageLoaded, setIsPageLoaded] = useState(false);
9
- const layoutId = useCurrentLayout();
9
+ const { layoutId, layoutDeviation } = useCurrentLayout();
10
10
 
11
11
  useEffect(() => {
12
12
  const onPageLoad = () => {
@@ -19,10 +19,17 @@ export const ArticleWrapper: FC<PropsWithChildren<{}>> = ({ children }) => {
19
19
  return () => window.removeEventListener('load', onPageLoad);
20
20
  }
21
21
  }, []);
22
+ const layoutDeviationStyle = {'--layout-deviation': layoutDeviation} as CSSProperties;
22
23
 
23
24
  return (
24
25
  <LayoutContext.Provider value={layoutId}>
25
- <div className="article-wrapper" style={{ opacity: layoutId && isPageLoaded ? 1 : 0 }}>
26
+ <div
27
+ className="article-wrapper"
28
+ style={{
29
+ opacity: layoutId && isPageLoaded ? 1 : 0,
30
+ ...layoutDeviationStyle
31
+ }}
32
+ >
26
33
  {children}
27
34
  </div>
28
35
  <JSXStyle id={id}>{`
@@ -51,6 +51,7 @@ export const Item: FC<ItemProps<TArticleItemAny>> = ({ item, sectionId}) => {
51
51
  const id = useId();
52
52
  const { layouts } = useCntrlContext();
53
53
  const layout = useLayoutContext();
54
+ const exemplary = layouts.find(l => l.id === layout)?.exemplary ?? 1;
54
55
  const [wrapperHeight, setWrapperHeight] = useState<undefined | number>(undefined);
55
56
  const { scale, scaleAnchor } = useItemScale(item, sectionId);
56
57
  const { top, left } = useItemPosition(item, sectionId);
@@ -86,10 +87,16 @@ export const Item: FC<ItemProps<TArticleItemAny>> = ({ item, sectionId}) => {
86
87
  isInitialRef.current = false;
87
88
  }, []);
88
89
 
90
+ const isRichText = isItemType(item, ArticleItemType.RichText);
91
+
89
92
  const styles = {
90
93
  transform: `scale(${scale})`,
91
94
  transformOrigin: ScaleAnchorMap[scaleAnchor],
92
- width: `${sizingAxis.x === SizingType.Manual ? `${width * 100}vw` : 'max-content'}`,
95
+ width: `${sizingAxis.x === SizingType.Manual
96
+ ? isRichText
97
+ ? `${width * exemplary}px`
98
+ : `${width * 100}vw`
99
+ : 'max-content'}`,
93
100
  height: `${sizingAxis.y === SizingType.Manual ? `${height * 100}vw` : 'unset'}`,
94
101
  top: stickyTop
95
102
  };
@@ -37,7 +37,12 @@ export const RichTextItem: FC<ItemProps<TRichTextItem>> = ({ item, sectionId, on
37
37
  filter: `blur(${blur * 100}vw)`
38
38
  }}
39
39
  >
40
- {content}
40
+ <div className="rich-text-scale" style={{
41
+ transformOrigin: 'top left',
42
+ transform: 'scale(var(--layout-deviation))'
43
+ }}>
44
+ {content}
45
+ </div>
41
46
  </div>
42
47
  <JSXStyle id={id}>
43
48
  {styles}
@@ -13,6 +13,6 @@ export const useRichTextItem = (item: TRichTextItem): [ReactNode[], string, TTyp
13
13
  const preset = presetId
14
14
  ? typePresets?.presets.find(p => p.id === presetId) ?? null
15
15
  : null;
16
- const [content, styles] = richTextConv.toHtml(item, layouts, !!preset);
16
+ const [content, styles] = richTextConv.toHtml(item, layouts);
17
17
  return [content, styles, preset];
18
18
  };
@@ -38,8 +38,7 @@ export const FontStyles: Record<string, Record<string, string>> = {
38
38
  export class RichTextConverter {
39
39
  toHtml(
40
40
  richText: TRichTextItem,
41
- layouts: TLayout[],
42
- hasPreset: boolean
41
+ layouts: TLayout[]
43
42
  ): [ReactNode[], string] {
44
43
  const { text, blocks = [] } = richText.commonParams;
45
44
  const root: ReactElement[] = [];
@@ -63,7 +62,7 @@ export class RichTextConverter {
63
62
  const lh = RichTextConverter.fromDraftToInline({
64
63
  name: 'LINEHEIGHT',
65
64
  value: currentLineHeight[l.id]
66
- });
65
+ }, l.exemplary);
67
66
  styleRules[l.id].push(`.rt_${richText.id}_br_${blockIndex} {${lh}}`);
68
67
  });
69
68
  continue;
@@ -91,7 +90,6 @@ export class RichTextConverter {
91
90
  text-align: ${ta};
92
91
  white-space: ${whiteSpace};
93
92
  overflow-wrap: break-word;
94
- ${!hasPreset ? 'line-height: 0;' : ''}
95
93
  }
96
94
  `);
97
95
  });
@@ -129,6 +127,7 @@ export class RichTextConverter {
129
127
  kids.push(sliceSymbols(content, offset));
130
128
  }
131
129
  for (const item of group) {
130
+ const { exemplary } = layouts.find(l => l.id === item.layout)!;
132
131
  const entitiesGroups = this.groupEntities(entities, item.styles) ?? [];
133
132
  for (const entitiesGroup of entitiesGroups) {
134
133
  if (!entitiesGroup.stylesGroup) continue;
@@ -140,7 +139,7 @@ export class RichTextConverter {
140
139
  }
141
140
  styleRules[item.layout].push(`
142
141
  .${blockClass} .s-${styleGroup.start}-${styleGroup.end} {
143
- ${styleGroup.styles.map(s => RichTextConverter.fromDraftToInline(s)).join('\n')}
142
+ ${styleGroup.styles.map(s => RichTextConverter.fromDraftToInline(s, exemplary)).join('\n')}
144
143
  }
145
144
  `);
146
145
  if (color) {
@@ -232,17 +231,17 @@ export class RichTextConverter {
232
231
  return entitiesGroups;
233
232
  }
234
233
 
235
- private static fromDraftToInline(draftStyle: Style): string {
234
+ private static fromDraftToInline(draftStyle: Style, exemplary: number): string {
236
235
  const { value, name } = draftStyle;
237
236
  const map: Record<string, Record<string, string | undefined>> = {
238
237
  'COLOR': { 'color': getResolvedValue(value, name) },
239
238
  'TYPEFACE': { 'font-family': `${value}` },
240
239
  'FONTSTYLE': value ? { ...FontStyles[value] } : {},
241
240
  'FONTWEIGHT': { 'font-weight': value },
242
- 'FONTSIZE': { 'font-size': `${parseFloat(value!) * 100}vw` },
243
- 'LINEHEIGHT': { 'line-height': `${parseFloat(value!) * 100}vw` },
244
- 'LETTERSPACING': { 'letter-spacing': `${parseFloat(value!) * 100}vw` },
245
- 'WORDSPACING': { 'word-spacing': `${parseFloat(value!) * 100}vw` },
241
+ 'FONTSIZE': { 'font-size': `${parseFloat(value!) * exemplary}px` },
242
+ 'LINEHEIGHT': { 'line-height': `${parseFloat(value!) * exemplary}px` },
243
+ 'LETTERSPACING': { 'letter-spacing': `${parseFloat(value!) * exemplary}px` },
244
+ 'WORDSPACING': { 'word-spacing': `${parseFloat(value!) * exemplary}px` },
246
245
  'TEXTTRANSFORM': value ? { 'text-transform': value as TextTransform } : { 'text-transform': TextTransform.None },
247
246
  'VERTICALALIGN': value ? { 'vertical-align': value as VerticalAlign } : { 'vertical-align': VerticalAlign.Unset },
248
247
  'TEXTDECORATION': { 'text-decoration': value }