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 util from '@ohos.util'
19
20const TAG = "SearchModel"
21
22/**
23 * Search service class
24 */
25export class SearchModel {
26  private rdbStore;
27
28  /**
29   * Search
30   *
31   * @param query - query content
32   */
33  public async search(noteDataArray: NoteData[], query: string): Promise<NoteData[]> {
34    LogUtil.info(TAG, "query is " + query)
35    if (!query) {
36      LogUtil.info(TAG, "query is null")
37      return []
38    }
39    let searchData: NoteData[] = [];
40    noteDataArray.forEach((noteData) => {
41      let base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/
42      let contentTextValue: string
43      if (base64regex.test(noteData.content_text) && noteData.content_text.length > 0) {
44        let Base64 = new util.Base64()
45        let textDecoder = new util.TextDecoder("utf-8", { ignoreBOM: true }) // utf-8:编码格式为utf-8,ignoreBOM:是否忽略BOM标记
46        let decodeStr = Base64.decodeSync(noteData.content_text)
47        contentTextValue = textDecoder.decode(decodeStr, { stream: false }) // stream:在随后的decode()调用中是否跟随附加数据块
48      } else {
49        contentTextValue = noteData.content_text
50      }
51      if (contentTextValue.replace(/<[^>]+>/g, "").toLowerCase().indexOf(query.toLowerCase()) != -1
52      || noteData.title.toLowerCase().indexOf(query.toLowerCase()) != -1) {
53        LogUtil.info(TAG, "uuid " + noteData.uuid)
54        searchData.push(noteData);
55      }
56    })
57    // 排序
58    return searchData;
59  }
60
61  splitToHighlightText(text: string, highlightKeyword): any[] {
62    let spans: any[] = []
63    var lowerSpans: string[] = text.toLowerCase().split(highlightKeyword.toLowerCase())
64    var keywordStartIndex = 0
65    var keywordLength = highlightKeyword.length
66
67    for (var i = 0; i < lowerSpans.length; i++) {
68      var normalText = text.substr(keywordStartIndex, lowerSpans[i].length)
69      spans.push({
70        type: 0,
71        text: normalText
72      })
73      // if not at last, append highlight keyword
74      if (i != lowerSpans.length - 1) {
75        keywordStartIndex += lowerSpans[i].length
76        var keywordText = text.substr(keywordStartIndex, keywordLength)
77        spans.push({
78          type: 1,
79          text: keywordText
80        })
81        keywordStartIndex += keywordLength
82      }
83    }
84
85    return spans
86  }
87}
88
89let searchModel = new SearchModel()
90
91export default searchModel as SearchModel