Browse Source

preview function works now

master
Christian Mueller 14 years ago
parent
commit
d57d3626aa
  1. 1
      .gitignore
  2. 5
      project.clj
  3. 43
      src-cljs/main.cljs
  4. 32
      src/NoteHub/views/common.clj
  5. 21
      src/NoteHub/views/pages.clj

1
.gitignore vendored

@ -1,3 +1,4 @@
resources/
pom.xml pom.xml
*jar *jar
/lib/ /lib/

5
project.clj

@ -3,6 +3,8 @@
:dependencies [[org.clojure/clojure "1.4.0"] :dependencies [[org.clojure/clojure "1.4.0"]
[hiccup "1.0.0"] [hiccup "1.0.0"]
[cssgen "0.2.6"] [cssgen "0.2.6"]
[jayq "0.1.0-alpha2"]
[fetch "0.1.0-alpha2"]
[org.pegdown/pegdown "1.1.0"] [org.pegdown/pegdown "1.1.0"]
[noir "1.3.0-beta1"]] [noir "1.3.0-beta1"]]
:plugins [[lein-cljsbuild "0.1.10"]] :plugins [[lein-cljsbuild "0.1.10"]]
@ -13,7 +15,8 @@
; The standard ClojureScript compiler options: ; The standard ClojureScript compiler options:
; (See the ClojureScript compiler documentation for details.) ; (See the ClojureScript compiler documentation for details.)
:compiler { :compiler {
:output-to "resources/public/js/main.js" ; default: main.js in current directory :output-dir "resources/public/cljs/"
:output-to "resources/public/cljs/main.js" ; default: main.js in current directory
:optimizations :whitespace :optimizations :whitespace
:pretty-print true}}]} :pretty-print true}}]}
:main NoteHub.server) :main NoteHub.server)

43
src-cljs/main.cljs

@ -1,21 +1,32 @@
(ns NoteHub.main (ns NoteHub.main
(:require [clojure.browser.dom :as dom] (:use [jayq.core :only [$ css inner val anim]])
[goog.dom :as gdom] (:require [fetch.remotes :as remotes]
[goog.style :as style] [clojure.browser.dom :as dom]
[goog.dom.classes :as classes] [clojure.browser.event :as event])
[clojure.browser.event :as event] (:require-macros [fetch.macros :as fm]))
[goog.editor.focus :as focus]))
(defn $ ; frequently used selectors
"The DOM-element selector." (def $draft ($ :#draft))
[selector] (def $preview ($ :#preview))
(dom/get-element selector))
(if-let [draft ($ "draft")] (defn scroll-to
(focus/focusInputField draft)) "scrolls to the given selector"
[$id]
(anim ($ :body) {:scrollTop ((js->clj (.offset $id)) "top")} 500))
; Show the Preview button as soon as the user starts typing. ; set focus to the draft textarea (if there is one)
(event/listen ($ "draft") (.focus $draft)
:keypress
; show the preview & publish buttons as soon as the user starts typing.
(.keypress $draft
(fn [e]
(css ($ :#buttons) {:display :block})))
; on a preview button click, transform markdown to html, put it
; to the preview layer and scroll to it
(.click ($ :#preview-button)
(fn [e] (fn [e]
(style/setStyle ($ "preview-button") "display" "block"))) (do
(fm/remote (md-to-html (val $draft)) [result]
(inner $preview result)
(scroll-to $preview)))))

32
src/NoteHub/views/common.clj

@ -19,7 +19,7 @@
(css (css
(rule ".centerized" (rule ".centerized"
:text-align :center) :text-align :center)
(rule ".button" (rule ".landing-button"
:box-shadow [0 :2px :5px :#aaa] :box-shadow [0 :2px :5px :#aaa]
:text-decoration :none :text-decoration :none
:font-size :1.5em :font-size :1.5em
@ -30,7 +30,7 @@
:padding :10px :padding :10px
helvetica-neue helvetica-neue
(rule "&:hover" (rule "&:hover"
:background :#2b3)) :background :#0b2))
(rule "html, body" (rule "html, body"
:color :#333 :color :#333
:margin 0 :margin 0
@ -50,30 +50,39 @@
(rule "& > h1:first-child" (rule "& > h1:first-child"
:text-align :center :text-align :center
:margin :2em)) :margin :2em))
(rule "pre"
:padding :1em
:border [:1px :dashed :gray]
:background :#efefef)
(rule "*:focus" (rule "*:focus"
:outline [:0px :none :transparent]) :outline [:0px :none :transparent])
(rule "textarea" (rule "textarea"
:width "900px" :width "900px"
:font-family :Courier :font-family :Courier
:font-size :1.3em :font-size :1.2em
:border :none :border :none
:height :600px) :height :600px
(rule "#preview-button" :margin-bottom :2em)
(rule ".hidden"
:display :none)
(rule ".button"
helvetica-neue helvetica-neue
:display :none
:cursor :pointer :cursor :pointer
:border [:1px :solid] :border [:1px :solid]
:background :white :opacity 0.7
:font-size :0.8em :font-size :1em
:opacity :0.8) :background :white)
(rule ".central-body" (rule ".central-body"
:width "900px" :width "900px"
:margin-top :5em :margin-top :5em
:margin-bottom :5em :margin-bottom :10em
:margin-left "auto" :margin-left "auto"
:margin-right "auto") :margin-right "auto")
(rule "h1" (rule "h1"
:font-size :2em) :font-size :2em)
(rule "#preview-start"
:border-bottom [:1px :dashed :gray]
:margin-bottom :10em)
(rule "h1, h2, h3, h4" (rule "h1, h2, h3, h4"
:font-family (gen-comma-list :font-family (gen-comma-list
"'Noticia Text'" "Georgia")))) "'Noticia Text'" "Georgia"))))
@ -82,6 +91,7 @@
(html5 (html5
[:head [:head
[:title "NoteHub - " title] [:title "NoteHub - " title]
(include-js "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js")
[:link {:href "http://fonts.googleapis.com/css?family=Noticia+Text:400,700" [:link {:href "http://fonts.googleapis.com/css?family=Noticia+Text:400,700"
:rel "stylesheet" :rel "stylesheet"
:type "text/css"}] :type "text/css"}]
@ -89,4 +99,4 @@
[:body [:body
content content
(javascript-tag "var CLOSURE_NO_DEPS = true;") (javascript-tag "var CLOSURE_NO_DEPS = true;")
(include-js "/js/main.js")])) (include-js "/cljs/main.js")]))

21
src/NoteHub/views/pages.clj

@ -1,9 +1,11 @@
(ns NoteHub.views.pages (ns NoteHub.views.pages
(:require [NoteHub.views.common :as common]) (:require [NoteHub.views.common :as common])
(:use (:use
[noir.response :only [content-type]]
[clojure.string :rename {replace sreplace} :only [trim split replace]] [clojure.string :rename {replace sreplace} :only [trim split replace]]
[noir.core :only [defpage]] [noir.core :only [defpage]]
[hiccup.form]) [hiccup.form]
[noir.fetch.remotes])
(:import [org.pegdown PegDownProcessor])) (:import [org.pegdown PegDownProcessor]))
(defpage "/" {} (defpage "/" {}
@ -13,19 +15,20 @@
[:h2 "Free hosting for markdown pages."] [:h2 "Free hosting for markdown pages."]
[:br] [:br]
[:br] [:br]
[:a.button {:href "/new"} "New Page"]])) [:a.landing-button {:href "/new"} "New Page"]]))
(defpage "/new" {} (defpage "/new" {}
(common/layout "New Markdown Note" (common/layout "New Markdown Note"
[:div.central-body [:div.central-body
(form-to [:post "/preview-note"] (form-to [:get "/preview-note"]
(text-area {:class "max-width"} :draft) (text-area {:class "max-width"} :draft)
(submit-button {:id "preview-button"} "Preview"))])) [:div#buttons.hidden
(submit-button {:style "float: left" :class "button"} "Publish")
[:button#preview-button.button {:type :button :style "float: right"} "Preview"]])]
[:div#preview-start]
[:article#preview.central-body]))
; Actions. ; Actions.
(defpage [:post "/preview-note"] {:keys [draft]} (defremote md-to-html [draft]
(let [get-title (comp trim #(sreplace % "#" "") first #(split % #"\n"))] (.markdownToHtml (PegDownProcessor.) draft))
(common/layout (get-title draft)
[:article.central-body
(.markdownToHtml (PegDownProcessor.) draft)])))

Loading…
Cancel
Save