@cntrl-site/sdk-nextjs 1.8.9-alpha.1 → 1.8.9-alpha.12

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.
@@ -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);
@@ -5,23 +5,34 @@ const jsx_runtime_1 = require("react/jsx-runtime");
5
5
  const react_1 = require("react");
6
6
  const InteractionsRegistry_1 = require("../interactions/InteractionsRegistry");
7
7
  const useCurrentLayout_1 = require("../common/useCurrentLayout");
8
+ const ArticleRectContext_1 = require("./ArticleRectContext");
8
9
  exports.InteractionsContext = (0, react_1.createContext)(undefined);
9
10
  const InteractionsProvider = ({ article, children }) => {
10
11
  const { layoutId } = (0, useCurrentLayout_1.useCurrentLayout)();
12
+ const articleRectObserver = (0, react_1.useContext)(ArticleRectContext_1.ArticleRectContext);
11
13
  const registry = (0, react_1.useMemo)(() => {
12
14
  if (!layoutId)
13
15
  return;
14
16
  return new InteractionsRegistry_1.InteractionsRegistry(article, layoutId);
15
17
  }, [layoutId]);
16
18
  (0, react_1.useEffect)(() => {
17
- if (!registry)
19
+ if (!registry || !articleRectObserver)
18
20
  return;
19
21
  const handleScroll = () => {
20
22
  const scrollY = window.scrollY;
21
23
  registry.notifyScrollTrigger(scrollY);
22
24
  };
23
- window.addEventListener('scroll', handleScroll, { passive: true });
24
- return () => window.removeEventListener('scroll', handleScroll);
25
+ return articleRectObserver.on('scroll', handleScroll);
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);
25
36
  }, [registry]);
26
37
  return ((0, jsx_runtime_1.jsx)(exports.InteractionsContext.Provider, { value: registry, children: children }));
27
38
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/sdk-nextjs",
3
- "version": "1.8.9-alpha.1",
3
+ "version": "1.8.9-alpha.12",
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.22.9-alpha.0",
34
+ "@cntrl-site/sdk": "^1.22.9-alpha.1",
35
35
  "@types/vimeo__player": "^2.18.0",
36
36
  "@vimeo/player": "^2.25.0",
37
37
  "html-react-parser": "^3.0.1",
@@ -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;
@@ -2,6 +2,7 @@ import { createContext, FC, PropsWithChildren, useContext, useEffect, useMemo }
2
2
  import { InteractionsRegistry } from '../interactions/InteractionsRegistry';
3
3
  import { Article } from '@cntrl-site/sdk';
4
4
  import { useCurrentLayout } from '../common/useCurrentLayout';
5
+ import { ArticleRectContext } from './ArticleRectContext';
5
6
 
6
7
  export const InteractionsContext = createContext<InteractionsRegistry | undefined>(undefined);
7
8
 
@@ -11,19 +12,30 @@ interface Props {
11
12
 
12
13
  export const InteractionsProvider: FC<PropsWithChildren<Props>> = ({ article, children }) => {
13
14
  const { layoutId } = useCurrentLayout();
15
+ const articleRectObserver = useContext(ArticleRectContext);
14
16
  const registry = useMemo(() => {
15
17
  if (!layoutId) return;
16
18
  return new InteractionsRegistry(article, layoutId);
17
19
  }, [layoutId]);
18
20
 
19
21
  useEffect(() => {
20
- if (!registry) return;
22
+ if (!registry || !articleRectObserver) return;
21
23
  const handleScroll = () => {
22
24
  const scrollY = window.scrollY;
23
25
  registry.notifyScrollTrigger(scrollY);
24
26
  };
25
- window.addEventListener('scroll', handleScroll, { passive: true });
26
- return () => window.removeEventListener('scroll', handleScroll);
27
+ return articleRectObserver.on('scroll', handleScroll);
28
+ }, [registry, articleRectObserver]);
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);
27
39
  }, [registry]);
28
40
 
29
41
  return (
@@ -1,5 +0,0 @@
1
- <component name="ProjectCodeStyleConfiguration">
2
- <state>
3
- <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
4
- </state>
5
- </component>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
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>
@@ -1,12 +0,0 @@
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 DELETED
@@ -1,6 +0,0 @@
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>