2024-05-25 12:48:35 +00:00
|
|
|
(defpackage #:hsx/element
|
2024-05-25 03:00:39 +00:00
|
|
|
(:use #:cl)
|
|
|
|
(:export #:element-kind
|
|
|
|
#:element-props
|
2024-05-25 15:57:06 +00:00
|
|
|
#:element-children
|
2024-05-25 03:00:39 +00:00
|
|
|
#:create-element
|
|
|
|
#:expand))
|
2024-05-25 12:48:35 +00:00
|
|
|
(in-package #:hsx/element)
|
2024-05-25 03:00:39 +00:00
|
|
|
|
|
|
|
(defclass element ()
|
|
|
|
((kind
|
|
|
|
:reader element-kind
|
|
|
|
:initarg :kind)
|
|
|
|
(props
|
|
|
|
:reader element-props
|
2024-05-25 15:57:06 +00:00
|
|
|
:initarg :props)
|
|
|
|
(children
|
|
|
|
:reader element-children
|
|
|
|
:initarg :children)))
|
2024-05-25 03:00:39 +00:00
|
|
|
|
|
|
|
(defun create-element (kind props &rest children)
|
|
|
|
(make-instance 'element
|
|
|
|
:kind kind
|
2024-05-25 15:57:06 +00:00
|
|
|
:props props
|
|
|
|
:children (flatten children)))
|
2024-05-25 03:00:39 +00:00
|
|
|
|
2024-05-25 11:09:09 +00:00
|
|
|
(defmethod expand ((elm element))
|
|
|
|
(with-accessors ((kind element-kind)
|
2024-05-25 15:57:06 +00:00
|
|
|
(props element-props)
|
|
|
|
(children element-children)) elm
|
2024-05-25 11:09:09 +00:00
|
|
|
(if (functionp kind)
|
2024-05-25 15:57:06 +00:00
|
|
|
(apply kind (append props
|
|
|
|
(and children
|
|
|
|
(list :children children))))
|
2024-05-25 11:09:09 +00:00
|
|
|
elm)))
|
|
|
|
|
|
|
|
;;;; utils
|
|
|
|
|
2024-05-25 03:00:39 +00:00
|
|
|
(defun flatten (x)
|
|
|
|
(labels ((rec (x acc)
|
|
|
|
(cond ((null x) acc)
|
|
|
|
((atom x) (cons x acc))
|
|
|
|
(t (rec
|
|
|
|
(car x)
|
|
|
|
(rec (cdr x) acc))))))
|
|
|
|
(rec x nil)))
|