@cntrl-site/sdk-nextjs 1.8.13 → 1.8.15

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.
Files changed (126) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +4 -4
  3. package/TODO.md +1 -1
  4. package/eslint.config.mjs +35 -35
  5. package/jest.config.js +6 -6
  6. package/lib/common/useItemFXData.js +7 -7
  7. package/lib/components/Article.js +11 -8
  8. package/lib/components/LayoutStyle.js +4 -4
  9. package/lib/components/Section/Section.js +43 -43
  10. package/lib/components/items/CodeEmbedItem/CodeEmbedItem.js +35 -35
  11. package/lib/components/items/ComponentItem/ComponentItem.js +12 -12
  12. package/lib/components/items/CompoundItem/CompoundChild.js +20 -20
  13. package/lib/components/items/CompoundItem/CompoundItem.js +14 -14
  14. package/lib/components/items/CustomItem/CustomItem.js +9 -9
  15. package/lib/components/items/EmbedVideoItem/VimeoEmbed.js +24 -24
  16. package/lib/components/items/EmbedVideoItem/YoutubeEmbed.js +27 -27
  17. package/lib/components/items/FileItem/ImageItem.js +39 -39
  18. package/lib/components/items/FileItem/VideoItem.js +46 -46
  19. package/lib/components/items/GroupItem/GroupItem.js +13 -13
  20. package/lib/components/items/Item.js +30 -30
  21. package/lib/components/items/RectangleItem/RectangleItem.js +21 -21
  22. package/lib/components/items/RichTextItem/RichTextItem.js +22 -22
  23. package/lib/provider/WebGLContextManagerContext.js +6 -0
  24. package/lib/utils/RichTextConverter/RichTextConverter.js +21 -21
  25. package/lib/utils/effects/useImageFx.js +56 -29
  26. package/lib/utils/effects/useVideoFx.js +56 -29
  27. package/package.json +57 -57
  28. package/src/common/useCurrentLayout.ts +52 -52
  29. package/src/common/useExemplary.ts +9 -9
  30. package/src/common/useItemFXData.ts +47 -47
  31. package/src/common/useKeyframeValue.ts +85 -85
  32. package/src/common/useRegisterResize.ts +16 -16
  33. package/src/components/Article.tsx +73 -66
  34. package/src/components/ArticleWrapper.tsx +21 -21
  35. package/src/components/Head.tsx +56 -56
  36. package/src/components/LayoutStyle.tsx +21 -21
  37. package/src/components/Page.tsx +33 -33
  38. package/src/components/ScrollPlaybackVideo.tsx +56 -56
  39. package/src/components/Section/Section.tsx +164 -164
  40. package/src/components/Section/useSectionColor.ts +19 -19
  41. package/src/components/Section/useSectionHeightMap.ts +19 -19
  42. package/src/components/items/CodeEmbedItem/CodeEmbedItem.tsx +137 -137
  43. package/src/components/items/CodeEmbedItem/useCodeEmbedItem.ts +36 -36
  44. package/src/components/items/ComponentItem/ComponentItem.tsx +65 -65
  45. package/src/components/items/ComponentItem/useComponentItem.ts +35 -35
  46. package/src/components/items/CompoundItem/CompoundChild.tsx +150 -150
  47. package/src/components/items/CompoundItem/CompoundItem.tsx +70 -70
  48. package/src/components/items/CompoundItem/useCompoundItem.ts +21 -21
  49. package/src/components/items/CustomItem/CustomItem.tsx +48 -48
  50. package/src/components/items/EmbedVideoItem/VimeoEmbed.tsx +202 -202
  51. package/src/components/items/EmbedVideoItem/YoutubeEmbed.tsx +187 -187
  52. package/src/components/items/EmbedVideoItem/useEmbedVideoItem.ts +46 -46
  53. package/src/components/items/FileItem/ImageItem.tsx +156 -156
  54. package/src/components/items/FileItem/VideoItem.tsx +185 -185
  55. package/src/components/items/FileItem/useFileItem.ts +73 -73
  56. package/src/components/items/GroupItem/GroupItem.tsx +79 -79
  57. package/src/components/items/GroupItem/useGroupItem.ts +34 -34
  58. package/src/components/items/Item.tsx +251 -251
  59. package/src/components/items/LinkWrapper.tsx +39 -39
  60. package/src/components/items/RectangleItem/RectangleItem.tsx +95 -95
  61. package/src/components/items/RectangleItem/useRectangleItem.ts +82 -82
  62. package/src/components/items/RichTextItem/RichTextItem.tsx +105 -105
  63. package/src/components/items/RichTextItem/useRichTextItem.ts +12 -12
  64. package/src/components/items/RichTextWrapper.tsx +15 -15
  65. package/src/components/items/itemsMap.ts +28 -28
  66. package/src/components/items/useDraggable.ts +252 -252
  67. package/src/components/items/useItemAngle.ts +13 -13
  68. package/src/components/items/useItemArea.ts +35 -35
  69. package/src/components/items/useItemPointerEvents.ts +23 -23
  70. package/src/components/items/useItemScale.ts +17 -17
  71. package/src/components/items/useItemTriggers.ts +15 -15
  72. package/src/components/items/useRichTextItemValues.ts +65 -65
  73. package/src/components/items/useSizing.ts +25 -25
  74. package/src/components/items/useStickyItemTop.ts +21 -21
  75. package/src/components/useLayoutContext.ts +7 -7
  76. package/src/index.ts +24 -24
  77. package/src/interactions/CSSPropertyNameMap.ts +38 -38
  78. package/src/interactions/InteractionsRegistry.ts +283 -283
  79. package/src/interactions/ItemInteractionCtrl.ts +77 -77
  80. package/src/interactions/getTransition.ts +27 -27
  81. package/src/interactions/types.ts +36 -36
  82. package/src/interactions/useItemInteractionCtrl.ts +18 -18
  83. package/src/provider/ArticleRectContext.ts +4 -4
  84. package/src/provider/CntrlContext.ts +5 -5
  85. package/src/provider/CntrlProvider.tsx +11 -11
  86. package/src/provider/CntrlSdkContext.ts +83 -83
  87. package/src/provider/CustomItemRegistry.ts +14 -14
  88. package/src/provider/CustomItemTypes.ts +3 -3
  89. package/src/provider/CustomSectionRegistry.ts +25 -25
  90. package/src/provider/InteractionsContext.old.tsx +66 -66
  91. package/src/provider/InteractionsContext.test.tsx +97 -97
  92. package/src/provider/InteractionsContext.tsx +28 -28
  93. package/src/provider/Keyframes.ts +11 -11
  94. package/src/provider/KeyframesContext.ts +5 -5
  95. package/src/provider/LayoutContext.ts +3 -3
  96. package/src/provider/WebGLContextManagerContext.tsx +4 -0
  97. package/src/provider/defaultContext.ts +8 -8
  98. package/src/provider/useCntrlContext.ts +8 -8
  99. package/src/styled-jsx.d.ts +1 -1
  100. package/src/utils/Animator/Animator.ts +371 -371
  101. package/src/utils/ArticleRectManager/ArticleRectObserver.ts +93 -93
  102. package/src/utils/ArticleRectManager/useArticleRectObserver.ts +17 -17
  103. package/src/utils/ArticleRectManager/useSectionRegistry.ts +11 -11
  104. package/src/utils/EventEmitter.ts +41 -41
  105. package/src/utils/RichTextConverter/RichTextConverter.tsx +326 -326
  106. package/src/utils/ScaleAnchorMap.ts +13 -13
  107. package/src/utils/StickyManager/StickyManager.ts +23 -23
  108. package/src/utils/Youtube/YouTubeIframeApiLoader.ts +58 -58
  109. package/src/utils/Youtube/YoutubeIframeApi.ts +101 -101
  110. package/src/utils/Youtube/useYouTubeIframeApi.ts +12 -12
  111. package/src/utils/binSearchInsertAt.ts +33 -33
  112. package/src/utils/castObject.ts +10 -10
  113. package/src/utils/effects/useImageFx.ts +156 -113
  114. package/src/utils/effects/useVideoFx.ts +158 -116
  115. package/src/utils/getAnchoredItemTop.ts +12 -12
  116. package/src/utils/getCompoundBoundaryStyles.ts +60 -60
  117. package/src/utils/getFontFamilyValue.ts +3 -3
  118. package/src/utils/getInvertedRanges.ts +43 -43
  119. package/src/utils/getItemTopStyle.ts +14 -14
  120. package/src/utils/getStyleFromItemStateAndParams.ts +8 -8
  121. package/src/utils/getValidYoutubeUrl.ts +29 -29
  122. package/src/utils/isItemType.ts +5 -5
  123. package/src/utils/rangeMap.ts +13 -13
  124. package/src/utils/useElementRect.ts +23 -23
  125. package/cntrl-site-sdk-nextjs-1.8.12.tgz +0 -0
  126. package/cntrl-site-sdk-nextjs-1.8.13.tgz +0 -0
@@ -1,283 +1,283 @@
1
- import { InteractionsRegistryPort, ItemInteractionCtrl } from './types';
2
- import {
3
- Article,
4
- ArticleItemType,
5
- Interaction,
6
- InteractionTrigger,
7
- ItemAny,
8
- } from '@cntrl-site/sdk';
9
- import { isItemType } from '../utils/isItemType';
10
-
11
- export class InteractionsRegistry implements InteractionsRegistryPort {
12
- private ctrls: Map<ItemId, ItemInteractionCtrl> = new Map();
13
- private items: ItemAny[];
14
- private interactions: Interaction[];
15
- private stateItemsIdsMap: StateItemsIdsMap;
16
- private interactionStateMap: InteractionStateMap;
17
- private itemsStages: ItemStages;
18
- private activeStateIdInteractionIdMap: Record<StateId, InteractionId>;
19
-
20
- constructor(article: Article, layoutId: string) {
21
- this.items = this.unpackItems(article);
22
- const interactions = article.interactions[layoutId] ?? [];
23
- const activeStatesIds = interactions.reduce<StateId[]>((map, inter) => {
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
- }
28
- map.push(activeStateId);
29
- return map;
30
- }, []);
31
- const interactionStateMap = interactions.reduce<InteractionStateMap>((map, { id, startStateId }) => {
32
- map[id] = startStateId;
33
- return map;
34
- }, {});
35
- this.activeStateIdInteractionIdMap = interactions.reduce<Record<StateId, InteractionId>>((map, interaction) => {
36
- const activeState = interaction.states.find((state) => state.id !== interaction.startStateId);
37
- if (activeState) {
38
- map[activeState.id] = interaction.id;
39
- }
40
- return map;
41
- }, {});
42
- const itemStages = this.getDefaultItemStages();
43
- const stateItemsIdsMap = activeStatesIds.reduce<StateItemsIdsMap>((map, stateId) => {
44
- map[stateId] = this.items
45
- .filter((item) => {
46
- const state = item.state[stateId] ?? {};
47
- const hasKeys = Object.keys(state).length !== 0;
48
- return hasKeys;
49
- })
50
- .map((item) => item.id);
51
- return map;
52
- }, {});
53
- this.interactions = interactions;
54
- this.itemsStages = itemStages;
55
- this.stateItemsIdsMap = stateItemsIdsMap;
56
- this.interactionStateMap = interactionStateMap;
57
- }
58
-
59
- register(itemId: ItemId, ctrl: ItemInteractionCtrl) {
60
- this.ctrls.set(itemId, ctrl);
61
- }
62
-
63
- getStatePropsForItem(itemId: string) {
64
- const { items } = this;
65
- const item = items.find((item) => item.id === itemId)!;
66
- const itemStages = this.itemsStages.filter((stage) => stage.itemId === itemId);
67
- itemStages.sort((a, b) => a.updated - b.updated);
68
- const itemStyles: StateProps = {};
69
- for (const stage of itemStages) {
70
- if (stage.type === 'active') {
71
- if (stage.isStartState) continue;
72
- const params = item.state[stage.stateId!] ?? {};
73
- for (const [key, stateDetails] of Object.entries(params)) {
74
- itemStyles[key] = {
75
- value: stateDetails.value
76
- };
77
- }
78
- }
79
- if (stage.type === 'transitioning') {
80
- const activeStateId = stage.direction === 'in' ? stage.to : stage.from;
81
- const params = item.state[activeStateId] ?? {};
82
- for (const [key, stateDetails] of Object.entries(params)) {
83
- const transitionDetails = stateDetails[stage.direction];
84
- if (!transitionDetails) continue;
85
- itemStyles[key] = {
86
- value: stage.direction === 'in' ? stateDetails.value : itemStyles[key]?.value,
87
- transition: {
88
- timing: transitionDetails.timing,
89
- duration: transitionDetails.duration,
90
- delay: transitionDetails.delay
91
- }
92
- };
93
- }
94
- }
95
- }
96
- return itemStyles;
97
- }
98
-
99
- getItemAvailableTriggers(itemId: string): Set<InteractionTrigger['type']> {
100
- const available = new Set<InteractionTrigger['type']>();
101
- const activeStates = Object.values(this.interactionStateMap);
102
- for (const interaction of this.interactions) {
103
- const { triggers } = interaction;
104
- for (const trigger of triggers) {
105
- if (trigger.itemId !== itemId) continue;
106
- if (activeStates.includes(trigger.from)) {
107
- available.add(trigger.type);
108
- }
109
- }
110
- }
111
- return available;
112
- }
113
-
114
- notifyTrigger(itemId: string, triggerType: TriggerType): void {
115
- const timestamp = Date.now();
116
- for (const interaction of this.interactions) {
117
- const currentStateId = this.getCurrentStateByInteractionId(interaction.id);
118
- const matchingTrigger = interaction.triggers.find((trigger) =>
119
- trigger.itemId === itemId
120
- && trigger.from === currentStateId
121
- && trigger.type === triggerType
122
- );
123
- if (!matchingTrigger) continue;
124
- const activeStateId = this.getActiveInteractionState(interaction.id);
125
- const isNewStateActive = matchingTrigger.to === activeStateId;
126
- this.setCurrentStateForInteraction(interaction.id, matchingTrigger.to);
127
- const transitioningItems = this.stateItemsIdsMap[activeStateId] ?? [];
128
- const state = interaction.states.find((state) => state.id === matchingTrigger.to);
129
- const actions = state?.actions ?? [];
130
- for (const action of actions) {
131
- const ctrl = this.ctrls.get(action.itemId);
132
- if (!ctrl) continue;
133
- ctrl.receiveAction(action.type);
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
- const itemsToNotify = new Set<ItemId>(transitioningItems);
148
- for (const trigger of interaction.triggers) {
149
- itemsToNotify.add(trigger.itemId);
150
- }
151
- this.notifyItemCtrlsChange(Array.from(itemsToNotify));
152
- this.notifyTransitionStartForItems(transitioningItems, activeStateId);
153
- }
154
- }
155
-
156
- notifyTransitionStartForItems(itemsIds: string[], activeStateId: string) {
157
- for (const itemId of itemsIds) {
158
- const ctrl = this.ctrls.get(itemId);
159
- const item = this.items.find((item) => item.id === itemId)!;
160
- const keys = Object.keys(item.state[activeStateId] ?? {});
161
- ctrl?.handleTransitionStart?.(keys);
162
- }
163
- }
164
-
165
- notifyTransitionEnd(itemId: string): void {
166
- const timestamp = Date.now();
167
- this.itemsStages = this.itemsStages.map((stage) => {
168
- if (stage.itemId !== itemId || stage.type !== 'transitioning') return stage;
169
- return {
170
- itemId,
171
- interactionId: stage.interactionId,
172
- type: 'active',
173
- stateId: stage.to,
174
- isStartState: stage.direction === 'out',
175
- updated: timestamp
176
- };
177
- });
178
- this.ctrls.get(itemId)?.receiveChange();
179
- }
180
-
181
- private getCurrentStateByInteractionId(id: InteractionId): string {
182
- let state;
183
- for (const interactionId of Object.keys(this.interactionStateMap)) {
184
- if (id !== interactionId) continue;
185
- state = this.interactionStateMap[interactionId];
186
- }
187
- if (!state) throw new Error(`Failed to find current state for interaction w/ id="${id}"`);
188
- return state;
189
- }
190
-
191
- private setCurrentStateForInteraction(interactionId: InteractionId, stateId: StateId) {
192
- this.interactionStateMap = {
193
- ...this.interactionStateMap,
194
- [interactionId]: stateId
195
- };
196
- }
197
-
198
- private getActiveInteractionState(interactionId: InteractionId): string {
199
- const { interactions } = this;
200
- const interaction = interactions.find((interaction) => interaction.id === interactionId)!;
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;
206
- }
207
-
208
- private notifyItemCtrlsChange(itemsIds: string[]) {
209
- for (const itemId of itemsIds) {
210
- this.ctrls.get(itemId)?.receiveChange();
211
- }
212
- }
213
-
214
- private unpackItems(article: Article): ItemAny[] {
215
- const itemsArr = [];
216
- for (const section of article.sections) {
217
- const items = this.getNestedItems(section.items);
218
- itemsArr.push(...items);
219
- }
220
- return itemsArr;
221
- }
222
-
223
- private getNestedItems(items: ItemAny[]): ItemAny[] {
224
- const allItems: ItemAny[] = [];
225
- for (const item of items) {
226
- if (isItemType(item, ArticleItemType.Group) || isItemType(item, ArticleItemType.Compound)) {
227
- const groupChildren = item?.items ?? [];
228
- const nestedItems = this.getNestedItems(groupChildren);
229
- allItems.push(...nestedItems);
230
- }
231
- allItems.push(item);
232
- }
233
- return allItems;
234
- }
235
-
236
- private getDefaultItemStages(): ItemStages {
237
- const timestamp = Date.now();
238
- const { items } = this;
239
- const stages: ItemStages = [];
240
- for (const item of items) {
241
- const itemStatesMap = item.state;
242
- if (!itemStatesMap) continue;
243
- for (const stateId of Object.keys(itemStatesMap)) {
244
- const interactionId = this.activeStateIdInteractionIdMap[stateId];
245
- if (!interactionId) continue;
246
- stages.push({
247
- itemId: item.id,
248
- interactionId,
249
- type: 'active',
250
- isStartState: true,
251
- updated: timestamp
252
- });
253
- }
254
- }
255
- return stages;
256
- }
257
- }
258
-
259
- type ItemStages = (TransitioningStage | ActiveStage)[];
260
- type TransitioningStage = {
261
- itemId: string;
262
- interactionId: string;
263
- type: 'transitioning';
264
- from: StateId;
265
- to: StateId;
266
- direction: 'in' | 'out';
267
- updated: number;
268
- };
269
- type ActiveStage = { type: 'active'; itemId: string; interactionId: string; stateId?: string; isStartState: boolean; updated: number; };
270
- type InteractionStateMap = Record<InteractionId, StateId>;
271
- type StateItemsIdsMap = Record<StateId, ItemId[]>;
272
- type TriggerType = InteractionTrigger['type'];
273
- type InteractionId = string;
274
- type StateId = string;
275
- type ItemId = string;
276
- type StateProps = Record<string, {
277
- value?: string | number;
278
- transition?: {
279
- timing: string;
280
- duration: number;
281
- delay: number;
282
- };
283
- }>;
1
+ import { InteractionsRegistryPort, ItemInteractionCtrl } from './types';
2
+ import {
3
+ Article,
4
+ ArticleItemType,
5
+ Interaction,
6
+ InteractionTrigger,
7
+ ItemAny,
8
+ } from '@cntrl-site/sdk';
9
+ import { isItemType } from '../utils/isItemType';
10
+
11
+ export class InteractionsRegistry implements InteractionsRegistryPort {
12
+ private ctrls: Map<ItemId, ItemInteractionCtrl> = new Map();
13
+ private items: ItemAny[];
14
+ private interactions: Interaction[];
15
+ private stateItemsIdsMap: StateItemsIdsMap;
16
+ private interactionStateMap: InteractionStateMap;
17
+ private itemsStages: ItemStages;
18
+ private activeStateIdInteractionIdMap: Record<StateId, InteractionId>;
19
+
20
+ constructor(article: Article, layoutId: string) {
21
+ this.items = this.unpackItems(article);
22
+ const interactions = article.interactions[layoutId] ?? [];
23
+ const activeStatesIds = interactions.reduce<StateId[]>((map, inter) => {
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
+ }
28
+ map.push(activeStateId);
29
+ return map;
30
+ }, []);
31
+ const interactionStateMap = interactions.reduce<InteractionStateMap>((map, { id, startStateId }) => {
32
+ map[id] = startStateId;
33
+ return map;
34
+ }, {});
35
+ this.activeStateIdInteractionIdMap = interactions.reduce<Record<StateId, InteractionId>>((map, interaction) => {
36
+ const activeState = interaction.states.find((state) => state.id !== interaction.startStateId);
37
+ if (activeState) {
38
+ map[activeState.id] = interaction.id;
39
+ }
40
+ return map;
41
+ }, {});
42
+ const itemStages = this.getDefaultItemStages();
43
+ const stateItemsIdsMap = activeStatesIds.reduce<StateItemsIdsMap>((map, stateId) => {
44
+ map[stateId] = this.items
45
+ .filter((item) => {
46
+ const state = item.state[stateId] ?? {};
47
+ const hasKeys = Object.keys(state).length !== 0;
48
+ return hasKeys;
49
+ })
50
+ .map((item) => item.id);
51
+ return map;
52
+ }, {});
53
+ this.interactions = interactions;
54
+ this.itemsStages = itemStages;
55
+ this.stateItemsIdsMap = stateItemsIdsMap;
56
+ this.interactionStateMap = interactionStateMap;
57
+ }
58
+
59
+ register(itemId: ItemId, ctrl: ItemInteractionCtrl) {
60
+ this.ctrls.set(itemId, ctrl);
61
+ }
62
+
63
+ getStatePropsForItem(itemId: string) {
64
+ const { items } = this;
65
+ const item = items.find((item) => item.id === itemId)!;
66
+ const itemStages = this.itemsStages.filter((stage) => stage.itemId === itemId);
67
+ itemStages.sort((a, b) => a.updated - b.updated);
68
+ const itemStyles: StateProps = {};
69
+ for (const stage of itemStages) {
70
+ if (stage.type === 'active') {
71
+ if (stage.isStartState) continue;
72
+ const params = item.state[stage.stateId!] ?? {};
73
+ for (const [key, stateDetails] of Object.entries(params)) {
74
+ itemStyles[key] = {
75
+ value: stateDetails.value
76
+ };
77
+ }
78
+ }
79
+ if (stage.type === 'transitioning') {
80
+ const activeStateId = stage.direction === 'in' ? stage.to : stage.from;
81
+ const params = item.state[activeStateId] ?? {};
82
+ for (const [key, stateDetails] of Object.entries(params)) {
83
+ const transitionDetails = stateDetails[stage.direction];
84
+ if (!transitionDetails) continue;
85
+ itemStyles[key] = {
86
+ value: stage.direction === 'in' ? stateDetails.value : itemStyles[key]?.value,
87
+ transition: {
88
+ timing: transitionDetails.timing,
89
+ duration: transitionDetails.duration,
90
+ delay: transitionDetails.delay
91
+ }
92
+ };
93
+ }
94
+ }
95
+ }
96
+ return itemStyles;
97
+ }
98
+
99
+ getItemAvailableTriggers(itemId: string): Set<InteractionTrigger['type']> {
100
+ const available = new Set<InteractionTrigger['type']>();
101
+ const activeStates = Object.values(this.interactionStateMap);
102
+ for (const interaction of this.interactions) {
103
+ const { triggers } = interaction;
104
+ for (const trigger of triggers) {
105
+ if (trigger.itemId !== itemId) continue;
106
+ if (activeStates.includes(trigger.from)) {
107
+ available.add(trigger.type);
108
+ }
109
+ }
110
+ }
111
+ return available;
112
+ }
113
+
114
+ notifyTrigger(itemId: string, triggerType: TriggerType): void {
115
+ const timestamp = Date.now();
116
+ for (const interaction of this.interactions) {
117
+ const currentStateId = this.getCurrentStateByInteractionId(interaction.id);
118
+ const matchingTrigger = interaction.triggers.find((trigger) =>
119
+ trigger.itemId === itemId
120
+ && trigger.from === currentStateId
121
+ && trigger.type === triggerType
122
+ );
123
+ if (!matchingTrigger) continue;
124
+ const activeStateId = this.getActiveInteractionState(interaction.id);
125
+ const isNewStateActive = matchingTrigger.to === activeStateId;
126
+ this.setCurrentStateForInteraction(interaction.id, matchingTrigger.to);
127
+ const transitioningItems = this.stateItemsIdsMap[activeStateId] ?? [];
128
+ const state = interaction.states.find((state) => state.id === matchingTrigger.to);
129
+ const actions = state?.actions ?? [];
130
+ for (const action of actions) {
131
+ const ctrl = this.ctrls.get(action.itemId);
132
+ if (!ctrl) continue;
133
+ ctrl.receiveAction(action.type);
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
+ const itemsToNotify = new Set<ItemId>(transitioningItems);
148
+ for (const trigger of interaction.triggers) {
149
+ itemsToNotify.add(trigger.itemId);
150
+ }
151
+ this.notifyItemCtrlsChange(Array.from(itemsToNotify));
152
+ this.notifyTransitionStartForItems(transitioningItems, activeStateId);
153
+ }
154
+ }
155
+
156
+ notifyTransitionStartForItems(itemsIds: string[], activeStateId: string) {
157
+ for (const itemId of itemsIds) {
158
+ const ctrl = this.ctrls.get(itemId);
159
+ const item = this.items.find((item) => item.id === itemId)!;
160
+ const keys = Object.keys(item.state[activeStateId] ?? {});
161
+ ctrl?.handleTransitionStart?.(keys);
162
+ }
163
+ }
164
+
165
+ notifyTransitionEnd(itemId: string): void {
166
+ const timestamp = Date.now();
167
+ this.itemsStages = this.itemsStages.map((stage) => {
168
+ if (stage.itemId !== itemId || stage.type !== 'transitioning') return stage;
169
+ return {
170
+ itemId,
171
+ interactionId: stage.interactionId,
172
+ type: 'active',
173
+ stateId: stage.to,
174
+ isStartState: stage.direction === 'out',
175
+ updated: timestamp
176
+ };
177
+ });
178
+ this.ctrls.get(itemId)?.receiveChange();
179
+ }
180
+
181
+ private getCurrentStateByInteractionId(id: InteractionId): string {
182
+ let state;
183
+ for (const interactionId of Object.keys(this.interactionStateMap)) {
184
+ if (id !== interactionId) continue;
185
+ state = this.interactionStateMap[interactionId];
186
+ }
187
+ if (!state) throw new Error(`Failed to find current state for interaction w/ id="${id}"`);
188
+ return state;
189
+ }
190
+
191
+ private setCurrentStateForInteraction(interactionId: InteractionId, stateId: StateId) {
192
+ this.interactionStateMap = {
193
+ ...this.interactionStateMap,
194
+ [interactionId]: stateId
195
+ };
196
+ }
197
+
198
+ private getActiveInteractionState(interactionId: InteractionId): string {
199
+ const { interactions } = this;
200
+ const interaction = interactions.find((interaction) => interaction.id === interactionId)!;
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;
206
+ }
207
+
208
+ private notifyItemCtrlsChange(itemsIds: string[]) {
209
+ for (const itemId of itemsIds) {
210
+ this.ctrls.get(itemId)?.receiveChange();
211
+ }
212
+ }
213
+
214
+ private unpackItems(article: Article): ItemAny[] {
215
+ const itemsArr = [];
216
+ for (const section of article.sections) {
217
+ const items = this.getNestedItems(section.items);
218
+ itemsArr.push(...items);
219
+ }
220
+ return itemsArr;
221
+ }
222
+
223
+ private getNestedItems(items: ItemAny[]): ItemAny[] {
224
+ const allItems: ItemAny[] = [];
225
+ for (const item of items) {
226
+ if (isItemType(item, ArticleItemType.Group) || isItemType(item, ArticleItemType.Compound)) {
227
+ const groupChildren = item?.items ?? [];
228
+ const nestedItems = this.getNestedItems(groupChildren);
229
+ allItems.push(...nestedItems);
230
+ }
231
+ allItems.push(item);
232
+ }
233
+ return allItems;
234
+ }
235
+
236
+ private getDefaultItemStages(): ItemStages {
237
+ const timestamp = Date.now();
238
+ const { items } = this;
239
+ const stages: ItemStages = [];
240
+ for (const item of items) {
241
+ const itemStatesMap = item.state;
242
+ if (!itemStatesMap) continue;
243
+ for (const stateId of Object.keys(itemStatesMap)) {
244
+ const interactionId = this.activeStateIdInteractionIdMap[stateId];
245
+ if (!interactionId) continue;
246
+ stages.push({
247
+ itemId: item.id,
248
+ interactionId,
249
+ type: 'active',
250
+ isStartState: true,
251
+ updated: timestamp
252
+ });
253
+ }
254
+ }
255
+ return stages;
256
+ }
257
+ }
258
+
259
+ type ItemStages = (TransitioningStage | ActiveStage)[];
260
+ type TransitioningStage = {
261
+ itemId: string;
262
+ interactionId: string;
263
+ type: 'transitioning';
264
+ from: StateId;
265
+ to: StateId;
266
+ direction: 'in' | 'out';
267
+ updated: number;
268
+ };
269
+ type ActiveStage = { type: 'active'; itemId: string; interactionId: string; stateId?: string; isStartState: boolean; updated: number; };
270
+ type InteractionStateMap = Record<InteractionId, StateId>;
271
+ type StateItemsIdsMap = Record<StateId, ItemId[]>;
272
+ type TriggerType = InteractionTrigger['type'];
273
+ type InteractionId = string;
274
+ type StateId = string;
275
+ type ItemId = string;
276
+ type StateProps = Record<string, {
277
+ value?: string | number;
278
+ transition?: {
279
+ timing: string;
280
+ duration: number;
281
+ delay: number;
282
+ };
283
+ }>;