media_player_client/src/components/SubtitleDialog.vue

378 lines
11 KiB
Vue
Raw Normal View History

2021-09-29 14:21:33 +08:00
<template>
<q-dialog
persistent
v-model="show_dialog"
@show="onShow"
@before-hide="resetData"
2022-01-25 19:41:28 +08:00
@keydown="
(evt) => {
if (!loading && evt.keyCode == 27) {
2022-01-25 19:41:28 +08:00
show_dialog = false;
}
}
"
2021-09-29 14:21:33 +08:00
>
2021-11-05 09:15:51 +08:00
<q-card class="overflow-hidden" style="max-width: 70vw">
2021-09-29 14:21:33 +08:00
<q-form @submit="onSubmit">
<q-card-section class="q-ma-none q-pa-sm">
<div class="row">
<div class="col-auto text-h6">
{{ $t("subtitle") }}
</div>
<q-space />
<div>
2021-11-05 09:15:51 +08:00
<q-btn
flat
round
color="secondary"
:icon="
$q.fullscreen.isActive ? 'fullscreen_exit' : 'fullscreen'
"
@click="toggleFullScreen"
>
<q-tooltip>
{{
$q.fullscreen.isActive
? $t("Exit Fullscreen")
: $t("Go Fullscreen")
}}
</q-tooltip>
</q-btn>
2021-09-29 14:21:33 +08:00
<q-btn
:loading="loading"
flat
round
icon="close"
color="red"
v-close-popup
>
<q-tooltip>
{{ $t("close") }}
</q-tooltip>
</q-btn>
</div>
</div>
</q-card-section>
<q-separator />
2022-01-18 08:52:51 +08:00
<q-card-section class="scroll" style="width: 60vw">
2021-09-29 14:21:33 +08:00
<q-list>
<q-item>
<q-item-section>
<q-input
:label="$t('background color')"
v-model="subtitle.background"
2022-01-18 08:52:51 +08:00
:disable="subtitle.bakcground_transparent"
2021-09-29 14:21:33 +08:00
:rules="['anyColor']"
2022-01-18 08:52:51 +08:00
hint=""
2021-09-29 14:21:33 +08:00
>
<template v-slot:append>
<q-icon name="colorize" class="cursor-pointer">
<q-popup-proxy
transition-show="scale"
transition-hide="scale"
>
<q-color v-model="subtitle.background" />
</q-popup-proxy>
</q-icon>
</template>
</q-input>
</q-item-section>
<q-item-section>
<q-checkbox
:label="$t('background transparent')"
v-model="subtitle.bakcground_transparent"
/>
</q-item-section>
</q-item>
<q-item>
<q-item-section>
<q-select
:label="$t('speed')"
v-model="subtitle.x_speed"
:options="[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 20]"
2021-09-29 14:21:33 +08:00
></q-select>
</q-item-section>
2022-01-18 08:52:51 +08:00
<q-item-section>
<q-input
type="number"
:label="$t('y offset')"
maxlength="4"
max="2160"
min="0"
v-model="subtitle.y_offset"
/>
</q-item-section>
</q-item>
<q-item>
<q-item-section>
<q-checkbox
:label="$t('enable subtitle')"
v-model="subtitle.show"
/>
</q-item-section>
2021-09-29 14:21:33 +08:00
</q-item>
<q-separator inset />
2022-01-18 08:52:51 +08:00
<q-item ref="editor_parent" style="height: 40vh">
2021-09-29 14:21:33 +08:00
<div class="col">
<div class="row">
<div ref="wysiwyg_toolbar" class="fit ck-reset_all"></div>
</div>
<div class="row" style="height: 90%">
2021-09-29 14:21:33 +08:00
<div ref="wysiwyg_editor"></div>
</div>
</div>
</q-item>
</q-list>
</q-card-section>
<q-separator />
<q-card-actions align="right">
<q-btn
:loading="loading"
flat
:label="$t('Cancel')"
color="primary"
v-close-popup
/>
<q-btn
ref="accept"
flat
:label="$t('Accept')"
:loading="loading"
type="submit"
color="primary"
/>
</q-card-actions>
</q-form>
</q-card>
</q-dialog>
</template>
<style scoped>
/* --------- EDITOR STYLES ---------------------------------------------------------------------------------------- */
.editor__editable,
/* Classic build. */
main .ck-editor[role='application'] .ck.ck-content,
/* Decoupled document build. */
.ck.editor__editable[role='textbox'],
.ck.ck-editor__editable[role='textbox'],
/* Inline & Balloon build. */
.ck.editor[role='textbox'] {
width: 100%;
background: #fff;
font-size: 1em;
min-height: var(--ck-sample-editor-min-height);
padding: 1.5em 2em;
}
main .ck-editor[role="application"] {
overflow: auto;
}
.ck.ck-editor__editable {
background: #fff;
border: 1px solid hsl(0, 0%, 70%);
width: 100%;
}
/* Because of sidebar `position: relative`, Edge is overriding the outline of a focused editor. */
.ck.ck-editor__editable {
position: relative;
z-index: var(--ck-sample-editor-z-index);
}
.editor-container {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
position: relative;
width: 100%;
justify-content: center;
}
</style>
<script lang="ts">
import {
defineComponent,
ref,
Ref,
watch,
computed,
onMounted,
onBeforeUnmount,
2021-11-05 09:15:51 +08:00
nextTick,
2021-09-29 14:21:33 +08:00
} from "vue";
import { useStore } from "src/store";
import { useQuasar } from "quasar";
import { useI18n } from "vue-i18n";
import GlobalData from "src/common/GlobalData";
import SubtitleEntity from "src/entities/SubtitleEntity";
2021-11-05 09:15:51 +08:00
import { log } from "util";
2021-09-29 14:21:33 +08:00
export default defineComponent({
name: "ComponentSubtitleDialog",
setup() {
let $store = useStore();
let $q = useQuasar();
let $t = useI18n();
let show_dialog = ref(false);
let loading = ref(false);
let subtitle = ref(new SubtitleEntity());
let wysiwyg_editor: Ref<HTMLElement | null> = ref(null);
let wysiwyg_toolbar: Ref<HTMLElement | null> = ref(null);
2021-11-05 09:15:51 +08:00
let editor_parent: Ref<HTMLElement | null> = ref(null);
2021-09-29 14:21:33 +08:00
let editor: any = null;
const createEditor = () => {
if (!wysiwyg_editor.value) {
throw "wysiwyg_editor is null";
}
2022-01-18 08:52:51 +08:00
const fontOptions: number[] = [];
for (let i = 9; i < 500; i += 3) {
2021-11-05 09:15:51 +08:00
fontOptions.push(i);
}
2021-09-29 14:21:33 +08:00
(window as any).DecoupledDocumentEditor.create(wysiwyg_editor.value, {
fontSize: {
2021-11-05 09:15:51 +08:00
options: fontOptions,
2021-09-29 14:21:33 +08:00
},
toolbar: {
items: [
"alignment",
"|",
"fontSize",
"fontColor",
"fontBackgroundColor",
"|",
"bold",
"italic",
"underline",
"strikethrough",
"|",
2022-01-18 08:52:51 +08:00
// "numberedList",
// "bulletedList",
// "todoList",
// "|",
2021-09-29 14:21:33 +08:00
"undo",
"redo",
],
},
language: "zh-cn",
licenseKey: "",
})
.then((_editor: any) => {
editor = _editor;
wysiwyg_toolbar.value?.appendChild(editor.ui.view.toolbar.element);
document.querySelector(".ck-toolbar")?.classList.add("ck-reset_all");
})
.catch((error: any) => {
console.error("Oops, something went wrong!");
console.error(
"Please, report the following error on https://github.com/ckeditor/ckeditor5/issues with the build id and the error stack trace:"
);
console.warn("Build id: t0wgz4abajwu-875d6cqqao7");
console.error(error);
});
};
return {
show_dialog,
loading,
subtitle,
wysiwyg_editor,
wysiwyg_toolbar,
2021-11-05 09:15:51 +08:00
editor_parent,
2021-09-29 14:21:33 +08:00
async showDialog() {
show_dialog.value = true;
loading.value = true;
try {
let response = await GlobalData.getInstance()
.getCurrentClient()
?.getSubtitle();
if (response) {
subtitle.value = response.subtitle;
createEditor();
}
loading.value = false;
} catch (e) {
console.log(e);
show_dialog.value = false;
}
},
onShow() {
if (editor) {
2021-11-05 09:15:51 +08:00
setTimeout(() => {
editor.ui.view.toolbar.isCompact = false;
editor.ui.view.toolbar.maxWidth = "100%";
}, 500);
2021-09-29 14:21:33 +08:00
document.querySelectorAll(".ck-dropdown__panel").forEach((item) => {
((item as HTMLElement)?.style as any).overflow = "auto";
2021-11-05 09:15:51 +08:00
((item as HTMLElement)?.style as any).overflowX = "hidden";
((item as HTMLElement)?.style as any).height = "40vh";
item.querySelectorAll(".ck-button__label").forEach((item_) => {
((item_ as HTMLElement)?.style as any).fontSize = "20px";
});
2021-09-29 14:21:33 +08:00
});
editor.setData(subtitle.value.text);
}
},
resetData() {
loading.value = false;
},
async onSubmit() {
loading.value = true;
try {
if (!editor) {
$q.notify({
color: "negative",
icon: "warning",
message: $t.t("editor error") + "!",
position: "top",
2021-12-22 11:25:04 +08:00
timeout: 1500,
2021-09-29 14:21:33 +08:00
});
return;
}
subtitle.value.text = editor.getData();
2021-09-29 14:21:33 +08:00
GlobalData.getInstance()
.getCurrentClient()
?.setSubtitle(subtitle.value);
$q.notify({
color: "positive",
icon: "done",
message: $t.t("set subtitle success") + "!",
position: "top",
2021-12-22 11:25:04 +08:00
timeout: 1500,
2021-09-29 14:21:33 +08:00
});
show_dialog.value = false;
} catch {}
loading.value = false;
},
2021-11-05 09:15:51 +08:00
toggleFullScreen(e: any) {
console.log(e);
const target = e.target.parentNode.parentNode.parentNode;
console.log(target);
$q.fullscreen
.toggle(target)
.then(() => {})
.catch((err) => {
console.error(err);
});
},
2021-09-29 14:21:33 +08:00
};
},
});
</script>