@cntrl-site/sdk-nextjs 1.8.9-alpha.2 → 1.8.9-alpha.4

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
@@ -104,13 +104,61 @@ class InteractionsRegistry {
104
104
  }
105
105
  return available;
106
106
  }
107
+ notifyLoad() {
108
+ var _a, _b;
109
+ const timestamp = Date.now();
110
+ for (const interaction of this.interactions) {
111
+ const currentStateId = this.getCurrentStateByInteractionId(interaction.id);
112
+ const matchingTrigger = interaction.triggers.find(trigger => 'position' in trigger && trigger.position === 0 && trigger.from === currentStateId);
113
+ if (!matchingTrigger)
114
+ continue;
115
+ const activeStateId = this.getActiveInteractionState(interaction.id);
116
+ const isNewStateActive = matchingTrigger.to === activeStateId;
117
+ this.setCurrentStateForInteraction(interaction.id, matchingTrigger.to);
118
+ const transitioningItems = (_a = this.stateItemsIdsMap[activeStateId]) !== null && _a !== void 0 ? _a : [];
119
+ const state = interaction.states.find((state) => state.id === matchingTrigger.to);
120
+ const actions = (_b = state === null || state === void 0 ? void 0 : state.actions) !== null && _b !== void 0 ? _b : [];
121
+ for (const action of actions) {
122
+ const ctrl = this.ctrls.get(action.itemId);
123
+ if (!ctrl)
124
+ continue;
125
+ ctrl.receiveAction(action.type);
126
+ }
127
+ this.itemsStages = this.itemsStages.map((stage) => {
128
+ if (stage.interactionId !== interaction.id)
129
+ return stage;
130
+ return {
131
+ itemId: stage.itemId,
132
+ interactionId: stage.interactionId,
133
+ type: 'transitioning',
134
+ from: stage.type === 'transitioning' ? stage.to : stage.stateId,
135
+ to: matchingTrigger.to,
136
+ direction: isNewStateActive ? 'in' : 'out',
137
+ updated: timestamp
138
+ };
139
+ });
140
+ const itemsToNotify = new Set(transitioningItems);
141
+ for (const trigger of interaction.triggers) {
142
+ if (!('itemId' in trigger))
143
+ continue;
144
+ itemsToNotify.add(trigger.itemId);
145
+ }
146
+ this.notifyItemCtrlsChange(Array.from(itemsToNotify));
147
+ this.notifyTransitionStartForItems(transitioningItems, activeStateId);
148
+ }
149
+ }
107
150
  notifyScrollTrigger(position) {
108
151
  var _a, _b;
109
152
  const timestamp = Date.now();
110
153
  for (const interaction of this.interactions) {
111
154
  const currentStateId = this.getCurrentStateByInteractionId(interaction.id);
112
- const matchingTrigger = interaction.triggers.find((trigger) => 'position' in trigger && (trigger.position < position ? trigger.from : trigger.to) === currentStateId // refactor
113
- );
155
+ const matchingTrigger = interaction.triggers.find((trigger) => {
156
+ if (!('position' in trigger) || trigger.position === 0)
157
+ return false;
158
+ const isScrollingDown = trigger.position < position;
159
+ const relevantState = isScrollingDown ? trigger.from : trigger.to;
160
+ return relevantState === currentStateId;
161
+ });
114
162
  if (!matchingTrigger || !('position' in matchingTrigger))
115
163
  continue;
116
164
  const activeStateId = this.getActiveInteractionState(interaction.id);
@@ -19,11 +19,21 @@ const InteractionsProvider = ({ article, children }) => {
19
19
  if (!registry || !articleRectObserver)
20
20
  return;
21
21
  const handleScroll = () => {
22
- registry.notifyScrollTrigger(window.scrollY);
22
+ const scrollY = window.scrollY;
23
+ registry.notifyScrollTrigger(scrollY);
23
24
  };
24
- articleRectObserver.on('scroll', handleScroll);
25
- return () => articleRectObserver.off('scroll', handleScroll);
25
+ return articleRectObserver.on('scroll', handleScroll);
26
26
  }, [registry, articleRectObserver]);
27
+ (0, react_1.useEffect)(() => {
28
+ if (!registry)
29
+ return;
30
+ registry.notifyLoad();
31
+ const handleLoad = () => {
32
+ registry.notifyLoad();
33
+ };
34
+ window.addEventListener('load', handleLoad);
35
+ return () => window.removeEventListener('load', handleLoad);
36
+ }, [registry]);
27
37
  return ((0, jsx_runtime_1.jsx)(exports.InteractionsContext.Provider, { value: registry, children: children }));
28
38
  };
29
39
  exports.InteractionsProvider = InteractionsProvider;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/sdk-nextjs",
3
- "version": "1.8.9-alpha.2",
3
+ "version": "1.8.9-alpha.4",
4
4
  "description": "SDK for Next.js",
5
5
  "author": "arsen@momdesign.nyc",
6
6
  "license": "MIT",
@@ -24,7 +24,7 @@ export const ScrollPlaybackVideo: FC<Props> = ({ sectionId, src, playbackParams,
24
24
  const scrollPos = articleRectObserver.getSectionScroll(sectionId);
25
25
  const time = rangeMap(scrollPos, playbackParams.from, playbackParams.to, 0, 1, true);
26
26
  setTime(toFixed(time));
27
- })
27
+ });
28
28
  }, [playbackParams?.from, playbackParams?.to, time]);
29
29
 
30
30
  const scrollVideoManager = useMemo<ScrollPlaybackVideoManager | null>(() => {
@@ -112,13 +112,59 @@ export class InteractionsRegistry implements InteractionsRegistryPort {
112
112
  return available;
113
113
  }
114
114
 
115
- notifyScrollTrigger(position: number) {
115
+ notifyLoad() {
116
116
  const timestamp = Date.now();
117
117
  for (const interaction of this.interactions) {
118
118
  const currentStateId = this.getCurrentStateByInteractionId(interaction.id);
119
- const matchingTrigger = interaction.triggers.find((trigger) =>
120
- 'position' in trigger && (trigger.position < position ? trigger.from : trigger.to) === currentStateId // refactor
119
+ const matchingTrigger = interaction.triggers.find(trigger =>
120
+ 'position' in trigger && trigger.position === 0 && trigger.from === currentStateId
121
121
  );
122
+ if (!matchingTrigger) continue;
123
+ const activeStateId = this.getActiveInteractionState(interaction.id);
124
+ const isNewStateActive = matchingTrigger.to === activeStateId;
125
+ this.setCurrentStateForInteraction(interaction.id, matchingTrigger.to);
126
+ const transitioningItems = this.stateItemsIdsMap[activeStateId] ?? [];
127
+ const state = interaction.states.find((state) => state.id === matchingTrigger.to);
128
+ const actions = state?.actions ?? [];
129
+ for (const action of actions) {
130
+ const ctrl = this.ctrls.get(action.itemId);
131
+ if (!ctrl) continue;
132
+ ctrl.receiveAction(action.type);
133
+ }
134
+
135
+ this.itemsStages = this.itemsStages.map((stage) => {
136
+ if (stage.interactionId !== interaction.id) return stage;
137
+ return {
138
+ itemId: stage.itemId,
139
+ interactionId: stage.interactionId,
140
+ type: 'transitioning',
141
+ from: stage.type === 'transitioning' ? stage.to : stage.stateId!,
142
+ to: matchingTrigger.to,
143
+ direction: isNewStateActive ? 'in' : 'out',
144
+ updated: timestamp
145
+ };
146
+ });
147
+
148
+ const itemsToNotify = new Set<ItemId>(transitioningItems);
149
+ for (const trigger of interaction.triggers) {
150
+ if (!('itemId' in trigger)) continue;
151
+ itemsToNotify.add(trigger.itemId);
152
+ }
153
+ this.notifyItemCtrlsChange(Array.from(itemsToNotify));
154
+ this.notifyTransitionStartForItems(transitioningItems, activeStateId);
155
+ }
156
+ }
157
+
158
+ notifyScrollTrigger(position: number) {
159
+ const timestamp = Date.now();
160
+ for (const interaction of this.interactions) {
161
+ const currentStateId = this.getCurrentStateByInteractionId(interaction.id);
162
+ const matchingTrigger = interaction.triggers.find((trigger) => {
163
+ if (!('position' in trigger) || trigger.position === 0) return false;
164
+ const isScrollingDown = trigger.position < position;
165
+ const relevantState = isScrollingDown ? trigger.from : trigger.to;
166
+ return relevantState === currentStateId;
167
+ });
122
168
  if (!matchingTrigger || !('position' in matchingTrigger)) continue;
123
169
  const activeStateId = this.getActiveInteractionState(interaction.id);
124
170
  const targetStateId = matchingTrigger.position > position ? matchingTrigger.from : matchingTrigger.to;
@@ -21,12 +21,23 @@ export const InteractionsProvider: FC<PropsWithChildren<Props>> = ({ article, ch
21
21
  useEffect(() => {
22
22
  if (!registry || !articleRectObserver) return;
23
23
  const handleScroll = () => {
24
- registry.notifyScrollTrigger(window.scrollY);
24
+ const scrollY = window.scrollY;
25
+ registry.notifyScrollTrigger(scrollY);
25
26
  };
26
- articleRectObserver.on('scroll', handleScroll);
27
- return () => articleRectObserver.off('scroll', handleScroll);
27
+ return articleRectObserver.on('scroll', handleScroll);
28
28
  }, [registry, articleRectObserver]);
29
29
 
30
+ useEffect(() => {
31
+ if (!registry) return;
32
+ registry.notifyLoad();
33
+
34
+ const handleLoad = () => {
35
+ registry.notifyLoad();
36
+ };
37
+ window.addEventListener('load', handleLoad);
38
+ return () => window.removeEventListener('load', handleLoad);
39
+ }, [registry]);
40
+
30
41
  return (
31
42
  <InteractionsContext.Provider value={registry}>
32
43
  {children}