feat: |Doc| use shadow DOM render mail html (#604)

This commit is contained in:
Dream Hunter
2025-03-08 10:53:45 +08:00
committed by GitHub
parent 97d24b2087
commit 908fc0cc86
13 changed files with 1086 additions and 971 deletions

View File

@@ -7,6 +7,7 @@ import { CloudDownloadRound, ReplyFilled, ForwardFilled } from '@vicons/material
import { useIsMobile } from '../utils/composables'
import { processItem, getDownloadEmlUrl } from '../utils/email-parser'
import { utcToLocalDate } from '../utils';
import ShadowHtmlComponent from "./ShadowHtmlComponent.vue";
const message = useMessage()
const isMobile = useIsMobile()
@@ -171,7 +172,7 @@ const refresh = async () => {
}
};
const backFirstPageAndRefresh = async () =>{
const backFirstPageAndRefresh = async () => {
page.value = 1;
await refresh();
}
@@ -380,7 +381,7 @@ onBeforeUnmount(() => {
<n-split class="left" direction="horizontal" :max="0.75" :min="0.25" :default-size="mailboxSplitSize"
:on-update:size="onSpiltSizeChange">
<template #1>
<div style="overflow: auto; height: 80vh;">
<div style="overflow: auto; min-height: 50vh; max-height: 100vh;">
<n-list hoverable clickable>
<n-list-item v-for="row in data" v-bind:key="row.id" @click="() => clickRow(row)"
:class="mailItemClass(row)">
@@ -396,10 +397,14 @@ onBeforeUnmount(() => {
{{ utcToLocalDate(row.created_at, useUTCDate) }}
</n-tag>
<n-tag type="info">
FROM: {{ row.source }}
<n-ellipsis style="max-width: 240px;">
{{ showEMailTo ? "FROM: " + row.source : row.source }}
</n-ellipsis>
</n-tag>
<n-tag v-if="showEMailTo" type="info">
TO: {{ row.address }}
<n-ellipsis style="max-width: 240px;">
TO: {{ row.address }}
</n-ellipsis>
</n-tag>
</template>
</n-thing>
@@ -460,7 +465,7 @@ onBeforeUnmount(() => {
<iframe v-else-if="useIframeShowMail" :srcdoc="curMail.message"
style="margin-top: 10px;width: 100%; height: 100%;">
</iframe>
<div v-else v-html="curMail.message" style="margin-top: 10px;"></div>
<ShadowHtmlComponent v-else :htmlContent="curMail.message" style="margin-top: 10px;" />
</n-card>
<n-card :bordered="false" embedded class="mail-item" v-else>
<n-result status="info" :title="t('pleaseSelectMail')">
@@ -498,7 +503,7 @@ onBeforeUnmount(() => {
{{ utcToLocalDate(row.created_at, useUTCDate) }}
</n-tag>
<n-tag type="info">
FROM: {{ row.source }}
{{ showEMailTo ? "FROM: " + row.source : row.source }}
</n-tag>
<n-tag v-if="showEMailTo" type="info">
TO: {{ row.address }}
@@ -560,7 +565,7 @@ onBeforeUnmount(() => {
<iframe v-else-if="useIframeShowMail" :srcdoc="curMail.message"
style="margin-top: 10px;width: 100%; height: 100%;">
</iframe>
<div v-else v-html="curMail.message" style="margin-top: 10px;"></div>
<ShadowHtmlComponent :key="curMail.id" v-else :htmlContent="curMail.message" style="margin-top: 10px;" />
</n-card>
</n-drawer-content>
</n-drawer>

View File

@@ -0,0 +1,75 @@
<template>
<div v-if="useFallback" v-html="htmlContent"></div>
<div v-else ref="shadowHost"></div>
</template>
<script setup>
import { ref, watch, onMounted, onBeforeUnmount, defineProps } from 'vue';
const props = defineProps({
htmlContent: {
type: String,
required: true,
},
});
const shadowHost = ref(null);
let shadowRoot = null;
const useFallback = ref(false);
/**
* Renders content into Shadow DOM with fallback to v-html
*/
const renderShadowDom = () => {
if (!shadowHost.value && !useFallback.value) return;
try {
// Don't attempt to use Shadow DOM if already in fallback mode
if (useFallback.value) return;
// Initialize Shadow DOM if not already created
if (!shadowRoot && shadowHost.value) {
try {
shadowRoot = shadowHost.value.attachShadow({ mode: 'open' });
} catch (error) {
console.warn('Shadow DOM not supported, falling back to v-html:', error);
useFallback.value = true;
return;
}
}
// Update content if Shadow DOM exists
if (shadowRoot) {
shadowRoot.innerHTML = props.htmlContent;
}
} catch (error) {
console.error('Failed to render Shadow DOM, falling back to v-html:', error);
useFallback.value = true;
}
};
// Initial render when component is mounted
onMounted(() => {
// Check if Shadow DOM is supported in this browser
if (typeof Element.prototype.attachShadow !== 'function') {
console.warn('Shadow DOM is not supported in this browser, using v-html fallback');
useFallback.value = true;
return;
}
renderShadowDom();
});
// Clean up resources when component is unmounted
onBeforeUnmount(() => {
if (shadowRoot) {
shadowRoot.innerHTML = '';
}
shadowRoot = null;
});
// Update Shadow DOM when htmlContent changes
watch(() => props.htmlContent, () => {
renderShadowDom();
}, { flush: 'post' });
</script>