Last active
August 29, 2015 14:09
-
-
Save brabadu/2a9f77f85cc3ab2a9f84 to your computer and use it in GitHub Desktop.
react in lighttable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns lt.plugins.user | |
(:require [clojure.string :as string] | |
[lt.object :as object] | |
[lt.objs.tabs :as tabs] | |
[lt.objs.command :as cmd]) | |
(:require-macros [lt.macros :refer [defui behavior]])) | |
(defui react-panel [this] | |
[:h1 {:id "app-root"} "React + LightTable!"]) | |
(defn el [name attr children] | |
(React/createElement name, attr, children)) | |
(defn format-time [d] | |
(let [values [(.getHours d) (.getMinutes d) (.getSeconds d)] | |
zero-prefixed (map (partial str "0") values) | |
normalized (map #(.slice % -2) zero-prefixed) | |
joined (string/join ":" normalized ) | |
] | |
joined)) | |
(def label | |
(React/createClass #js {:displayName "myLabel" | |
:render (fn [] (this-as this | |
(el "h1" nil (str "React tick is: " (-> this .-props .-time)))))})) | |
;; Define an object prototype | |
(object/object* ::user.react-timer | |
:tags [:user.hello] | |
:behaviors [::on-close-timer-destroy] | |
:init (fn [this] | |
(react-panel this))) | |
(behavior ::on-close-timer-destroy | |
:triggers #{:close} | |
:reaction (fn [this] | |
(js/window.clearTimeout (:interval @this)) | |
(object/raise this :destroy))) | |
(def react-timer (object/create ::user.react-timer)) | |
(cmd/command {:command :user.show-time | |
:desc "User: Show time" | |
:exec (fn [] | |
(tabs/add-or-focus! react-timer) | |
(let [app-root (.getElementById js/document "app-root") | |
label-inst (React/render (el label #js {:time (format-time (js/Date.))} []) | |
app-root) | |
refresh-timer (fn [] | |
(prn (format-time (js/Date.))) | |
(.setProps label-inst #js {:time (format-time (js/Date.))})) | |
interval (js/window.setInterval refresh-timer 1000)] | |
(object/merge! react-timer {:interval interval})))}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(set! *print-level* 5)
[:app :lt.objs.settings/pair-keymap-diffs]