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