@cntrl-site/sdk-nextjs 0.17.0 → 0.17.2

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.
@@ -12,7 +12,7 @@ const useRegisterResize = (ref, onResize) => {
12
12
  return;
13
13
  const observer = new resize_observer_polyfill_1.default((entries) => {
14
14
  const [entry] = entries;
15
- onResize(entry.target.getBoundingClientRect().height / window.innerWidth);
15
+ onResize(entry.target.clientHeight / window.innerWidth);
16
16
  });
17
17
  observer.observe(ref);
18
18
  return () => {
@@ -71,8 +71,6 @@ const Item = ({ item, sectionId, articleHeight }) => {
71
71
  const ItemComponent = itemsMap[item.type] || noop;
72
72
  const sectionTop = rectObserver ? rectObserver.getSectionTop(sectionId) : 0;
73
73
  const handleItemResize = (height) => {
74
- var _a, _b;
75
- const itemSectionTop = (_b = (_a = itemWrapperRef.current) === null || _a === void 0 ? void 0 : _a.offsetTop) !== null && _b !== void 0 ? _b : 0;
76
74
  if (!layout)
77
75
  return;
78
76
  const sticky = item.sticky[layout];
@@ -80,7 +78,7 @@ const Item = ({ item, sectionId, articleHeight }) => {
80
78
  setWrapperHeight(undefined);
81
79
  return;
82
80
  }
83
- const itemArticleOffset = (sectionTop + itemSectionTop) / window.innerWidth;
81
+ const itemArticleOffset = sectionTop / window.innerWidth + stickyTop;
84
82
  const maxStickyTo = articleHeight - itemArticleOffset - height;
85
83
  const end = sticky.to
86
84
  ? Math.min(maxStickyTo, sticky.to)
@@ -93,8 +91,10 @@ const Item = ({ item, sectionId, articleHeight }) => {
93
91
  isInitialRef.current = false;
94
92
  }, []);
95
93
  const isRichText = isItemType(item, sdk_1.ArticleItemType.RichText);
94
+ if (!layout)
95
+ return null;
96
96
  const styles = {
97
- top: stickyTop,
97
+ top: `${(0, getAnchoredItemTop_1.getAnchoredItemTop)(stickyTop, sectionHeight[layout], item.area[layout].anchorSide)}`,
98
98
  height: isRichText && itemHeight ? `${itemHeight * 100}vw` : 'unset'
99
99
  };
100
100
  return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ className: `item-wrapper-${item.id}`, ref: itemWrapperRef, style: isInitialRef.current ? {} : Object.assign({ top, left }, (wrapperHeight !== undefined ? { height: `${wrapperHeight * 100}vw` } : {})) }, { children: [(0, jsx_runtime_1.jsx)("div", Object.assign({ suppressHydrationWarning: true, className: `item-${item.id}`, style: isInitialRef.current ? {} : styles }, { children: (0, jsx_runtime_1.jsx)(RichTextWrapper, Object.assign({ isRichText: isRichText }, { children: (0, jsx_runtime_1.jsx)("div", Object.assign({ className: `item-${item.id}-inner`, style: {
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useStickyItemTop = void 0;
4
- const sdk_1 = require("@cntrl-site/sdk");
5
4
  const useKeyframeValue_1 = require("../../common/useKeyframeValue");
6
- const getAnchoredItemTop_1 = require("../../utils/getAnchoredItemTop");
7
5
  const useLayoutContext_1 = require("../useLayoutContext");
8
6
  function useStickyItemTop(item, sectionHeightMap, sectionId) {
9
7
  const layoutId = (0, useLayoutContext_1.useLayoutContext)();
@@ -13,8 +11,6 @@ function useStickyItemTop(item, sectionHeightMap, sectionId) {
13
11
  return item.area[layoutId];
14
12
  }, (animator, scroll, value) => animator.getPositions(value, scroll), sectionId, [layoutId]);
15
13
  const sticky = layoutId ? item.sticky[layoutId] : undefined;
16
- const sectionHeight = layoutId ? sectionHeightMap[layoutId] : '0vh';
17
- const anchorSide = layoutId ? item.area[layoutId].anchorSide : sdk_1.AnchorSide.Top;
18
- return sticky ? `${(0, getAnchoredItemTop_1.getAnchoredItemTop)(top - sticky.from, sectionHeight, anchorSide)}` : 0;
14
+ return sticky ? top - sticky.from : 0;
19
15
  }
20
16
  exports.useStickyItemTop = useStickyItemTop;
@@ -169,8 +169,24 @@ class RichTextConverter {
169
169
  }
170
170
  groupEntities(entities, styleGroups) {
171
171
  const entitiesGroups = [];
172
- if (!styleGroups || styleGroups.length === 0)
172
+ if (!entities.length && !styleGroups)
173
173
  return;
174
+ if (!styleGroups || styleGroups.length === 0) {
175
+ const dividersSet = entities.reduce((ds, s) => {
176
+ ds.add(s.start);
177
+ ds.add(s.end);
178
+ return ds;
179
+ }, new Set([entities[0].start, entities[entities.length - 1].end]));
180
+ const dividers = Array.from(dividersSet).sort((a, b) => a - b);
181
+ for (let i = 0; i < dividers.length - 1; i += 1) {
182
+ const start = dividers[i];
183
+ const end = dividers[i + 1];
184
+ const entity = entities.find(e => e.start === start);
185
+ entitiesGroups.push(Object.assign({ stylesGroup: [], start,
186
+ end }, (entity && { link: entity.data.url })));
187
+ }
188
+ return entitiesGroups;
189
+ }
174
190
  if (entities.length === 0) {
175
191
  entitiesGroups.push({
176
192
  stylesGroup: styleGroups,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/sdk-nextjs",
3
- "version": "0.17.0",
3
+ "version": "0.17.2",
4
4
  "description": "SDK for Next.js",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/index.ts",
@@ -6,7 +6,7 @@ export const useRegisterResize = (ref: HTMLElement | null, onResize?: (height: n
6
6
  if (!ref || !onResize) return;
7
7
  const observer = new ResizeObserver((entries) => {
8
8
  const [entry] = entries;
9
- onResize(entry.target.getBoundingClientRect().height / window.innerWidth);
9
+ onResize(entry.target.clientHeight / window.innerWidth);
10
10
  });
11
11
  observer.observe(ref);
12
12
  return () => {
@@ -5,7 +5,6 @@ import {
5
5
  ArticleItemType,
6
6
  getLayoutStyles,
7
7
  TArticleItem,
8
- TStickyParams,
9
8
  TArticleItemAny,
10
9
  } from '@cntrl-site/sdk';
11
10
  import { RectangleItem } from './items/RectangleItem';
@@ -94,14 +93,13 @@ export const Item: FC<ItemWrapperProps> = ({ item, sectionId, articleHeight }) =
94
93
  const sectionTop = rectObserver ? rectObserver.getSectionTop(sectionId) : 0;
95
94
 
96
95
  const handleItemResize = (height: number) => {
97
- const itemSectionTop = itemWrapperRef.current?.offsetTop ?? 0;
98
96
  if (!layout) return;
99
97
  const sticky = item.sticky[layout];
100
98
  if (!sticky) {
101
99
  setWrapperHeight(undefined);
102
100
  return;
103
101
  }
104
- const itemArticleOffset = (sectionTop + itemSectionTop) / window.innerWidth;
102
+ const itemArticleOffset = sectionTop / window.innerWidth + stickyTop;
105
103
  const maxStickyTo = articleHeight - itemArticleOffset - height;
106
104
  const end = sticky.to
107
105
  ? Math.min(maxStickyTo, sticky.to)
@@ -117,8 +115,10 @@ export const Item: FC<ItemWrapperProps> = ({ item, sectionId, articleHeight }) =
117
115
 
118
116
  const isRichText = isItemType(item, ArticleItemType.RichText);
119
117
 
118
+ if (!layout) return null;
119
+
120
120
  const styles = {
121
- top: stickyTop,
121
+ top: `${getAnchoredItemTop(stickyTop, sectionHeight[layout], item.area[layout].anchorSide)}`,
122
122
  height: isRichText && itemHeight ? `${itemHeight * 100}vw` : 'unset'
123
123
  };
124
124
 
@@ -1,6 +1,5 @@
1
- import { AnchorSide, TArticleItemAny } from '@cntrl-site/sdk';
1
+ import { TArticleItemAny } from '@cntrl-site/sdk';
2
2
  import { useKeyframeValue } from '../../common/useKeyframeValue';
3
- import { getAnchoredItemTop } from '../../utils/getAnchoredItemTop';
4
3
  import { useLayoutContext } from '../useLayoutContext';
5
4
 
6
5
  export function useStickyItemTop(item: TArticleItemAny, sectionHeightMap: Record<string, string>, sectionId: string) {
@@ -16,7 +15,5 @@ export function useStickyItemTop(item: TArticleItemAny, sectionHeightMap: Record
16
15
  [layoutId]
17
16
  );
18
17
  const sticky = layoutId ? item.sticky[layoutId] : undefined;
19
- const sectionHeight = layoutId ? sectionHeightMap[layoutId] : '0vh';
20
- const anchorSide = layoutId ? item.area[layoutId].anchorSide : AnchorSide.Top;
21
- return sticky ? `${getAnchoredItemTop(top - sticky.from, sectionHeight, anchorSide)}` : 0
18
+ return sticky ? top - sticky.from : 0
22
19
  }
@@ -201,7 +201,27 @@ export class RichTextConverter {
201
201
 
202
202
  private groupEntities(entities: RichText.Entity[], styleGroups?: StyleGroup[]): EntitiesGroup[] | undefined {
203
203
  const entitiesGroups: EntitiesGroup[] = [];
204
- if (!styleGroups || styleGroups.length === 0) return;
204
+ if (!entities.length && !styleGroups) return;
205
+ if (!styleGroups || styleGroups.length === 0) {
206
+ const dividersSet = entities.reduce((ds, s) => {
207
+ ds.add(s.start);
208
+ ds.add(s.end);
209
+ return ds;
210
+ }, new Set<number>([entities[0].start, entities[entities.length - 1].end]));
211
+ const dividers = Array.from(dividersSet).sort((a, b) => a - b);
212
+ for (let i = 0; i < dividers.length - 1; i += 1) {
213
+ const start = dividers[i];
214
+ const end = dividers[i + 1];
215
+ const entity = entities.find(e => e.start === start);
216
+ entitiesGroups.push({
217
+ stylesGroup: [],
218
+ start,
219
+ end,
220
+ ...(entity && { link: entity.data.url })
221
+ });
222
+ }
223
+ return entitiesGroups;
224
+ }
205
225
  if (entities.length === 0) {
206
226
  entitiesGroups.push({
207
227
  stylesGroup: styleGroups,
Binary file