Skip to content

Commit

Permalink
feat: implement quo2 code snippet preview component
Browse files Browse the repository at this point in the history
  • Loading branch information
codemaster115 committed Sep 18, 2023
1 parent 9df1b85 commit 0cf9538
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/quo2/components/code/code/style.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
(defn theme
[theme-key]
(case theme-key
:hljs-comment (colors/theme-colors colors/neutral-40 colors/neutral-60)
:hljs-title (colors/custom-color-by-theme :sky 50 60)
:hljs-keyword (colors/custom-color-by-theme :green 50 60)
:hljs-string (colors/custom-color-by-theme :turquoise 50 60)
:hljs-literal (colors/custom-color-by-theme :turquoise 50 60)
:hljs-number (colors/custom-color-by-theme :turquoise 50 60)
:line-number colors/neutral-40
:hljs-comment (colors/theme-colors colors/neutral-40 colors/neutral-60)
:hljs-title (colors/custom-color-by-theme :sky 50 60)
:hljs-keyword (colors/custom-color-by-theme :green 50 60)
:hljs-string (colors/custom-color-by-theme :turquoise 50 60)
:hljs-literal (colors/custom-color-by-theme :turquoise 50 60)
:hljs-number (colors/custom-color-by-theme :turquoise 50 60)
:hljs-symbol (colors/custom-color-by-theme :orange 50 60)
:hljs-builtin-name (colors/custom-color-by-theme :pink 50 60)
:line-number colors/neutral-40
nil))

(defn text-style
Expand Down
39 changes: 39 additions & 0 deletions src/quo2/components/code/snippet_preview/style.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(ns quo2.components.code.snippet-preview.style
(:require [quo2.foundations.colors :as colors]
[quo2.components.code.code.style :as code-style]))

(defn container
[theme]
{:overflow :hidden
:width 108
:background-color (colors/theme-colors colors/neutral-20 colors/neutral-80 theme)
:border-radius 8})

(defn line-number-container
[theme]
{:position :absolute
:bottom 0
:top 0
:left 0
:width 20
:background-color (colors/theme-colors colors/neutral-10 colors/neutral-70 theme)})

(defn text-style
[class-names]
(let [text-color (->> class-names
(map keyword)
(some (fn [class-name]
(when-let [text-color (code-style/theme class-name)]
text-color))))]
(cond-> {:flex-shrink 1
:line-height 18}
text-color (assoc :color text-color))))

(def line {:flex 1 :flex-direction :row})

(defn line-number
[width]
{:width width
:align-items :center
:justify-content :center})

86 changes: 86 additions & 0 deletions src/quo2/components/code/snippet_preview/view.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
(ns quo2.components.code.snippet-preview.view
(:require [react-native.core :as rn]
[react-native.syntax-highlighter :as highlighter]
[reagent.core :as reagent]
[cljs-bean.core :as bean]
[quo2.components.code.snippet-preview.style :as style]
[quo2.theme :as theme]
[quo2.components.markdown.text :as text]
[clojure.string :as string]))

(defn- render-nodes
[nodes]
(map (fn [{:keys [children value last-line?] :as node}]
(println last-line? value)
(if children
(into [text/text
(cond-> {:weight :code
:size :paragraph-2
:style (style/text-style (get-in node [:properties :className]))}
last-line? (assoc :number-of-lines 1))]
(render-nodes children))
;; Remove newlines as we already render each line separately.
(string/trim-newline value)))
nodes))

(defn- line
[{:keys [line-number line-number-width]} children]
[rn/view {:style style/line}
[rn/view {:style (style/line-number line-number-width)}
[text/text
{:style (style/text-style ["line-number"])
:weight :code
:size :paragraph-2}
line-number]]
[rn/view
{:style {:flex 1
:padding-horizontal 8
:padding-vertical 3}}
children]])

(defn- code-block
[{:keys [rows line-number-width]}]
[rn/view
(->> rows
(render-nodes)
(map-indexed (fn [idx row-content]
[line
{:line-number (inc idx)
:line-number-width line-number-width}
row-content]))
(into [:<>]))])

(defn- f-native-renderer
[{:keys [rows max-lines theme]
:or {max-lines ##Inf}}]
(let [total-rows (count rows)
number-rows-to-show (min (count rows) max-lines)
truncated? (< number-rows-to-show total-rows)
rows-to-show-coll (if truncated?
(as-> rows $
(update $ number-rows-to-show assoc :last-line? true)
(take (inc number-rows-to-show) $))
rows)]
[rn/view {:style (style/container theme)}
[rn/view {:style (style/line-number-container theme)}]
[code-block
{:rows rows-to-show-coll
:line-number-width 20}]]))

(defn- view-internal
[_]
(fn [{:keys [language theme]} children]
[highlighter/highlighter
{:language language
:renderer (fn [^js/Object props]
(reagent/as-element
[:f> f-native-renderer
{:rows (-> props .-rows bean/->clj)
:max-lines 0
:theme theme}]))
:show-line-numbers false
:style {}
:custom-style {:background-color nil}}
children]))

(def view (theme/with-theme view-internal))
2 changes: 2 additions & 0 deletions src/quo2/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
quo2.components.calendar.calendar-day.view
quo2.components.calendar.calendar-year.view
quo2.components.code.snippet
quo2.components.code.snippet-preview.view
quo2.components.colors.color-picker.view
quo2.components.common.notification-dot.view
quo2.components.common.separator.view
Expand Down Expand Up @@ -165,6 +166,7 @@

;;;; Code
(def snippet quo2.components.code.snippet/snippet)
(def snippet-preview quo2.components.code.snippet-preview.view/view)

;;;; Cards
(def small-option-card quo2.components.onboarding.small-option-card.view/small-option-card)
Expand Down
40 changes: 40 additions & 0 deletions src/status_im2/contexts/quo_preview/code/snippet_preview.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
(ns status-im2.contexts.quo-preview.code.snippet-preview
(:require [quo2.core :as quo]
[reagent.core :as reagent]
[status-im2.contexts.quo-preview.preview :as preview]))

(def go-example
"for(let ind")

(def clojure-example
"(for [x [0 1 2 3 4 5]
:let [y (* x 3)]
:when (even? y)]
y)")

(def examples
{:clojure {:language :clojure
:text clojure-example}
:go {:language :go
:text go-example}})

(def descriptor
[{:key :language
:type :select
:options [{:key :clojure}
{:key :go}]}
{:label "Syntax highlight?"
:key :syntax
:type :boolean}])

(defn view
[]
(let [state (reagent/atom {:language :clojure
:max-lines 1
:syntax true})]
(fn []
(let [language (if (:syntax @state) (:language @state) :text)
text (-> (:language @state) examples :text)]
[preview/preview-container {:state state :descriptor descriptor}
[quo/snippet-preview
{:language language} text]]))))
5 changes: 4 additions & 1 deletion src/status_im2/contexts/quo_preview/main.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
[status-im2.contexts.quo-preview.calendar.calendar-year :as calendar-year]
[status-im2.contexts.quo-preview.browser.browser-input :as browser-input]
[status-im2.contexts.quo-preview.code.snippet :as code-snippet]
[status-im2.contexts.quo-preview.code.snippet-preview :as code-snippet-preview]
[status-im2.contexts.quo-preview.graph.interactive-graph :as interactive-graph]
[status-im2.contexts.quo-preview.graph.wallet-graph :as wallet-graph]
[status-im2.contexts.quo-preview.colors.color-picker :as color-picker]
Expand Down Expand Up @@ -177,7 +178,9 @@
{:name :calendar-year
:component calendar-year/view}]
:code [{:name :snippet
:component code-snippet/view}]
:component code-snippet/view}
{:name :snippet-preview
:component code-snippet-preview/view}]
:colors [{:name :color-picker
:component color-picker/view}]
:community [{:name :community-card-view
Expand Down

0 comments on commit 0cf9538

Please sign in to comment.