Fork me on GitHub
#clerk
<
2023-10-06
>
Sean Swezey08:10:03

How do I use the ::clerk/viewers key in (clerk/table {::clerk/viewers ???} {…}) ? I’m trying to use a custom number formatter within the table. I’ve tried putting a modified viewer/table-viewer , a seq of it, a seq of viewer/table-viewers, only my modified viewer/number-viewer, and I can’t get anything to render. Most errors mention it can’t find a viewer with key ::viewer/table-viewer including if it’s the only item in there. I wasn’t able to find any examples within the Clerk demos.

Andrea12:10:37

hi @U05CLS4TF6Z both these example work with the latest clerk

(clerk/with-viewers
 (clerk/add-viewers [{:pred number? :render-fn '(fn [_ _] [:em "num"])}])
 (clerk/table [[1] [2]]))

(clerk/table
 {::clerk/viewers (clerk/add-viewers [{:pred number? :render-fn '(fn [_ _] [:em "num"])}])}
 [[1] [2]])

Andrea12:10:43

the point is with-viewers (and the associated ::clerk/viewers option) actually do specify all the available viewers, so clerk/dd-viewers prepends your custom ones to the list of the defaults

Sean Swezey12:10:02

I appreciate it, I couldn’t figure out that connecting piece from the docs

👍 1
Sean Swezey13:10:54

So interestingly, this doesn’t work when I want the number formatter to be used within the table. Attaching a snippet

Sean Swezey13:10:09

The number viewer must first be attached to a table viewer

Andrea13:10:46

that’s very odd, there should be no need to add the number viewer to the inner table viewers

Andrea13:10:24

I guess that has something to do with named viewers

Andrea13:10:29

in fact this works again:

(def n-viewer
  (dissoc (assoc viewer/number-viewer :render-fn '(fn [_ _] [:em "num"]))
          :name))
(clerk/table
 {::clerk/viewers (clerk/add-viewers [n-viewer])}
 [[1] [2]])

Sean Swezey13:10:24

Yep, dissocing the name allowed it to work

Andrea13:10:47

that must be something related to how add-viewers work in wrt named viewers, but I don’t recall why we did this: https://github.com/nextjournal/clerk/blob/fc6df7186bfea2571fff2dd790d0ad63f8d6295f/src/nextjournal/clerk/viewer.cljc#L666-L670 cc @U5H74UNSF?

Sean Swezey13:10:41

Either way, it’s an amazing tool and I want to thank yall for creating it!

mkvlr13:10:24

add-viewers! now does in-place positional replacement to make it easier to override them

mkvlr13:10:05

there’s also a first-class :add-viewers attribute on the table-viewer, also makes customization easier

mkvlr13:10:38

still ned to do a pass to clarify this in the docs

Sean Swezey13:10:29

I saw that originally, but wasn’t sure which was the preferred/recommended way to do

Andrea13:10:27

> Anonymous viewers (without a :name) or new named viewers will be prepended to the viewer stack I see, but having to dissoc the name of an existing viewer to have it take precedence sounds a bit odd

mkvlr13:10:39

(ns scratch
  (:require [nextjournal.clerk.viewer :as viewer]
            [nextjournal.clerk :as clerk]))

(def my-number-viewer
  (assoc viewer/number-viewer :render-fn '(fn [_ _] [:em "num"])))

(clerk/add-viewers! [(update viewer/table-viewer :add-viewers clerk/add-viewers [my-number-viewer])])

(clerk/table [[1] [2]])

mkvlr13:10:57

this is the recommended way, no need to dissoc anything

mkvlr13:10:20

but use :add-viewers instead of :nextjournal/viewers because the former takes precendence

mkvlr13:10:46

let me know if that makes sense

Sean Swezey13:10:31

Yep, makes sense. The add-viewers! changes the default viewers correct?

mkvlr13:10:03

or rather, if you used :nextjournal/viewers but left the :add-viewers on the table-viewer as-is, the table viewer would still use its original number viewer, so you woudln’t see the change

mkvlr13:10:13

yes, can also do it just locally

mkvlr13:10:30

add-viewers! changes the defaults for that namespace

ack 1
mkvlr13:10:03

@U9EQP1K0X let’s go over this and document it next week