1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import {LogUtil} from '../../baseUtil/LogUtil' 17import NoteData from '../../model/databaseModel/NoteData' 18import ohosDataRdb from '@ohos.data.rdb'; 19import {TableName, NoteTableColumn} from '../../model/databaseModel/EnumData' 20import NoteUtil from '../../baseUtil/NoteUtil' 21import RdbStoreUtil from '../../baseUtil/RdbStoreUtil' 22import SysDefData from '../../model/databaseModel/SysDefData' 23import util from '@ohos.util' 24 25const TAG = "SearchModel" 26 27/** 28 * Search service class 29 */ 30export class SearchModel { 31 private rdbStore; 32 33 /** 34 * Search 35 * 36 * @param query - query content 37 */ 38 public async search(noteDataArray: NoteData[], query: string): Promise<NoteData[]> { 39 LogUtil.info(TAG, "query is " + query) 40 if (!query) { 41 LogUtil.info(TAG, "query is null") 42 return [] 43 } 44 let searchData: NoteData[] = []; 45 noteDataArray.forEach((noteData) => { 46 let base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/ 47 let contentTextValue: string 48 if (base64regex.test(noteData.content_text)) { 49 let Base64 = new util.Base64() 50 let textDecoder = new util.TextDecoder("utf-8", { ignoreBOM: true }) // utf-8:编码格式为utf-8,ignoreBOM:是否忽略BOM标记 51 let decodeStr = Base64.decodeSync(noteData.content_text) 52 contentTextValue = textDecoder.decode(decodeStr, { stream: false }) // stream:在随后的decode()调用中是否跟随附加数据块 53 } else { 54 contentTextValue = noteData.content_text 55 } 56 if (contentTextValue.replace(/<[^>]+>/g, "").toLowerCase().indexOf(query.toLowerCase()) != -1 57 || noteData.title.toLowerCase().indexOf(query.toLowerCase()) != -1) { 58 LogUtil.info(TAG, "uuid " + noteData.uuid) 59 searchData.push(noteData); 60 } 61 }) 62 // 排序 63 return searchData; 64 } 65 66 splitToHighlightText(text: string, highlightKeyword): any[] { 67 let spans: any[] = [] 68 var lowerSpans: string[] = text.toLowerCase().split(highlightKeyword.toLowerCase()) 69 var keywordStartIndex = 0 70 var keywordLength = highlightKeyword.length 71 72 for (var i = 0; i < lowerSpans.length; i++) { 73 var normalText = text.substr(keywordStartIndex, lowerSpans[i].length) 74 spans.push({ 75 type: 0, 76 text: normalText 77 }) 78 // if not at last, append highlight keyword 79 if (i != lowerSpans.length - 1) { 80 keywordStartIndex += lowerSpans[i].length 81 var keywordText = text.substr(keywordStartIndex, keywordLength) 82 spans.push({ 83 type: 1, 84 text: keywordText 85 }) 86 keywordStartIndex += keywordLength 87 } 88 } 89 90 return spans 91 } 92} 93 94let searchModel = new SearchModel() 95 96export default searchModel as SearchModel