@cntrl-site/sdk-nextjs 1.7.0 → 1.8.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.
@@ -0,0 +1,5 @@
1
+ <component name="ProjectCodeStyleConfiguration">
2
+ <state>
3
+ <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
4
+ </state>
5
+ </component>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/sdk-nextjs.iml" filepath="$PROJECT_DIR$/.idea/sdk-nextjs.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="WEB_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$">
5
+ <excludeFolder url="file://$MODULE_DIR$/temp" />
6
+ <excludeFolder url="file://$MODULE_DIR$/.tmp" />
7
+ <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
+ </content>
9
+ <orderEntry type="inheritedJdk" />
10
+ <orderEntry type="sourceFolder" forTests="false" />
11
+ </component>
12
+ </module>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
@@ -4,7 +4,7 @@ exports.useCurrentLayout = void 0;
4
4
  const useCntrlContext_1 = require("../provider/useCntrlContext");
5
5
  const react_1 = require("react");
6
6
  const ArticleRectContext_1 = require("../provider/ArticleRectContext");
7
- const useCurrentLayout = () => {
7
+ function useCurrentLayout() {
8
8
  const { layouts } = (0, useCntrlContext_1.useCntrlContext)();
9
9
  const articleRectObserver = (0, react_1.useContext)(ArticleRectContext_1.ArticleRectContext);
10
10
  const layoutRanges = (0, react_1.useMemo)(() => {
@@ -39,5 +39,5 @@ const useCurrentLayout = () => {
39
39
  });
40
40
  }, [articleRectObserver, getCurrentLayout]);
41
41
  return { layoutId, layoutDeviation: deviation };
42
- };
42
+ }
43
43
  exports.useCurrentLayout = useCurrentLayout;
@@ -66,7 +66,7 @@ function useDraggable(data, dragHandler, preventDragOnChildren = false) {
66
66
  setDragState(state => (Object.assign(Object.assign({}, state), { drag: false, lastX: state.currentX - state.startX + state.lastX, lastY: state.currentY - state.startY + state.lastY })));
67
67
  (_a = untrackMouseMoveRef.current) === null || _a === void 0 ? void 0 : _a.call(untrackMouseMoveRef);
68
68
  }, [setDragState]);
69
- const handleScroll = (0, react_1.useCallback)((event) => {
69
+ const handleScroll = (0, react_1.useCallback)(() => {
70
70
  setDragState(state => state);
71
71
  }, [setDragState]);
72
72
  const trackMouseMove = (0, react_1.useCallback)(() => {
@@ -5,13 +5,16 @@ const sdk_1 = require("@cntrl-site/sdk");
5
5
  const isItemType_1 = require("../utils/isItemType");
6
6
  class InteractionsRegistry {
7
7
  constructor(article, layoutId) {
8
- this.layoutId = layoutId;
8
+ var _a;
9
9
  this.ctrls = new Map();
10
- const { interactions } = article;
11
10
  this.items = this.unpackItems(article);
11
+ const interactions = (_a = article.interactions[layoutId]) !== null && _a !== void 0 ? _a : [];
12
12
  const activeStatesIds = interactions.reduce((map, inter) => {
13
13
  var _a;
14
14
  const activeStateId = (_a = inter.states.find((state) => state.id !== inter.startStateId)) === null || _a === void 0 ? void 0 : _a.id;
15
+ if (!activeStateId) {
16
+ throw new Error(`Failed to find active state for interaction w/ id="${inter.id}"`);
17
+ }
15
18
  map.push(activeStateId);
16
19
  return map;
17
20
  }, []);
@@ -30,9 +33,8 @@ class InteractionsRegistry {
30
33
  const stateItemsIdsMap = activeStatesIds.reduce((map, stateId) => {
31
34
  map[stateId] = this.items
32
35
  .filter((item) => {
33
- var _a, _b;
34
- const layoutStates = (_a = item.state[layoutId]) !== null && _a !== void 0 ? _a : {};
35
- const state = (_b = layoutStates === null || layoutStates === void 0 ? void 0 : layoutStates[stateId]) !== null && _b !== void 0 ? _b : {};
36
+ var _a;
37
+ const state = (_a = item.state[stateId]) !== null && _a !== void 0 ? _a : {};
36
38
  const hasKeys = Object.keys(state).length !== 0;
37
39
  return hasKeys;
38
40
  })
@@ -48,8 +50,8 @@ class InteractionsRegistry {
48
50
  this.ctrls.set(itemId, ctrl);
49
51
  }
50
52
  getStatePropsForItem(itemId) {
51
- var _a, _b, _c, _d, _e;
52
- const { items, layoutId } = this;
53
+ var _a, _b, _c;
54
+ const { items } = this;
53
55
  const item = items.find((item) => item.id === itemId);
54
56
  const itemStages = this.itemsStages.filter((stage) => stage.itemId === itemId);
55
57
  itemStages.sort((a, b) => a.updated - b.updated);
@@ -58,7 +60,7 @@ class InteractionsRegistry {
58
60
  if (stage.type === 'active') {
59
61
  if (stage.isStartState)
60
62
  continue;
61
- const params = (_b = (_a = item.state[layoutId]) === null || _a === void 0 ? void 0 : _a[stage.stateId]) !== null && _b !== void 0 ? _b : {};
63
+ const params = (_a = item.state[stage.stateId]) !== null && _a !== void 0 ? _a : {};
62
64
  for (const [key, stateDetails] of Object.entries(params)) {
63
65
  itemStyles[key] = {
64
66
  value: stateDetails.value
@@ -67,14 +69,17 @@ class InteractionsRegistry {
67
69
  }
68
70
  if (stage.type === 'transitioning') {
69
71
  const activeStateId = stage.direction === 'in' ? stage.to : stage.from;
70
- const params = (_d = (_c = item.state[layoutId]) === null || _c === void 0 ? void 0 : _c[activeStateId]) !== null && _d !== void 0 ? _d : {};
72
+ const params = (_b = item.state[activeStateId]) !== null && _b !== void 0 ? _b : {};
71
73
  for (const [key, stateDetails] of Object.entries(params)) {
74
+ const transitionDetails = stateDetails[stage.direction];
75
+ if (!transitionDetails)
76
+ continue;
72
77
  itemStyles[key] = {
73
- value: stage.direction === 'in' ? stateDetails.value : (_e = itemStyles[key]) === null || _e === void 0 ? void 0 : _e.value,
78
+ value: stage.direction === 'in' ? stateDetails.value : (_c = itemStyles[key]) === null || _c === void 0 ? void 0 : _c.value,
74
79
  transition: {
75
- timing: stateDetails[stage.direction].timing,
76
- duration: stateDetails[stage.direction].duration,
77
- delay: stateDetails[stage.direction].delay
80
+ timing: transitionDetails.timing,
81
+ duration: transitionDetails.duration,
82
+ delay: transitionDetails.delay
78
83
  }
79
84
  };
80
85
  }
@@ -90,7 +95,7 @@ class InteractionsRegistry {
90
95
  for (const trigger of triggers) {
91
96
  if (trigger.itemId !== itemId)
92
97
  continue;
93
- if (activeStates.some((stateId) => trigger.from === stateId)) {
98
+ if (activeStates.includes(trigger.from)) {
94
99
  available.add(trigger.type);
95
100
  }
96
101
  }
@@ -141,12 +146,12 @@ class InteractionsRegistry {
141
146
  }
142
147
  }
143
148
  notifyTransitionStartForItems(itemsIds, activeStateId) {
144
- var _a, _b, _c;
149
+ var _a, _b;
145
150
  for (const itemId of itemsIds) {
146
151
  const ctrl = this.ctrls.get(itemId);
147
152
  const item = this.items.find((item) => item.id === itemId);
148
- const keys = Object.keys((_b = (_a = item.state[this.layoutId]) === null || _a === void 0 ? void 0 : _a[activeStateId]) !== null && _b !== void 0 ? _b : {});
149
- (_c = ctrl === null || ctrl === void 0 ? void 0 : ctrl.handleTransitionStart) === null || _c === void 0 ? void 0 : _c.call(ctrl, keys);
153
+ const keys = Object.keys((_a = item.state[activeStateId]) !== null && _a !== void 0 ? _a : {});
154
+ (_b = ctrl === null || ctrl === void 0 ? void 0 : ctrl.handleTransitionStart) === null || _b === void 0 ? void 0 : _b.call(ctrl, keys);
150
155
  }
151
156
  }
152
157
  notifyTransitionEnd(itemId) {
@@ -156,7 +161,7 @@ class InteractionsRegistry {
156
161
  if (stage.itemId !== itemId || stage.type !== 'transitioning')
157
162
  return stage;
158
163
  return {
159
- itemId: itemId,
164
+ itemId,
160
165
  interactionId: stage.interactionId,
161
166
  type: 'active',
162
167
  stateId: stage.to,
@@ -184,7 +189,11 @@ class InteractionsRegistry {
184
189
  var _a;
185
190
  const { interactions } = this;
186
191
  const interaction = interactions.find((interaction) => interaction.id === interactionId);
187
- return (_a = interaction.states.find(state => state.id !== interaction.startStateId)) === null || _a === void 0 ? void 0 : _a.id;
192
+ const activeStateId = (_a = interaction.states.find(state => state.id !== interaction.startStateId)) === null || _a === void 0 ? void 0 : _a.id;
193
+ if (!activeStateId) {
194
+ throw new Error(`Failed to find active state for interaction w/ id="${interactionId}"`);
195
+ }
196
+ return activeStateId;
188
197
  }
189
198
  notifyItemCtrlsChange(itemsIds) {
190
199
  var _a;
@@ -215,10 +224,10 @@ class InteractionsRegistry {
215
224
  }
216
225
  getDefaultItemStages() {
217
226
  const timestamp = Date.now();
218
- const { items, layoutId } = this;
227
+ const { items } = this;
219
228
  const stages = [];
220
229
  for (const item of items) {
221
- const itemStatesMap = item.state[layoutId];
230
+ const itemStatesMap = item.state;
222
231
  if (!itemStatesMap)
223
232
  continue;
224
233
  for (const stateId of Object.keys(itemStatesMap)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/sdk-nextjs",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "description": "SDK for Next.js",
5
5
  "author": "arsen@momdesign.nyc",
6
6
  "license": "MIT",
@@ -31,7 +31,7 @@
31
31
  "@antfu/eslint-config": "^3.8.0",
32
32
  "@cntrl-site/color": "^1.0.0",
33
33
  "@cntrl-site/effects": "^1.3.2",
34
- "@cntrl-site/sdk": "^1.21.1",
34
+ "@cntrl-site/sdk": "^1.22.0",
35
35
  "@types/vimeo__player": "^2.18.0",
36
36
  "@vimeo/player": "^2.25.0",
37
37
  "html-react-parser": "^3.0.1",
@@ -14,7 +14,7 @@ type UseCurrentLayoutReturn = {
14
14
  layoutDeviation: number;
15
15
  };
16
16
 
17
- export const useCurrentLayout = (): UseCurrentLayoutReturn => {
17
+ export function useCurrentLayout(): UseCurrentLayoutReturn {
18
18
  const { layouts } = useCntrlContext();
19
19
  const articleRectObserver = useContext(ArticleRectContext);
20
20
  const layoutRanges = useMemo(() => {
@@ -49,4 +49,4 @@ export const useCurrentLayout = (): UseCurrentLayoutReturn => {
49
49
  }, [articleRectObserver, getCurrentLayout]);
50
50
 
51
51
  return { layoutId, layoutDeviation: deviation };
52
- };
52
+ }
@@ -96,10 +96,10 @@ export function useDraggable(
96
96
  }));
97
97
  untrackMouseMoveRef.current?.();
98
98
  },
99
- [ setDragState]
99
+ [setDragState]
100
100
  );
101
101
 
102
- const handleScroll = useCallback<EventHandler<Event>>((event) => {
102
+ const handleScroll = useCallback<EventHandler<Event>>(() => {
103
103
  setDragState(state => state);
104
104
  }, [setDragState]);
105
105
 
@@ -150,10 +150,10 @@ export function useDraggable(
150
150
  lastY: state.lastY
151
151
  };
152
152
  }
153
-
153
+
154
154
  const movementX = touch.clientX - state.currentX;
155
155
  const movementY = touch.clientY - state.currentY;
156
-
156
+
157
157
  return {
158
158
  ...state,
159
159
  currentX: state.currentX + movementX,
@@ -198,7 +198,7 @@ export function useDraggable(
198
198
  if (event.target !== data.draggableRef && data.draggableRef.contains(event.target)) return;
199
199
  }
200
200
  document.body.style.overflow = 'hidden';
201
- document.documentElement.style.overflow = 'hidden';
201
+ document.documentElement.style.overflow = 'hidden';
202
202
  const selection = document.getSelection();
203
203
  if (selection) {
204
204
  selection.removeAllRanges();
@@ -17,11 +17,14 @@ export class InteractionsRegistry implements InteractionsRegistryPort {
17
17
  private itemsStages: ItemStages;
18
18
  private activeStateIdInteractionIdMap: Record<StateId, InteractionId>;
19
19
 
20
- constructor(article: Article, private layoutId: string) {
21
- const { interactions } = article;
20
+ constructor(article: Article, layoutId: string) {
22
21
  this.items = this.unpackItems(article);
22
+ const interactions = article.interactions[layoutId] ?? [];
23
23
  const activeStatesIds = interactions.reduce<StateId[]>((map, inter) => {
24
- const activeStateId = inter.states.find((state) => state.id !== inter.startStateId)?.id!;
24
+ const activeStateId = inter.states.find((state) => state.id !== inter.startStateId)?.id;
25
+ if (!activeStateId) {
26
+ throw new Error(`Failed to find active state for interaction w/ id="${inter.id}"`);
27
+ }
25
28
  map.push(activeStateId);
26
29
  return map;
27
30
  }, []);
@@ -40,8 +43,7 @@ export class InteractionsRegistry implements InteractionsRegistryPort {
40
43
  const stateItemsIdsMap = activeStatesIds.reduce<StateItemsIdsMap>((map, stateId) => {
41
44
  map[stateId] = this.items
42
45
  .filter((item) => {
43
- const layoutStates = item.state[layoutId] ?? {};
44
- const state = layoutStates?.[stateId] ?? {};
46
+ const state = item.state[stateId] ?? {};
45
47
  const hasKeys = Object.keys(state).length !== 0;
46
48
  return hasKeys;
47
49
  })
@@ -59,7 +61,7 @@ export class InteractionsRegistry implements InteractionsRegistryPort {
59
61
  }
60
62
 
61
63
  getStatePropsForItem(itemId: string) {
62
- const { items, layoutId } = this;
64
+ const { items } = this;
63
65
  const item = items.find((item) => item.id === itemId)!;
64
66
  const itemStages = this.itemsStages.filter((stage) => stage.itemId === itemId);
65
67
  itemStages.sort((a, b) => a.updated - b.updated);
@@ -67,7 +69,7 @@ export class InteractionsRegistry implements InteractionsRegistryPort {
67
69
  for (const stage of itemStages) {
68
70
  if (stage.type === 'active') {
69
71
  if (stage.isStartState) continue;
70
- const params = item.state[layoutId]?.[stage.stateId!] ?? {};
72
+ const params = item.state[stage.stateId!] ?? {};
71
73
  for (const [key, stateDetails] of Object.entries(params)) {
72
74
  itemStyles[key] = {
73
75
  value: stateDetails.value
@@ -76,14 +78,16 @@ export class InteractionsRegistry implements InteractionsRegistryPort {
76
78
  }
77
79
  if (stage.type === 'transitioning') {
78
80
  const activeStateId = stage.direction === 'in' ? stage.to : stage.from;
79
- const params = item.state[layoutId]?.[activeStateId] ?? {};
81
+ const params = item.state[activeStateId] ?? {};
80
82
  for (const [key, stateDetails] of Object.entries(params)) {
83
+ const transitionDetails = stateDetails[stage.direction];
84
+ if (!transitionDetails) continue;
81
85
  itemStyles[key] = {
82
86
  value: stage.direction === 'in' ? stateDetails.value : itemStyles[key]?.value,
83
87
  transition: {
84
- timing: stateDetails[stage.direction].timing,
85
- duration: stateDetails[stage.direction].duration,
86
- delay: stateDetails[stage.direction].delay
88
+ timing: transitionDetails.timing,
89
+ duration: transitionDetails.duration,
90
+ delay: transitionDetails.delay
87
91
  }
88
92
  };
89
93
  }
@@ -99,7 +103,7 @@ export class InteractionsRegistry implements InteractionsRegistryPort {
99
103
  const { triggers } = interaction;
100
104
  for (const trigger of triggers) {
101
105
  if (trigger.itemId !== itemId) continue;
102
- if (activeStates.some((stateId) => trigger.from === stateId)) {
106
+ if (activeStates.includes(trigger.from)) {
103
107
  available.add(trigger.type);
104
108
  }
105
109
  }
@@ -153,7 +157,7 @@ export class InteractionsRegistry implements InteractionsRegistryPort {
153
157
  for (const itemId of itemsIds) {
154
158
  const ctrl = this.ctrls.get(itemId);
155
159
  const item = this.items.find((item) => item.id === itemId)!;
156
- const keys = Object.keys(item.state[this.layoutId]?.[activeStateId] ?? {});
160
+ const keys = Object.keys(item.state[activeStateId] ?? {});
157
161
  ctrl?.handleTransitionStart?.(keys);
158
162
  }
159
163
  }
@@ -163,7 +167,7 @@ export class InteractionsRegistry implements InteractionsRegistryPort {
163
167
  this.itemsStages = this.itemsStages.map((stage) => {
164
168
  if (stage.itemId !== itemId || stage.type !== 'transitioning') return stage;
165
169
  return {
166
- itemId: itemId,
170
+ itemId,
167
171
  interactionId: stage.interactionId,
168
172
  type: 'active',
169
173
  stateId: stage.to,
@@ -173,7 +177,7 @@ export class InteractionsRegistry implements InteractionsRegistryPort {
173
177
  });
174
178
  this.ctrls.get(itemId)?.receiveChange();
175
179
  }
176
-
180
+
177
181
  private getCurrentStateByInteractionId(id: InteractionId): string {
178
182
  let state;
179
183
  for (const interactionId of Object.keys(this.interactionStateMap)) {
@@ -194,7 +198,11 @@ export class InteractionsRegistry implements InteractionsRegistryPort {
194
198
  private getActiveInteractionState(interactionId: InteractionId): string {
195
199
  const { interactions } = this;
196
200
  const interaction = interactions.find((interaction) => interaction.id === interactionId)!;
197
- return interaction.states.find(state => state.id !== interaction.startStateId)?.id!;
201
+ const activeStateId = interaction.states.find(state => state.id !== interaction.startStateId)?.id;
202
+ if (!activeStateId) {
203
+ throw new Error(`Failed to find active state for interaction w/ id="${interactionId}"`);
204
+ }
205
+ return activeStateId;
198
206
  }
199
207
 
200
208
  private notifyItemCtrlsChange(itemsIds: string[]) {
@@ -227,10 +235,10 @@ export class InteractionsRegistry implements InteractionsRegistryPort {
227
235
 
228
236
  private getDefaultItemStages(): ItemStages {
229
237
  const timestamp = Date.now();
230
- const { items, layoutId } = this;
238
+ const { items } = this;
231
239
  const stages: ItemStages = [];
232
240
  for (const item of items) {
233
- const itemStatesMap = item.state[layoutId];
241
+ const itemStatesMap = item.state;
234
242
  if (!itemStatesMap) continue;
235
243
  for (const stateId of Object.keys(itemStatesMap)) {
236
244
  const interactionId = this.activeStateIdInteractionIdMap[stateId];
@@ -32,9 +32,9 @@ export const InteractionsProvider: FC<PropsWithChildren<Props>> = ({ interaction
32
32
  for (const interaction of interactions) {
33
33
  const activeStateId = interactionsStatesMap[interaction.id];
34
34
  const matchingTrigger = interaction.triggers.find((trigger) =>
35
- trigger.itemId === itemId &&
36
- trigger.from === activeStateId &&
37
- trigger.type === triggerType
35
+ trigger.itemId === itemId
36
+ && trigger.from === activeStateId
37
+ && trigger.type === triggerType
38
38
  );
39
39
  if (matchingTrigger) {
40
40
  return {
@@ -52,7 +52,8 @@ export const InteractionsProvider: FC<PropsWithChildren<Props>> = ({ interaction
52
52
  interactionsStatesMap,
53
53
  interactions,
54
54
  getItemTrigger
55
- }}>
55
+ }}
56
+ >
56
57
  {children}
57
58
  </InteractionsContextOld.Provider>
58
59
  );
Binary file
Binary file