Showing
2 changed files
with
42 additions
and
5 deletions
| @@ -114,6 +114,7 @@ export struct SearchComponent { | @@ -114,6 +114,7 @@ export struct SearchComponent { | ||
| 114 | console.log(TAG, JSON.stringify(err)) | 114 | console.log(TAG, JSON.stringify(err)) |
| 115 | }) | 115 | }) |
| 116 | } | 116 | } |
| 117 | + // console.log(TAG, 'this.relatedSearchContentsData', JSON.stringify(this.relatedSearchContentsData)) | ||
| 117 | } | 118 | } |
| 118 | 119 | ||
| 119 | getSearchHint() { | 120 | getSearchHint() { |
| 1 | import { SearchRelatedItem } from '../../viewmodel/SearchRelatedItem' | 1 | import { SearchRelatedItem } from '../../viewmodel/SearchRelatedItem' |
| 2 | +import { SearchShowRed, titleInitRes, textItem } from '../../utils/searchShowRed'; | ||
| 2 | 3 | ||
| 3 | const TAG = "SearchRelatedComponent" | 4 | const TAG = "SearchRelatedComponent" |
| 4 | 5 | ||
| @@ -12,6 +13,23 @@ export struct SearchRelatedComponent { | @@ -12,6 +13,23 @@ export struct SearchRelatedComponent { | ||
| 12 | @Prop searchText: string | 13 | @Prop searchText: string |
| 13 | @Prop percent:number = 1 | 14 | @Prop percent:number = 1 |
| 14 | 15 | ||
| 16 | + // 正则过滤检索词 | ||
| 17 | + createCaseInsensitiveRegex(str: string, pattern: string) { | ||
| 18 | + let regex = new RegExp(pattern, 'gi'); | ||
| 19 | + return str.replace(regex, (match) => `<em>${match}</em>`); | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + checkForPattern(str: string, pattern: string) { | ||
| 23 | + let highlightedStr = this.createCaseInsensitiveRegex(str, pattern); | ||
| 24 | + return highlightedStr; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + titleInit(str: string, pattern: string) { | ||
| 28 | + const title = this.checkForPattern(str, pattern) | ||
| 29 | + const titleInitRes:titleInitRes = SearchShowRed.titleInit(title) | ||
| 30 | + return titleInitRes | ||
| 31 | + } | ||
| 32 | + | ||
| 15 | build() { | 33 | build() { |
| 16 | Column() { | 34 | Column() { |
| 17 | List() { | 35 | List() { |
| @@ -28,11 +46,29 @@ export struct SearchRelatedComponent { | @@ -28,11 +46,29 @@ export struct SearchRelatedComponent { | ||
| 28 | 46 | ||
| 29 | Text(){ | 47 | Text(){ |
| 30 | ForEach(item.data_arr,(item:string)=>{ | 48 | ForEach(item.data_arr,(item:string)=>{ |
| 31 | - Span(item) | ||
| 32 | - .fontColor(item===this.searchText?$r('app.color.color_ED2800'):$r('app.color.color_000000')) | ||
| 33 | - .fontSize(`${this.calcHeight(31)}lpx`) | ||
| 34 | - .fontWeight(400) | ||
| 35 | - .lineHeight(`${this.calcHeight(50)}lpx`) | 49 | + if (this.titleInit(item, this.searchText).titleMarked) { |
| 50 | + ForEach(this.titleInit(item, this.searchText).textArr, (textItem: textItem) => { | ||
| 51 | + if (textItem.isRed) { | ||
| 52 | + Span(textItem.content) | ||
| 53 | + .fontColor($r('app.color.color_ED2800')) | ||
| 54 | + .fontSize(`${this.calcHeight(31)}lpx`) | ||
| 55 | + .fontWeight(400) | ||
| 56 | + .lineHeight(`${this.calcHeight(50)}lpx`) | ||
| 57 | + } else { | ||
| 58 | + Span(textItem.content) | ||
| 59 | + .fontColor($r('app.color.color_000000')) | ||
| 60 | + .fontSize(`${this.calcHeight(31)}lpx`) | ||
| 61 | + .fontWeight(400) | ||
| 62 | + .lineHeight(`${this.calcHeight(50)}lpx`) | ||
| 63 | + } | ||
| 64 | + }) | ||
| 65 | + } else { | ||
| 66 | + Span(item) | ||
| 67 | + .fontColor($r('app.color.color_000000')) | ||
| 68 | + .fontSize(`${this.calcHeight(31)}lpx`) | ||
| 69 | + .fontWeight(400) | ||
| 70 | + .lineHeight(`${this.calcHeight(50)}lpx`) | ||
| 71 | + } | ||
| 36 | }) | 72 | }) |
| 37 | } | 73 | } |
| 38 | .maxLines(1) | 74 | .maxLines(1) |
-
Please register or login to post a comment