Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion entry/src/main/ets/pages/V2/Index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ struct Index {
<del>del标签</del>
<big>big标签</big>
<small>small标签</small>
<a href="http://www.baidu.com">a标签</a>
<div><a href="http://www.baidu.com">我是超链接:默认的a标签</a></div>
<div><a href="http://www.baidu.com" style="color:orange">我是超链接:内部设置文字颜色的a标签</a></div>
<span>带换行符号或者空格符号的文本:
【0乳糖,特添优质冷榨生椰浆】使用IIAC大赛金奖咖啡豆,现萃香醇Espresso,遇见鲜椰冷榨生椰浆,椰香浓郁,香甜清爽,带给你不一样的拿铁体验!\r\n\r\n主要原料:浓缩咖啡、冷冻椰浆/椰浆饮品、原味调味糖浆\r\n\r\n图片及包装仅供参考,请以实物为准。\r\n尽快享用风味更佳哦~
</span>
Expand All @@ -46,10 +47,18 @@ struct Index {
<img width="500px" resource="1" src="app.media.icon" />
<video height="500px" loop muted autoplay src="https://www.w3school.com.cn/i/video/shanghai.mp4" />
`,

imageProp: {
webp: true,
copyEnable: true
},
alinkProp: {
linkColor: "#e62828",
decoration: {
color: "#447dbd",
type: 'None'
}
},
}
this.richTextModel.needScroll = true;
this.richTextModel.onLinkPress = (e) => {
Expand Down
2 changes: 2 additions & 0 deletions library/index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export type {
HeadingStyle,
HtmlParserResult,
ImageProp,
ALinkProp,
ALinkDecorationProp,
NodeInfo,
Nullable,
SimpleNode,
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/ets/common/types/consants.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export interface SpecialStyles {
a: {
decoration: {
type: TextDecorationType;
color: Color;
color: Color | string;
};
} & BaseFontAttrs;
video: {
Expand Down
16 changes: 16 additions & 0 deletions library/src/main/ets/common/types/htmlParser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ export interface ImageProp {
copyEnable?: boolean;
}


export interface ALinkDecorationProp {
/*a标签下划线的格式*/
type?: 'None' | 'Underline' | 'Overline' | 'LineThrough';
/*a标签下划线的颜色*/
color?: string;
}


export interface ALinkProp {
/*a标签链接的默认颜色*/
linkColor?: string;
/*a标签下划线的格式*/
decoration?: ALinkDecorationProp
}

export interface CustomHandler {
start: Function;
end: Nullable<Function>;
Expand Down
19 changes: 12 additions & 7 deletions library/src/main/ets/common/utils/css/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Color, FontStyle, FontWeight, TextAlign, TextDecorationType, Visibility } from '../../types/artUIEnum';
import type { HeadingStyle, SpecialStyles } from '../../types/consants';
import type { ArtStyleObject, NodeInfo, StyleObject } from '../../types/htmlParser';
import type { ALinkProp, ArtStyleObject, NodeInfo, StyleObject } from '../../types/htmlParser';
import { attrEnums, attrsMap, specialAttrsMap } from './constants';

/**
Expand Down Expand Up @@ -157,17 +157,18 @@ export function excludeExtendsParentArtUIStyle(style?: ArtStyleObject, node?: No
/**
* @description: 设置HTML标签的初始样式
* @param {string} tagName 标签
* @param {ALinkProp} alinkProp a标签的属性设置
* @returns {*}
*/
export function setHtmlAttributes(baseFontSize: number, baseFontColor: string, tagName?: string,) {
export function setHtmlAttributes(baseFontSize: number, baseFontColor: string, alinkProp: ALinkProp, tagName?: string) {
if (!tagName) {
return {};
}
// 使用对象映射查找并返回对应标签的样式
const predefinedStyle =
(headingStyles(baseFontSize as number, baseFontColor as string)[tagName] ||
specialStyles(baseFontSize as number, baseFontColor as string)[tagName]) ||
{ fontSize: baseFontSize, fontColor: baseFontColor };
specialStyles(baseFontSize as number, baseFontColor as string, alinkProp)[tagName]) ||
{ fontSize: baseFontSize, fontColor: baseFontColor};
return predefinedStyle;
}

Expand Down Expand Up @@ -234,7 +235,7 @@ export function headingStyles(baseFontSize: number, baseFontColor: string): Head
* @param {number} baseFontSize 基准字体大小
* @returns {*} SpecialStyles
*/
export function specialStyles(baseFontSize: number, baseFontColor: string): SpecialStyles {
export function specialStyles(baseFontSize: number, baseFontColor: string, alinkProp: ALinkProp): SpecialStyles {
const baseStyles: SpecialStyles = {
b: { fontWeight: FontWeight.Bold, fontColor: baseFontColor, fontSize: baseFontSize },
strong: { fontWeight: FontWeight.Bold, fontColor: baseFontColor, fontSize: baseFontSize },
Expand Down Expand Up @@ -274,8 +275,12 @@ export function specialStyles(baseFontSize: number, baseFontColor: string): Spec
strike: { decoration: { type: TextDecorationType.LineThrough }, fontColor: baseFontColor, fontSize: baseFontSize },
del: { decoration: { type: TextDecorationType.LineThrough }, fontColor: baseFontColor, fontSize: baseFontSize },
a: {
fontColor: Color.Blue,
decoration: { type: TextDecorationType.Underline, color: Color.Blue },
fontColor: alinkProp.linkColor ?? Color.Blue,
decoration: { type: alinkProp.decoration?.type === 'None' ? TextDecorationType.None :
alinkProp.decoration?.type === 'Underline' ? TextDecorationType.Underline :
alinkProp.decoration?.type === 'Overline' ? TextDecorationType.Overline :
alinkProp.decoration?.type === 'LineThrough' ? TextDecorationType.LineThrough :
TextDecorationType.Underline, color: alinkProp.decoration?.color ?? alinkProp.linkColor ?? Color.Blue },
fontSize: baseFontSize
},
video: {
Expand Down
17 changes: 16 additions & 1 deletion library/src/main/ets/common/utils/html/html-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { PixelUnit, RichTextOption } from '../../../components/hprichtext/i
import type { Resource } from '../../types/artUIBase';
import { Color, ImageFit } from '../../types/artUIEnum';
import type {
ALinkProp,
Attribute,
CustomHandler,
HtmlParserResult,
Expand Down Expand Up @@ -56,6 +57,17 @@ const defaultImageProp: ImageProp = {
webp: false
};

/*默认自定义的alink配置*/
const defaultAlinkProp: ALinkProp = {
/*a标签链接的默认颜色*/
linkColor: Color.Blue,
/*a标签下划线的格式*/
decoration: {
type: 'Underline',
color: Color.Blue
}
}

class HTMLParser {
public results: HtmlParserResult = {
nodes: [],
Expand All @@ -64,6 +76,7 @@ class HTMLParser {
private bufArray: NodeInfo[] = [];
private customHandler: CustomHandler = defaultCustomHandler;
private imageProp: ImageProp = defaultImageProp;
private alinkProp: ALinkProp = defaultAlinkProp;
private html: string = '';
private baseFontSize: number | Resource = 16;
private basePixelUnit: PixelUnit = 'vp';
Expand All @@ -75,6 +88,7 @@ class HTMLParser {
{
customHandler,
imageProp,
alinkProp,
baseFontSize,
basePixelUnit,
basePixelRatio,
Expand All @@ -83,6 +97,7 @@ class HTMLParser {
}: RichTextOption) {
customHandler && (this.customHandler = customHandler);
imageProp && Object.assign(this.imageProp, imageProp);
alinkProp && Object.assign(this.alinkProp, alinkProp);
baseFontSize && (this.baseFontSize = baseFontSize);
basePixelUnit && (this.basePixelUnit = basePixelUnit);
basePixelRatio && (this.basePixelRatio = basePixelRatio);
Expand Down Expand Up @@ -202,7 +217,7 @@ class HTMLParser {

// 子节点继承父节点样式(需要排除不需要继承的样式)
let htmlStyles = {};
htmlStyles = setHtmlAttributes(this.baseFontSize as number, this.baseFontColor as string, node.tag);
htmlStyles = setHtmlAttributes(this.baseFontSize as number, this.baseFontColor as string, this.alinkProp ,node.tag);

// 整合父标签过滤之后的标签默认样式+可继承样式+自身style样式【顺序很重要】
node.artUIStyleObject =
Expand Down
3 changes: 2 additions & 1 deletion library/src/main/ets/components/hprichtext/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FontAttr, PositionAttr, Resource, ResourceColor, ShapeAttr, } from '../../common/types/artUIBase';
import { Color } from '../../common/types/artUIEnum';

import type { CustomHandler, ImageProp, NodeInfo } from '../../common/types/htmlParser';
import type { ALinkProp, CustomHandler, ImageProp, NodeInfo } from '../../common/types/htmlParser';


export interface TextBuilderOptions {
Expand Down Expand Up @@ -90,6 +90,7 @@ export interface RichTextOption {
basePixelUnit?: PixelUnit;
basePixelRatio?: number | Resource;
imageProp?: ImageProp;
alinkProp?: ALinkProp;
customHandler?: CustomHandler;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CustomHandler, ImageProp } from '../../../common/types';
import type { ALinkProp, CustomHandler, ImageProp } from '../../../common/types';
import type { CopyPressMethod, LinkPressMethod, PixelUnit } from '../index';

export class RichTextOption {
Expand All @@ -8,6 +8,7 @@ export class RichTextOption {
basePixelUnit?: PixelUnit;
basePixelRatio?: number | Resource;
imageProp?: ImageProp;
alinkProp?: ALinkProp;
customHandler?: CustomHandler;
}

Expand Down