From aad64bc256f65be5f33455c92966aa68e11c7552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCller?= Date: Thu, 25 Dec 2014 15:56:10 +0100 Subject: [PATCH] expected note title feature added --- resources/public/js/publishing.js | 29 ++++++++++++++++++++++++++++- src/notehub/api.clj | 11 +++++++---- src/notehub/handler.clj | 6 ++++++ src/notehub/views.clj | 10 +++++++--- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/resources/public/js/publishing.js b/resources/public/js/publishing.js index e3551b7..a65aab5 100644 --- a/resources/public/js/publishing.js +++ b/resources/public/js/publishing.js @@ -3,7 +3,28 @@ var iosDetected = navigator.userAgent.match("(iPad|iPod|iPhone)"); var timer = null; var timerDelay = iosDetected ? 800 : 400; var show = function(elem) { elem.style.display = "block" } -var $note, $action, $preview, $plain_password, $input_elems, $dashed_line, updatePreview; +var $note, $action, $preview, $plain_password, $input_elems, $dashed_line, $proposed_title, updatePreview; +var firstLines_; +var backendTimer; + +function updateProposedTitle() { + clearTimeout(backendTimer); + backendTimer = setTimeout(function () { + var http = new XMLHttpRequest(); + var url = "/propose-title"; + http.open("POST", url, true); + http.onreadystatechange = function() { + if(http.readyState == 4 && http.status == 200) { + var now = new Date(); + $proposed_title.innerHTML = + "Expected URL: https://www.notehub.org/" + + now.getFullYear() + "/" + (now.getMonth()+1) + "/" + now.getDate() + "/" + + http.responseText; + } + } + http.send($note.value); + }, 500); +} function md2html(input){ return marked(input); @@ -14,6 +35,7 @@ function onLoad () { $action = $("action"); $preview = $("preview"); $plain_password = $("plain-password"); + $proposed_title = $("proposed-title"); $input_elems = $("input-elems"); $dashed_line = $("dashed-line"); updatePreview = function(){ @@ -24,6 +46,11 @@ function onLoad () { show($dashed_line); show($input_elems); $preview.innerHTML = md2html(content); + var firstLines = content.split("\n", 2); + if(firstLines_ != firstLines) { + firstLines_ = firstLines; + updateProposedTitle(); + } }, delay); }; if($action){ diff --git a/src/notehub/api.clj b/src/notehub/api.clj index 1f4f6f6..2a920f5 100644 --- a/src/notehub/api.clj +++ b/src/notehub/api.clj @@ -75,6 +75,12 @@ :publisher (storage/get-publisher noteID)}) {:status (create-response false "noteID '%s' unknown" noteID)})) +(defn propose-title [note] + (let [raw-title (filter #(or (= \- %) (Character/isLetterOrDigit %)) + (-> note derive-title trim (sreplace " " "-") lower-case)) + max-length (get-setting :max-title-length #(Integer/parseInt %) 80)] + (apply str (take max-length raw-title)))) + (defn post-note [{:keys [note pid signature password hostURL] :as params}] ;(log "post-note: %s" {:pid pid :signature signature :password password :note note}) @@ -86,10 +92,7 @@ (if (empty? errors) (let [[year month day] (map str (get-date)) params (select-keys params [:text-size :header-size :text-font :header-font :theme]) - raw-title (filter #(or (= \- %) (Character/isLetterOrDigit %)) - (-> note derive-title trim (sreplace " " "-") lower-case)) - max-length (get-setting :max-title-length #(Integer/parseInt %) 80) - proposed-title (apply str (take max-length raw-title)) + proposed-title (propose-title note) title (first (drop-while #(storage/note-exists? (build-key year month day %)) (cons proposed-title (map #(str proposed-title "-" (+ 2 %)) (range))))) diff --git a/src/notehub/handler.clj b/src/notehub/handler.clj index 219068b..0f30409 100644 --- a/src/notehub/handler.clj +++ b/src/notehub/handler.clj @@ -59,6 +59,12 @@ (GET "/" [] landing-page) + (POST "/propose-title" {body :body} + (let [note (slurp body)] + (return-content-type + "text/plain; charset=utf-8" + (api/propose-title note)))) + (GET "/:year/:month/:day/:title/export" [year month day title] (when-let [md-text (:note (api/get-note {:noteID (api/build-key year month day title)}))] (return-content-type "text/plain; charset=utf-8" md-text))) diff --git a/src/notehub/views.clj b/src/notehub/views.clj index 74252a6..a114f4f 100644 --- a/src/notehub/views.clj +++ b/src/notehub/views.clj @@ -60,10 +60,14 @@ fields (text-area :note content) [:fieldset#input-elems {:class css-class} - (text-field {:class "ui-elem" :placeholder (get-message passwd-msg)} - :plain-password) + (text-field {:class "ui-elem" + :placeholder (get-message passwd-msg)} + :plain-password) " " (submit-button {:class "button ui-elem" - :id :publish-button} (get-message command))])]))) + :id :publish-button} (get-message command)) + [:br] + [:br] + [:div#proposed-title {:style "color: #aaa; font-size: 0.8em"}]])]))) (def landing-page (layout :no-js {} (get-message :page-title)