This commit is contained in:
zoujiandong
2025-08-03 16:29:54 +08:00
parent 43abd1f3d6
commit 6e320c3944
74 changed files with 1954 additions and 974 deletions
File diff suppressed because one or more lines are too long
@@ -49,20 +49,22 @@ export function parseHtmlWithVideo(richText) {
* @returns {Promise} 转换后的富文本 注意异步处理
*/
export async function replaceVideoWithImageRender(richText, customCallback) {
console.log(1);
// 正则表达式用于匹配 <video> 标签以及其内部的 <source> 标签
const videoRegex = /<video\s+([^>]+)>(.*?)<\/video>/gi;
console.log(2);
// 找到所有的 <video> 标签
const matches = [];
let match;
while ((match = videoRegex.exec(richText)) !== null) {
matches.push(match);
}
console.log(3);
// 并行处理每个 <video> 标签,生成对应的缩略图
const replacements = await Promise.all(
matches.map(async (match) => {
console.log(5);
const [fullMatch, attributes, content] = match;
// 匹配 <source> 标签中的 src 属性
@@ -75,28 +77,34 @@ export async function replaceVideoWithImageRender(richText, customCallback) {
}
// 生成视频封面图
console.log('5-1')
let thumbnailRes
if (customCallback) thumbnailRes = await customCallback(videoUrl) // 自定义封面处理
if (!thumbnailRes) thumbnailRes = config.video_thumbnail // 无效值则默认封面处理
//if (customCallback) thumbnailRes = await customCallback(videoUrl)
console.log('5-2')
// 自定义封面处理
//if (!thumbnailRes) thumbnailRes = config.video_thumbnail // 无效值则默认封面处理
console.log('5-3')
thumbnailRes="https://caseplatform.oss-cn-beijing.aliyuncs.com/prod/static/shipinfengmian.jpg"
// 过滤掉不需要的属性,例如 controls
const filteredAttributes = attributes
.split(/\s+/)
.filter(attr => !attr.startsWith('controls'))
.join(' ');
.join(' ').replace('src=','').replace("").replaceAll('width="100%"','').replaceAll('"','').replace(/\s+/g, "");;
console.log('5-4')
// 构建新的 img 标签,继承 video 的属性(除了 controls)并添加 data-custom 属性
const imgTag = `<img ${filteredAttributes} src="${thumbnailRes}" data-custom="url=${videoUrl}" />`;
const imgTag = `<img src="${thumbnailRes}" data-custom="url=${filteredAttributes}">`;
console.log(6);
return { fullMatch, imgTag };
}));
console.log(7);
// 使用 replacements 替换原始的 <video> 标签
let result = richText;
for (const { fullMatch, imgTag } of replacements) {
console.log(8);
result = result.replace(fullMatch, imgTag);
}
console.log("打印结果2")
console.log(result)
return result;
}
@@ -307,21 +307,35 @@ export default {
* @returns {void}
*/
this.editorCtx.initHtml = async (html, customCallback) => {
console.log('打印html');
console.log(html);
let transHtml = await replaceVideoWithImageRender(html, customCallback)
console.log('transHtml')
console.log(transHtml);
// #ifdef APP || H5
this.editorCtx.changePasteMode('text') // text模式下可以防止初始化时对格式的影响
// #endif
setTimeout(() => {
this.editorCtx.setContents({
html: transHtml,
success: () => {
console.log('成功了')
// 主动触发一次input回调事件
this.editorCtx.changeInput()
// #ifdef APP || H5
if (this.pasteMode == 'origin') this.editorCtx.changePasteMode('origin')
// #endif
this.editorCtx.blur();
}
},
fail:(err)=>{
console.log('失败了')
uni.showToast({
title:err,
icon:'none'
})
}
})
})
}