/
mod.rs
171 lines (151 loc) · 5.11 KB
/
mod.rs
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
//! Monaco editor as a [Yew](https://yew.rs) component.
//! Requires the "yew" feature.
use crate::{
api::{CodeEditor as CodeEditorModel, TextModel},
sys::editor::IStandaloneEditorConstructionOptions,
};
use std::{cell::RefCell, rc::Rc};
use web_sys::HtmlElement;
use yew::{html, Callback, Classes, Component, Context, Html, NodeRef, Properties};
#[derive(Clone, Debug, PartialEq, Properties)]
pub struct CodeEditorProps<OPT: std::cmp::PartialEq + Clone + Into<IStandaloneEditorConstructionOptions> = IStandaloneEditorConstructionOptions> {
#[prop_or_default]
pub link: Option<CodeEditorLink>,
/// Changing the options will cause the editor to be re-created.
#[prop_or_default]
pub options: Option<OPT>,
#[prop_or_default]
pub model: Option<TextModel>,
/// This could be called multiple times if the `options` field changes.
/// You can use this to initialise the editor
#[prop_or_default]
pub on_editor_created: Callback<CodeEditorLink>,
#[prop_or_default]
pub classes: Classes,
}
type ModelCell = Rc<RefCell<Option<CodeEditorModel>>>;
/// CodeEditor component.
#[derive(Debug)]
pub struct CodeEditor {
node_ref: NodeRef,
editor: ModelCell,
}
impl CodeEditor {
fn emit_editor_created(&self, ctx: &Context<Self>) {
let CodeEditorProps {
link,
on_editor_created,
..
} = &ctx.props();
// reuse the link we were given or create a new one
let link = link
.clone()
.unwrap_or_else(|| CodeEditorLink::new_connected(self.editor.clone()));
on_editor_created.emit(link);
}
}
impl Component for CodeEditor {
type Message = ();
type Properties = CodeEditorProps;
fn create(ctx: &Context<Self>) -> Self {
let editor = ModelCell::default();
if let Some(editor_link) = &ctx.props().link {
editor_link.connect(editor.clone());
}
Self {
node_ref: NodeRef::default(),
editor,
}
}
fn changed(&mut self, ctx: &Context<Self>, old_props: &Self::Properties) -> bool {
// these are the new values
let CodeEditorProps {
link,
options,
model,
on_editor_created: _,
..
} = &ctx.props();
let mut should_render = false;
if link != &old_props.link {
// make sure to connect the new link to this component
if let Some(link) = &link {
link.connect(self.editor.clone());
}
}
// changing options requires re-create
if options != &old_props.options {
should_render = true;
}
// change the attached model
if model != &old_props.model {
if let Some(editor) = &mut *self.editor.borrow_mut() {
match model {
Some(model) => editor.set_model(model),
None => {
editor.detach_model();
}
}
}
}
if should_render {
// if we're gonna re-create the editor we need to clean up the old one first so
// as not to cause issues
self.editor.replace(None);
}
should_render
}
fn view(&self, ctx: &Context<Self>) -> Html {
let Self {
node_ref, editor, ..
} = self;
let props = ctx.props();
debug_assert!(
editor.borrow().is_none(),
"previous editor must be disposed before re-creating"
);
html! {
<div ref={node_ref.clone()} class={props.classes.clone()} />
}
}
fn rendered(&mut self, ctx: &Context<Self>, _first_render: bool) {
let el = self
.node_ref
.cast::<HtmlElement>()
.expect("failed to resolve editor element");
let props = ctx.props();
let editor = CodeEditorModel::create(&el, props.options.clone());
if let Some(model) = &props.model {
// initially we only update the model if it was actually given as a prop.
// this way a value or model can be given in the options and it won't be
// detached immediately
editor.set_model(model)
}
self.editor.replace(Some(editor));
self.emit_editor_created(ctx);
}
}
/// Link to control a [`CodeEditor`].
#[derive(Clone, Debug, Default)]
pub struct CodeEditorLink(RefCell<ModelCell>);
impl CodeEditorLink {
pub fn new() -> Self {
Self(RefCell::default())
}
fn new_connected(model_cell: ModelCell) -> Self {
Self(RefCell::new(model_cell))
}
fn connect(&self, model_cell: ModelCell) {
self.0.replace(model_cell);
}
/// Get access to the underlying [`CodeEditor`].
/// The return value is `None` if the link isn't connected.
pub fn with_editor<T>(&self, f: impl FnOnce(&CodeEditorModel) -> T) -> Option<T> {
self.0.borrow().borrow().as_ref().map(f)
}
}
impl PartialEq for CodeEditorLink {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.0.borrow(), &other.0.borrow())
}
}