Files: 1a326ad6e229f48d252a5d8e4e8dd65a803f4d58 / app / page / search.js
2078 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Struct, Value } = require('mutant') |
3 | const pull = require('pull-stream') |
4 | const next = require('pull-next-query') |
5 | const Scroller = require('pull-scroll') |
6 | const TextNodeSearcher = require('text-node-searcher') |
7 | |
8 | exports.gives = nest('app.page.search') |
9 | |
10 | exports.needs = nest({ |
11 | 'app.html.filter': 'first', |
12 | 'app.html.scroller': 'first', |
13 | 'message.html.render': 'first', |
14 | 'sbot.pull.log': 'first', |
15 | 'sbot.pull.stream': 'first' |
16 | }) |
17 | |
18 | var whitespace = /\s+/ |
19 | |
20 | exports.create = function (api) { |
21 | return nest('app.page.search', searchPage) |
22 | |
23 | function searchPage (location) { |
24 | const query = location.query.trim() |
25 | |
26 | const search = Struct({ |
27 | fulltext: Struct({ |
28 | isDone: Value(false) |
29 | }), |
30 | matches: Value(0) |
31 | }) |
32 | |
33 | const searchHeader = h('Search', [ |
34 | h('header', h('h1', query)) |
35 | ]) |
36 | const { filterMenu, filterDownThrough, resetFeed } = api.app.html.filter(draw) |
37 | const { container, content } = api.app.html.scroller({ prepend: [searchHeader, filterMenu] }) |
38 | |
39 | function renderMsg (msg) { |
40 | var el = api.message.html.render(msg) |
41 | var queryTerms = query.split(whitespace) |
42 | |
43 | highlight(el, createOrRegExp(queryTerms)) |
44 | return el |
45 | } |
46 | |
47 | function draw () { |
48 | resetFeed({ container, content }) |
49 | |
50 | // TODO figure out how to step on kinda orderless search results |
51 | pull( |
52 | // api.sbot.pull.stream(sbot => next(sbot.search.query, { query, limit: 500 })), |
53 | api.sbot.pull.stream(sbot => sbot.search.query({ query, limit: 500 })), |
54 | filterDownThrough(), |
55 | pull.through(() => search.matches.set(search.matches() + 1)), |
56 | Scroller(container, content, renderMsg, false, false) |
57 | ) |
58 | } |
59 | |
60 | draw() |
61 | |
62 | container.title = '?' + query |
63 | return container |
64 | } |
65 | } |
66 | |
67 | function createOrRegExp (ary) { |
68 | return new RegExp(ary.map(function (e) { |
69 | return '\\b' + e + '\\b' |
70 | }).join('|'), 'i') |
71 | } |
72 | |
73 | function highlight (el, query) { |
74 | var searcher = new TextNodeSearcher({container: el}) |
75 | searcher.query = query |
76 | searcher.highlight() |
77 | return el |
78 | } |
79 | |
80 |
Built with git-ssb-web