Skip to content

Commit

Permalink
chore: refactor code snippet component
Browse files Browse the repository at this point in the history
  • Loading branch information
codemaster115 committed Sep 19, 2023
1 parent 1bc9b3c commit 19c4e28
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 327 deletions.
89 changes: 0 additions & 89 deletions src/quo2/components/code/code/style.cljs

This file was deleted.

1 change: 0 additions & 1 deletion src/quo2/components/code/common/style.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns quo2.components.code.common.style
(:require [quo2.foundations.colors :as colors]))


;; Example themes:
;; https://github.com/react-syntax-highlighter/react-syntax-highlighter/tree/master/src/styles/hljs
(defn theme
Expand Down
2 changes: 1 addition & 1 deletion src/quo2/components/code/common/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
:on-press on-copy-press}
:main-icons/copy]]])]))

(defn snippet
(defn view
[{:keys [language max-lines on-copy-press preview?]} children]
[highlighter/highlighter
{:language language
Expand Down
110 changes: 0 additions & 110 deletions src/quo2/components/code/snippet.cljs

This file was deleted.

15 changes: 15 additions & 0 deletions src/quo2/components/code/snippet/view.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(ns quo2.components.code.snippet.view
(:require [quo2.theme :as theme]
[quo2.components.code.common.view :as code-common]))

(defn- view-internal
[_]
(fn [{:keys [language max-lines on-copy-press]} children]
[code-common/view
{:language language
:max-lines max-lines
:on-copy-press on-copy-press
:preview? false}
children]))

(def view (theme/with-theme view-internal))
39 changes: 0 additions & 39 deletions src/quo2/components/code/snippet_preview/style.cljs

This file was deleted.

94 changes: 9 additions & 85 deletions src/quo2/components/code/snippet_preview/view.cljs
Original file line number Diff line number Diff line change
@@ -1,86 +1,10 @@
(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))
(:require [quo2.components.code.common.view :as code-common]))

(defn view
[{:keys [language]} children]
[code-common/view
{:preview? true
:language language
:max-lines 0}
children])
4 changes: 2 additions & 2 deletions src/quo2/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
quo2.components.calendar.calendar.view
quo2.components.calendar.calendar-day.view
quo2.components.calendar.calendar-year.view
quo2.components.code.snippet
quo2.components.code.snippet.view
quo2.components.code.snippet-preview.view
quo2.components.colors.color-picker.view
quo2.components.common.notification-dot.view
Expand Down Expand Up @@ -165,7 +165,7 @@
(def calendar-year quo2.components.calendar.calendar-year.view/view)

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

;;;; Cards
Expand Down

0 comments on commit 19c4e28

Please sign in to comment.