Warning, /graphics/arianna/src/content/ui/SearchModel.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: LGPL-2.1-only or LGPL-3.0-only or LicenseRef-KDE-Accepted-LGPL
0003 
0004 import QtQuick 2.15
0005 
0006 ListModel {
0007     id: root
0008 
0009     property bool loading: false
0010 
0011     signal searchTriggered(text: string)
0012 
0013     function search(text) {
0014         loading = false;
0015         searchTriggered(text);
0016     }
0017 
0018     function resultFound(query, results) {
0019         clear();
0020         loading = false;
0021 
0022         const markupEscape = text => text ? text.replace(/&/g, "&amp;")
0023             .replace(/</g, "&lt;")
0024             .replace(/>/g, "&gt;") : '';
0025         const regexEscape = str => str ? str.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&') : '';
0026         const regex = new RegExp(regexEscape(query), 'ig');
0027         results.forEach(({ cfi, excerpt, section }) => {
0028             const text = markupEscape(excerpt.trim().replace(/\n/g, ' '));
0029             const markup = text.replace(regex, `<strong>${regex.exec(text)[0]}</strong>`);
0030             const sectionMarkup = `<span alpha="50%" size="smaller">${
0031                 markupEscape(section)}</span>`
0032 
0033             root.append({
0034                 cfi: cfi,
0035                 markup: markup,
0036                 sectionMarkup: sectionMarkup
0037             });
0038         });
0039     }
0040 }