From 85167a06a5f7520adee4b4c3ac853c12c81bd29f Mon Sep 17 00:00:00 2001 From: Chris Badahdah Date: Tue, 27 Aug 2024 22:57:48 -0700 Subject: [PATCH] Add docs for `diff-text` --- dev/tasks/docs.clj | 3 +++ resources/viewers.edn | 1 + src/portal/ui/viewer/diff_text.cljs | 15 +++++++++------ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/dev/tasks/docs.clj b/dev/tasks/docs.clj index 63e111de..9b91f9f9 100644 --- a/dev/tasks/docs.clj +++ b/dev/tasks/docs.clj @@ -94,6 +94,9 @@ ::v/diff {:file "portal/ui/viewer/diff.cljs" :examples [(vary-meta d/diff-data dissoc ::v/default)]} + ::v/diff-text + {:file "portal/ui/viewer/diff_text.cljs" + :examples [d/diff-text-data]} ::v/tree {:examples [(vary-meta d/hiccup dissoc ::v/default)]} ::v/code diff --git a/resources/viewers.edn b/resources/viewers.edn index 25f77250..b2f90a43 100644 --- a/resources/viewers.edn +++ b/resources/viewers.edn @@ -57,6 +57,7 @@ {:name :portal.viewer/diff, :doc "Diff a collection of values successively starting with the first two."} + {:name :portal.viewer/diff-text, :doc "Diff two strings."} {:name :portal.viewer/markdown, :doc "Parse string as markdown and view as html."} {:name :portal.viewer/hiccup, diff --git a/src/portal/ui/viewer/diff_text.cljs b/src/portal/ui/viewer/diff_text.cljs index 4c66ea45..48ce42fb 100644 --- a/src/portal/ui/viewer/diff_text.cljs +++ b/src/portal/ui/viewer/diff_text.cljs @@ -1,5 +1,6 @@ (ns portal.ui.viewer.diff-text (:require ["diff" :as df] + [clojure.spec.alpha :as s] [clojure.string :as str] [portal.colors :as c] [portal.ui.icons :as icons] @@ -8,6 +9,13 @@ [portal.ui.styled :as d] [portal.ui.theme :as theme])) +;;; :spec +(s/def ::diff-text (s/cat :a string? :b string?)) +;;; + +(defn- diff-text? [value] + (s/valid? ::diff-text value)) + (defn- changed? [^js item] (or (some-> item .-added) (some-> item .-removed))) @@ -106,13 +114,8 @@ :border-bottom-left-radius (:border-radius theme) :border-bottom-right-radius (:border-radius theme)}}]])) -(defn- text-diff? [value] - (and (coll? value) - (string? (first value)) - (string? (second value)))) - (def viewer - {:predicate text-diff? + {:predicate diff-text? :component inspect-text-diff :name :portal.viewer/diff-text :doc "Diff two strings."}) \ No newline at end of file