Skip to content

Commit

Permalink
add memo_once; fmt code; tag 0.0.19
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Dec 6, 2024
1 parent 32eac27 commit 5fb2c18
Show file tree
Hide file tree
Showing 34 changed files with 469 additions and 68 deletions.
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiye/respo",
"version": "0.0.18",
"version": "0.0.19",
"deps": {
"tiye/dom-ffi": "0.0.6",
"tiye/cirru-parser": "0.0.9"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mbt.respo",
"version": "0.0.18",
"version": "0.0.19",
"main": "index.js",
"repository": "git@github.com:Respo/respo.mbt.git",
"author": "tiye <jiyinyiyong@gmail.com>",
Expand Down
7 changes: 4 additions & 3 deletions src/lib/app.mbt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/// get basic App structure
///| get basic App structure
pub(all) struct RespoApp[Model] {
store : Ref[Model]
storage_key : String
mount_target : @dom_ffi.Node
}

/// backup store to local storage before unload

///| backup store to local storage before unload
pub fn backup_model_beforeunload[Model : ToJson](
self : RespoApp[Model]
) -> Unit {
Expand All @@ -22,6 +21,7 @@ pub fn backup_model_beforeunload[Model : ToJson](
window.set_onbeforeunload(beforeunload)
}

///|
pub fn try_load_storage[Model : @json.FromJson + Default](
key : String
) -> Model {
Expand Down Expand Up @@ -50,6 +50,7 @@ pub fn try_load_storage[Model : @json.FromJson + Default](
}
}

///|
pub fn RespoApp::render_loop[Model, ActionOp](
self : RespoApp[Model],
renderer : () -> @node.RespoNode[ActionOp]!@node.RespoCommonError,
Expand Down
10 changes: 8 additions & 2 deletions src/lib/dialog/alert.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// The options for alert modal.
///| The options for alert modal.
// #[derive(Debug, Clone, Default)]
pub(all) struct AlertOptions {
/// inline style for backdrop
Expand All @@ -11,6 +11,7 @@ pub(all) struct AlertOptions {
button_text : String?
} derive(Default)

///|
fn comp_alert_modal[T](
options : AlertOptions,
show : Bool,
Expand Down Expand Up @@ -104,12 +105,13 @@ fn comp_alert_modal[T](
}

// #[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, RespoState)]
///|
struct AlertPluginState {
show : Bool
text : String?
} derive(Default, ToJson, @json.FromJson)

/// abstraction for Alert modal, new with `AlertOption`,
///| abstraction for Alert modal, new with `AlertOption`,
/// just displaying a message, you read it, you close it
pub(all) struct AlertPlugin[T] {
state : AlertPluginState
Expand All @@ -120,6 +122,7 @@ pub(all) struct AlertPlugin[T] {
on_read : (@node.DispatchFn[T]) -> Unit
}

///|
pub fn render[T : @node.RespoAction](
self : AlertPlugin[T]
) -> @node.RespoNode[T] {
Expand Down Expand Up @@ -172,6 +175,7 @@ pub fn render[T : @node.RespoAction](
)
}

///|
pub fn show[T : @node.RespoAction](
self : AlertPlugin[T],
dispatch : @node.DispatchFn[T],
Expand All @@ -180,6 +184,7 @@ pub fn show[T : @node.RespoAction](
dispatch.run_state!(self.cursor, AlertPluginState::{ show: true, text })
}

///|
pub fn close[T : @node.RespoAction](
self : AlertPlugin[T],
dispatch : @node.DispatchFn[T]
Expand All @@ -194,6 +199,7 @@ pub fn close[T : @node.RespoAction](
}
}

///|
pub fn AlertPlugin::new[T : @node.RespoAction](
states : @respo.RespoStatesTree,
options : AlertOptions,
Expand Down
11 changes: 9 additions & 2 deletions src/lib/dialog/confirm.mbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
///|
let next_task_name : String = "_RESPO_CONFIRM_NEXT_TASK"

/// options for confirm dialog
///| options for confirm dialog
pub(all) struct ConfirmOptions {
/// inline style for backdrop
backdrop_style : @node.RespoStyle
Expand All @@ -12,8 +13,10 @@ pub(all) struct ConfirmOptions {
button_text : String?
} derive(Default)

///|
let button_name : String = "dialog-button"

///|
fn comp_confirm_modal[T](
options : ConfirmOptions,
show : Bool,
Expand Down Expand Up @@ -117,12 +120,13 @@ fn comp_confirm_modal[T](
).to_node()
}

///|
struct ConfirmPluginState {
show : Bool
text : String?
} derive(Default, ToJson, @json.FromJson)

/// Popup a confirmation dialog, confirm to process next task
///| Popup a confirmation dialog, confirm to process next task
pub(all) struct ConfirmPlugin[T] {
state : ConfirmPluginState
options : ConfirmOptions
Expand All @@ -132,6 +136,7 @@ pub(all) struct ConfirmPlugin[T] {
on_confirm : (@node.DispatchFn[T]) -> Unit
}

///|
pub fn render[T : @node.RespoAction](
self : ConfirmPlugin[T]
) -> @node.RespoNode[T] {
Expand Down Expand Up @@ -175,6 +180,7 @@ pub fn render[T : @node.RespoAction](
)
}

///|
pub fn show[T : @node.RespoAction](
self : ConfirmPlugin[T],
dispatch : @node.DispatchFn[T],
Expand All @@ -186,6 +192,7 @@ pub fn show[T : @node.RespoAction](
dispatch.run_state!(self.cursor, s)
}

///|
pub fn ConfirmPlugin::new[T](
states : @respo.RespoStatesTree,
options : ConfirmOptions,
Expand Down
20 changes: 18 additions & 2 deletions src/lib/dialog/dialogs.mbt
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
///|
struct EffectFocus {
show : Bool
} derive(Eq, ToJson, @json.FromJson)

///|
impl @node.RespoEffect for EffectFocus with updated(self, el) -> Unit {
let show : Bool = self.show
if show {
focus_element(el, button_name)
}
}

///|
fn focus_element(el : @dom_ffi.Node, name : String) -> Unit {
let element = el.reinterpret_as_element().query_selector("." + name) // TODO need to handle None in FFI
element.focus()
// app::util::warn_log!("Attempted to focus on element '{}', but it was not found in the DOM.", name);
}

///|
struct EffectModalFade {
show : Bool
} derive(Eq, ToJson, @json.FromJson)

///|
impl @node.RespoEffect for EffectModalFade with before_update(self, el) {
let show = self.show
if not(show) {
Expand All @@ -45,6 +50,7 @@ impl @node.RespoEffect for EffectModalFade with before_update(self, el) {
}
}

///|
impl @node.RespoEffect for EffectModalFade with updated(self, el) {
let show : Bool = self.show
if show {
Expand All @@ -64,10 +70,12 @@ impl @node.RespoEffect for EffectModalFade with updated(self, el) {
}
}

///|
struct EffectDrawerFade {
show : Bool
} derive(Eq, ToJson, @json.FromJson)

///|
impl @node.RespoEffect for EffectDrawerFade with before_update(self, el) {
let show = self.show
if not(show) {
Expand All @@ -94,6 +102,7 @@ impl @node.RespoEffect for EffectDrawerFade with before_update(self, el) {
}
}

///|
impl @node.RespoEffect for EffectDrawerFade with updated(self, el) {
let show = self.show
if show {
Expand All @@ -113,8 +122,10 @@ impl @node.RespoEffect for EffectDrawerFade with updated(self, el) {
}
}

///|
type EffectModalClose Unit derive(Eq, ToJson, @json.FromJson)

///|
impl @node.RespoEffect for EffectModalClose with mounted(_self, el) {
let window = @dom_ffi.window()
let listener = fn(event : @dom_ffi.KeyboardEvent) {
Expand All @@ -138,6 +149,7 @@ impl @node.RespoEffect for EffectModalClose with mounted(_self, el) {

}

///|
impl @node.RespoEffect for EffectModalClose with before_unmount(_self, el) {
let el = el.reinterpret_as_element()
let listener = el.dirty_get_attribute_fn(temp_listener) // dirty
Expand All @@ -147,10 +159,10 @@ impl @node.RespoEffect for EffectModalClose with before_unmount(_self, el) {

}

/// put listener on the element, directly on the element
///| put listener on the element, directly on the element
let temp_listener : String = "temp_listener"

/// handle global keydown event
///| handle global keydown event
pub fn comp_esc_listener[T](
_show : Bool,
on_close : (@node.DispatchFn[T]) -> Unit
Expand All @@ -175,6 +187,7 @@ pub fn comp_esc_listener[T](
).to_node()
}

///|
let css_backdrop : String = declare_static_style(
[
(
Expand All @@ -188,6 +201,7 @@ let css_backdrop : String = declare_static_style(
],
)

///|
let css_modal_card : String = declare_static_style(
[
(
Expand All @@ -207,6 +221,7 @@ let css_modal_card : String = declare_static_style(
],
)

///|
let css_drawer_card : String = declare_static_style(
[
(
Expand All @@ -229,6 +244,7 @@ let css_drawer_card : String = declare_static_style(
],
)

///|
let css_button : String = declare_static_style(
[
(
Expand Down
15 changes: 13 additions & 2 deletions src/lib/dialog/drawer.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// The options for custom drawer.
///| The options for custom drawer.
// #[derive(Debug, Clone, Default)]
pub(all) struct DrawerOptions[T] {
/// inline style for backdrop
Expand All @@ -11,18 +11,22 @@ pub(all) struct DrawerOptions[T] {
render : DrawerRenderer[T]
} derive(Default)

///|
type DrawerRenderer[T] ((@node.DispatchFn[T]) -> Unit!@node.RespoCommonError) -> @node.RespoNode[
T,
]!@node.RespoCommonError

///|
fn to_string[T](self : DrawerRenderer[T]) -> String {
"(DrawerRenderer \{self})"
}

///|
pub fn DrawerRenderer::default[T]() -> DrawerRenderer[T] {
fn(_close) { div([]) }
}

///|
pub fn DrawerRenderer::new[T](
renderer : ((@node.DispatchFn[T]) -> Unit!@node.RespoCommonError) -> @node.RespoNode[
T,
Expand All @@ -31,6 +35,7 @@ pub fn DrawerRenderer::new[T](
renderer
}

///|
fn run[T](
self : DrawerRenderer[T],
close : (@node.DispatchFn[T]) -> Unit!@node.RespoCommonError
Expand All @@ -39,6 +44,7 @@ fn run[T](
f!(close)
}

///|
fn comp_drawer[T](
options : DrawerOptions[T],
show : Bool,
Expand Down Expand Up @@ -117,11 +123,12 @@ fn comp_drawer[T](
).to_node()
}

///|
struct DrawerPluginState {
show : Bool
} derive(Default, ToJson, @json.FromJson)

/// a drawer that you can render you down card body
///| a drawer that you can render you down card body
// #[derive(Debug, Clone)]
pub(all) struct DrawerPlugin[T] {
state : DrawerPluginState
Expand All @@ -130,6 +137,7 @@ pub(all) struct DrawerPlugin[T] {
cursor : Array[String]
}

///|
pub fn render[T : @node.RespoAction](
self : DrawerPlugin[T]
) -> @node.RespoNode[T]!@node.RespoCommonError {
Expand All @@ -147,20 +155,23 @@ pub fn render[T : @node.RespoAction](
)
}

///|
pub fn show[T : @node.RespoAction](
self : DrawerPlugin[T],
dispatch : @node.DispatchFn[T]
) -> Unit!@node.RespoCommonError {
dispatch.run_state!(self.cursor, DrawerPluginState::{ show: true })
}

///|
pub fn close[T : @node.RespoAction](
self : DrawerPlugin[T],
dispatch : @node.DispatchFn[T]
) -> Unit!@node.RespoCommonError {
dispatch.run_state!(self.cursor, DrawerPluginState::{ show: false })
}

///|
pub fn DrawerPlugin::new[T : @node.RespoAction](
states : @respo.RespoStatesTree,
options : DrawerOptions[T]
Expand Down
Loading

0 comments on commit 5fb2c18

Please sign in to comment.