当前位置:网站首页>How to quickly design a set of cross end components that support rendering rich text content
How to quickly design a set of cross end components that support rendering rich text content
2022-07-29 02:25:00 【Ink fragrance^_^】
background
I wonder if you have ever met this kind of demand , Configure a section of rich text in the management background html Content , At the beginning , You only need to support rendering to the browser , But as the company's business changes , May need to be used on applets , Even ios perhaps android And so on
Render to h5 Or browser like ( Ignore xss Safety issues such as )
In general, the framework is directly used , similar
vue: vue-html
react: dangeroushtml
How to render to applet side
Because the applet uses its own unique tag , for example view label , But the background configuration is html Text string , How to handle html become view, Many people don't know how to do it , I think this is impossible , It is the product that raises unreasonable demands , See here , You can think about it first , If it's you , What would you do ?
Let's talk about my thinking process
taro
Because I was using taro Write applet , Simplify requirements to understand , Just need to put div label Render into a view label , How to change ? I thought of babel, because taro It supports cross end , The principle of cross end is to use babel Make an abstract syntax tree , Become the language effect you want

Look at the picture above , Everything is bright , But there are two fatal problems
babel It's static parsing , Rich text requirements are calculated by the code runtime , Contrary to
babel The bag is too big
Later, it was directly rejected babel
At this time, let's abstract it , The original requirement is simplified div become view , Let's simplify it a little bit div Become virtual dom , fictitious dom become view
html Turn it into virtual DOM
jquery
At this time, I thought of jquery A grammar of ,jquery Of $(' a section html') Can handle html become dom, And then a meal of operation to turn jq Source code , Not satisfied , shortcoming :
dom Too many structures are attached jq Useless properties ,
html There are some contents jq The limitations of the framework
vue、react
Look over vue、react Some of the source code
vue: vue-template-compiler
react: babel-loader Will be able to elemennt Turn it into createElement
shortcoming :
Resolved virtual dom There are still many limitations of the framework itself
Do you want to write a ? The need is urgent , It is impossible to write by yourself
babel
The essence is how div Become a clean virtual dom Node ? Finally, I can't go around and return to the origin .babel, By looking at the source code, I found a noun called html-parse Open it directly github search , Found many libraries , But most of them are experiments or demo Level
html-parse-stringify[1]
Finally, I chose this library
advantage :
Light weightCompatibility is good. , Many attribute descriptions can matchGenerated virtual dom Not dependent on the framework 、 platform , Namely json Text
var HTML = require('html-parse-stringify')
// this html:
var html = '<div class="oh"><p>hi</p></div>'
// becomes this AST:
var ast = HTML.parse(html)
console.log(ast)
/*
{
// can be `tag`, `text` or `component`
type: 'tag',
// name of tag if relevant
name: 'div',
// parsed attribute object
attrs: {
class: 'oh'
},
// whether this is a self-closing tag
// such as <img/>
voidElement: false,
// an array of child nodes
// we see the same structure
// repeated in each of these
children: [
{
type: 'tag',
name: 'p',
attrs: {},
voidElement: false,
children: [
// this is a text node
// it also has a `type`
// but nothing other than
// a `content` containing
// its text.
{
type: 'text',
content: 'hi'
}
]
}
]
}
*/
fictitious DOM Convert to other end code
With virtual dom, The applet side can traverse through depth first , Virtual layer by layer dom Render into view label The implementation is similar to the following code , The same is true for other terminals
Use
<View className="index">
<TaroRichText
htmls={` Access content <span class="highlight"><a appid="495ec7a9-a3e8-42ad-a1ee-f14b8e0af1e3" pagepath="pages/renderer/renderer"> Activity online reservation registration Lala </a></span>
`}
/>
</View>
Core source code
export default function Tree(props: IProps) {
const { doms } = props;
console.log(doms);
const { children = [] } = doms || {};
return (
<View>
// Depending on the type , Simulate different styles , adopt class simulation
{doms.type === 'text' && <View>{doms.content}</View>}
{/* <View>{ Other types , Write your own function rendering }</View> */}
<View>
{children.length &&
children.map(dom => {
return <Tree doms={dom} />;
})}
</View>
</View>
);
}边栏推荐
- QT learning notes -37.qregex and regular expressions
- TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(2)
- 自定义mvc原理和框架实现
- 多边形点测试
- 2022.7.27-----leetcode.592
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 2, sign in question GJK
- 响应式织梦模板酒店客房类网站
- webview攻击
- Resolve the conflict with vetur when using eslint, resulting in double quotation marks and comma at the end of saving
- "Activity recommendation" rush rush! 2022 international open source Festival has new content
猜你喜欢

Custom MVC principle and framework implementation

MySQL stores JSON format data

响应式织梦模板户外露营类网站

"Activity recommendation" rush rush! 2022 international open source Festival has new content

Responsive dream weaving template hotel room website

On Multithreading

基于C51实现数码管的显示

In 2022, the official data of programming language ranking came, which was an eye opener

Chapter 3 business function development (deletion and modification of clue remarks)

QT qstackedwidget multi interface switching
随机推荐
Data query of MySQL (multi table query)
2022.7.27-----leetcode.592
如果非要在多线程中使用 ArrayList 会发生什么?
[RT learning note 1] RT thread peripheral routine - control LED light flashing
[one · data | chained binary tree]
ES6事件绑定(v-on用法)
Derivation of Euler angle differential equation
In 2022, the official data of programming language ranking came, which was an eye opener
费曼学习法(符号表)
What should I do if excel opens a CSV file containing Chinese characters and there is garbled code?
年中总结 | 与自己对话,活在当下,每走一步都算数
How to customize a new tab in Duoyu security browser?
响应式织梦模板化妆美妆类网站
Experiment 2: Arduino's tricolor lamp experiment
基于对象的实时空间音频渲染丨Dev for Dev 专栏
基于C51实现数码管的显示
What is scope and scope chain
Full solution of 3D model format | including more than 70 kinds of RVT, 3ds, DWG, FBX, IFC, osgb, obj, etc
Quanzhi t3/a40i industrial core board, 4-core [email protected] The localization rate reaches 100%
Cookies and sessions