chore: slide code

This commit is contained in:
SouthFox 2024-10-28 16:25:14 +08:00
parent 67091c5df1
commit 493991ef8f

View file

@ -27,6 +27,25 @@
(hoot hashtables) (hoot hashtables)
(ice-9 match)) (ice-9 match))
(define *template* '())
(define (wrap-template template)
`(div (@ (id "container"))
(button (@ (click ,(lambda (event)
(set! *template* template-click)
(render))))
"Click")
(button (@ (click ,(lambda (event)
(set! *template* template-task)
(render))))
"Task")
(div (@ (id "application")) ,(template))))
(define (render)
(let ((old (get-element-by-id "container")))
(unless (external-null? old) (remove! old))
(append-child! (document-body) (sxml->dom (wrap-template *template*)))))
(define (sxml->dom exp) (define (sxml->dom exp)
(match exp (match exp
;; The simple case: a string representing a text node. ;; The simple case: a string representing a text node.
@ -67,10 +86,8 @@
(children (add-children children))) (children (add-children children)))
elem)))) elem))))
(define *clicks* 0)
(define *template* '())
;; Click ;; Click
(define *clicks* 0)
(define (template-click) (define (template-click)
`(div `(div
(p ,(number->string *clicks*) " clicks") (p ,(number->string *clicks*) " clicks")
@ -80,11 +97,6 @@
"Click me!"))) "Click me!")))
;; Task ;; Task
(define (render)
(let ((old (get-element-by-id "container")))
(unless (external-null? old) (remove! old))
(append-child! (document-body) (sxml->dom (wrap-template *template*)))))
(define-record-type <task> (define-record-type <task>
(make-task name done?) (make-task name done?)
task? task?
@ -92,10 +104,8 @@
(done? task-done? set-task-done!)) (done? task-done? set-task-done!))
(define *tasks* '()) (define *tasks* '())
(define (add-task! task) (define (add-task! task)
(set! *tasks* (cons task *tasks*))) (set! *tasks* (cons task *tasks*)))
(define (remove-task! task) (define (remove-task! task)
(set! *tasks* (delq task *tasks*))) (set! *tasks* (delq task *tasks*)))
@ -109,12 +119,10 @@
(render)))) (render))))
(checked ,(task-done? task)))) (checked ,(task-done? task))))
(span (@ (style "padding: 0 1em 0 1em;")) (span (@ (style "padding: 0 1em 0 1em;"))
;; Strikethrough if task is done.
,(if (task-done? task) ,(if (task-done? task)
`(s ,(task-name task)) `(s ,(task-name task))
(task-name task))) (task-name task)))
(a (@ (href "#") (a (@ (href "#")
;; Remove task on click.
(click ,(lambda (event) (click ,(lambda (event)
(remove-task! task) (remove-task! task)
(render)))) (render))))
@ -136,17 +144,5 @@
"Add task"))) "Add task")))
;; Main ;; Main
(define (wrap-template template)
`(div (@ (id "container"))
(button (@ (click ,(lambda (event)
(set! *template* template-click)
(render))))
"Click")
(button (@ (click ,(lambda (event)
(set! *template* template-task)
(render))))
"Task")
(div (@ (id "application")) ,(template))))
(set! *template* template-task) (set! *template* template-task)
(render) (render)