แก้ไข align center ของ video

This commit is contained in:
AnandaTon 2024-07-04 16:16:29 +07:00
parent 0470184363
commit 38cf193b7c

View file

@ -1,277 +1,263 @@
<script lang="ts" setup> <script lang="ts" setup>
import { nextTick, onMounted, onUnmounted, ref } from "vue"; import { nextTick, onMounted, onUnmounted, ref } from "vue"
import { useRoute } from "vue-router"; import { useRoute } from "vue-router"
import MarkdownIt, { type Token } from "markdown-it"; import MarkdownIt, { type Token } from "markdown-it"
// @ts-expect-error // @ts-expect-error
import mditFigureWithPCaption from "markdown-it-image-figures"; import mditFigureWithPCaption from "markdown-it-image-figures"
import mditAnchor from "markdown-it-anchor"; import mditAnchor from "markdown-it-anchor"
import { useManualStore } from "@/stores/manual"; import { useManualStore } from "@/stores/manual"
import { storeToRefs } from "pinia"; import { storeToRefs } from "pinia"
// @ts-expect-error // @ts-expect-error
import mditVideo from "markdown-it-video"; import mditVideo from "markdown-it-video"
const md = new MarkdownIt() const md = new MarkdownIt()
.use(mditAnchor) .use(mditAnchor)
.use(mditVideo) .use(mditVideo, {
.use(mditFigureWithPCaption, { youtube: { width: "50%", height: "auto", autoplay: false },
figcaption: "alt", })
html: true, .use(mditFigureWithPCaption, {
linkify: true, figcaption: "alt",
typography: true, html: true,
}); linkify: true,
typography: true,
})
const route = useRoute(); const route = useRoute()
const manual = useManualStore(); const manual = useManualStore()
const { toc } = storeToRefs(manual); const { toc } = storeToRefs(manual)
const text = ref(""); const text = ref("")
const parsed = ref<Token[]>([]); const parsed = ref<Token[]>([])
const chapter = ref(0); const chapter = ref(0)
const found = ref(false); const found = ref(false)
const active = ref(""); const active = ref("")
function onScroll() { function onScroll() {
let current = ""; let current = ""
document.querySelectorAll<HTMLElement>("h2,h3").forEach((v) => { document.querySelectorAll<HTMLElement>("h2,h3").forEach((v) => {
if ( if (window.top && window.top.scrollY + window.innerHeight / 2 > v.offsetTop) {
window.top && current = v.id
window.top.scrollY + window.innerHeight / 2 > v.offsetTop }
) { })
current = v.id; active.value = current
}
});
active.value = current;
} }
function onResize() { function onResize() {
if (window.innerWidth > 1024) { if (window.innerWidth > 1024) {
toc.value = true; toc.value = true
} }
} }
async function scrollTo(id: string) { async function scrollTo(id: string) {
const pos = document.getElementById(id)?.offsetTop; const pos = document.getElementById(id)?.offsetTop
await nextTick(() => { await nextTick(() => {
if (window.innerWidth < 1024) toc.value = false; if (window.innerWidth < 1024) toc.value = false
}); })
if (pos) if (pos)
window.scrollTo({ window.scrollTo({
top: pos, top: pos,
behavior: "smooth", behavior: "smooth",
}); })
} }
onMounted(async () => { onMounted(async () => {
window.addEventListener("scroll", onScroll); window.addEventListener("scroll", onScroll)
window.addEventListener("resize", onResize); window.addEventListener("resize", onResize)
if (typeof route.params.name === "string") { if (typeof route.params.name === "string") {
const res = await fetch(`/documents/${route.params.name}.md`); const res = await fetch(`/documents/${route.params.name}.md`)
if (res && res.ok) { if (res && res.ok) {
const pattern = /chapter-(\d+)/; const pattern = /chapter-(\d+)/
const match = pattern.exec(route.params.name); const match = pattern.exec(route.params.name)
if (!match) return; if (!match) return
text.value = await res.text(); text.value = await res.text()
found.value = true; found.value = true
chapter.value = +match[1] - 1; chapter.value = +match[1] - 1
parsed.value = md.parse(text.value, {}); parsed.value = md.parse(text.value, {})
} }
} }
}); })
onUnmounted(() => { onUnmounted(() => {
window.removeEventListener("scroll", onScroll); window.removeEventListener("scroll", onScroll)
window.removeEventListener("resize", onResize); window.removeEventListener("resize", onResize)
}); })
</script> </script>
<template> <template>
<div v-if="found" style="display: flex" class="markdown"> <div v-if="found" style="display: flex" class="markdown">
<div <div style="width: 100%; flex: 1; background-color: white; border-radius: 0.5rem; border: 1px solid #e1e1e9; padding: 1rem" v-html="md.render(text.replaceAll('images/', '/images/'))"></div>
style=" </div>
width: 100%;
flex: 1;
background-color: white;
border-radius: 0.5rem;
border: 1px solid #e1e1e9;
padding: 1rem;
"
v-html="md.render(text.replaceAll('images/', '/images/'))"
></div>
</div>
<q-drawer <q-drawer v-if="toc" side="right" class="bg-grey-2" show-if-above v-model="toc" :width="250" :behavior="$q.screen.width > 1024 ? 'desktop' : 'mobile'">
v-if="toc" <q-scroll-area class="fit">
side="right" <q-list padding>
class="bg-grey-2" <template v-for="(token, idx) in parsed">
show-if-above <q-item
v-model="toc" class="tabNative"
:width="250" active-class="text-blue-7 active-item text-weight-medium tabActive"
:behavior="$q.screen.width > 1024 ? 'desktop' : 'mobile'" :active="active === token.attrGet('id')"
> @click="scrollTo(token.attrGet('id') || '')"
<q-scroll-area class="fit"> v-if="token.tag === 'h2' && token.type === 'heading_open'"
<q-list padding> clickable
<template v-for="(token, idx) in parsed"> v-ripple
<q-item dense
class="tabNative" exact
active-class="text-blue-7 active-item text-weight-medium tabActive" >
:active="active === token.attrGet('id')" <q-item-section>
@click="scrollTo(token.attrGet('id') || '')" <q-item-label>
v-if="token.tag === 'h2' && token.type === 'heading_open'" <q-icon size="11px" name="mdi-circle-medium" />
clickable <span class="q-pl-xs">{{ parsed[idx + 1].content }}</span>
v-ripple </q-item-label>
dense </q-item-section>
exact </q-item>
> <q-item
<q-item-section> v-if="token.tag === 'h3' && token.type === 'heading_open'"
<q-item-label> class="tabNative child-tab"
<q-icon size="11px" name="mdi-circle-medium" /> active-class="text-blue-7 active-item text-weight-medium tabActive"
<span class="q-pl-xs">{{ parsed[idx + 1].content }}</span> :active="active === token.attrGet('id')"
</q-item-label> @click="scrollTo(token.attrGet('id') || '')"
</q-item-section> clickable
</q-item> v-ripple
<q-item dense
v-if="token.tag === 'h3' && token.type === 'heading_open'" exact
class="tabNative child-tab" >
active-class="text-blue-7 active-item text-weight-medium tabActive" <q-item-section>
:active="active === token.attrGet('id')" <q-item-label>
@click="scrollTo(token.attrGet('id') || '')" <span class="q-pl-xl">{{ parsed[idx + 1].content }}</span>
clickable </q-item-label>
v-ripple </q-item-section>
dense </q-item>
exact </template>
> </q-list>
<q-item-section> </q-scroll-area>
<q-item-label> </q-drawer>
<span class="q-pl-xl">{{ parsed[idx + 1].content }}</span>
</q-item-label>
</q-item-section>
</q-item>
</template>
</q-list>
</q-scroll-area>
</q-drawer>
</template> </template>
<style scoped> <style scoped>
.toc { .toc {
top: 4rem; top: 4rem;
} }
.active { .active {
color: red; color: red;
} }
.markdown { .markdown {
counter-set: h1 v-bind(chapter); counter-set: h1 v-bind(chapter);
counter-reset: h1; counter-reset: h1;
} }
.markdown :deep(:where(h1, h2, h3, h4, h5, h6)) { .markdown :deep(:where(h1, h2, h3, h4, h5, h6)) {
line-height: 1.5; line-height: 1.5;
padding-block: 1rem !important; padding-block: 1rem !important;
} }
.markdown :deep(h1) { .markdown :deep(h1) {
counter-reset: h2; counter-reset: h2;
} }
.markdown :deep(h2) { .markdown :deep(h2) {
counter-reset: h3; counter-reset: h3;
} }
.markdown :deep(h3) { .markdown :deep(h3) {
counter-reset: h4; counter-reset: h4;
} }
.markdown :deep(h1:before) { .markdown :deep(h1:before) {
counter-increment: h1; counter-increment: h1;
content: counter(h1) ". "; content: counter(h1) ". ";
} }
.markdown :deep(h2:before) { .markdown :deep(h2:before) {
counter-increment: h2; counter-increment: h2;
content: counter(h1) "." counter(h2) " "; content: counter(h1) "." counter(h2) " ";
} }
.markdown :deep(h3:before) { .markdown :deep(h3:before) {
counter-increment: h3; counter-increment: h3;
content: counter(h1) "." counter(h2) "." counter(h3) " "; content: counter(h1) "." counter(h2) "." counter(h3) " ";
} }
.markdown :deep(h4:before) { .markdown :deep(h4:before) {
counter-increment: h4; counter-increment: h4;
content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) " "; content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) " ";
} }
.markdown :deep(blockquote) { .markdown :deep(blockquote) {
background-color: #ecebeb; background-color: #ecebeb;
border-radius: 8px; border-radius: 8px;
padding: 8px; padding: 8px;
margin-bottom: 1rem; margin-bottom: 1rem;
} }
.markdown :deep(blockquote > p:last-child) { .markdown :deep(blockquote > p:last-child) {
margin-bottom: 0; margin-bottom: 0;
} }
.markdown :deep(img) { .markdown :deep(img) {
vertical-align: middle; vertical-align: middle;
} }
.markdown :deep(p img) { .markdown :deep(p img) {
padding-inline: 0.25rem; padding-inline: 0.25rem;
} }
.markdown :deep(figure) { .markdown :deep(figure) {
margin: 0; margin: 0;
text-align: center; text-align: center;
padding: 1rem; padding: 1rem;
width: 100%; width: 100%;
} }
.markdown :deep(figure img) { .markdown :deep(figure img) {
max-width: 90%; max-width: 90%;
} }
.markdown :deep(p:has(img:only-child) img) { .markdown :deep(p:has(img:only-child) img) {
max-width: 100%; max-width: 100%;
} }
.markdown :deep(.abc) { .markdown :deep(.abc) {
color: green; color: green;
} }
.markdown :deep(h1) { .markdown :deep(h1) {
text-align: left; text-align: left;
margin-top: -1rem; margin-top: -1rem;
margin-inline: -1rem; margin-inline: -1rem;
font-size: 24px; font-size: 24px;
font-weight: 700; font-weight: 700;
background-color: #ecebeb; background-color: #ecebeb;
border-radius: 8px 8px 0px 0px; border-radius: 8px 8px 0px 0px;
padding: 0px 16px; padding: 0px 16px;
} }
.markdown :deep(figcaption) { .markdown :deep(figcaption) {
text-align: center; text-align: center;
} }
.markdown :deep(h2) { .markdown :deep(h2) {
text-align: left; text-align: left;
margin-block: 0; margin-block: 0;
font-size: 18px; font-size: 18px;
font-weight: 600; font-weight: 600;
padding: 0px 16px; padding: 0px 16px;
color: #02a998; color: #02a998;
} }
.markdown :deep(h3) { .markdown :deep(h3) {
text-align: left; text-align: left;
margin-block: 0; margin-block: 0;
font-size: 16px; font-size: 16px;
font-weight: 600; font-weight: 600;
padding: 0px 16px; padding: 0px 16px;
color: #02a998; color: #02a998;
}
.markdown :deep(iframe) {
display: block;
margin: 0 auto;
height: 50vh; /* Adjust this value as needed */
} }
</style> </style>