Compare commits

..

2 Commits

Author SHA1 Message Date
Tim
08fe8a30c1 fix: remove config 2025-09-11 16:49:25 +08:00
Tim
6f4b17f96e feat: add webcodec compressor 2025-09-11 16:32:15 +08:00
17 changed files with 2672 additions and 116 deletions

View File

@@ -16,4 +16,4 @@ NUXT_PUBLIC_GOOGLE_CLIENT_ID=777830451304-nt8afkkap18gui4f9entcha99unal744.apps.
NUXT_PUBLIC_GITHUB_CLIENT_ID=Ov23liVkO1NPAX5JyWxJ
NUXT_PUBLIC_DISCORD_CLIENT_ID=1394985417044000779
NUXT_PUBLIC_TWITTER_CLIENT_ID=ZTRTU05KSk9KTTJrTTdrVC1tc1E6MTpjaQ
NUXT_PUBLIC_TELEGRAM_BOT_ID=8450237135
NUXT_PUBLIC_TELEGRAM_BOT_ID=8450237135

View File

@@ -356,7 +356,7 @@ body {
}
.d2h-file-name {
font-size: 14px !important;
font-size: 12px !important;
}
.d2h-file-header {
@@ -371,14 +371,14 @@ body {
padding-left: 10px !important;
}
/* .d2h-diff-table {
.d2h-diff-table {
font-size: 6px !important;
}
.d2h-code-line ins {
height: 100%;
font-size: 13px !important;
} */
}
/* .d2h-code-line {
height: 12px;

View File

@@ -35,7 +35,6 @@ const isImageIcon = (icon) => {
display: flex;
flex-direction: row;
gap: 10px;
min-height: 25px;
}
.article-info-item {
@@ -64,9 +63,5 @@ const isImageIcon = (icon) => {
.article-info-item {
font-size: 10px;
}
.article-category-container {
min-height: 20px;
}
}
</style>

View File

@@ -44,7 +44,6 @@ const isImageIcon = (icon) => {
display: flex;
flex-direction: row;
gap: 10px;
min-height: 25px;
}
.article-info-item {
@@ -73,9 +72,5 @@ const isImageIcon = (icon) => {
.article-info-item {
font-size: 10px;
}
.article-tags-container {
min-height: 20px;
}
}
</style>

View File

@@ -342,7 +342,7 @@ const copyCommentLink = () => {
const handleContentClick = (e) => {
handleMarkdownClick(e)
if (e.target.tagName === 'IMG' && !e.target.classList.contains('emoji')) {
if (e.target.tagName === 'IMG') {
const container = e.target.parentNode
const imgs = [...container.querySelectorAll('img')].map((i) => i.src)
lightboxImgs.value = imgs

View File

@@ -5,10 +5,7 @@
</div>
<div class="message-bottom-container">
<div class="message-submit" :class="{ disabled: isDisabled }" @click="submit">
<template v-if="!loading">
发送
<span class="shortcut-icon" v-if="!isMobile"> {{ isMac ? '' : 'Ctrl' }} </span>
</template>
<template v-if="!loading"> 发送 </template>
<template v-else> <loading-four /> 发送中... </template>
</div>
</div>
@@ -24,8 +21,6 @@ import {
getEditorTheme as getEditorThemeUtil,
getPreviewTheme as getPreviewThemeUtil,
} from '~/utils/vditor'
import { useIsMobile } from '~/utils/screen'
import { isMac } from '~/utils/device'
import '~/assets/global.css'
export default {
@@ -49,7 +44,6 @@ export default {
const vditorInstance = ref(null)
const text = ref('')
const editorId = ref(props.editorId)
const isMobile = useIsMobile()
if (!editorId.value) {
editorId.value = 'editor-' + useId()
}
@@ -90,28 +84,6 @@ export default {
applyTheme()
},
})
// 不是手机的情况下不添加快捷键
if (!isMobile.value) {
// 添加快捷键监听 (Ctrl+Enter 或 Cmd+Enter)
const handleKeydown = (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
e.preventDefault()
submit()
}
}
const el = document.getElementById(editorId.value)
if (el) {
el.addEventListener('keydown', handleKeydown)
}
onUnmounted(() => {
if (el) {
el.removeEventListener('keydown', handleKeydown)
}
})
}
})
onUnmounted(() => {
@@ -149,7 +121,7 @@ export default {
},
)
return { submit, isDisabled, editorId, isMac, isMobile }
return { submit, isDisabled, editorId }
},
}
</script>
@@ -196,17 +168,4 @@ export default {
.message-submit:not(.disabled):hover {
background-color: var(--primary-color-hover);
}
/** 评论按钮快捷键样式 */
.shortcut-icon {
padding: 2px 6px;
border-radius: 6px;
font-size: 12px;
font-weight: 500;
line-height: 1.2;
background-color: rgba(0, 0, 0, 0.25);
}
.comment-submit.disabled .shortcut-icon {
background-color: rgba(0, 0, 0, 0);
}
</style>

View File

@@ -1,15 +1,22 @@
/**
* 文件上传配置
* 文件上传配置 - 简化版
* 专注于 WebCodecs + MP4Box.js 视频压缩,支持 Chrome/Safari
*/
// 声明全局变量以避免 TypeScript 错误
/* global useRuntimeConfig */
export const UPLOAD_CONFIG = {
// 视频文件配置
VIDEO: {
// 文件大小限制 (字节)
MAX_SIZE: 20 * 1024 * 1024,
MAX_SIZE: 20 * 1024 * 1024, // 20mb
TARGET_SIZE: 5 * 1024 * 1024, // 5mb
// 支持的输入格式
SUPPORTED_FORMATS: ['mp4', 'webm', 'avi', 'mov', 'wmv', 'flv', 'mkv', 'm4v', 'ogv'],
// 输出格式 - MP4 (兼容性最好)
OUTPUT_FORMAT: 'mp4',
OUTPUT_CODEC: 'h264',
},
// 图片文件配置

View File

@@ -1,5 +1,4 @@
import { defineNuxtConfig } from 'nuxt/config'
export default defineNuxtConfig({
devServer: {
host: '0.0.0.0',
@@ -97,7 +96,26 @@ export default defineNuxtConfig({
},
},
vite: {
optimizeDeps: {},
build: {},
build: {
// increase warning limit and split large libraries into separate chunks
// chunkSizeWarningLimit: 1024,
// rollupOptions: {
// output: {
// manualChunks(id) {
// if (id.includes('node_modules')) {
// if (id.includes('vditor')) {
// return 'vditor'
// }
// if (id.includes('echarts')) {
// return 'echarts'
// }
// if (id.includes('highlight.js')) {
// return 'highlight'
// }
// }
// },
// },
// },
},
},
})

View File

File diff suppressed because it is too large Load Diff

View File

@@ -25,6 +25,7 @@
"ldrs": "^1.0.0",
"markdown-it": "^14.1.0",
"mermaid": "^10.9.4",
"mp4box": "^2.1.1",
"nanoid": "^5.1.5",
"nprogress": "^0.2.0",
"nuxt": "latest",

View File

@@ -594,6 +594,13 @@ const sanitizeDescription = (text) => stripMarkdown(text)
margin-bottom: 10px;
}
.article-tags-container {
display: flex;
flex-direction: row;
align-items: center;
gap: 10px;
}
.article-tag-item {
display: flex;
flex-direction: row;

View File

@@ -20,7 +20,7 @@
</div>
</div>
<div class="messages-list" ref="messagesListEl">
<div class="messages-list" ref="messagesListEl" @click="handleContentClick">
<div v-if="loading" class="loading-container">
<l-hatch size="28" stroke="4" speed="3.5" color="var(--primary-color)"></l-hatch>
</div>
@@ -50,11 +50,7 @@
<div class="reply-content" v-html="renderMarkdown(item.replyTo.content)"></div>
</div>
<div class="message-content">
<div
class="info-content-text"
v-html="renderMarkdown(item.content)"
@click="handleContentClick"
></div>
<div class="info-content-text" v-html="renderMarkdown(item.content)"></div>
</div>
<ReactionsGroup
:model-value="item.reactions"
@@ -467,7 +463,7 @@ function minimize() {
function handleContentClick(e) {
handleMarkdownClick(e)
if (e.target.tagName === 'IMG' && !e.target.classList.contains('emoji')) {
if (e.target.tagName === 'IMG') {
const container = e.target.parentNode
const imgs = [...container.querySelectorAll('img')].map((i) => i.src)
lightboxImgs.value = imgs

View File

@@ -434,7 +434,7 @@ const removeCommentFromList = (id, list) => {
const handleContentClick = (e) => {
handleMarkdownClick(e)
if (e.target.tagName === 'IMG' && !e.target.classList.contains('emoji')) {
if (e.target.tagName === 'IMG') {
const container = e.target.parentNode
const imgs = [...container.querySelectorAll('img')].map((i) => i.src)
lightboxImgs.value = imgs
@@ -445,7 +445,7 @@ const handleContentClick = (e) => {
const onCommentDeleted = (id) => {
removeCommentFromList(Number(id), comments.value)
fetchTimeline()
fetchComments()
}
const {
@@ -557,7 +557,7 @@ const postComment = async (parentUserName, text, clear) => {
if (res.ok) {
const data = await res.json()
console.debug('Post comment response data', data)
await fetchTimeline()
await fetchComments()
clear()
if (data.reward && data.reward > 0) {
toast.success(`评论成功,获得 ${data.reward} 经验值`)
@@ -612,7 +612,7 @@ const approvePost = async () => {
status.value = 'PUBLISHED'
toast.success('已通过审核')
await refreshPost()
await fetchTimeline()
await fetchChangeLogs()
} else {
toast.error('操作失败')
}
@@ -628,7 +628,7 @@ const pinPost = async () => {
if (res.ok) {
toast.success('已置顶')
await refreshPost()
await fetchTimeline()
await fetchChangeLogs()
} else {
toast.error('操作失败')
}
@@ -644,7 +644,7 @@ const unpinPost = async () => {
if (res.ok) {
toast.success('已取消置顶')
await refreshPost()
await fetchTimeline()
await fetchChangeLogs()
} else {
toast.error('操作失败')
}
@@ -660,7 +660,7 @@ const excludeRss = async () => {
if (res.ok) {
rssExcluded.value = true
toast.success('已标记为rss不推荐')
await fetchTimeline()
await fetchChangeLogs()
} else {
toast.error('操作失败')
}
@@ -676,8 +676,7 @@ const includeRss = async () => {
if (res.ok) {
rssExcluded.value = false
toast.success('已标记为rss推荐')
await refreshPost()
await fetchTimeline()
await fetchChangeLogs()
} else {
toast.error('操作失败')
}
@@ -694,7 +693,7 @@ const closePost = async () => {
closed.value = true
toast.success('已关闭')
await refreshPost()
await fetchTimeline()
await fetchChangeLogs()
} else {
toast.error('操作失败')
}
@@ -711,7 +710,7 @@ const reopenPost = async () => {
closed.value = false
toast.success('已重新打开')
await refreshPost()
await fetchTimeline()
await fetchChangeLogs()
} else {
toast.error('操作失败')
}
@@ -756,7 +755,7 @@ const rejectPost = async () => {
status.value = 'REJECTED'
toast.success('已驳回')
await refreshPost()
await fetchTimeline()
await fetchChangeLogs()
} else {
toast.error('操作失败')
}

View File

@@ -1,28 +0,0 @@
export const isClient = typeof window !== 'undefined' && typeof document !== 'undefined'
export const isMac = getIsMac()
function getIsMac() {
if (!isClient) {
return false
}
try {
// 优先使用现代浏览器的 navigator.userAgentData API
if (navigator.userAgentData && navigator.userAgentData.platform) {
return navigator.userAgentData.platform === 'macOS'
}
// 降级到传统的 User-Agent 检测
if (navigator.userAgent) {
return /Mac|iPhone|iPad|iPod/i.test(navigator.userAgent)
}
// 默认返回false
return false
} catch (error) {
// 异常处理,记录错误并返回默认值
console.warn('检测Mac设备时发生错误:', error)
return false
}
}

View File

@@ -3,7 +3,8 @@ import { getToken, authState } from './auth'
import { searchUsers, fetchFollowings, fetchAdmins } from './user'
import { tiebaEmoji } from './tiebaEmoji'
import vditorPostCitation from './vditorPostCitation.js'
import { checkFileSize, formatFileSize } from './videoCompressor.js'
import { checkFileSize, formatFileSize, compressVideo, VIDEO_CONFIG } from './videoCompressor.js'
import { UPLOAD_CONFIG } from '../config/uploadConfig.js'
export function getEditorTheme() {
return document.documentElement.dataset.theme === 'dark' ? 'dark' : 'classic'
@@ -94,6 +95,7 @@ export function createVditor(editorId, options = {}) {
const file = files[0]
const ext = file.name.split('.').pop().toLowerCase()
const videoExts = ['mp4', 'webm', 'avi', 'mov', 'wmv', 'flv', 'mkv', 'm4v', 'ogv']
const isVideo = videoExts.includes(ext)
// 检查文件大小
const sizeCheck = checkFileSize(file)
@@ -111,10 +113,61 @@ export function createVditor(editorId, options = {}) {
return '文件过大'
}
let processedFile = file
// 如果是视频文件且需要压缩
if (isVideo && sizeCheck.needsCompression) {
try {
vditor.tip('视频压缩中...', 0)
vditor.disabled()
// 使用 WebCodecs 压缩视频
processedFile = await compressVideo(file, (progress) => {
const messages = {
initializing: '初始化编码器',
preparing: '准备压缩',
analyzing: '分析视频',
compressing: '压缩中',
finalizing: '完成压缩',
completed: '压缩完成',
}
const message = messages[progress.stage] || progress.stage
vditor.tip(`${message} ${progress.progress}%`, 0)
})
const originalSize = formatFileSize(file.size)
const compressedSize = formatFileSize(processedFile.size)
const savings = Math.round((1 - processedFile.size / file.size) * 100)
vditor.tip(`压缩完成!${originalSize}${compressedSize} (节省 ${savings}%)`, 2000)
// 压缩成功但仍然超过最大限制,则阻止上传
if (processedFile.size > VIDEO_CONFIG.MAX_SIZE) {
vditor.tip(
`压缩后仍超过限制 (${formatFileSize(VIDEO_CONFIG.MAX_SIZE)}). 请降低分辨率或码率后再上传。`,
4000,
)
vditor.enable()
return '压缩后仍超过大小限制'
}
} catch (error) {
// 压缩失败时,如果原文件超过最大限制,则阻止上传
if (file.size > VIDEO_CONFIG.MAX_SIZE) {
vditor.tip(
`视频压缩失败,且文件超过限制 (${formatFileSize(VIDEO_CONFIG.MAX_SIZE)}). 请先压缩后再上传。`,
4000,
)
vditor.enable()
return '视频压缩失败且文件过大'
}
vditor.tip('视频压缩失败,将尝试上传原文件', 3000)
processedFile = file
}
}
vditor.tip('文件上传中', 0)
vditor.disabled()
const res = await fetch(
`${API_BASE_URL}/api/upload/presign?filename=${encodeURIComponent(file.name)}`,
`${API_BASE_URL}/api/upload/presign?filename=${encodeURIComponent(processedFile.name)}`,
{ headers: { Authorization: `Bearer ${getToken()}` } },
)
if (!res.ok) {
@@ -123,7 +176,7 @@ export function createVditor(editorId, options = {}) {
return '获取上传地址失败'
}
const info = await res.json()
const put = await fetch(info.uploadUrl, { method: 'PUT', body: file })
const put = await fetch(info.uploadUrl, { method: 'PUT', body: processedFile })
if (!put.ok) {
vditor.enable()
vditor.tip('上传失败')

View File

@@ -1,8 +1,10 @@
/**
* 视频上传工具
* 基于 WebCodecs + MP4Box.js 的视频压缩工具
* 专为现代浏览器 (Chrome/Safari) 优化
*/
import { UPLOAD_CONFIG } from '../config/uploadConfig.js'
import { compressVideoWithWebCodecs, isWebCodecSupported } from './webcodecVideoCompressor.js'
// 导出配置供外部使用
export const VIDEO_CONFIG = UPLOAD_CONFIG.VIDEO
@@ -15,6 +17,7 @@ export function checkFileSize(file) {
isValid: file.size <= VIDEO_CONFIG.MAX_SIZE,
actualSize: file.size,
maxSize: VIDEO_CONFIG.MAX_SIZE,
needsCompression: file.size > VIDEO_CONFIG.TARGET_SIZE,
}
}
@@ -28,3 +31,42 @@ export function formatFileSize(bytes) {
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]
}
/**
* 压缩视频文件 - 使用 WebCodecs
*/
export async function compressVideo(file, onProgress = () => {}) {
// 检查是否需要压缩
const sizeCheck = checkFileSize(file)
if (!sizeCheck.needsCompression) {
onProgress({ stage: 'completed', progress: 100 })
return file
}
// 检查 WebCodecs 支持
if (!isWebCodecSupported()) {
throw new Error('当前浏览器不支持视频压缩功能,请使用支持 WebCodecs 的浏览器')
}
try {
return await compressVideoWithWebCodecs(file, { onProgress })
} catch (error) {
console.error('WebCodecs 压缩失败:', error)
throw new Error(`视频压缩失败: ${error.message}`)
}
}
/**
* 预加载 WebCodecs可选的性能优化
*/
export async function preloadVideoCompressor() {
try {
if (!isWebCodecSupported()) {
throw new Error('当前浏览器不支持 WebCodecs')
}
return { success: true, message: 'WebCodecs 已就绪' }
} catch (error) {
console.warn('WebCodecs 检测失败:', error)
return { success: false, error: error.message }
}
}

View File

@@ -0,0 +1,98 @@
import MP4Box from 'mp4box'
// 检查 WebCodecs 支持
export function isWebCodecSupported() {
return typeof window !== 'undefined' && typeof window.VideoEncoder !== 'undefined'
}
// 使用 WebCodecs + MP4Box.js 压缩视频
export async function compressVideoWithWebCodecs(file, opts = {}) {
const { onProgress = () => {}, width = 720, bitrate = 1_000_000 } = opts
if (!isWebCodecSupported()) {
throw new Error('当前浏览器不支持 WebCodecs')
}
onProgress({ stage: 'initializing', progress: 0 })
// 加载原始视频
const url = URL.createObjectURL(file)
const video = document.createElement('video')
video.src = url
video.muted = true
await video.play().catch(() => {})
video.pause()
await new Promise((resolve) => {
if (video.readyState >= 2) resolve()
else video.onloadedmetadata = () => resolve()
})
const targetWidth = width
const targetHeight = Math.round((video.videoHeight / video.videoWidth) * width)
const canvas = document.createElement('canvas')
canvas.width = targetWidth
canvas.height = targetHeight
const ctx = canvas.getContext('2d')
const chunks = []
const encoder = new VideoEncoder({
output: (chunk) => {
chunks.push(chunk)
},
error: (e) => {
throw e
},
})
encoder.configure({
codec: 'avc1.42001E',
width: targetWidth,
height: targetHeight,
bitrate,
framerate: 30,
})
const duration = video.duration
const frameCount = Math.floor(duration * 30)
for (let i = 0; i < frameCount; i++) {
video.currentTime = i / 30
await new Promise((res) => (video.onseeked = res))
ctx.drawImage(video, 0, 0, targetWidth, targetHeight)
const bitmap = await createImageBitmap(canvas)
const frame = new VideoFrame(bitmap, { timestamp: (i / 30) * 1000000 })
encoder.encode(frame)
frame.close()
bitmap.close()
onProgress({ stage: 'compressing', progress: Math.round(((i + 1) / frameCount) * 80) })
}
await encoder.flush()
onProgress({ stage: 'finalizing', progress: 90 })
const mp4box = MP4Box.createFile()
const track = mp4box.addTrack({
timescale: 1000,
width: targetWidth,
height: targetHeight,
})
let dts = 0
chunks.forEach((chunk) => {
const data = new Uint8Array(chunk.byteLength)
chunk.copyTo(data)
mp4box.addSample(track, data.buffer, {
duration: chunk.duration ? chunk.duration / 1000 : 33,
dts,
cts: dts,
is_sync: chunk.type === 'key',
})
dts += chunk.duration ? chunk.duration / 1000 : 33
})
const arrayBuffer = mp4box.flush()
const outputFile = new File([arrayBuffer], file.name.replace(/\.[^.]+$/, '.mp4'), {
type: 'video/mp4',
})
onProgress({ stage: 'completed', progress: 100 })
URL.revokeObjectURL(url)
return outputFile
}