Skip to main content

perspective_viewer/components/
expression_editor.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use std::rc::Rc;
14
15use perspective_client::{ExprValidationError, clone};
16use yew::prelude::*;
17
18use super::form::code_editor::*;
19use crate::session::{Session, SessionMetadata, SessionMetadataRc};
20use crate::tasks::{ExprValidation, validate_expression};
21
22#[derive(Properties, PartialEq, Clone)]
23pub struct ExpressionEditorProps {
24    pub on_save: Callback<()>,
25    pub on_validate: Callback<bool>,
26    pub on_input: Callback<Rc<String>>,
27    pub alias: Option<String>,
28    pub disabled: bool,
29
30    #[prop_or_default]
31    pub reset_count: u8,
32
33    /// Session metadata snapshot — threaded from `SessionProps`.
34    pub metadata: SessionMetadataRc,
35
36    /// Selected theme name, threaded for PortalModal consumers.
37    #[prop_or_default]
38    pub selected_theme: Option<String>,
39
40    // State
41    pub session: Session,
42}
43
44#[derive(Debug)]
45pub enum ExpressionEditorMsg {
46    SetExpr(Rc<String>),
47    ValidateComplete(ExprValidation),
48}
49
50/// Expression editor component `CodeEditor` and a button toolbar.
51pub struct ExpressionEditor {
52    expr: Rc<String>,
53    error: Option<ExprValidationError>,
54    oninput: Callback<Rc<String>>,
55
56    /// Monotonically increasing request id used to drop stale
57    /// validation results when the user types faster than the engine
58    /// can validate.
59    validation_req_id: u64,
60
61    /// The id of the most recently dispatched validation; the result
62    /// is only applied when its echoed id matches.
63    last_dispatched_req_id: u64,
64}
65
66impl Component for ExpressionEditor {
67    type Message = ExpressionEditorMsg;
68    type Properties = ExpressionEditorProps;
69
70    fn create(ctx: &Context<Self>) -> Self {
71        let oninput = ctx.link().callback(ExpressionEditorMsg::SetExpr);
72        let expr = initial_expr(&ctx.props().metadata, &ctx.props().alias);
73        ctx.link()
74            .send_message(Self::Message::SetExpr(expr.clone()));
75
76        Self {
77            error: None,
78            expr,
79            oninput,
80            validation_req_id: 0,
81            last_dispatched_req_id: 0,
82        }
83    }
84
85    fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
86        match msg {
87            ExpressionEditorMsg::SetExpr(val) => {
88                ctx.props().on_input.emit(val.clone());
89                self.expr = val.clone();
90                self.validation_req_id += 1;
91                self.last_dispatched_req_id = self.validation_req_id;
92                let cb = ctx.link().callback(ExpressionEditorMsg::ValidateComplete);
93                validate_expression(
94                    &ctx.props().session,
95                    cb,
96                    self.validation_req_id,
97                    (*val).clone(),
98                );
99                true
100            },
101            ExpressionEditorMsg::ValidateComplete(result) => {
102                if result.req_id != self.last_dispatched_req_id {
103                    // Stale result from a superseded request — ignore.
104                    return false;
105                }
106                self.error = result.error;
107                if self.error.is_none() {
108                    let _: Option<bool> = try {
109                        let alias = ctx.props().alias.as_ref()?;
110                        let session = &ctx.props().session;
111                        let old = ctx.props().metadata.get_expression_by_alias(alias)?;
112                        let is_edited = *self.expr != old;
113                        session
114                            .metadata_mut()
115                            .set_edit_by_alias(alias, self.expr.to_string());
116
117                        is_edited
118                    };
119
120                    ctx.props().on_validate.emit(true);
121                } else {
122                    ctx.props().on_validate.emit(false);
123                }
124                true
125            },
126        }
127    }
128
129    fn view(&self, ctx: &Context<Self>) -> Html {
130        let disabled_class = ctx.props().disabled.then_some("disabled");
131        clone!(ctx.props().disabled);
132        html! {
133            <>
134                <label class="item_title">{ "Expression" }</label>
135                <div id="editor-container" class={disabled_class}>
136                    <CodeEditor
137                        autofocus=true
138                        expr={&self.expr}
139                        autosuggest=true
140                        error={self.error.clone().map(|x| x.into())}
141                        {disabled}
142                        oninput={self.oninput.clone()}
143                        onsave={ctx.props().on_save.clone()}
144                        theme={ctx.props().selected_theme.clone().unwrap_or_default()}
145                    />
146                    <div id="psp-expression-editor-meta">
147                        <div class="error">
148                            { &self.error.clone().map(|e| e.error_message).unwrap_or_default() }
149                        </div>
150                    </div>
151                </div>
152            </>
153        }
154    }
155
156    fn changed(&mut self, ctx: &Context<Self>, old_props: &Self::Properties) -> bool {
157        if ctx.props().alias != old_props.alias
158            || ctx.props().reset_count != old_props.reset_count
159            || (ctx.props().alias.is_some() && ctx.props().metadata != old_props.metadata)
160        {
161            ctx.link()
162                .send_message(ExpressionEditorMsg::SetExpr(initial_expr(
163                    &ctx.props().metadata,
164                    &ctx.props().alias,
165                )));
166            false
167        } else {
168            true
169        }
170    }
171}
172
173fn initial_expr(metadata: &SessionMetadata, alias: &Option<String>) -> Rc<String> {
174    alias
175        .as_ref()
176        .and_then(|alias| metadata.get_expression_by_alias(alias))
177        .unwrap_or_default()
178        .into()
179}