当前位置:网站首页>Wechat applet does not use plug-ins, rendering videos in rich text, image adaptation, plus version
Wechat applet does not use plug-ins, rendering videos in rich text, image adaptation, plus version
2022-07-25 16:22:00 【Bitter summer Bluegrass】
Applet native rich-text Rendering video is not supported , So it needs to be displayed after processing , It mainly uses string cutting and matching
The problem of rich text image adaptation
When we get rich text data from the backend , We make use of replace Method to modify the style of the picture , Make sure the picture is consistent with the width of the phone
//richtext Is rich text
let content = richtext.replace(/<img/gi, '<img style="max-width:100%;height:auto"') // Picture adaptation
If there are inexplicable blank areas in rich text , It may be that the rich text contains a newline label , We will <br> Label replaced
//richtext For the rich text passed from the back end
dealRichText(richtext) {
let content = richtext.replace(/<img/gi, '<img style="max-width:100%;height:auto"').replace(/<br\/>/g, '');
return content;
}
Rendering video
There are also many ways online , But my rich text is generated by Baidu rich text editor , It's a little complicated , That's true :
<p>
Casual words
</p>
<p>
<img src="https://weiqing.kuxia.top/uploads/20220720/0fa5869614f227c88c7c89737836814c.png" title="" alt=""/>
</p>
<p>
<video class="edui-upload-video vjs-default-skin video-js" controls="" preload="none" width="420" height="280" src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" data-setup="{}">
<source src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" type="video/mp4"/>
</video>
<video class="edui-upload-video vjs-default-skin video-js" controls="" preload="none" width="420" height="280" src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" data-setup="{}">
<source src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" type="video/mp4"/>
</video>
</p>
<p style="white-space: normal;">
Casual words
</p>
<p style="white-space: normal;">
<img src="https://xxx.xxx.top/uploads/20220720/0fa5869614f227c88c7c89737836814c.png" title="" alt=""/>
</p>
<p style="white-space: normal;">
<video class="edui-upload-video vjs-default-skin video-js" controls="" preload="none" width="420" height="280" src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" data-setup="{}">
<source src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" type="video/mp4"/>
</video>
<video class="edui-upload-video vjs-default-skin video-js" controls="" preload="none" width="420" height="280" src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" data-setup="{}">
<source src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" type="video/mp4"/>
</video>
</p>
<p>
<br/>
</p>
This is my casual call , The video link has also changed ( Project confidentiality ), Structurally, each p A tag is a paragraph , There may be multiple videos in a paragraph , Treatment scheme :
1、 Handle
dealRichText(richtext) {
let content = richtext.replace(/<img/gi, '<img style="max-width:100%;height:auto"').replace(/<br\/>/g, ''); // Replace the width and height of the picture
let textArr = content.split(/<p/); // Split the string by paragraph
let contentArr = [];
// Prevent one or more videos , For cutting
textArr.forEach(val => {
let arr = val.split(/<video/)
contentArr.push(...arr)
})
let newArr = []; // Processed array
contentArr.forEach((val, i) => {
if (val != '' && val != ">") {
// Prevent empty strings
let matchResult = val.match(/(?<= class=\"edui-upload-video).+(?=\<\/video\>)/); // Whether there is video in the matching string
if (matchResult) {
let a = matchResult[0].match(/(?<=src=\").+(?=\" data)/); // Match video links
newArr.push({
type: 'video',
content: a[0]
})
} else {
newArr.push({
type: 'text',
content: "<p" + val
})
}
}
})
return newArr;
},
The final result ( Array ):
0: {
type: "text", content: "<p> Casual words </p>"}
1: {
type: "text", content: "<p><img style="max-width:100%;height:auto" src="ht…f227c88c7c89737836814c.png" title="" alt=""/></p>"}
2: {
type: "video", content: "https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4"}
3: {
type: "video", content: "https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4"}
4: {
type: "text", content: "<p style="white-space: normal;"> Casual words </p>"}
5: {
type: "text", content: "<p style="white-space: normal;"><img style="max-wi…f227c88c7c89737836814c.png" title="" alt=""/></p>"}
6: {
type: "text", content: "<p style="white-space: normal;">"}
7: {
type: "video", content: "https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4"}
8: {
type: "video", content: "https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4"}
9: {
type: "text", content: "<p></p>"}
There are still some minor flaws in the data , But it doesn't affect the use
2、 Rendering
<!-- config.remark It's the last data , That is, the array above -->
<view wx:for="{
{config.remark}}" wx:key="index">
<rich-text nodes="{
{item.content}}" wx:if="{
{item.type == 'text'}}"></rich-text>
<video src="{
{item.content}}" wx:if="{
{item.type == 'video'}}" style="width: 100%;" controls></video>
</view>
边栏推荐
- The presentation logic of mail sending and receiving inbox outbox and reply to the problem of broken chain
- 如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?
- I interviewed 8 companies and got 5 offers in a week. Share my experience
- 只有1000元能买什么理财产品赚钱?
- MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
- 2W word detailed data Lake: concept, characteristics, architecture and cases
- MySQL tutorial 68-as setting alias
- MyBaits
- [Shakespeare: keep the fun of being a man]
- 02. 将参数props限制在一个类型的列表中
猜你喜欢

How to build an enterprise level OLAP data engine for massive data and high real-time requirements?

Crazy God redis notes 12

Product upgrade observation station in June

MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具

Product dynamics - Android 13 high-efficiency adaptation new upgrade

吴恩达逻辑回归2

Is the win11 dynamic tile gone? Method of restoring dynamic tile in Win 11

tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框

Emqx cloud update: more parameters are added to log analysis, which makes monitoring, operation and maintenance easier

终极套娃 2.0 | 云原生交付的封装
随机推荐
C Music
聊聊如何用 Redis 实现分布式锁?
2W word detailed data Lake: concept, characteristics, architecture and cases
C# 模拟抽奖
从业务需求出发,开启IDC高效运维之路
Test Driven Development (TDD) online practice room | classes open on September 17
[Shakespeare: keep the fun of being a man]
Breakthrough in core technology of the large humanoid Service Robot Walker x
High score technical document sharing of ink Sky Wheel - Database Security (48 in total)
伦敦银K线图的各种有用形态
Leetcode:528. select randomly according to the weight [ordinary random failure + prefix and dichotomy]
Emqx cloud update: more parameters are added to log analysis, which makes monitoring, operation and maintenance easier
使用 Terraform 在 AWS 上快速部署 MQTT 集群
02. Limit the parameter props to a list of types
leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】
Waterfall flow layout
Verifiable random function VRF
mysql 隔离级别事务
MySQL isolation level transactions
Mysql读写锁