From 312ab3db174d00f32d44f70cc5cdca910d84f3b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Fri, 5 Jul 2019 22:29:21 +0200 Subject: [PATCH] Implement bookmarklet --- bookmarks/models.py | 4 ++- .../templates/bookmarks/bookmarklet.html | 24 +++++++++++++++++ bookmarks/templates/bookmarks/bookmarklet.js | 9 +++++++ bookmarks/templates/bookmarks/close.html | 9 +++++++ bookmarks/templates/bookmarks/form.html | 18 ++++++++++--- bookmarks/templates/bookmarks/layout.html | 2 +- bookmarks/templates/bookmarks/new.html | 2 +- bookmarks/templatetags/bookmarks.py | 3 ++- bookmarks/urls.py | 2 ++ bookmarks/views/bookmarks.py | 27 +++++++++++++++++-- 10 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 bookmarks/templates/bookmarks/bookmarklet.html create mode 100644 bookmarks/templates/bookmarks/bookmarklet.js create mode 100644 bookmarks/templates/bookmarks/close.html diff --git a/bookmarks/models.py b/bookmarks/models.py index 4efa7af..5821ff6 100644 --- a/bookmarks/models.py +++ b/bookmarks/models.py @@ -75,7 +75,9 @@ class BookmarkForm(forms.ModelForm): required=False) description = forms.CharField(required=False, widget=forms.Textarea()) + # Hidden field that determines whether to close window/tab after saving the bookmark + auto_close = forms.CharField(required=False) class Meta: model = Bookmark - fields = ['url', 'tag_string', 'title', 'description'] + fields = ['url', 'tag_string', 'title', 'description', 'auto_close'] diff --git a/bookmarks/templates/bookmarks/bookmarklet.html b/bookmarks/templates/bookmarks/bookmarklet.html new file mode 100644 index 0000000..91eaf64 --- /dev/null +++ b/bookmarks/templates/bookmarks/bookmarklet.html @@ -0,0 +1,24 @@ +{% extends "bookmarks/layout.html" %} + +{% block content %} +
+
+
+

Bookmarklet

+
+

The bookmarklet is a quick way to add new bookmarks without opening the linkding application + first. Here's how it works:

+
    +
  • Drag the bookmarklet below into your browsers bookmark bar / toolbar
  • +
  • Open the website that you want to bookmark
  • +
  • Click the bookmarklet in your browsers toolbar
  • +
  • linkding opens in a new window or tab and allows you to add a bookmark for the site
  • +
  • After saving the bookmark the linkding window closes and you are back on your website
  • +
+

Drag the following bookmarklet to your browsers toolbar:

+ 📎 Add bookmark +
+
+{% endblock %} + diff --git a/bookmarks/templates/bookmarks/bookmarklet.js b/bookmarks/templates/bookmarks/bookmarklet.js new file mode 100644 index 0000000..ba0ab08 --- /dev/null +++ b/bookmarks/templates/bookmarks/bookmarklet.js @@ -0,0 +1,9 @@ +(function() { + var bookmarkUrl = window.location; + var applicationUrl = '{{ application_url }}'; + + applicationUrl += '?url=' + bookmarkUrl; + applicationUrl += '&auto_close'; + + window.open(applicationUrl); +})(); diff --git a/bookmarks/templates/bookmarks/close.html b/bookmarks/templates/bookmarks/close.html new file mode 100644 index 0000000..f560857 --- /dev/null +++ b/bookmarks/templates/bookmarks/close.html @@ -0,0 +1,9 @@ +{% extends "bookmarks/layout.html" %} + +{% block content %} + +

You can now close this window.

+{% endblock %} + diff --git a/bookmarks/templates/bookmarks/form.html b/bookmarks/templates/bookmarks/form.html index bede7c9..767a6b5 100644 --- a/bookmarks/templates/bookmarks/form.html +++ b/bookmarks/templates/bookmarks/form.html @@ -2,6 +2,7 @@
{% csrf_token %} + {{ form.auto_close|attr:"type:hidden" }}
{{ form.url|add_class:"form-input"|attr:"autofocus" }} @@ -15,7 +16,8 @@ {{ form.tag_string|add_class:"form-input" }}
- Enter any number of tags separated by space and without the hash (#). If a tag does not exist it will be + Enter any number of tags separated by space and without the hash (#). If a tag does not + exist it will be automatically created.
{{ form.tag_string.errors }} @@ -43,7 +45,11 @@ {{ form.description.errors }}
- + {% if auto_close %} + + {% else %} + + {% endif %} Nevermind
@@ -56,7 +62,9 @@ const titleInput = document.getElementById('{{ form.title.id_for_label }}'); const descriptionInput = document.getElementById('{{ form.description.id_for_label }}'); - urlInput.addEventListener('input', () => { + urlInput.addEventListener('input', updateMetadata); + + function updateMetadata() { toggleIcon(titleInput, true); toggleIcon(descriptionInput, true); @@ -70,12 +78,14 @@ toggleIcon(titleInput, false); toggleIcon(descriptionInput, false); }); - }); + } function toggleIcon(input, show) { const icon = input.parentNode.querySelector('i.form-icon'); icon.style['visibility'] = show ? 'visible' : 'hidden'; } + + if (urlInput.value) updateMetadata(); })();
diff --git a/bookmarks/templates/bookmarks/layout.html b/bookmarks/templates/bookmarks/layout.html index 1f24076..2c16ce3 100644 --- a/bookmarks/templates/bookmarks/layout.html +++ b/bookmarks/templates/bookmarks/layout.html @@ -22,7 +22,7 @@ {% if request.user.is_authenticated %} diff --git a/bookmarks/templates/bookmarks/new.html b/bookmarks/templates/bookmarks/new.html index b7ee771..5cbc9fa 100644 --- a/bookmarks/templates/bookmarks/new.html +++ b/bookmarks/templates/bookmarks/new.html @@ -8,7 +8,7 @@

New bookmark

- {% bookmark_form form %} + {% bookmark_form form auto_close %}
diff --git a/bookmarks/templatetags/bookmarks.py b/bookmarks/templatetags/bookmarks.py index 5ba6020..29c55a5 100644 --- a/bookmarks/templatetags/bookmarks.py +++ b/bookmarks/templatetags/bookmarks.py @@ -8,9 +8,10 @@ register = template.Library() @register.inclusion_tag('bookmarks/form.html', name='bookmark_form') -def bookmark_form(form: BookmarkForm): +def bookmark_form(form: BookmarkForm, auto_close: bool = False): return { 'form': form, + 'auto_close': auto_close } diff --git a/bookmarks/urls.py b/bookmarks/urls.py index df08386..dcfea83 100644 --- a/bookmarks/urls.py +++ b/bookmarks/urls.py @@ -11,8 +11,10 @@ urlpatterns = [ # Bookmarks path('bookmarks', views.bookmarks_index, name='index'), path('bookmarks/new', views.bookmarks_new, name='new'), + path('bookmarks/close', views.bookmarks_close, name='close'), path('bookmarks//edit', views.bookmarks_edit, name='edit'), path('bookmarks//remove', views.bookmarks_remove, name='remove'), + path('bookmarklet', views.bookmarks_bookmarklet, name='bookmarklet'), # Settings path('settings', views.settings_index, name='settings_index'), path('settings/import', views.settings_bookmark_import, name='settings_import'), diff --git a/bookmarks/views/bookmarks.py b/bookmarks/views/bookmarks.py index 9694e41..a3946bb 100644 --- a/bookmarks/views/bookmarks.py +++ b/bookmarks/views/bookmarks.py @@ -35,16 +35,27 @@ def bookmarks_index(request): @login_required def bookmarks_new(request): + initial_url = request.GET.get('url') + initial_auto_close = 'auto_close' in request.GET + if request.method == 'POST': form = BookmarkForm(request.POST) + auto_close = form.data['auto_close'] if form.is_valid(): current_user = request.user create_bookmark(form, current_user) - return HttpResponseRedirect(reverse('bookmarks:index')) + if auto_close: + return HttpResponseRedirect(reverse('bookmarks:close')) + else: + return HttpResponseRedirect(reverse('bookmarks:index')) else: form = BookmarkForm() + if initial_url: + form.initial['url'] = initial_url + if initial_auto_close: + form.initial['auto_close'] = 'true' - return render(request, 'bookmarks/new.html', {'form': form}) + return render(request, 'bookmarks/new.html', {'form': form, 'auto_close': initial_auto_close}) @login_required @@ -67,3 +78,15 @@ def bookmarks_remove(request, bookmark_id: int): bookmark = Bookmark.objects.get(pk=bookmark_id) bookmark.delete() return HttpResponseRedirect(reverse('bookmarks:index')) + + +@login_required +def bookmarks_bookmarklet(request): + return render(request, 'bookmarks/bookmarklet.html', { + 'application_url': request.build_absolute_uri("/bookmarks/new") + }) + + +@login_required +def bookmarks_close(request): + return render(request, 'bookmarks/close.html')