src/views/blog-post-editor.lisp

DOWNLOAD
(defpackage murja.views.blog-post-editor
  (:use
   :cl
   :murja.newui.dispatcher
   :murja.newui
   :murja.views.components.tab)
  (:import-from :murja.middleware.auth :*user*)
  (:export :editor)
  (:local-nicknames (:tabs :murja.models.tab))
  (:local-nicknames (:json :com.inuoe.jzon))
  (:local-nicknames (:modelpost :murja.models.post)))

(in-package :murja.views.blog-post-editor)

(defun editor (post)

  (with-state ((title (modelpost:post-title post))
	       (content (modelpost:article post))) ()
    (c :div (:class "posteditor-tab")
       (c :div (:class "editor-top")
	  (c :input (:type "text"
		     :name "title"
		     :onchange (e (lambda (value) (setf title value)))
		     :value title))
	  (c :button (:onclick (e (lambda (v)
				    (declare (ignore v))
				    (format t "Toimiiko edes tää?~%"))))
	     "Click me"))

       (tabs "Editor"
	     (list (make-instance 'tabs:tab
				  :title "Editor"
				  :subtab t				  
				  :component (lambda (&rest all)
					       (declare (ignore all))
					       (c :div (:class "editor-container")
						  (c :div (:name "content"
							   :id "editor-post-content"
							   :onchange (e (lambda (value) (setf content value))
									;; ace-editor's event parameters are useless, to get the whole article one has to hack like this in the frontend
									:parameter-value-getter "ace.edit('editor-post-content').getValue()"))
						     content))))
		   (make-instance 'tabs:tab
				  :title "Preview"
				  :subtab t
				  :component (lambda (&rest lol)
					       (declare (ignore lol))
					       (c :div ()
						  (let ((post (make-instance 'modelpost:post
									     :title title
									     :id -1
									     :created-at nil
									     :tags nil 
									     :content content
									     :creator *user*)))
						    (murja.views.components.post:post post :show-footer? t))))))
						
	     nil
	     :id "posteditor-preview-tab"
	     ))))

(deftab /blog/post/edit (:route "/blog/post/:id/edit"
			 :require-login t
			 :captured-route-params (id)
			 :needed-abilities (list "edit-post")
			 :subtab t
			 :inject-to-head ((c :script (:src "https://cdnjs.cloudflare.com/ajax/libs/ace/1.43.1/ace.min.js"
						      :integrity "sha512-4pjReWfvI2kek2LBL2nn0btGloS+00a3CvuBXdSOY0DjuMm3YJy8M5CKWKaXd4QJG5Fh4iKlLuboM1ru7FHT6Q=="
						      :crossorigin "anonymous"
						      :referrerpolicy "no-referrer") )
					  (c :script (:src "/resources/murja-ace.js"))))
  (let ((post (modelpost:get-post id :allow-hidden? t)))
    (editor post)))