Skip to content

Commit e42535b

Browse files
authored
highlight autocomplete results
highlight autocomplete results
1 parent 115d428 commit e42535b

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
<li {{on "click" this.onClick}}>
2-
<div class="icon {{@item.type}}"></div>
3-
<div class="title">{{@item.title}}</div>
4-
<div class="path text-muted">{{@item.path}}</div>
5-
</li>
1+
<li
2+
{{on "click" this.onClick}}>
3+
<div class="d-flex flex-row">
4+
<div class="icon {{@item.type}}"></div>
5+
<div class="title">{{@item.title}}</div>
6+
<div class="path text-muted">{{@item.path}}</div>
7+
</div>
8+
</li>
9+
<div class="text-muted px-3 pb-3" {{textToDom @item.highlight}}>
10+
</div>

app/components/search_results/document/index.hbs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
{{@result_node.title}}
77
</LinkTo>
88
</div>
9-
109
<Node::Tags
1110
@tags={{this.tags}}
1211
@more_than_max_visible_tags={{this.more_than_max_visible_tags}} />
13-
14-
</div>
12+
</div>

app/modifiers/text_to_dom.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Modifier from 'ember-modifier';
2+
3+
4+
export default class TextToDom extends Modifier {
5+
/*
6+
Converts plain text to HTML DOM elements and inserts them
7+
as children to `this.element`
8+
9+
Autocomplete receives text matching highlights as plain text e.g.
10+
11+
"This is <em>great</em> match".
12+
13+
Besides the fact that you cannot style plain text, it is also
14+
rendered with <em> tag.
15+
To fix this problem hightlight text is converted to DOM nodes.
16+
*/
17+
didReceiveArguments() {
18+
19+
let text = this.args.positional[0],
20+
template,
21+
nodes,
22+
that = this,
23+
em_element,
24+
text_element;
25+
26+
// inspired from https://stackoverflow.com/a/35385518
27+
// HTML5's <template> tag can be used to convinently convert
28+
// text into DOM nodes
29+
template = document.createElement('template');
30+
template.innerHTML = text;
31+
32+
// voila!
33+
nodes = template.content.childNodes;
34+
35+
nodes.forEach(node => {
36+
// is it <em>some match</em> ?
37+
if (node.nodeName == 'EM') {
38+
em_element = document.createElement('em');
39+
em_element.textContent = node.firstChild.textContent;
40+
that.element.appendChild(em_element);
41+
} else {
42+
// otherwise node is a plain text
43+
text_element = document.createTextNode(node.textContent);
44+
that.element.appendChild(text_element);
45+
}
46+
});
47+
}
48+
49+
}

app/styles/search.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
padding: 0;
2121
z-index: 1;
2222

23+
.text-muted {
24+
em {
25+
color: green;
26+
font-weight: bold;
27+
}
28+
}
29+
2330
li:hover {
2431
background-color: #e9e9e9;
2532
border-left: 1px solid #0d6efd;

0 commit comments

Comments
 (0)