From: Frédéric Péters Date: Thu, 4 Jun 2020 12:20:11 +0000 (+0200) Subject: add mini-live edit, for quick fixes X-Git-Tag: v2022~35 X-Git-Url: https://git.0d.be/?p=chloro.git;a=commitdiff_plain;h=ed4760479d8f34e6d9acc1659141571ad7d3e790;hp=59248bad7396acd8e4cf14978d9ceba94f72ef39 add mini-live edit, for quick fixes --- diff --git a/chloro/phyll/static/js/chloro.js b/chloro/phyll/static/js/chloro.js new file mode 100644 index 0000000..9988be4 --- /dev/null +++ b/chloro/phyll/static/js/chloro.js @@ -0,0 +1,27 @@ +$(function() { + $('div[contenteditable]').on('input', function(event) { + if (event.originalEvent.inputType == "insertParagraph") { + var sel = document.getSelection(); + var anchorNode = sel.anchorNode; + var prev_p = sel.anchorNode.previousSibling; + if (prev_p.tagName != 'P') { + prev_p = $(prev_p).parents('p')[0]; + } + var title_match = prev_p.innerText.match(/^(h[1-6]). /); + if (title_match) { + var title = document.createElement(title_match[1]); + title.innerHTML = prev_p.innerHTML; + title.textContent = title.textContent.slice(4); + prev_p.replaceWith(title); + } + } + return true; + }); + $('#save').on('click', function() { + var text = $('div[contenteditable]')[0].innerHTML; + $.post('api-save/', {text: text}).fail(function() { + $('#save').css('background', 'red'); + }); + return false; + }); +}); diff --git a/chloro/phyll/templates/phyll/base.html b/chloro/phyll/templates/phyll/base.html index 54a1f4a..4f231e1 100644 --- a/chloro/phyll/templates/phyll/base.html +++ b/chloro/phyll/templates/phyll/base.html @@ -1,4 +1,4 @@ -{% load i18n %} +{% load gadjo i18n %} @@ -6,6 +6,10 @@ {% block page-title %}Coin web de Frédéric Péters{% endblock %} + {% if request.user.is_staff %} + + + {% endif %} {% block bottom-head %} {% endblock %} diff --git a/chloro/phyll/templates/phyll/note_detail.html b/chloro/phyll/templates/phyll/note_detail.html index 240929b..d0b9bc3 100644 --- a/chloro/phyll/templates/phyll/note_detail.html +++ b/chloro/phyll/templates/phyll/note_detail.html @@ -7,7 +7,8 @@ {% block body %}

{{ object.title }}

-
{{ object.text|safe }}
+
{{ object.text|safe }}
+{% if request.user.is_staff %}{% endif %}
{{ object.creation_timestamp|date:"j E Y, H:i"|lower }}
diff --git a/chloro/phyll/templates/phyll/note_form.html b/chloro/phyll/templates/phyll/note_form.html index 3cc0ab5..927ae53 100644 --- a/chloro/phyll/templates/phyll/note_form.html +++ b/chloro/phyll/templates/phyll/note_form.html @@ -2,7 +2,6 @@ {% load gadjo i18n static %} {% block bottom-head %} - {% endblock %} diff --git a/chloro/phyll/urls.py b/chloro/phyll/urls.py index 37c2c64..d49525e 100644 --- a/chloro/phyll/urls.py +++ b/chloro/phyll/urls.py @@ -38,6 +38,7 @@ urlpatterns = [ r'^(?P[\w:-]+)/delete/$', staff_member_required(views.NoteDeleteView.as_view(), login_url='login'), ), + url(r'^(?P[\w:-]+)/api-save/$', staff_member_required(views.NoteApiSaveView.as_view())), url(r'^new-note/$', staff_member_required(views.NoteAddView.as_view(), login_url='login')), url(r'^feeds/(?P[\w:-]+)/atom$', views.AtomFeed()), url(r'^feed/atom$', views.AtomFeed()), diff --git a/chloro/phyll/views.py b/chloro/phyll/views.py index 84b40e7..108a8ea 100644 --- a/chloro/phyll/views.py +++ b/chloro/phyll/views.py @@ -19,8 +19,10 @@ import urllib.parse from django.conf import settings from django.contrib.syndication.views import Feed from django.core.exceptions import PermissionDenied -from django.http import Http404 +from django.http import HttpResponse, Http404 from django.utils.feedgenerator import Atom1Feed +from django.views import View +from django.views.decorators.csrf import csrf_exempt from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView, TemplateView from .models import Note @@ -50,6 +52,20 @@ class NoteEditView(UpdateView): fields = ['title', 'slug', 'text', 'tags', 'published'] +class NoteApiSaveView(View): + http_method_names = ['post'] + + @csrf_exempt + def dispatch(self, *args, **kwargs): + return super().dispatch(*args, **kwargs) + + def post(self, request, *args, **kwargs): + note = Note.objects.get(slug=kwargs['slug']) + note.text = request.POST['text'] + note.save() + return HttpResponse('ok') + + class NoteAddView(CreateView): model = Note fields = ['title', 'slug', 'text', 'tags', 'published']