@cntrl-site/sdk-nextjs 0.25.0 → 0.26.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,221 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ScrollPlaybackVideoManager = void 0;
7
+ const ua_parser_js_1 = __importDefault(require("ua-parser-js"));
8
+ const VideoDecoder_1 = __importDefault(require("./VideoDecoder"));
9
+ class ScrollPlaybackVideoManager {
10
+ constructor(options) {
11
+ this.currentTime = 0;
12
+ this.targetTime = 0;
13
+ this.canvas = null;
14
+ this.context = null;
15
+ this.frames = [];
16
+ this.frameRate = 0;
17
+ this.transitioning = false;
18
+ this.debug = false;
19
+ this.frameThreshold = 0;
20
+ this.transitionSpeed = 10;
21
+ this.useWebCodecs = true;
22
+ this.resize = () => {
23
+ if (this.debug)
24
+ console.info('ScrollVideo resizing...');
25
+ if (this.canvas) {
26
+ this.setCoverStyle(this.canvas);
27
+ }
28
+ else if (this.video) {
29
+ this.setCoverStyle(this.video);
30
+ }
31
+ this.paintCanvasFrame(Math.floor(this.currentTime * this.frameRate));
32
+ };
33
+ this.decodeVideo = () => {
34
+ if (!this.video)
35
+ return;
36
+ if (this.useWebCodecs && this.video.src) {
37
+ (0, VideoDecoder_1.default)(this.video.src, (frame) => {
38
+ this.frames.push(frame);
39
+ }, this.debug).then(() => {
40
+ if (!this.video || !this.container)
41
+ return;
42
+ if (this.frames.length === 0) {
43
+ if (this.debug)
44
+ console.error('No frames were received from webCodecs');
45
+ return;
46
+ }
47
+ this.frameRate = this.frames.length / this.video.duration;
48
+ if (this.debug)
49
+ console.info('Received', this.frames.length, 'frames');
50
+ this.canvas = document.createElement('canvas');
51
+ this.context = this.canvas.getContext('2d');
52
+ this.video.style.display = 'none';
53
+ this.container.appendChild(this.canvas);
54
+ this.paintCanvasFrame(Math.floor(this.currentTime * this.frameRate));
55
+ }).catch(() => {
56
+ if (this.debug)
57
+ console.error('Error encountered while decoding video');
58
+ this.frames = [];
59
+ this.video.load();
60
+ });
61
+ }
62
+ };
63
+ this.resizeObserver = new ResizeObserver(() => {
64
+ this.resize();
65
+ });
66
+ const { src, videoContainer } = options;
67
+ if (typeof document !== 'object') {
68
+ console.error('ScrollVideo must be initiated in a DOM context');
69
+ return;
70
+ }
71
+ if (!videoContainer) {
72
+ console.error('scrollVideoContainer must be a valid DOM object');
73
+ return;
74
+ }
75
+ if (!src) {
76
+ console.error('Must provide valid video src to ScrollVideo');
77
+ return;
78
+ }
79
+ this.container = typeof videoContainer === 'string' ? document.getElementById(videoContainer) : videoContainer;
80
+ this.resizeObserver.observe(this.container);
81
+ this.video = document.createElement('video');
82
+ this.video.src = src;
83
+ this.video.preload = 'auto';
84
+ this.video.tabIndex = 0;
85
+ this.video.playsInline = true;
86
+ this.video.muted = true;
87
+ this.video.pause();
88
+ this.video.load();
89
+ this.container.appendChild(this.video);
90
+ const browserEngine = new ua_parser_js_1.default().getEngine();
91
+ this.isSafari = browserEngine.name === 'WebKit';
92
+ if (this.debug && this.isSafari)
93
+ console.info('Safari browser detected');
94
+ this.video.addEventListener('loadedmetadata', () => this.setTargetTimePercent(0, true), { once: true });
95
+ this.video.addEventListener('progress', this.resize);
96
+ this.decodeVideo();
97
+ }
98
+ setCoverStyle(el) {
99
+ if (el && this.container) {
100
+ el.style.position = 'absolute';
101
+ el.style.top = '50%';
102
+ el.style.left = '50%';
103
+ el.style.transform = 'translate(-50%, -50%)';
104
+ const { width: containerWidth, height: containerHeight } = this.container.getBoundingClientRect();
105
+ const width = el.videoWidth || el.width;
106
+ const height = el.videoHeight || el.height;
107
+ if (containerWidth / containerHeight > width / height) {
108
+ el.style.width = '100%';
109
+ el.style.height = 'auto';
110
+ }
111
+ else {
112
+ el.style.height = '100%';
113
+ el.style.width = 'auto';
114
+ }
115
+ }
116
+ }
117
+ paintCanvasFrame(frameNum) {
118
+ if (this.canvas) {
119
+ const frameIdx = Math.min(frameNum, this.frames.length - 1);
120
+ const currFrame = this.frames[frameIdx];
121
+ if (currFrame && this.container) {
122
+ if (this.debug)
123
+ console.info('Painting frame', frameIdx);
124
+ this.canvas.width = currFrame.width;
125
+ this.canvas.height = currFrame.height;
126
+ const { width, height } = this.container.getBoundingClientRect();
127
+ this.resetCanvasDimensions(width, height, currFrame.width, currFrame.height);
128
+ this.context.drawImage(currFrame, 0, 0, currFrame.width, currFrame.height);
129
+ }
130
+ }
131
+ }
132
+ transitionToTargetTime(jump) {
133
+ if (!this.video)
134
+ return;
135
+ if (this.debug)
136
+ console.info('Transitioning targetTime:', this.targetTime, 'currentTime:', this.currentTime);
137
+ if (isNaN(this.targetTime) || Math.abs(this.currentTime - this.targetTime) < this.frameThreshold) {
138
+ this.video.pause();
139
+ this.transitioning = false;
140
+ return;
141
+ }
142
+ // Make sure we don't go out of time bounds
143
+ if (this.targetTime > this.video.duration) {
144
+ this.targetTime = this.video.duration;
145
+ }
146
+ if (this.targetTime < 0) {
147
+ this.targetTime = 0;
148
+ }
149
+ // How far forward we need to transition
150
+ const transitionForward = this.targetTime - this.currentTime;
151
+ if (this.canvas) {
152
+ // Update currentTime and paint the closest frame
153
+ this.currentTime += transitionForward / (256 / this.transitionSpeed);
154
+ // If jump, we go directly to the frame
155
+ if (jump) {
156
+ this.currentTime = this.targetTime;
157
+ }
158
+ this.paintCanvasFrame(Math.floor(this.currentTime * this.frameRate));
159
+ }
160
+ else if (jump || this.isSafari || this.targetTime - this.currentTime < 0) {
161
+ this.video.pause();
162
+ this.currentTime += transitionForward / (64 / this.transitionSpeed);
163
+ // If jump, we go directly to the frame
164
+ if (jump) {
165
+ this.currentTime = this.targetTime;
166
+ }
167
+ this.video.currentTime = this.currentTime;
168
+ }
169
+ else {
170
+ // Otherwise, we play the video and adjust the playbackRate to get a smoother
171
+ // animation effect.
172
+ const playbackRate = Math.max(Math.min(transitionForward * 4, this.transitionSpeed, 16), 1);
173
+ if (this.debug)
174
+ console.info('ScrollVideo playbackRate:', playbackRate);
175
+ if (!isNaN(playbackRate)) {
176
+ this.video.playbackRate = playbackRate;
177
+ this.video.play();
178
+ }
179
+ this.currentTime = this.video.currentTime;
180
+ }
181
+ if (typeof requestAnimationFrame === 'function') {
182
+ requestAnimationFrame(() => this.transitionToTargetTime(jump));
183
+ }
184
+ }
185
+ resetCanvasDimensions(w, h, frameW, frameH) {
186
+ if (!this.canvas)
187
+ return;
188
+ if (w / h > frameW / frameH) {
189
+ this.canvas.style.width = '100%';
190
+ this.canvas.style.height = 'auto';
191
+ }
192
+ else {
193
+ this.canvas.style.height = '100%';
194
+ this.canvas.style.width = 'auto';
195
+ }
196
+ }
197
+ setTargetTimePercent(setPercentage, jump = true) {
198
+ if (!this.video)
199
+ return;
200
+ this.targetTime = Math.max(Math.min(setPercentage, 1), 0)
201
+ * (this.frames.length && this.frameRate ? this.frames.length / this.frameRate : this.video.duration);
202
+ if (!jump && Math.abs(this.currentTime - this.targetTime) < this.frameThreshold)
203
+ return;
204
+ if (!jump && this.transitioning)
205
+ return;
206
+ if (!this.canvas && !this.video.paused)
207
+ this.video.play();
208
+ this.transitioning = true;
209
+ this.transitionToTargetTime(jump);
210
+ }
211
+ destroy() {
212
+ var _a;
213
+ this.resizeObserver.unobserve(this.container);
214
+ (_a = this.video) === null || _a === void 0 ? void 0 : _a.removeEventListener('progress', this.resize);
215
+ if (this.debug)
216
+ console.info('Destroying ScrollVideo');
217
+ if (this.container)
218
+ this.container.innerHTML = '';
219
+ }
220
+ }
221
+ exports.ScrollPlaybackVideoManager = ScrollPlaybackVideoManager;
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.Writer = void 0;
27
+ // @ts-ignore
28
+ const MP4Box = __importStar(require("mp4box"));
29
+ class Writer {
30
+ constructor(size) {
31
+ this.data = new Uint8Array(size);
32
+ this.idx = 0;
33
+ this.size = size;
34
+ }
35
+ getData() {
36
+ if (this.idx !== this.size)
37
+ throw new Error('Mismatch between size reserved and sized used');
38
+ return this.data.slice(0, this.idx);
39
+ }
40
+ writeUint8(value) {
41
+ this.data.set([value], this.idx);
42
+ this.idx += 1;
43
+ }
44
+ writeUint16(value) {
45
+ const arr = new Uint16Array(1);
46
+ arr[0] = value;
47
+ const buffer = new Uint8Array(arr.buffer);
48
+ this.data.set([buffer[1], buffer[0]], this.idx);
49
+ this.idx += 2;
50
+ }
51
+ writeUint8Array(value) {
52
+ this.data.set(value, this.idx);
53
+ this.idx += value.length;
54
+ }
55
+ }
56
+ exports.Writer = Writer;
57
+ const getExtradata = (avccBox) => {
58
+ let i;
59
+ let size = 7;
60
+ for (i = 0; i < avccBox.SPS.length; i += 1) {
61
+ // nalu length is encoded as a uint16.
62
+ size += 2 + avccBox.SPS[i].length;
63
+ }
64
+ for (i = 0; i < avccBox.PPS.length; i += 1) {
65
+ // nalu length is encoded as a uint16.
66
+ size += 2 + avccBox.PPS[i].length;
67
+ }
68
+ const writer = new Writer(size);
69
+ writer.writeUint8(avccBox.configurationVersion);
70
+ writer.writeUint8(avccBox.AVCProfileIndication);
71
+ writer.writeUint8(avccBox.profile_compatibility);
72
+ writer.writeUint8(avccBox.AVCLevelIndication);
73
+ writer.writeUint8(avccBox.lengthSizeMinusOne + (63 << 2));
74
+ writer.writeUint8(avccBox.nb_SPS_nalus + (7 << 5));
75
+ for (i = 0; i < avccBox.SPS.length; i += 1) {
76
+ writer.writeUint16(avccBox.SPS[i].length);
77
+ writer.writeUint8Array(avccBox.SPS[i].nalu);
78
+ }
79
+ writer.writeUint8(avccBox.nb_PPS_nalus);
80
+ for (i = 0; i < avccBox.PPS.length; i += 1) {
81
+ writer.writeUint16(avccBox.PPS[i].length);
82
+ writer.writeUint8Array(avccBox.PPS[i].nalu);
83
+ }
84
+ return writer.getData();
85
+ };
86
+ const decodeVideo = (src, emitFrame, { VideoDecoder, EncodedVideoChunk, debug }) => new Promise((resolve, reject) => {
87
+ if (debug)
88
+ console.info('Decoding video from', src);
89
+ try {
90
+ const mp4boxfile = MP4Box.createFile();
91
+ let codec;
92
+ const decoder = new VideoDecoder({
93
+ output: (frame) => {
94
+ console.log(frame);
95
+ createImageBitmap(frame, { resizeQuality: 'low' }).then((bitmap) => {
96
+ emitFrame(bitmap);
97
+ frame.close();
98
+ if (decoder.decodeQueueSize <= 0) {
99
+ setTimeout(() => {
100
+ if (decoder.state !== 'closed') {
101
+ decoder.close();
102
+ resolve();
103
+ }
104
+ }, 500);
105
+ }
106
+ });
107
+ },
108
+ error: (e) => {
109
+ console.error(e);
110
+ reject(e);
111
+ },
112
+ });
113
+ mp4boxfile.onReady = (info) => {
114
+ if (info && info.videoTracks && info.videoTracks[0]) {
115
+ [{ codec }] = info.videoTracks;
116
+ if (debug)
117
+ console.info('Video with codec:', codec);
118
+ const avccBox = mp4boxfile.moov.traks[0].mdia.minf.stbl.stsd.entries[0].avcC;
119
+ const extradata = getExtradata(avccBox);
120
+ decoder.configure({ codec, description: extradata });
121
+ mp4boxfile.setExtractionOptions(info.videoTracks[0].id);
122
+ mp4boxfile.start();
123
+ }
124
+ else
125
+ reject(new Error('URL provided is not a valid mp4 video file.'));
126
+ };
127
+ mp4boxfile.onSamples = (track_id, ref, samples) => {
128
+ for (let i = 0; i < samples.length; i += 1) {
129
+ const sample = samples[i];
130
+ const type = sample.is_sync ? 'key' : 'delta';
131
+ const chunk = new EncodedVideoChunk({
132
+ type,
133
+ timestamp: sample.cts,
134
+ duration: sample.duration,
135
+ data: sample.data,
136
+ });
137
+ decoder.decode(chunk);
138
+ }
139
+ };
140
+ fetch(src).then((res) => {
141
+ const reader = res.body.getReader();
142
+ let offset = 0;
143
+ //@ts-ignore
144
+ function appendBuffers({ done, value }) {
145
+ if (done) {
146
+ mp4boxfile.flush();
147
+ return null;
148
+ }
149
+ const buf = value.buffer;
150
+ buf.fileStart = offset;
151
+ offset += buf.byteLength;
152
+ mp4boxfile.appendBuffer(buf);
153
+ return reader.read().then(appendBuffers);
154
+ }
155
+ return reader.read().then(appendBuffers);
156
+ });
157
+ }
158
+ catch (e) {
159
+ reject(e);
160
+ }
161
+ });
162
+ exports.default = (src, emitFrame, debug) => {
163
+ if (typeof VideoDecoder === 'function' && typeof EncodedVideoChunk === 'function') {
164
+ if (debug)
165
+ console.info('WebCodecs is natively supported, using native version...');
166
+ return decodeVideo(src, emitFrame, {
167
+ VideoDecoder,
168
+ EncodedVideoChunk,
169
+ debug,
170
+ });
171
+ }
172
+ if (debug)
173
+ console.info('WebCodecs is not available in this browser.');
174
+ return Promise.resolve();
175
+ };
@@ -31,7 +31,7 @@ class RichTextConverter {
31
31
  const content = text.slice(block.start, block.end);
32
32
  const entities = (_a = block.entities.sort((a, b) => a.start - b.start)) !== null && _a !== void 0 ? _a : [];
33
33
  if (content.length === 0) {
34
- root.push((0, jsx_runtime_1.jsx)("div", Object.assign({ className: `rt_${richText.id}_br_${blockIndex}` }, { children: (0, jsx_runtime_1.jsx)("br", {}) })));
34
+ root.push((0, jsx_runtime_1.jsx)("div", { className: `rt_${richText.id}_br_${blockIndex}`, children: (0, jsx_runtime_1.jsx)("br", {}) }));
35
35
  layouts.forEach(l => {
36
36
  const lhForLayout = currentLineHeight[l.id];
37
37
  if (lhForLayout === undefined)
@@ -82,7 +82,7 @@ class RichTextConverter {
82
82
  if (offset < style.start) {
83
83
  entityKids.push(sliceSymbols(content, offset, style.start));
84
84
  }
85
- entityKids.push((0, jsx_runtime_1.jsx)("span", Object.assign({ className: `s-${style.start}-${style.end}` }, { children: sliceSymbols(content, style.start, style.end) }), style.start));
85
+ entityKids.push((0, jsx_runtime_1.jsx)("span", { className: `s-${style.start}-${style.end}`, children: sliceSymbols(content, style.start, style.end) }, style.start));
86
86
  offset = style.end;
87
87
  }
88
88
  if (offset < entity.end) {
@@ -90,7 +90,7 @@ class RichTextConverter {
90
90
  offset = entity.end;
91
91
  }
92
92
  if (entity.link) {
93
- kids.push((0, jsx_runtime_1.jsx)(LinkWrapper_1.LinkWrapper, Object.assign({ url: entity.link, target: entity.target }, { children: entityKids }), entity.start));
93
+ kids.push((0, jsx_runtime_1.jsx)(LinkWrapper_1.LinkWrapper, { url: entity.link, target: entity.target, children: entityKids }, entity.start));
94
94
  continue;
95
95
  }
96
96
  kids.push(...entityKids);
@@ -127,7 +127,7 @@ class RichTextConverter {
127
127
  }
128
128
  }
129
129
  }
130
- root.push((0, jsx_runtime_1.jsx)("div", Object.assign({ className: blockClass }, { children: kids }), blockClass));
130
+ root.push((0, jsx_runtime_1.jsx)("div", { className: blockClass, children: kids }, blockClass));
131
131
  }
132
132
  }
133
133
  const styles = layouts.map(l => `
@@ -9,4 +9,4 @@ var YTPlayerState;
9
9
  YTPlayerState[YTPlayerState["Paused"] = 2] = "Paused";
10
10
  YTPlayerState[YTPlayerState["Buffering"] = 3] = "Buffering";
11
11
  YTPlayerState[YTPlayerState["VideoCued"] = 5] = "VideoCued";
12
- })(YTPlayerState = exports.YTPlayerState || (exports.YTPlayerState = {}));
12
+ })(YTPlayerState || (exports.YTPlayerState = YTPlayerState = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/sdk-nextjs",
3
- "version": "0.25.0",
3
+ "version": "0.26.0",
4
4
  "description": "SDK for Next.js",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/index.ts",
@@ -24,11 +24,14 @@
24
24
  "@cntrl-site/color": "^1.0.0",
25
25
  "@cntrl-site/effects": "^1.0.5",
26
26
  "@cntrl-site/sdk": "^1.8.0",
27
+ "@types/ua-parser-js": "^0.7.39",
27
28
  "@types/vimeo__player": "^2.18.0",
28
29
  "@vimeo/player": "^2.20.1",
29
30
  "html-react-parser": "^3.0.1",
31
+ "mp4box": "^0.5.2",
30
32
  "resize-observer-polyfill": "^1.5.1",
31
- "styled-jsx": "^5.0.2"
33
+ "styled-jsx": "^5.0.2",
34
+ "ua-parser-js": "^1.0.37"
32
35
  },
33
36
  "peerDependencies": {
34
37
  "@types/lodash.isequal": "^4.5.6",
@@ -45,7 +48,7 @@
45
48
  "@types/react": "^18.0.15",
46
49
  "jest": "^29.3.1",
47
50
  "ts-jest": "^29.0.3",
48
- "typescript": "^4.9.5"
51
+ "typescript": "^5.2.2"
49
52
  },
50
53
  "directories": {
51
54
  "lib": "lib"
@@ -0,0 +1,56 @@
1
+ import React, { FC, useContext, useEffect, useMemo, useState } from 'react';
2
+ import { ScrollPlaybackVideoManager } from '../utils/PlaybackVideoConverter/ScrollPlaybackVideoManager';
3
+ import { rangeMap } from '../utils/rangeMap';
4
+ import { ArticleRectContext } from '../provider/ArticleRectContext';
5
+
6
+ type PlaybackParams = { from: number, to: number };
7
+
8
+ interface Props {
9
+ sectionId: string;
10
+ src: string;
11
+ playbackParams: PlaybackParams | null;
12
+ style?: React.CSSProperties;
13
+ className: string;
14
+ }
15
+
16
+ export const ScrollPlaybackVideo: FC<Props> = ({ sectionId, src, playbackParams, style, className}) => {
17
+ const [containerElement, setContainerElement] = useState<HTMLDivElement | null>(null);
18
+ const [time, setTime] = useState(0);
19
+ const articleRectObserver = useContext(ArticleRectContext);
20
+
21
+ useEffect(() => {
22
+ if (!playbackParams || !articleRectObserver) return;
23
+ return articleRectObserver.on('scroll', () => {
24
+ const scrollPos = articleRectObserver.getSectionScroll(sectionId);
25
+ const time = rangeMap(scrollPos, playbackParams.from, playbackParams.to, 0, 1, true);
26
+ setTime(toFixed(time));
27
+ })
28
+ }, [playbackParams?.from, playbackParams?.to, time]);
29
+
30
+ const scrollVideoManager = useMemo<ScrollPlaybackVideoManager | null>(() => {
31
+ if (!containerElement) return null;
32
+ const manager = new ScrollPlaybackVideoManager({
33
+ src,
34
+ videoContainer: containerElement
35
+ });
36
+ return manager;
37
+ }, [containerElement, src]);
38
+
39
+ useEffect(() => {
40
+ return () => {
41
+ scrollVideoManager?.destroy();
42
+ };
43
+ }, [scrollVideoManager]);
44
+
45
+ useEffect(() => {
46
+ if (scrollVideoManager && time >= 0 && time <= 1) {
47
+ scrollVideoManager.setTargetTimePercent(time);
48
+ }
49
+ }, [time, scrollVideoManager]);
50
+
51
+ return <div className={className} style={style} ref={setContainerElement} />;
52
+ };
53
+
54
+ function toFixed(num: number) {
55
+ return Number(num.toFixed(3));
56
+ }
@@ -1,4 +1,4 @@
1
- import { FC, useContext, useEffect, useId, useMemo, useRef, useState } from 'react';
1
+ import { FC, useId, useMemo, useRef, useState } from 'react';
2
2
  import JSXStyle from 'styled-jsx/style';
3
3
  import { CntrlColor } from '@cntrl-site/color';
4
4
  import { ArticleItemType, getLayoutStyles, VideoItem as TVideoItem } from '@cntrl-site/sdk';
@@ -9,13 +9,8 @@ import { useItemAngle } from '../useItemAngle';
9
9
  import { useCntrlContext } from '../../provider/useCntrlContext';
10
10
  import { getHoverStyles, getTransitions } from '../../utils/HoverStyles/HoverStyles';
11
11
  import { useRegisterResize } from "../../common/useRegisterResize";
12
- import { rangeMap } from '../../utils/rangeMap';
13
- import { ArticleRectContext } from '../../provider/ArticleRectContext';
14
12
  import { useLayoutContext } from '../useLayoutContext';
15
-
16
- // To prevent video behaviour that drops to the first frame
17
- // when close to the end
18
- const SCROLL_TIME_SHIFT = 0.1;
13
+ import { ScrollPlaybackVideo } from '../ScrollPlaybackVideo';
19
14
 
20
15
  export const VideoItem: FC<ItemProps<TVideoItem>> = ({ item, sectionId, onResize }) => {
21
16
  const id = useId();
@@ -25,40 +20,12 @@ export const VideoItem: FC<ItemProps<TVideoItem>> = ({ item, sectionId, onResize
25
20
  const borderColor = useMemo(() => CntrlColor.parse(strokeColor), [strokeColor]);
26
21
  const [ref, setRef] = useState<HTMLDivElement | null>(null);
27
22
  const videoRef = useRef<HTMLVideoElement | null>(null);
28
- const articleRectObserver = useContext(ArticleRectContext);
29
- const rafId = useRef<number | undefined>();
30
23
  const layoutId = useLayoutContext();
31
- const scrollPlayback = item.layoutParams[layoutId!].scrollPlayback
32
-
33
- useEffect(() => {
34
- const video = videoRef.current;
35
- if (!video || !scrollPlayback) {
36
- video?.play();
37
- return;
38
- }
39
- video?.pause();
40
- const scrollVideo = () => {
41
- rafId.current = window.requestAnimationFrame(scrollVideo);
42
- if (!articleRectObserver) return;
43
- const scrollPos = articleRectObserver.getSectionScroll(sectionId);
44
- if (!video.duration) return;
45
- const time = rangeMap(scrollPos, scrollPlayback.from, scrollPlayback.to, 0, video.duration, true);
46
- if (scrollPos > scrollPlayback.from && scrollPos < scrollPlayback.to) {
47
- if (toFixed(video.currentTime) === toFixed(time - SCROLL_TIME_SHIFT)) return;
48
- video.currentTime = Math.max(0, time - SCROLL_TIME_SHIFT);
49
- }
50
- };
51
- rafId.current = window.requestAnimationFrame(scrollVideo);
52
-
53
- return () => {
54
- if (rafId.current) {
55
- window.cancelAnimationFrame(rafId.current);
56
- rafId.current = undefined;
57
- }
58
- }
59
- }, [scrollPlayback]);
24
+ const scrollPlayback = item.layoutParams[layoutId!].scrollPlayback;
25
+ const hasScrollPlayback = scrollPlayback !== null;
60
26
 
61
27
  useRegisterResize(ref, onResize);
28
+
62
29
  return (
63
30
  <LinkWrapper url={item.link?.url} target={item.link?.target}>
64
31
  <div
@@ -67,24 +34,39 @@ export const VideoItem: FC<ItemProps<TVideoItem>> = ({ item, sectionId, onResize
67
34
  style={{
68
35
  opacity: `${opacity}`,
69
36
  transform: `rotate(${angle}deg)`,
70
- filter: `blur(${blur * 100}vw)`
37
+ filter: `blur(${blur * 100}vw)`,
38
+ overflow: hasScrollPlayback ? 'hidden' : 'auto'
71
39
  }}
72
40
  >
73
- <video
74
- ref={videoRef}
75
- autoPlay
76
- muted
77
- loop
78
- playsInline
79
- className={`video video-${item.id}`}
80
- style={{
41
+ {hasScrollPlayback ? (
42
+ <ScrollPlaybackVideo
43
+ sectionId={sectionId}
44
+ src={item.commonParams.url}
45
+ playbackParams={scrollPlayback}
46
+ style={{
47
+ borderRadius: `${radius * 100}vw`,
48
+ borderWidth: `${strokeWidth * 100}vw`,
49
+ borderColor: `${borderColor.toCss()}`
50
+ }}
51
+ className={`video video-playback-wrapper video-${item.id}`}
52
+ />
53
+ ) : (
54
+ <video
55
+ ref={videoRef}
56
+ autoPlay
57
+ muted
58
+ loop
59
+ playsInline
60
+ className={`video video-${item.id}`}
61
+ style={{
81
62
  borderRadius: `${radius * 100}vw`,
82
63
  borderWidth: `${strokeWidth * 100}vw`,
83
64
  borderColor: `${borderColor.toCss()}`
84
- }}
85
- >
86
- <source src={item.commonParams.url} />
87
- </video>
65
+ }}
66
+ >
67
+ <source src={item.commonParams.url} />
68
+ </video>
69
+ )}
88
70
  </div>
89
71
  <JSXStyle id={id}>{`
90
72
  @supports not (color: oklch(42% 0.3 90 / 1)) {
@@ -111,6 +93,10 @@ export const VideoItem: FC<ItemProps<TVideoItem>> = ({ item, sectionId, onResize
111
93
  .video-${item.id} {
112
94
  border-color: ${strokeColor};
113
95
  }
96
+ .video-playback-wrapper {
97
+ display: flex;
98
+ justify-content: center;
99
+ }
114
100
  ${getLayoutStyles(layouts, [item.state.hover], ([hoverParams]) => {
115
101
  return (`
116
102
  .video-wrapper-${item.id} {
@@ -131,7 +117,3 @@ export const VideoItem: FC<ItemProps<TVideoItem>> = ({ item, sectionId, onResize
131
117
  </LinkWrapper>
132
118
  );
133
119
  };
134
-
135
- function toFixed(num: number) {
136
- return Number(num.toFixed(5));
137
- }