git ssb

16+

Dominic / patchbay



Tree: 55335207126eb15c3a0f1834ffb8bbcbd32658a8

Files: 55335207126eb15c3a0f1834ffb8bbcbd32658a8 / app / page / search.js

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

Built with git-ssb-web