view.rs 3.5 KB
use mogwai::prelude::*;

use crate::data::icons::*;
use super::logic::MarkdownLogic;

pub(super) fn markdown_view( tx_logic: broadcast::Sender<MarkdownLogic>
                           , rx_toggle: broadcast::Receiver<bool>
                           , rx_patches: mpmc::Receiver<ListPatch<ViewBuilder<Dom>>>
                           , tx_dom: broadcast::Sender<Dom>
                           ) -> ViewBuilder<Dom>
{
    let input_filter = tx_logic
                     . sink()
                     . contra_map(|_| MarkdownLogic::Update);
    let store_filter = tx_logic
                     . sink()
                     . contra_map(|_| MarkdownLogic::Store);
    let toggle_filter = tx_logic
                      . sink()
                      . contra_map(|_| MarkdownLogic::Toggle);
    let select_filter = tx_logic
                      . sink()
                      . contra_map(|_e| MarkdownLogic::Select);
    let discard_filter = tx_logic
                       . sink()
                       . contra_map(|_| MarkdownLogic::Discard);

    let (tx_toggle_editor, rx_toggle_editor) = broadcast::bounded(1);
    let ( tx_toggle_top_padding
        , rx_toggle_top_padding) = broadcast::bounded(1);

    /*
     * This takes a bool toggle value and turns it into various view changes
     * by filling internal channels accordingly...
     * Is that the way to go?
     */
    mogwai::spawn( rx_toggle
                 . for_each(move |toggle| {
                     let tx_toggle_editor = tx_toggle_editor.clone();
                     let tx_toggle_top_padding = tx_toggle_top_padding.clone();
                     async move {
                         match toggle {
                             true => {
                                 tx_toggle_editor . broadcast(String::from("block"))
                                                  . await.unwrap();
                                 tx_toggle_top_padding . broadcast(String::from("13em"))
                                                       . await.unwrap();
                             },
                             false => {
                                 tx_toggle_editor . broadcast(String::from("none"))
                                                  . await.unwrap();
                                 tx_toggle_top_padding . broadcast(String::from("0"))
                                                       . await.unwrap();
                             },
                         }
                     }})
                 );

    builder! {
        <div class="markdown"
             style:width="33%"
             post:build=move |_: &mut Dom| {
                 tx_logic.try_broadcast(MarkdownLogic::Choose(None)).unwrap();
             }
             capture:view=tx_dom.sink()>
            <div style:cursor="text">
                <div>
                    <div>
                    <button on:click=select_filter>{select_icon()}</button>
                    <ul patch:children=rx_patches>
                    </ul>
                    </div>
                    <button on:click=store_filter>{save_icon()}</button>
                    <button on:click=discard_filter>{undo_point_icon()}</button>
                    <button on:click=toggle_filter>{edit_icon()}</button>
                </div>
                <pre contenteditable="true"
                     style:display=("none", rx_toggle_editor)
                     on:input=input_filter>
                </pre>
            </div>
            <div style:padding_top=("0", rx_toggle_top_padding)></div>
        </div>
    }
}