1
- import React , { memo , useEffect , useState , useRef } from "react"
1
+ import React , { useState } from "react"
2
2
import { useDebounce } from "react-use"
3
3
import { atom , useRecoilValue , useRecoilState } from "recoil"
4
4
import { InitPlugin } from "../@types/plugin"
5
5
import tailwind from "../tailwind.scss"
6
- import { DPlayer } from "./dplayerLoader"
7
- import style from "./style.scss"
6
+ import { DPlayerWrapper } from "./components/DPlayerWrapper"
8
7
import { DPlayerCommentPayload } from "./types"
9
8
10
9
/**
@@ -35,107 +34,22 @@ const main: InitPlugin = {
35
34
key : `${ prefix } .opacity` ,
36
35
default : 1 ,
37
36
} )
38
-
39
- const DPlayerWrapper : React . VFC < {
40
- comment : DPlayerCommentPayload | null
41
- } > = memo ( ( { comment } ) => {
42
- const dplayerElementRef = useRef < HTMLDivElement > ( null )
43
- const player = useRef < DPlayer | null > ( )
44
-
45
- const danmaku = {
46
- id : "mirakutest" ,
47
- user : "mirakutest" ,
48
- api : "" ,
49
- bottom : "10%" ,
50
- unlimited : true ,
51
- }
52
-
53
- const commentOpacity = useRecoilValue ( opacityAtom )
54
-
55
- useEffect ( ( ) => {
56
- if ( ! player . current ) return
57
- player . current . danmaku . opacity ( commentOpacity )
58
- } , [ commentOpacity ] )
59
-
60
- useEffect ( ( ) => {
61
- const playerRef = player . current
62
- if ( ! playerRef || ! comment || playerRef . video . paused === true ) {
63
- return
64
- }
65
- if ( comment . source . startsWith ( "5ch" ) ) {
66
- setTimeout ( ( ) => playerRef . danmaku . draw ( comment ) , comment . timeMs || 0 )
67
- } else {
68
- playerRef . danmaku . draw ( comment )
69
- }
70
- } , [ comment ] )
71
-
72
- useEffect ( ( ) => {
73
- const playerInstance = new DPlayer ( {
74
- container : dplayerElementRef . current ,
75
- live : true ,
76
- autoplay : true ,
77
- screenshot : true ,
78
- video : {
79
- url : "https://user-images.githubusercontent.com/7887955/135782430-ec36baf3-2f12-4629-b89e-0628d1baa91b.mp4" ,
80
- } ,
81
- danmaku,
82
- lang : "ja-jp" ,
83
- subtitle : {
84
- type : "webvtt" ,
85
- fontSize : "20px" ,
86
- color : "#fff" ,
87
- bottom : "40px" ,
88
- // TODO: Typing correctly
89
- } as never ,
90
- apiBackend : {
91
- read : ( option ) => {
92
- option . success ( [ { } ] )
93
- } ,
94
- send : ( option , item , callback ) => {
95
- callback ( )
96
- } ,
97
- } ,
98
- contextmenu : [ ] ,
99
- loop : true ,
100
- volume : 0 ,
101
- hotkey : false ,
102
- } )
103
-
104
- playerInstance . danmaku . opacity ( commentOpacity )
105
- playerInstance . danmaku . show ( )
106
-
107
- playerInstance . on ( "pause" as DPlayer . DPlayerEvents . pause , ( ) => {
108
- playerInstance . play ( )
109
- } )
110
-
111
- player . current = playerInstance
112
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
113
- // @ts -ignore
114
- window . dplayer = playerInstance
115
-
116
- const timer = setInterval ( ( ) => {
117
- playerInstance . video . currentTime = 0
118
- } , 30 * 1000 )
119
- return ( ) => {
120
- player . current ?. destroy ( )
121
- player . current = null
122
- clearInterval ( timer )
123
- }
124
- } , [ ] )
125
-
126
- return (
127
- < >
128
- < style > { style } </ style >
129
- < div ref = { dplayerElementRef } > </ div >
130
- </ >
131
- )
37
+ const zoomAtom = atom < number > ( {
38
+ key : `${ prefix } .zoom` ,
39
+ default : 1 ,
132
40
} )
133
41
134
42
return {
135
43
...meta ,
136
44
exposedAtoms : [ { type : "atom" , atom : commentAtom } ] ,
137
- sharedAtoms : [ { type : "atom" , atom : opacityAtom } ] ,
138
- storedAtoms : [ { type : "atom" , atom : opacityAtom } ] ,
45
+ sharedAtoms : [
46
+ { type : "atom" , atom : opacityAtom } ,
47
+ { type : "atom" , atom : zoomAtom } ,
48
+ ] ,
49
+ storedAtoms : [
50
+ { type : "atom" , atom : opacityAtom } ,
51
+ { type : "atom" , atom : zoomAtom } ,
52
+ ] ,
139
53
setup ( ) {
140
54
return
141
55
} ,
@@ -145,7 +59,11 @@ const main: InitPlugin = {
145
59
position : "onPlayer" ,
146
60
component : ( ) => {
147
61
const comment = useRecoilValue ( commentAtom )
148
- return < DPlayerWrapper comment = { comment } />
62
+ const opacity = useRecoilValue ( opacityAtom )
63
+ const zoom = useRecoilValue ( zoomAtom )
64
+ return (
65
+ < DPlayerWrapper comment = { comment } opacity = { opacity } zoom = { zoom } />
66
+ )
149
67
} ,
150
68
} ,
151
69
{
@@ -154,13 +72,16 @@ const main: InitPlugin = {
154
72
label : meta . name ,
155
73
component : ( ) => {
156
74
const [ opacity , setOpacity ] = useRecoilState ( opacityAtom )
75
+ const [ zoom , setZoom ] = useRecoilState ( zoomAtom )
157
76
const [ rangeOpacity , setRangeOpacity ] = useState ( opacity * 10 )
77
+ const [ rangeZoom , setRangeZoom ] = useState ( zoom * 10 )
158
78
useDebounce (
159
79
( ) => {
160
80
setOpacity ( rangeOpacity / 10 )
81
+ setZoom ( rangeZoom / 10 )
161
82
} ,
162
83
100 ,
163
- [ rangeOpacity ]
84
+ [ rangeOpacity , rangeZoom ]
164
85
)
165
86
return (
166
87
< >
@@ -182,6 +103,23 @@ const main: InitPlugin = {
182
103
setRangeOpacity ( p )
183
104
} }
184
105
/>
106
+ < span > { rangeOpacity / 10 } </ span >
107
+ </ label >
108
+ < label className = "block mt-4" >
109
+ < span > 表示倍率</ span >
110
+ < input
111
+ type = "range"
112
+ className = "block mt-2 rounded-md w-full"
113
+ min = { 10 }
114
+ max = { 30 }
115
+ value = { rangeZoom }
116
+ onChange = { ( e ) => {
117
+ const p = parseInt ( e . target . value )
118
+ if ( Number . isNaN ( p ) ) return
119
+ setRangeZoom ( p )
120
+ } }
121
+ />
122
+ < span > { rangeZoom / 10 } </ span >
185
123
</ label >
186
124
</ form >
187
125
</ div >
0 commit comments