当前位置: 首页 > news >正文

网站的相关性 实用性网站开发综合实训总结

网站的相关性 实用性,网站开发综合实训总结,色块布局网站首页模板,汕尾网站建设 生意好吗需求 支持文本插入,比如 xxx {product_name} xxx ,如果提供了product_name变量的值为feedback,则可以渲染出 xxx feedback xxx。支持链接解析,比如 [baidu](https://www.baidu.com/),可以直接渲染成超链接的形式。支持…

需求

  • 支持文本插入,比如 xxx {product_name} xxx ,如果提供了product_name变量的值为feedback,则可以渲染出 xxx feedback xxx
  • 支持链接解析,比如 [baidu](https://www.baidu.com/),可以直接渲染成超链接的形式。
  • 支持插入reactnode元素,比如 xxx {jump_node} xxx,且jump_node是一个reactnode元素,则可以将node节点插入到{}位置上。

实现

步骤:

  • 先解析链接, 返回这样子的数据结构。超链接返回url-text的对象,非超链接直接返回文本字符串
export interface LinkPart {text: string;url?: string;onClick?: string;
}export type ParsedTextPart = string | LinkPart;[{text: 'baidu',url: 'https://www.baidu.com/',},'other content','other content',
];
  • 遍历解析后的超链接数组,如果是对象,则渲染超链接;如果是字符串,继续解析
  • 解析字符串,判断需要解析的{}里面的文本是否是纯文本,如果是纯文本,则直接Text渲染;如果是react元素,则渲染该元素

TextTemplate.tsx:

import React, { ReactNode } from 'react';
import { routeCenter } from '@shopeepay-rn/route-center';
import { usePageContainerContext } from '@shopeepay-rn/page-container';
import { StyleProp, Text, TextStyle, View, ViewStyle } from 'react-native';
import { parseLinkText } from '../../utils';
import styles from './styles';interface Props {template: string;// eg: {product_name:'spp', click_node:<Text></Text>}replaceValueMap: Record<string, string | number | ReactNode>;textStyle?: StyleProp<TextStyle>;containerStyle?: StyleProp<ViewStyle>;
}/*** 支持解析字符串、解析react元素、解析超链接* @param template 需要解析的字符串* @param replaceValueMap 需要替换的key-value,value支持字符串和react元素* @param textStyle 字体样式* @param containerStyle 容器样式* @returns react元素*/
export const TextTemplate = ({template,replaceValueMap,textStyle,containerStyle,
}: Props) => {const { rootTag } = usePageContainerContext();const parseText = (text: string, index: number) => {const result: React.ReactNode[] = [];let lastIndex = 0;text.replace(/{(\w+)}/g, (match: string, key: string, offset: number) => {const replaceValue = replaceValueMap[key];if (offset > lastIndex) {// 未被匹配到的result.push(<Text key={index} style={textStyle}>{text.substring(lastIndex, offset)}</Text>);}if (React.isValidElement(replaceValue)) {// 需要替换的是reactnode元素result.push(React.cloneElement(replaceValue, { key: index }));} else if (typeof replaceValue === 'string') {// 需要替换的是字符串result.push(<Text key={index} style={textStyle}>{replaceValue}</Text>);}lastIndex = offset + match.length;return '';});if (lastIndex < text.length) {result.push(<Text key={index} style={textStyle}>{text.substring(lastIndex)}</Text>);}return result;};const parseTemplate = (text: string) => {// 解析链接const linkTexts = parseLinkText(text);return linkTexts?.map((part, index) => {return typeof part === 'string' ? (// 对于字符串,需要解析 纯字符串 还是 reactnode元素parseText(part, index)) : (<Textkey={index}style={styles.link}onPress={() =>routeCenter.navigateWeb(part.url || '',{navbar: {title: part.text || '',},},rootTag)}>{part.text}</Text>);});};return (<View style={[styles.textView, containerStyle]}><Text>{parseTemplate(template)}</Text></View>);
};

parseLinkText.ts:

export interface LinkPart {text: string;url?: string;onClick?: string;
}export type ParsedTextPart = string | LinkPart;const parseLinkText = (text: string): ParsedTextPart[] => {const regex = /\[([^\]]+)\]\(([^)]+)\)/g;const parts: ParsedTextPart[] = [];let lastIndex = 0;text.replace(regex,(match: string, p1: string, p2: string, offset: number) => {if (offset > lastIndex) {parts.push(text.substring(lastIndex, offset));}parts.push({ text: p1, url: p2 });lastIndex = offset + match.length;return '';});if (lastIndex < text.length) {parts.push(text.substring(lastIndex));}return parts;
};// FIXME: 添加 unit test
export { parseLinkText };

使用

<TextTemplatetemplate={"you can test the TextTemplate component, parse {string_text}, parse [baidu](https://www.baidu.com/) link, parse {click_node} to show popup"}replaceValueMap={{string_text:"test string",click_node:<Text>other react node</Text>}}textStyle={styles.titleText}
/>
http://www.yayakq.cn/news/571275/

相关文章:

  • html5手机网站调用微信分享英文网站怎么设置中文
  • 湖南做网站 找磐石网络一流公司需要网站 该怎么做
  • 湘潭学校网站建设 磐石网络第一室内设计用什么软件比较好
  • 苏州网站建设推广服务柯桥建设集团网站
  • 宁夏建设局网站衡阳县做淘宝网站建设
  • 网站改版 降权汝州建设局网站
  • 网站可以做动态背景吗wordpress 加入地图
  • 移动端网站开发与网页开发区别什么是网络营销的漏斗原理
  • php网站开发工程师找工作广州网站推广排名
  • 网站设计制作策划wordpress去掉自豪
  • 做电商网站电商公司WordPress 简单的问答
  • 网站模块删除品牌建设网站
  • 网络架构方案书周口网站优化
  • 建设企业网站注意事项wordpress商城多重分类
  • 银川市建设诚信平台网站下载中国移动app免费下载安装
  • 涿州规划建设局网站廊坊seo排名优化网站
  • ( )是网站可以提供给用户的价值wordpress 关闭rss
  • 合肥做网站价格wordpress登陆代码
  • 微信生活门户网站源码珠海网站制作费用
  • 免费做网站模板在哪里做上海企业一户式查询
  • 个人网站 论坛好听顺口的装修公司名字
  • 大学网站栏目建设常州市做网站
  • 无锡网站建设wuxi8878哪些是网站建设
  • 做棋牌网站一般多少钱在线做试卷的网站
  • 外贸网站平台下载网站建设企业蛋糕
  • 深圳做网站的公司 cheungdomwordpress段落开头空两格
  • wordpress图片站iis创建网站
  • 昆山网站建设kshuituo陌上香坊是做盗版的网站吗
  • 网站分为几种媒介星软文平台官网
  • 网站开发后端框架什么意思wordpress开发视频教程