zhenghy

Merge remote-tracking branch 'origin/main'

@@ -9,8 +9,11 @@ import { onlyWifiLoadImg } from '../../utils/lazyloadImg'; @@ -9,8 +9,11 @@ import { onlyWifiLoadImg } from '../../utils/lazyloadImg';
9 import { persistentStorage, hasClicked } from '../../utils/persistentStorage'; 9 import { persistentStorage, hasClicked } from '../../utils/persistentStorage';
10 import { InfomationCardClick } from '../../utils/infomationCardClick'; 10 import { InfomationCardClick } from '../../utils/infomationCardClick';
11 11
12 -const TAG: string = 'Card2Component';  
13 - 12 +const TAG: string = 'Card2Component'
  13 +interface textItem {
  14 + content: string,
  15 + isRed: boolean
  16 +}
14 /** 17 /**
15 * 大图卡: 18 * 大图卡:
16 * compstyle:2 19 * compstyle:2
@@ -27,9 +30,7 @@ export struct Card2Component { @@ -27,9 +30,7 @@ export struct Card2Component {
27 @State clicked: boolean = false; 30 @State clicked: boolean = false;
28 @ObjectLink compDTO: CompDTO 31 @ObjectLink compDTO: CompDTO
29 @State titleMarked: boolean = false; 32 @State titleMarked: boolean = false;
30 - @State str01: string = '';  
31 - @State str02: string = '';  
32 - @State str03: string = ''; 33 + @State textArr: textItem[] = []
33 34
34 async aboutToAppear(): Promise<void> { 35 async aboutToAppear(): Promise<void> {
35 this.clicked = hasClicked(this.contentDTO.objectId) 36 this.clicked = hasClicked(this.contentDTO.objectId)
@@ -40,12 +41,45 @@ export struct Card2Component { @@ -40,12 +41,45 @@ export struct Card2Component {
40 titleInit() { 41 titleInit() {
41 if (this.contentDTO.title?.includes('<em>') && this.contentDTO.title?.includes('</em>')) { 42 if (this.contentDTO.title?.includes('<em>') && this.contentDTO.title?.includes('</em>')) {
42 this.titleMarked = true; 43 this.titleMarked = true;
43 - this.str01 = this.contentDTO.title?.split('<em>')[0] || '';  
44 - this.str02 = this.contentDTO.title?.split('<em>')[1].split('</em>')[0] || '';  
45 - this.str03 = this.contentDTO.title?.split('<em>')[1].split('</em>')[1] || ''; 44 + // this.str01 = this.contentDTO.title?.split('<em>')[0] || '';
  45 + // this.str02 = this.contentDTO.title?.split('<em>')[1].split('</em>')[0] || '';
  46 + // this.str03 = this.contentDTO.title?.split('<em>')[1].split('</em>')[1] || '';
  47 +
  48 + const html: string = this.contentDTO.title;
  49 + const pattern = /<[a-z]+[1-6]?\b[^>]*>(.*?)<\/[a-z]+[1-6]?>/g;
  50 + const res: string[] = [];
  51 + let match: RegExpExecArray | null;
  52 +
  53 + while ((match = pattern.exec(html)) !== null) {
  54 + const content: string = match[1].trim();
  55 + res.push(content);
  56 + }
  57 +
  58 + let textArr: textItem[] = [];
  59 + this.formatTitle(html, res, 0, textArr);
  60 + this.textArr = textArr;
46 } 61 }
47 } 62 }
48 63
  64 + formatTitle(textStr: string, matchArr: string[], index: number, textArr: textItem[]) {
  65 + const item: string = matchArr[index];
  66 + const arr = textStr.split(item);
  67 + arr.forEach((str: string, ind: number) => {
  68 + if (ind === 0) {
  69 + textArr.push({
  70 + content: str.replaceAll('<em>', '').replaceAll('</em>', ''),
  71 + isRed: false
  72 + } as textItem)
  73 + textArr.push({
  74 + content: item,
  75 + isRed: true
  76 + })
  77 + } else if (ind === 1) {
  78 + this.formatTitle(str, matchArr, index + 1, textArr)
  79 + }
  80 + })
  81 + }
  82 +
49 build() { 83 build() {
50 Column() { 84 Column() {
51 Column() { 85 Column() {
@@ -85,10 +119,18 @@ export struct Card2Component { @@ -85,10 +119,18 @@ export struct Card2Component {
85 119
86 Text() { 120 Text() {
87 if (this.titleMarked) { 121 if (this.titleMarked) {
88 - Span(this.str01)  
89 - Span(this.str02)  
90 - .fontColor(0xED2800)  
91 - Span(this.str03) 122 + // Span(this.str01)
  123 + // Span(this.str02)
  124 + // .fontColor(0xED2800)
  125 + // Span(this.str03)
  126 + ForEach(this.textArr, (textItem: textItem) => {
  127 + if (textItem.isRed) {
  128 + Span(textItem.content)
  129 + .fontColor(0xED2800)
  130 + } else {
  131 + Span(textItem.content)
  132 + }
  133 + })
92 } else { 134 } else {
93 Span(this.contentDTO.newsTitle) 135 Span(this.contentDTO.newsTitle)
94 } 136 }