Skip to content

Commit

Permalink
feat: Search highlight (#29)
Browse files Browse the repository at this point in the history
* add: gitginore db folder and .vscode settings

* feat: search phrase highlighting

* refactor: highlight function moved to sekizai tag

* fix: keyerror for '/browse' with empty parameters

* refactor: delete unecessary js block
  • Loading branch information
JSerwatka authored May 6, 2021
1 parent 427b5bb commit 675c237
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
8 changes: 7 additions & 1 deletion clipping_manager/templates/clipping_manager/browse.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends "sidebar.html" %}
{% load cms_tags i18n crispy_forms_tags %}
{% load staticfiles sekizai_tags %}

{% block sidebar %}
<div class="card">
Expand Down Expand Up @@ -53,4 +54,9 @@ <h1>{% trans "Your Clippings" %}</h1>

{% include "partials/pagination.html" %}

{% endblock %}
{% addtoblock "js" %}
<script src="{% static 'js/helper-functions.js' %}" onload="highlightClippings('{{contains}}')"></script>
{% endaddtoblock %}
{% endblock %}

{% render_block "js" %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="mb-5">
<p><em>{{ clipping.content }}</em></p>
<p class="clipping-content"><em>{{ clipping.content }}</em></p>
<span class="text-secondary">&mdash; {{ clipping.book.title }}</span>
</div>
1 change: 1 addition & 0 deletions clipping_manager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def get_context_data(self, **kwargs):
ctx = super(ClippingsBrowseView, self).get_context_data(**kwargs)
ctx['clippings_count'] = Clipping.objects.for_user(self.request.user).count()
ctx['books_count'] = Book.objects.for_user(self.request.user).count()
ctx['contains'] = self.request.GET.get('content', '')
ctx['filter'] = self.filter
return ctx

Expand Down
4 changes: 4 additions & 0 deletions static/css/main.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions static/js/helper-functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Highlights the searched phrase ('Content contains') in clippings
function highlightClippings(wordToHighlight) {
// If empty -> stop the function
if (!wordToHighlight) return;

// Get all clippings' text
const clippings = document.querySelectorAll('.clipping-content > em');

// Highlighs searched-for phrase in each clipping (case-insensitive)
clippings.forEach((clipping) => {
const regex = new RegExp(wordToHighlight, 'gi');
const highlightedClipping = clipping.innerHTML.replace(regex, (foundWord) => {
return `<span class="highlight">${foundWord}</span>`;
});

clipping.innerHTML = highlightedClipping;
});
}
4 changes: 4 additions & 0 deletions static/scss/_basics.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ a {
&:hover {
color: $color-secondary;
}
}

.highlight {
background: #ffc600;
}

0 comments on commit 675c237

Please sign in to comment.