Skip to content

Commit 2036a1e

Browse files
committed
feat: shadow DOM option
1 parent 99d6177 commit 2036a1e

8 files changed

Lines changed: 26 additions & 32 deletions

File tree

readme-zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ new cplayer({
7676
|width|`''`|播放器宽度。|
7777
|size|`12px`|播放器尺寸。|
7878
|style|`''`|附加的css样式。|
79+
|shadowDom|`'true'`|启用 [shadow DOM](https://developer.mozilla.org/zh-CN/docs/Web/Web_Components/Using_shadow_DOM)|
7980
|showPlaylistButton|`'true'`|显示播放列表按钮|
8081
|dropDownMenuMode|`'bottom'`|菜单(播放列表和歌曲信息)的显示模式, ‘bottom’ 底部、 'top' 顶部、 'none' 不显示|
8182

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ new cplayer({
7878
|width|`''`|播放器宽度。|
7979
|size|`12px`|播放器尺寸。|
8080
|style|`''`|附加的css样式。|
81+
|shadowDom|`'true'`|启用 [shadow DOM](https://developer.mozilla.org/zh-CN/docs/Web/Web_Components/Using_shadow_DOM)|
8182
|showPlaylistButton|`'true'`|显示播放列表按钮|
8283
|dropDownMenuMode|`'bottom'`|菜单(播放列表和歌曲信息)的显示模式, ‘bottom’ 底部、 'top' 顶部、 'none' 不显示|
8384

src/example.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ window.addEventListener("load",
7474
let players = [ new cplayer({
7575
...options,
7676
playlist,
77-
element: document.getElementById('app1')
77+
element: document.getElementById('app1'),
78+
shadowDom: false
7879
}), new cplayer({
7980
...options,
8081
playlist: playlist.push(playlist.shift()) && playlist,

src/lib/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export default class cplayer extends EventEmitter {
206206
}
207207

208208
private isPlaying() {
209-
return this.audioElement.currentTime > 0 && !this.audioElement.paused && !this.audioElement.ended && this.audioElement.readyState > 2;
209+
return !!this.audioElement && this.audioElement.currentTime > 0 && !this.audioElement.paused && !this.audioElement.ended && this.audioElement.readyState > 2;
210210
}
211211

212212
public openAudio(audio: IAudioItem = this.nowplay) {
@@ -346,7 +346,7 @@ export default class cplayer extends EventEmitter {
346346
this._volume = parseFloat(volume as string);
347347
if (this.audioElement)
348348
this.audioElement.volume = Math.max(0.0, Math.min(1.0, this._volume));
349-
this.emit('volumechange', this.audioElement.volume);
349+
this.emit('volumechange', this.volume);
350350
}
351351

352352
public destroy() {

src/lib/mediaSession.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const defaultPoster = require('../defaultposter.jpg')
66
export function cplayerMediaSessionPlugin(player: cplayer)
77
{
88
if ('mediaSession' in navigator) {
9-
navigator.mediaSession.metadata = mediaMetadata(player.nowplay);
9+
if (player.nowplay) navigator.mediaSession.metadata = mediaMetadata(player.nowplay);
1010
navigator.mediaSession.setActionHandler('play', () => player.play());
1111
navigator.mediaSession.setActionHandler('pause', () => player.pause());
1212
navigator.mediaSession.setActionHandler('previoustrack', () => player.prev());

src/lib/view.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export interface ICplayerViewOption {
5656
style?: string;
5757
dark?: boolean;
5858
big?: boolean;
59+
shadowDom?: boolean;
5960
dropDownMenuMode?: 'bottom' | 'top' | 'none' | string;
6061
}
6162

@@ -69,7 +70,8 @@ const defaultOption: ICplayerViewOption = {
6970
dropDownMenuMode: 'bottom',
7071
width: '',
7172
size: '12px',
72-
style: ''
73+
style: '',
74+
shadowDom: true
7375
}
7476

7577

@@ -129,13 +131,13 @@ export default class cplayerView extends EventEmitter {
129131
};
130132
this.player = player;
131133
if (this.options.generateBeforeElement) {
132-
if ((this.options.element as any).createShadowRoot) {
134+
if ((this.options.element as any).createShadowRoot && options.shadowDom !== false) {
133135
this.rootElement = createBeforeShadowElement(this.options.element, htmlTemplate, style + this.options.style);
134136
} else {
135137
this.rootElement = createBeforeElement(this.options.element, htmlTemplate, style + this.options.style);
136138
}
137139
} else {
138-
if ((this.options.element as any).createShadowRoot) {
140+
if ((this.options.element as any).createShadowRoot && options.shadowDom !== false) {
139141
this.rootElement = createShadowElement(this.options.element, htmlTemplate, style + this.options.style);
140142
} else {
141143
this.rootElement = createElement(this.options.element, htmlTemplate, style + this.options.style);
@@ -165,12 +167,12 @@ export default class cplayerView extends EventEmitter {
165167
this.big();
166168
}
167169

168-
this.setPoster(this.player.nowplay.poster || defaultPoster);
170+
// this.setPoster(this.player.nowplay.poster || defaultPoster);
169171
this.setProgress(this.player.currentTime / this.player.duration,
170172
this.player.currentTime,
171173
this.player.duration);
172-
this.elementLinks.title.innerText = this.player.nowplay.name;
173-
this.elementLinks.artist.innerText = this.player.nowplay.artist || '';
174+
// this.elementLinks.title.innerText = this.player.nowplay.name;
175+
// this.elementLinks.artist.innerText = this.player.nowplay.artist || '';
174176
this.updateLyric();
175177
this.updatePlaylist();
176178
}
@@ -381,6 +383,11 @@ export default class cplayerView extends EventEmitter {
381383
}
382384

383385
private updateLyric(playedTime: number = 0) {
386+
if (!this.player.nowplay) {
387+
this.setLyric(null);
388+
return;
389+
}
390+
384391
if (this.player.nowplay.lyric && typeof this.player.nowplay.lyric !== 'string' && this.player.played) {
385392
let lyric = this.player.nowplay.lyric.getLyric(playedTime * 1000);
386393
let nextLyric = this.player.nowplay.lyric.getNextLyric(playedTime * 1000);

webpack.config.example.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,12 @@ module.exports = {
9999
]
100100
},
101101
{
102-
test: /\.(png|jpg)$/,
102+
test: /\.(ttf|otf|woff|woff2|eot|png|jpg|mp3|mp4)$/,
103103
use: [{
104104
loader: 'url-loader',
105105
options: {
106-
limit: 8192
107-
}
108-
}]
109-
},
110-
{
111-
test: /\.(ttf|otf|woff|woff2|eot)$/,
112-
use: [{
113-
loader: 'url-loader',
114-
options: {
115-
limit: 8192
106+
limit: 8192,
107+
esModule: false
116108
}
117109
}]
118110
},

webpack.config.prod.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,12 @@ module.exports = {
9090
]
9191
},
9292
{
93-
test: /\.(png|jpg)$/,
93+
test: /\.(ttf|otf|woff|woff2|eot|png|jpg|mp3|mp4)$/,
9494
use: [{
9595
loader: 'url-loader',
9696
options: {
97-
limit: 8192
98-
}
99-
}]
100-
},
101-
{
102-
test: /\.(ttf|otf|woff|woff2|eot)$/,
103-
use: [{
104-
loader: 'url-loader',
105-
options: {
106-
limit: 8192
97+
limit: 8192,
98+
esModule: false
10799
}
108100
}]
109101
},

0 commit comments

Comments
 (0)