使用vue3
<template>
<div v-html="content" class="html-content text-black ql-editor" @click="jumpLink($event)" v-if="htmlContent"></div>
</template>
<script setup lang="ts">
import { showImagePreview } from 'vant';
import { UtilService } from '@common/services/util.service';
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import { onMounted, onUnmounted, ref } from 'vue';
import { Utils } from '@common/utils/utils';
import { log } from '@common/utils';
import LoopBtn from '@mobile/components/player/loopBtn';
import FullscreenBtn from '@mobile/components/player/fullscreenBtn';
import videoJs from 'video.js';
import 'video.js/dist/video-js.min.css';
let props = defineProps(['htmlContent', 'fillImg']);
let emits = defineEmits(['onImageOpen']);
//正在显示提示框,默认不显示
let showing = false;
//是否在预览图片
let previewImg = false;
let content = ref();
let myPlayerList = ref([]);
// 初始化
onBeforeMount(() => {
if (props.htmlContent.indexOf('
解析:
// 富文本样式css
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
//videojs
import videoJs from 'video.js';
import 'video.js/dist/video-js.min.css';
//循环播放 和 全屏播放 按钮组件
import LoopBtn from '@mobile/components/player/loopBtn';
import FullscreenBtn from '@mobile/components/player/fullscreenBtn';
解决 VIDEOJS: WARN: Player “media-player-0” is already initialised. Options will not be applied.
onBeforeUnmount(() => {
if (myPlayerList.value) {
myPlayerList.value.forEach((ele) => {
ele.dispose();
});
}
});
通过videoJs注册组件
videoJs.registerComponent(‘FullscreenBtn’, FullscreenBtn);
videoJs.registerComponent(‘LoopBtn’, LoopBtn);
循环播放组件
//@ts-ignore
import videoJs, { VideoJsPlayer } from 'video.js';
import unLoop from '@mobile/assets/icon/unLoop.svg';
import loop from '@mobile/assets/icon/loop.svg';
// 这里直接获取的是Button组件,因为需要的其实就是控制条的一个button
const VideoButton = videoJs.getComponent('Button');
//@ts-ignore
class loopBtn extends VideoButton {
player: any;
loop: boolean = false;
icon: HTMLImageElement;
constructor(player: any, options: Object) {
super(player, options);
//@ts-ignore
this.addClass('vjs-control');
//@ts-ignore
this.addClass('vjs-button');
//@ts-ignore
this.addClass('vjs-loop-btn');
this.player = player;
}
// 自定义按钮的dom结构,createEl会在使用组件的时候自动调用
createEl(tag: string, props?: any, attributes?: any): HTMLButtonElement {
const nextBtnEl: HTMLButtonElement = document.createElement('button');
this.icon = document.createElement('img');
this.icon.src = unLoop;
// 将按钮图标放到按钮中
videoJs.dom.appendContent(nextBtnEl, this.icon);
return nextBtnEl;
}
handleClick(): void {
this.loop = !this.loop;
if (this.loop) {
//@ts-ignore
this.icon.src = loop;
this.player.loop(true);
} else {
//@ts-ignore
this.icon.src = unLoop;
this.player.loop(false);
}
}
}
export default loopBtn;
全屏播放组件
//@ts-ignore
import videoJs, { VideoJsPlayer } from 'video.js';
import unFullscreen from '@mobile/assets/icon/unFullscreen.svg';
import fullscreen from '@mobile/assets/icon/fullscreen.svg';
// 这里直接获取的是Button组件
const VideoButton = videoJs.getComponent('Button');
//@ts-ignore
class FullscreenBtn extends VideoButton {
player: any;
isFullscreen: boolean = false;
icon: HTMLImageElement;
that: any;
video: any;
constructor(player: any, options: Object) {
super(player, options);
//@ts-ignore
this.addClass('vjs-control');
//@ts-ignore
this.addClass('vjs-button');
//@ts-ignore
this.addClass('vjs-fullscreen-btn');
this.player = player;
this.video = this.player.el_;
this.that = this;
}
// 自定义按钮的dom结构,createEl会在使用组件的时候自动调用
createEl(tag: string, props?: any, attributes?: any): HTMLButtonElement {
const nextBtnEl: HTMLButtonElement = document.createElement('button');
this.icon = document.createElement('img');
this.icon.src = fullscreen;
// 将按钮图标放到按钮中
videoJs.dom.appendContent(nextBtnEl, this.icon);
return nextBtnEl;
}
handleClick(): void {
this.isFullscreen = !this.isFullscreen;
if (this.isFullscreen) {
//@ts-ignore
this.icon.src = unFullscreen;
// if (this.video.parentNode.requestFullscreen) {
// this.video.parentNode.requestFullscreen();
// }
// if (document.documentElement.requestFullscreen) {
// document.documentElement.requestFullscreen();
// }
this.video.classList.add('vjs-fullscreen-enter');
} else {
//@ts-ignore
this.icon.src = fullscreen;
// if (document.fullscreenElement) {
// document.exitFullscreen().then();
// }
this.video.classList.remove('vjs-fullscreen-enter');
}
}
}
export default FullscreenBtn;