Be able to load from gist.github.co
This commit is contained in:
parent
7a3c1f61d0
commit
3d4bd4cea9
1 changed files with 78 additions and 27 deletions
|
|
@ -21,6 +21,15 @@
|
||||||
;; Use read-string to undo escaping of characters by pr-str (e.g. newlines)
|
;; Use read-string to undo escaping of characters by pr-str (e.g. newlines)
|
||||||
(read-string (str "\"" raw-code "\""))))
|
(read-string (str "\"" raw-code "\""))))
|
||||||
|
|
||||||
|
(defn load-gist [gist callback]
|
||||||
|
(let [set-content (fn [progress-event]
|
||||||
|
(callback (.. progress-event -srcElement -responseText)))
|
||||||
|
oreq (js/XMLHttpRequest.)]
|
||||||
|
(.addEventListener oreq "load" set-content)
|
||||||
|
(.open oreq "GET" (str "https://gist.githubusercontent.com/" gist "/raw"))
|
||||||
|
(.send oreq)))
|
||||||
|
|
||||||
|
|
||||||
(defn bookmarklet-href [code-str]
|
(defn bookmarklet-href [code-str]
|
||||||
(str "javascript:(function(){"
|
(str "javascript:(function(){"
|
||||||
"var runCode = function() {
|
"var runCode = function() {
|
||||||
|
|
@ -39,44 +48,86 @@
|
||||||
runCode() }"
|
runCode() }"
|
||||||
"})();"))
|
"})();"))
|
||||||
|
|
||||||
|
(defn query-params []
|
||||||
|
(let [query-str (.substring js/window.location.search 1)]
|
||||||
|
(into {}
|
||||||
|
(map (fn [pair]
|
||||||
|
(let [[k v] (.split pair "=" 2)]
|
||||||
|
[(keyword (js/decodeURIComponent k))
|
||||||
|
(js/decodeURIComponent v)])))
|
||||||
|
(.split query-str "&"))))
|
||||||
|
|
||||||
|
|
||||||
|
(def *initial-name (r/atom nil))
|
||||||
|
(def *initial-code (r/atom nil))
|
||||||
|
|
||||||
|
;; Initialize code
|
||||||
|
(let [{:keys [gist]} (query-params)]
|
||||||
|
(cond gist
|
||||||
|
(do
|
||||||
|
(reset! *initial-name "---")
|
||||||
|
(reset! *initial-code ";; loading from gist")
|
||||||
|
(load-gist gist (fn [content]
|
||||||
|
(let [[code meta-str] (reverse (clojure.string/split content #";;---+\n"))
|
||||||
|
{bookmark-name :name} (when meta-str
|
||||||
|
(read-string meta-str))]
|
||||||
|
(when bookmark-name
|
||||||
|
(reset! *initial-name bookmark-name))
|
||||||
|
(reset! *initial-code code)))))
|
||||||
|
|
||||||
|
:else
|
||||||
|
(do
|
||||||
|
(reset! *initial-name "My first bookmarklet")
|
||||||
|
(reset! *initial-code (str "; This is the code of your bookmarklet\n"
|
||||||
|
(pr-str '(js/alert "Hello")))))))
|
||||||
|
|
||||||
|
(defn bookmark-name-field [initial-name *bookmark-name]
|
||||||
|
(let [*name (r/atom initial-name)]
|
||||||
|
[(fn []
|
||||||
|
[:input {:type "text"
|
||||||
|
:placeholder "The name of the Bookmarklet"
|
||||||
|
:value @*name
|
||||||
|
:on-change (fn [e]
|
||||||
|
(let [v (.. e -target -value)]
|
||||||
|
(reset! *name v)
|
||||||
|
(reset! *bookmark-name
|
||||||
|
(if (clojure.string/blank? v)
|
||||||
|
(str "Bookmarklet " (rand-int 1000))
|
||||||
|
v))))}])]))
|
||||||
|
|
||||||
|
(defn editor [*code]
|
||||||
|
[:textarea
|
||||||
|
{:rows 10 :cols 80
|
||||||
|
:value @*code
|
||||||
|
:on-drop (fn [e]
|
||||||
|
(let [bookmarklet (js/decodeURIComponent (.. e -dataTransfer (getData "text")))
|
||||||
|
cljs-snippet (read-code bookmarklet)
|
||||||
|
new-code (if cljs-snippet
|
||||||
|
(str "; Extracted snippet\n" cljs-snippet)
|
||||||
|
(str "; Failed to extract snippet\n" bookmarklet))]
|
||||||
|
(js/console.log "Dropped" bookmarklet)
|
||||||
|
(set! (.. e -target -value) new-code)
|
||||||
|
(reset! *code new-code)
|
||||||
|
(.preventDefault e)))
|
||||||
|
:on-change (fn [e] (reset! *code (.. e -target -value)))}])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(defn workspace []
|
(defn workspace []
|
||||||
(let [value (str "; This is the code of your bookmarklet\n"
|
(let [value @*initial-code
|
||||||
(pr-str '(js/alert "Hello")))
|
|
||||||
*code (r/atom value)
|
*code (r/atom value)
|
||||||
bookmark-name "Bookmark"
|
bookmark-name @*initial-name
|
||||||
*bookmark-name (r/atom bookmark-name)]
|
*bookmark-name (r/atom bookmark-name)]
|
||||||
|
[:div
|
||||||
[:div
|
[bookmark-name-field bookmark-name *bookmark-name]
|
||||||
[:input {:type "text"
|
|
||||||
:placeholder bookmark-name
|
|
||||||
:on-change (fn [e]
|
|
||||||
(let [v (.. e -target -value)]
|
|
||||||
(reset! *bookmark-name
|
|
||||||
(if (clojure.string/blank? v)
|
|
||||||
bookmark-name
|
|
||||||
v))))}]
|
|
||||||
[:br]
|
[:br]
|
||||||
[:textarea {:rows 10 :cols 80
|
[editor *code]
|
||||||
:on-drop (fn [e]
|
|
||||||
(let [bookmarklet (js/decodeURIComponent (.. e -dataTransfer (getData "text")))
|
|
||||||
cljs-snippet (read-code bookmarklet)
|
|
||||||
new-code (if cljs-snippet
|
|
||||||
(str "; Extracted snippet\n" cljs-snippet)
|
|
||||||
(str "; Failed to extract snippet\n" bookmarklet))]
|
|
||||||
(js/console.log "Dropped" bookmarklet)
|
|
||||||
(set! (.. e -target -value) new-code)
|
|
||||||
(reset! *code new-code)
|
|
||||||
(.preventDefault e)))
|
|
||||||
:on-change (fn [e] (reset! *code (.. e -target -value)))}
|
|
||||||
value]
|
|
||||||
[:br]
|
[:br]
|
||||||
[:br]
|
[:br]
|
||||||
"Click the link below"[:br]
|
"Click the link below"[:br]
|
||||||
"or"[:br]
|
"or"[:br]
|
||||||
"Drag the link to the bookmarks bar" [:br]
|
"Drag the link to the bookmarks bar" [:br]
|
||||||
[(fn []
|
[(fn []
|
||||||
(js/console.log "Loaded", @*code)
|
|
||||||
[(fn [] [:a {:href (bookmarklet-href @*code)} @*bookmark-name])])
|
[(fn [] [:a {:href (bookmarklet-href @*code)} @*bookmark-name])])
|
||||||
*code]]))
|
*code]]))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue