-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
171 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Component/BlazorComponent.Web/rollup.config.xgplayer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { defineConfig } from "rollup"; | ||
import { terser } from "rollup-plugin-terser"; | ||
|
||
import commonjs from "@rollup/plugin-commonjs"; | ||
import json from "@rollup/plugin-json"; | ||
import resolve from "@rollup/plugin-node-resolve"; | ||
import typescript from "@rollup/plugin-typescript"; | ||
|
||
export default defineConfig([ | ||
{ | ||
input: "./src/proxies/xgplayer/index.ts", | ||
output: [ | ||
{ | ||
file: "../../../../MASA.Blazor/wwwroot/js/proxies/xgplayer-proxy.js", | ||
format: "esm", | ||
sourcemap: true, | ||
}, | ||
], | ||
plugins: [typescript(), json(), resolve(), commonjs(), terser()], | ||
}, | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,27 @@ | ||
import * as slider from "./components/slider"; | ||
import * as interop from "./interop"; | ||
import * as overlayable from "./mixins/overlayable"; | ||
import { MarkdownParser } from "./proxies/markdown-it"; | ||
|
||
declare global { | ||
interface Window { | ||
BlazorComponent: any; | ||
MasaBlazor: any; | ||
MasaBlazor: { | ||
extendMarkdownIt?: (parser: MarkdownParser) => void; | ||
xgplayerPlugins: any[]; | ||
xgplayerPluginOptions?: { [prop: string]: any }; | ||
}; | ||
} | ||
} | ||
|
||
window.BlazorComponent = { | ||
interop: { | ||
...interop, | ||
...overlayable, | ||
...slider | ||
...slider, | ||
}, | ||
}; | ||
|
||
window.MasaBlazor = {}; | ||
window.MasaBlazor = { | ||
xgplayerPlugins: [], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/Component/BlazorComponent.Web/src/proxies/xgplayer/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import Xgplayer, { IPlayerOptions } from "xgplayer"; | ||
import MusicPreset, { Music } from "xgplayer-music"; | ||
import Mobile from "xgplayer/es/plugins/mobile"; | ||
import Play from "xgplayer/es/plugins/play"; | ||
import Playbackrate from "xgplayer/es/plugins/playbackRate"; | ||
import Progress from "xgplayer/es/plugins/progress"; | ||
import Time from "xgplayer/es/plugins/time"; | ||
import Volume from "xgplayer/es/plugins/volume"; | ||
|
||
export type XgplayerOptions = Omit< | ||
IPlayerOptions, | ||
"id" | "el" | "url" | "plugins" | ||
> & { | ||
music?: Music; | ||
}; | ||
|
||
class XgplayerProxy { | ||
el: HTMLElement; | ||
initOptions: XgplayerOptions; | ||
player: Xgplayer; | ||
|
||
constructor( | ||
selector: string, | ||
url: IPlayerOptions["url"], | ||
options: XgplayerOptions | ||
) { | ||
const el: HTMLElement = document.querySelector(selector); | ||
|
||
if (!el) { | ||
throw new Error( | ||
"[Xgplayer] this selector of DOM node that player to mount on is required." | ||
); | ||
} | ||
|
||
if (!url) { | ||
throw new Error("[Xgplayer] this media resource url is required."); | ||
} | ||
|
||
this.initOptions = options; | ||
this.el = el; | ||
this.init(url, options); | ||
} | ||
|
||
invokeVoid(prop: string, args: any[]) { | ||
if (this.player[prop] && typeof this.player[prop] === "function") { | ||
this.player[prop](...args); | ||
} | ||
} | ||
|
||
getPropsAndStates() { | ||
return { | ||
autoplay: this.player.autoplay, | ||
crossOrigin: this.player.crossOrigin, | ||
currentSrc: this.player.currentSrc, | ||
currentTime: this.player.currentTime, | ||
duration: this.player.duration, | ||
cumulateTime: this.player.cumulateTime, | ||
volume: this.player.volume, | ||
muted: this.player.muted, | ||
defaultMuted: this.player.defaultMuted, | ||
playbackRate: this.player.playbackRate, | ||
loop: this.player.loop, | ||
src: this.player.src, | ||
lang: this.player.lang, | ||
version: this.player.version, | ||
state: this.player.state, | ||
ended: this.player.ended, | ||
paused: this.player.paused, | ||
networkState: this.player.networkState, | ||
readyState: this.player.readyState, | ||
isFullscreen: this.player.isFullscreen, | ||
isCssFullscreen: this.player.isCssfullScreen, | ||
isSeeking: this.player.isSeeking, | ||
isActive: this.player.isActive, | ||
}; | ||
} | ||
|
||
init(url: IPlayerOptions["url"], options: XgplayerOptions) { | ||
let playerOptions: IPlayerOptions = { | ||
el: this.el, | ||
url, | ||
}; | ||
|
||
if (options.music) { | ||
playerOptions = { | ||
...playerOptions, | ||
presets: [MusicPreset], | ||
plugins: [Mobile, Progress, Play, Playbackrate, Time, Volume], | ||
...options, | ||
}; | ||
} else { | ||
playerOptions = { | ||
...playerOptions, | ||
...options, | ||
}; | ||
} | ||
|
||
if (window.MasaBlazor.xgplayerPlugins) { | ||
playerOptions.plugins = [ | ||
...playerOptions.plugins, | ||
...window.MasaBlazor.xgplayerPlugins, | ||
]; | ||
|
||
playerOptions = { | ||
...playerOptions, | ||
...window.MasaBlazor.xgplayerPluginOptions, | ||
}; | ||
} | ||
|
||
this.player = new Xgplayer(playerOptions); | ||
} | ||
|
||
destroy() { | ||
this.player.destroy(); | ||
this.player = null; | ||
} | ||
} | ||
|
||
export function init( | ||
selector: string, | ||
url: IPlayerOptions["url"], | ||
options: XgplayerOptions | ||
) { | ||
const instance = new XgplayerProxy(selector, url, options); | ||
return instance; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters