@barabel324/react-player 0.0.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.
package/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # React + TypeScript + Vite
2
+
3
+ This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
4
+
5
+ Currently, two official plugins are available:
6
+
7
+ - [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) for Fast Refresh
8
+ - [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
9
+
10
+ ## Expanding the ESLint configuration
11
+
12
+ If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:
13
+
14
+ ```js
15
+ export default tseslint.config([
16
+ globalIgnores(['dist']),
17
+ {
18
+ files: ['**/*.{ts,tsx}'],
19
+ extends: [
20
+ // Other configs...
21
+
22
+ // Remove tseslint.configs.recommended and replace with this
23
+ ...tseslint.configs.recommendedTypeChecked,
24
+ // Alternatively, use this for stricter rules
25
+ ...tseslint.configs.strictTypeChecked,
26
+ // Optionally, add this for stylistic rules
27
+ ...tseslint.configs.stylisticTypeChecked,
28
+
29
+ // Other configs...
30
+ ],
31
+ languageOptions: {
32
+ parserOptions: {
33
+ project: ['./tsconfig.node.json', './tsconfig.app.json'],
34
+ tsconfigRootDir: import.meta.dirname,
35
+ },
36
+ // other options...
37
+ },
38
+ },
39
+ ])
40
+ ```
41
+
42
+ You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:
43
+
44
+ ```js
45
+ // eslint.config.js
46
+ import reactX from 'eslint-plugin-react-x'
47
+ import reactDom from 'eslint-plugin-react-dom'
48
+
49
+ export default tseslint.config([
50
+ globalIgnores(['dist']),
51
+ {
52
+ files: ['**/*.{ts,tsx}'],
53
+ extends: [
54
+ // Other configs...
55
+ // Enable lint rules for React
56
+ reactX.configs['recommended-typescript'],
57
+ // Enable lint rules for React DOM
58
+ reactDom.configs.recommended,
59
+ ],
60
+ languageOptions: {
61
+ parserOptions: {
62
+ project: ['./tsconfig.node.json', './tsconfig.app.json'],
63
+ tsconfigRootDir: import.meta.dirname,
64
+ },
65
+ // other options...
66
+ },
67
+ },
68
+ ])
69
+ ```
@@ -0,0 +1,74 @@
1
+ import { jsx as a } from "react/jsx-runtime";
2
+ import { useRef as s, useImperativeHandle as d, useEffect as l } from "react";
3
+ import { u } from "./index-COqINEuJ.js";
4
+ const p = async () => new Promise((o, i) => {
5
+ if (window.VK?.VideoPlayer) {
6
+ o(window.VK);
7
+ return;
8
+ }
9
+ const r = "https://vk.com/js/api/videoplayer.js";
10
+ if (Array.from(document.scripts).find(
11
+ (e) => e.src === r
12
+ )) {
13
+ const e = setInterval(() => {
14
+ window.VK?.VideoPlayer && (clearInterval(e), o(window.VK));
15
+ }, 300);
16
+ setTimeout(() => {
17
+ clearInterval(e), i(new Error("VK Video API timeout"));
18
+ }, 15e3);
19
+ return;
20
+ }
21
+ const t = document.createElement("script");
22
+ t.src = r, t.async = !0, t.onload = () => {
23
+ const e = () => {
24
+ window.VK?.VideoPlayer ? o(window.VK) : setTimeout(e, 300);
25
+ };
26
+ e();
27
+ }, t.onerror = () => {
28
+ i(new Error("Failed to load VK Video API"));
29
+ };
30
+ const c = document.getElementsByTagName("script")[0];
31
+ c.parentNode?.insertBefore(t, c);
32
+ }), m = (o) => {
33
+ const i = /^https:\/\/vkvideo\.ru\/video(-\d+)_(\d+)$/, r = o.match(i);
34
+ if (!r)
35
+ throw new Error(`Некорректный формат ссылки ${o}`);
36
+ const n = r[1], t = r[2];
37
+ return `https://vkvideo.ru/video_ext.php?oid=${n}&id=${t}`;
38
+ }, V = ({
39
+ ref: o,
40
+ url: i
41
+ }) => {
42
+ const r = s(null), n = s(null), t = u(), c = () => {
43
+ n.current && n.current.play?.();
44
+ };
45
+ return d(o, () => ({
46
+ play: c
47
+ })), l(() => {
48
+ if (r.current)
49
+ return p().then((e) => {
50
+ n.current = e.VideoPlayer(r.current), t({ type: "canplay" });
51
+ }).catch((e) => {
52
+ console.error(e);
53
+ }), () => {
54
+ n.current && n.current.destroy();
55
+ };
56
+ }, []), /* @__PURE__ */ a(
57
+ "iframe",
58
+ {
59
+ width: "100%",
60
+ height: "100%",
61
+ ref: r,
62
+ src: `${m(i)}&js_api=1`,
63
+ allow: "autoplay; fullscreen; accelerometer; gyroscope; picture-in-picture; encrypted-media",
64
+ "data-testid": "embed-iframe",
65
+ frameBorder: "0",
66
+ scrolling: "no",
67
+ title: "player",
68
+ allowFullScreen: !0
69
+ }
70
+ );
71
+ };
72
+ export {
73
+ V as default
74
+ };
@@ -0,0 +1,41 @@
1
+ import { jsx as u } from "react/jsx-runtime";
2
+ import { u as p } from "./index-COqINEuJ.js";
3
+ import { useRef as c, useImperativeHandle as i, useEffect as l } from "react";
4
+ const f = "player-rutube", m = {
5
+ parent: f
6
+ }, h = ({
7
+ ref: t,
8
+ url: a
9
+ }) => {
10
+ const e = c(null), s = p(), n = () => {
11
+ e.current && e.current.contentWindow?.postMessage(JSON.stringify({
12
+ type: "player:play",
13
+ data: {}
14
+ }), "*");
15
+ };
16
+ return i(t, () => ({
17
+ play: n
18
+ })), l(() => {
19
+ const r = (o) => {
20
+ if (!e.current) return;
21
+ JSON.parse(o.data).type === "player:ready" && s({ type: "canplay" });
22
+ };
23
+ return window.addEventListener("message", r), () => {
24
+ window.removeEventListener("message", r);
25
+ };
26
+ }, []), /* @__PURE__ */ u(
27
+ "iframe",
28
+ {
29
+ ref: e,
30
+ width: "100%",
31
+ height: "100%",
32
+ src: a,
33
+ allowFullScreen: !0,
34
+ frameBorder: "0",
35
+ className: m.parent
36
+ }
37
+ );
38
+ };
39
+ export {
40
+ h as default
41
+ };
@@ -0,0 +1,61 @@
1
+ import { jsx as l } from "react/jsx-runtime";
2
+ import { useRef as s, useImperativeHandle as d, useEffect as p } from "react";
3
+ import { u as y } from "./index-COqINEuJ.js";
4
+ const m = (r) => {
5
+ const a = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/, t = r.match(a);
6
+ return t && t[7].length === 11 ? t[7] : "";
7
+ }, f = async () => await new Promise((r) => {
8
+ if (window.YT)
9
+ r(window.YT);
10
+ else {
11
+ const a = "https://www.youtube.com/iframe_api", n = Array.from(document.querySelectorAll("script")).find((e) => e.src === a) ?? document.createElement("script");
12
+ if (n.src) {
13
+ const e = setInterval(() => {
14
+ window.YT?.PlayerState && (r(window.YT), clearInterval(e));
15
+ }, 300);
16
+ setTimeout(() => {
17
+ clearInterval(e);
18
+ }, 6e4);
19
+ } else {
20
+ n.src = a;
21
+ const e = document.getElementsByTagName("script")[0];
22
+ e.parentNode && e.parentNode.insertBefore(n, e), window.onYouTubeIframeAPIReady = () => {
23
+ r(window.YT);
24
+ };
25
+ }
26
+ }
27
+ }), w = "player-youtube", h = {
28
+ parent: w
29
+ }, b = ({
30
+ ref: r,
31
+ url: a
32
+ }) => {
33
+ const t = s(null), n = s(null), e = y(), i = (o) => {
34
+ n.current = o.target, e({
35
+ type: "canplay"
36
+ });
37
+ }, u = () => {
38
+ n.current?.playVideo && n.current.playVideo();
39
+ };
40
+ return d(r, () => ({
41
+ play: u
42
+ })), p(() => {
43
+ if (!t.current) return;
44
+ const o = m(a);
45
+ f().then((c) => {
46
+ new c.Player(t.current, {
47
+ width: "100%",
48
+ height: "100%",
49
+ videoId: o,
50
+ events: {
51
+ onReady: i
52
+ }
53
+ });
54
+ }).catch((c) => {
55
+ console.error(c);
56
+ });
57
+ }, []), /* @__PURE__ */ l("div", { className: h.parent, ref: t });
58
+ };
59
+ export {
60
+ b as default
61
+ };
@@ -0,0 +1,137 @@
1
+ import { jsx as s, jsxs as y } from "react/jsx-runtime";
2
+ import { createContext as w, useContext as h, useReducer as x, lazy as p, useMemo as C, useRef as $ } from "react";
3
+ import d from "classix";
4
+ const n = {
5
+ show: "show",
6
+ hide: "hide",
7
+ showButton: "showButton",
8
+ hideButton: "hideButton",
9
+ canplay: "canplay"
10
+ }, v = {
11
+ showButton: !1,
12
+ showPreview: !0,
13
+ canplay: !1
14
+ }, k = (e, t) => {
15
+ const { type: r } = t;
16
+ switch (r) {
17
+ case n.show:
18
+ return { ...e, showButton: !0, showPreview: !0 };
19
+ case n.hide:
20
+ return { ...e, showButton: !1, showPreview: !1 };
21
+ case n.showButton:
22
+ return { ...e, showButton: !0 };
23
+ case n.hideButton:
24
+ return { ...e, showButton: !1 };
25
+ case n.canplay:
26
+ return { ...e, canplay: !0, showButton: !0 };
27
+ default:
28
+ return e;
29
+ }
30
+ }, P = w(v), m = w(() => {
31
+ }), _ = ({ children: e }) => {
32
+ const [t, r] = x(k, v);
33
+ return /* @__PURE__ */ s(P.Provider, { value: t, children: /* @__PURE__ */ s(m.Provider, { value: r, children: e }) });
34
+ }, N = () => {
35
+ const e = h(P);
36
+ if (e === void 0)
37
+ throw new Error("usePlayerState нужно использовать внутри PlayerProvider");
38
+ return e;
39
+ }, E = () => {
40
+ const e = h(m);
41
+ if (e === void 0)
42
+ throw new Error("usePlayerDispatch нужно использовать внутри PlayerProvider");
43
+ return e;
44
+ }, R = "button-play", S = {
45
+ parent: R
46
+ }, T = ({
47
+ className: e,
48
+ onClick: t
49
+ }) => /* @__PURE__ */ s(
50
+ "button",
51
+ {
52
+ className: d(S.parent, e),
53
+ type: "button",
54
+ onClick: t
55
+ }
56
+ ), c = "player-preview", u = {
57
+ parent: c,
58
+ wrapper: `${c}__wrapper`,
59
+ button: `${c}__button`,
60
+ preview: `${c}__preview`,
61
+ player: `${c}__player`
62
+ }, g = ({
63
+ className: e,
64
+ preview: t,
65
+ onPlay: r,
66
+ children: a
67
+ }) => {
68
+ const { showButton: i, showPreview: l, canplay: b } = N(), f = E(), B = () => {
69
+ b && (f({
70
+ type: n.hide
71
+ }), r && r());
72
+ };
73
+ return /* @__PURE__ */ y("div", { className: d(u.parent, e), children: [
74
+ (i || t && l) && /* @__PURE__ */ y(
75
+ "div",
76
+ {
77
+ className: u.wrapper,
78
+ onClick: B,
79
+ children: [
80
+ i && /* @__PURE__ */ s(
81
+ T,
82
+ {
83
+ className: u.button
84
+ }
85
+ ),
86
+ t && l && /* @__PURE__ */ s("img", { className: u.preview, src: t.src, alt: t.alt ?? "preview" })
87
+ ]
88
+ }
89
+ ),
90
+ /* @__PURE__ */ s("div", { className: u.player, children: a })
91
+ ] });
92
+ }, o = {
93
+ youtube: "youtube",
94
+ rutube: "rutube",
95
+ video: "video",
96
+ vk: "vk"
97
+ }, D = {
98
+ [o.youtube]: p(async () => await import("./index-C-0pNFYJ.js")),
99
+ [o.rutube]: p(async () => await import("./index-3eLEuMe8.js")),
100
+ [o.video]: p(async () => await import("./index-CSgcS1fk.js")),
101
+ [o.vk]: p(async () => await import("./index-0qY1-A4q.js"))
102
+ }, V = (e) => {
103
+ const t = /^(?:https?:\/\/)?(?:m\.|www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/, r = /^(?:https?:\/\/)?(?:rutu\.be\/|rutube\.ru(\/play\/))/, a = /^(?:https?:\/\/)?(?:www\.|m\.)?(?:vk\.com|vkvideo\.ru)\/.+$/;
104
+ return r.test(e) ? o.rutube : t.test(e) ? o.youtube : a.test(e) ? o.vk : o.video;
105
+ }, j = "player", A = {
106
+ parent: j
107
+ }, L = ({
108
+ url: e,
109
+ preview: t
110
+ }) => {
111
+ const r = C(() => {
112
+ const l = V(e);
113
+ return D[l];
114
+ }, [e]), a = $(null), i = () => {
115
+ a.current && a.current.play();
116
+ };
117
+ return /* @__PURE__ */ s(
118
+ g,
119
+ {
120
+ className: A.parent,
121
+ preview: t,
122
+ onPlay: i,
123
+ children: /* @__PURE__ */ s(
124
+ r,
125
+ {
126
+ ref: a,
127
+ url: e
128
+ }
129
+ )
130
+ }
131
+ );
132
+ }, I = (e) => /* @__PURE__ */ s(_, { children: /* @__PURE__ */ s(L, { ...e }) });
133
+ export {
134
+ n as P,
135
+ I as a,
136
+ E as u
137
+ };
@@ -0,0 +1,47 @@
1
+ import { jsx as i } from "react/jsx-runtime";
2
+ import { u as y, P as t } from "./index-COqINEuJ.js";
3
+ import d from "classix";
4
+ import { useRef as m, useImperativeHandle as P } from "react";
5
+ const f = "player-video", h = {
6
+ parent: f
7
+ }, A = ({
8
+ className: r,
9
+ ref: n,
10
+ url: s
11
+ }) => {
12
+ const e = m(null), a = y(), o = () => {
13
+ a({
14
+ type: t.hideButton
15
+ });
16
+ }, l = () => {
17
+ a({
18
+ type: t.showButton
19
+ });
20
+ }, c = () => {
21
+ a({
22
+ type: t.canplay
23
+ });
24
+ }, p = () => {
25
+ e.current && (o(), e.current.play().catch((u) => {
26
+ console.error(u);
27
+ }));
28
+ };
29
+ return P(n, () => ({
30
+ play: p
31
+ })), /* @__PURE__ */ i(
32
+ "video",
33
+ {
34
+ className: d(h.parent, r),
35
+ ref: e,
36
+ controls: !0,
37
+ src: s,
38
+ preload: "auto",
39
+ onPlay: o,
40
+ onPause: l,
41
+ onCanPlay: c
42
+ }
43
+ );
44
+ };
45
+ export {
46
+ A as default
47
+ };
@@ -0,0 +1 @@
1
+ .button-play{--button-play-size: 3.375rem;position:relative;width:var(--button-play-size);min-width:var(--button-play-size);height:var(--button-play-size);background-color:var(--color-green-200);border-radius:50%}.button-play:before{content:"";position:absolute;top:50%;left:58%;border:.3125rem solid transparent;border-left:.5625rem solid var(--color-beige-100);transform:translate(-50%,-50%)}.player-preview{position:relative;width:100%;height:100%;object-fit:cover}.player-preview .player-preview__wrapper{position:absolute;inset:0;cursor:pointer}.player-preview .player-preview__button{z-index:2;position:absolute;top:50%;left:50%;transform:translate(-50%) translateY(-50%)}.player-preview .player-preview__preview{z-index:1;position:absolute;top:0;left:0;width:100%;height:100%;object-fit:cover}.player-preview .player-preview__player{width:100%;height:100%}.player{position:relative;margin:50px;width:300px;height:300px}.player-youtube,.player-rutube{display:block;width:100%;height:100%}.player-video{display:block;width:100%;height:100%;object-fit:cover;object-position:center}
@@ -0,0 +1,31 @@
1
+ import { RefObject } from 'react';
2
+
3
+ declare type FCClass<P = object> = React.FC<P & React.PropsWithChildren & {
4
+ className?: string;
5
+ }>;
6
+
7
+ export declare const Player: FCClass<TPlayer>;
8
+
9
+ declare type TPlayer = {
10
+ preview?: TPlayerPreview['preview'];
11
+ url: TPlayerBase['url'];
12
+ };
13
+
14
+ declare type TPlayerBase = {
15
+ ref: RefObject<TPlayerBaseMethods | null>;
16
+ url: string;
17
+ };
18
+
19
+ declare type TPlayerBaseMethods = {
20
+ play: () => void;
21
+ };
22
+
23
+ declare type TPlayerPreview = {
24
+ preview?: {
25
+ src: string;
26
+ alt: string;
27
+ };
28
+ onPlay?: () => void;
29
+ };
30
+
31
+ export { }
@@ -0,0 +1,4 @@
1
+ import { a as e } from "./index-COqINEuJ.js";
2
+ export {
3
+ e as Player
4
+ };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@barabel324/react-player",
3
+ "version": "0.0.0",
4
+ "type": "module",
5
+ "main": "./dist/react-player.js",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./dist/react-player.js"
9
+ }
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "scripts": {
15
+ "dev": "vite",
16
+ "build": "tsc -b && vite build",
17
+ "lint": "eslint .",
18
+ "preview": "vite preview",
19
+ "prepublishOnly": "npm run build"
20
+ },
21
+ "peerDependencies": {
22
+ "classix": "^2.2.2",
23
+ "react": "^19.1.1",
24
+ "react-dom": "^19.1.1"
25
+ },
26
+ "devDependencies": {
27
+ "@eslint/js": "^9.33.0",
28
+ "@microsoft/api-extractor": "^7.52.11",
29
+ "@types/node": "^24.2.1",
30
+ "@types/react": "^19.1.10",
31
+ "@types/react-dom": "^19.1.7",
32
+ "@vitejs/plugin-react": "^5.0.0",
33
+ "classix": "^2.2.2",
34
+ "eslint": "^9.33.0",
35
+ "eslint-plugin-react-hooks": "^5.2.0",
36
+ "eslint-plugin-react-refresh": "^0.4.20",
37
+ "globals": "^16.3.0",
38
+ "react": "^19.1.1",
39
+ "react-dom": "^19.1.1",
40
+ "sass-embedded": "^1.90.0",
41
+ "typescript": "~5.8.3",
42
+ "typescript-eslint": "^8.39.1",
43
+ "unplugin-dts": "^1.0.0-beta.6",
44
+ "vite": "^7.1.2"
45
+ }
46
+ }