diff --git a/sx/sx/pretext-client.sx b/sx/sx/pretext-client.sx index 29e45891..168c5302 100644 --- a/sx/sx/pretext-client.sx +++ b/sx/sx/pretext-client.sx @@ -1,4 +1,4 @@ -;; Pretext island — full version with :ref (bug fix applied) +;; Pretext island — effect as let binding (defisland ~pretext-demo/live () @@ -10,99 +10,97 @@ (doc (host-global "document")) (canvas (host-call doc "createElement" "canvas")) (ctx (host-call canvas "getContext" "2d")) - (el-ref (signal nil))) - (effect - (fn - () - (let - ((el (deref el-ref)) - (w (deref mxw)) - (sz (deref font-size)) - (opt (deref use-optimal))) - (when - el + (el-ref (signal nil)) + (_eff + (effect + (fn + () (let - ((lh (* sz 1.5))) - (host-set! - ctx - "font" - (str sz "px 'Pretext Serif', DejaVu Serif, serif")) - (let - ((widths (map (fn (wd) (host-get (host-call ctx "measureText" wd) "width")) words)) - (spw (host-get (host-call ctx "measureText" " ") "width"))) + ((el (deref el-ref)) + (w (deref mxw)) + (sz (deref font-size)) + (opt (deref use-optimal))) + (when + el + (host-set! + ctx + "font" + (str sz "px 'Pretext Serif', DejaVu Serif, serif")) (let - ((ranges (if opt (break-lines widths spw w) (break-lines-greedy widths spw w))) - (lines - (pretext-layout-lines - words - widths - (if - opt - (break-lines widths spw w) - (break-lines-greedy widths spw w)) - spw - w - lh))) - (host-set! el "innerHTML" "") + ((lh (* sz 1.5)) + (widths + (map + (fn + (wd) + (host-get (host-call ctx "measureText" wd) "width")) + words)) + (spw + (host-get (host-call ctx "measureText" " ") "width"))) (let - ((info (host-call doc "createElement" "div"))) - (host-set! info "className" "px-4 pt-3 pb-1") - (host-set! - info - "innerHTML" - (str - "" - (len lines) - " lines — " - w - "px / " - sz - "px / " - (if opt "optimal" "greedy") - "")) - (host-call el "appendChild" info)) - (let - ((container (host-call doc "createElement" "div"))) - (host-set! - container - "style" - (str - "position:relative;height:" - (* (len lines) lh) - "px;padding:12px 16px")) + ((ranges (if opt (break-lines widths spw w) (break-lines-greedy widths spw w)))) (let - render-lines - ((li 0)) - (when - (< li (len lines)) - (let - ((line (nth lines li)) - (wds (get (nth lines li) :words))) + ((lines (pretext-layout-lines words widths ranges spw w lh)) + (info (host-call doc "createElement" "div")) + (container (host-call doc "createElement" "div"))) + (host-set! el "innerHTML" "") + (host-set! info "className" "px-4 pt-3 pb-1") + (host-set! + info + "innerHTML" + (str + "" + (len lines) + " lines — " + w + "px / " + sz + "px / " + (if opt "optimal" "greedy") + "")) + (host-call el "appendChild" info) + (host-set! + container + "style" + (str + "position:relative;height:" + (+ (* (len lines) lh) 24) + "px;padding:12px 16px")) + (let + rl + ((li 0)) + (when + (< li (len lines)) (let - render-words - ((wi 0)) - (when - (< wi (len wds)) - (let - ((pw (nth wds wi)) - (span - (host-call doc "createElement" "span"))) - (host-set! span "textContent" (get pw :word)) - (host-set! - span - "style" - (str - "position:absolute;left:" - (+ (get pw :x) 16) - "px;top:" - (+ (get line :y) 12) - "px;font:" - sz - "px 'Pretext Serif',serif;white-space:nowrap")) - (host-call container "appendChild" span) - (render-words (+ wi 1)))))) - (render-lines (+ li 1)))) - (host-call el "appendChild" container))))))))) + ((line (nth lines li)) + (wds (get (nth lines li) :words))) + (let + rw + ((wi 0)) + (when + (< wi (len wds)) + (let + ((pw (nth wds wi)) + (span + (host-call doc "createElement" "span"))) + (host-set! + span + "textContent" + (get pw :word)) + (host-set! + span + "style" + (str + "position:absolute;left:" + (+ (get pw :x) 16) + "px;top:" + (+ (get line :y) 12) + "px;font:" + sz + "px 'Pretext Serif',serif;white-space:nowrap")) + (host-call container "appendChild" span) + (rw (+ wi 1)))))) + (rl (+ li 1)))) + (host-call el "appendChild" container)))))))))) (div (~tw :tokens "space-y-4") (div @@ -144,6 +142,6 @@ :on-click (fn (e) (reset! use-optimal (not (deref use-optimal)))) (if (deref use-optimal) "Knuth-Plass" "Greedy")))) (div - :class "rounded-lg border border-stone-200 bg-white overflow-hidden" + :class "rounded-lg border border-stone-200 bg-white" :ref (fn (el) (reset! el-ref el)) "")))) \ No newline at end of file