Insert code, fix text filter

This commit is contained in:
Adrian Woźniak 2020-08-15 23:16:05 +02:00
parent 5267ce8a11
commit fbaa9fb98a
4 changed files with 186 additions and 88 deletions

132
Cargo.lock generated
View File

@ -421,13 +421,13 @@ checksum = "71938f30533e4d95a6d17aa530939da3842c2ab6f4f84b9dae68447e4129f74a"
[[package]]
name = "async-trait"
version = "0.1.36"
version = "0.1.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a265e3abeffdce30b2e26b7a11b222fe37c6067404001b434101457d0385eb92"
checksum = "caae68055714ff28740f310927e04f2eba76ff580b16fb18ed90073ee71646f7"
dependencies = [
"proc-macro2",
"quote",
"syn",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@ -680,31 +680,31 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "chrono"
version = "0.4.13"
version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c74d84029116787153e02106bf53e66828452a4b325cc8652b788b5967c0a0b6"
checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b"
dependencies = [
"js-sys",
"num-integer",
"num-traits",
"serde",
"time 0.1.43",
"wasm-bindgen",
"js-sys",
"num-integer",
"num-traits",
"serde",
"time 0.1.43",
"wasm-bindgen",
]
[[package]]
name = "clap"
version = "2.33.2"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10040cdf04294b565d9e0319955430099ec3813a64c952b86a41200ad714ae48"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
@ -727,19 +727,21 @@ dependencies = [
[[package]]
name = "comrak"
version = "0.8.0"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b818732a109eeabbe99fee28030ff8ecbd606889fcd25509ed933e6c69b7aa69"
checksum = "0d325e4f2ffff52ca77d995bb675494d5364aa332499d5f7c7fbb28c25e671f6"
dependencies = [
"clap",
"entities",
"lazy_static",
"pest",
"pest_derive",
"regex",
"twoway",
"typed-arena",
"unicode_categories",
"clap",
"entities",
"lazy_static",
"pest",
"pest_derive",
"regex",
"shell-words",
"twoway",
"typed-arena",
"unicode_categories",
"xdg",
]
[[package]]
@ -935,9 +937,9 @@ checksum = "134951f4028bdadb9b84baf4232681efbf277da25144b9b0ad65df75946c422b"
[[package]]
name = "either"
version = "1.5.3"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
checksum = "cd56b59865bce947ac5958779cfa508f6c3b9497cc762b7e24a12d11ccde2c4f"
[[package]]
name = "email"
@ -1762,9 +1764,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lazycell"
version = "1.2.1"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "lettre"
@ -2855,22 +2857,22 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "serde"
version = "1.0.114"
version = "1.0.115"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3"
checksum = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5"
dependencies = [
"serde_derive",
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.114"
version = "1.0.115"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8e"
checksum = "609feed1d0a73cc36a0182a840a9b37b4a82f0b1150369f0536a9e3f2a31dc48"
dependencies = [
"proc-macro2",
"quote",
"syn",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@ -2920,12 +2922,18 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69"
dependencies = [
"block-buffer",
"digest",
"fake-simd",
"opaque-debug",
"block-buffer",
"digest",
"fake-simd",
"opaque-debug",
]
[[package]]
name = "shell-words"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6fa3938c99da4914afedd13bf3d79bcb6c277d1b2c398d23257a304d9e1b074"
[[package]]
name = "shlex"
version = "0.1.1"
@ -2950,9 +2958,9 @@ checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
[[package]]
name = "smallvec"
version = "1.4.1"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3757cb9d89161a2f24e1cf78efa0c1fcff485d18e3f55e0aa3480824ddaa0f3f"
checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252"
[[package]]
name = "socket2"
@ -3285,22 +3293,22 @@ checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860"
[[package]]
name = "tracing"
version = "0.1.18"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0aae59226cf195d8e74d4b34beae1859257efb4e5fed3f147d2dc2c7d372178"
checksum = "6d79ca061b032d6ce30c660fded31189ca0b9922bf483cd70759f13a2d86786c"
dependencies = [
"cfg-if",
"log",
"tracing-core",
"cfg-if",
"log",
"tracing-core",
]
[[package]]
name = "tracing-core"
version = "0.1.13"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d593f98af59ebc017c0648f0117525db358745a8894a8d684e185ba3f45954f9"
checksum = "db63662723c316b43ca36d833707cc93dff82a02ba3d7e354f342682cc8b3545"
dependencies = [
"lazy_static",
"lazy_static",
]
[[package]]
@ -3761,6 +3769,12 @@ dependencies = [
"winapi-build",
]
[[package]]
name = "xdg"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57"
[[package]]
name = "xml-rs"
version = "0.8.3"

View File

@ -78,7 +78,6 @@ pub fn define() {
r#"
<style>
:host { display: block; border: 1px solid black; }
:host { margin-left: 400px; }
#view { background: rgba(43, 48, 59, 255); padding: 1rem; }
#view span { white-space: pre; font-family: 'Source Code Pro', monospace; }
</style>

View File

@ -81,7 +81,8 @@ pub enum RteMsg {
// code
InsertCode(bool),
CodeSetLang(String),
CodeChanged(String),
InjectCode,
RequestFocus(uuid::Uuid),
}
@ -148,7 +149,8 @@ impl RteMsg {
RteMsg::InsertTable { .. } => None,
// code
RteMsg::InsertCode(_) => None,
RteMsg::CodeSetLang(_) => None,
RteMsg::CodeChanged(_) => None,
RteMsg::InjectCode => None,
// indent
RteMsg::ChangeIndent(RteIndentMsg::Increase) => Some(ExecCommand::new("indent")),
@ -185,6 +187,38 @@ pub struct StyledRteTableState {
pub struct StyledRteCodeState {
pub visible: bool,
pub lang: StyledSelectState,
pub code: String,
languages: Vec<String>,
}
impl StyledRteCodeState {
pub fn new(field_id: FieldId) -> Self {
let mut languages: Vec<String> = crate::hi::SYNTAX_SET
.syntaxes()
.iter()
.map(|s| s.name.clone())
.collect();
languages.sort();
Self {
visible: false,
lang: StyledSelectState::new(
FieldId::Rte(RteField::CodeLang(Box::new(field_id.clone()))),
vec![],
),
code: "".to_string(),
languages,
}
}
pub fn languages(&self) -> &Vec<String> {
&self.languages
}
pub fn reset(&mut self) {
self.code.clear();
self.lang.reset();
self.visible = false;
}
}
#[derive(Debug)]
@ -207,13 +241,7 @@ impl StyledRteState {
rows: 3,
cols: 3,
},
code_tooltip: StyledRteCodeState {
visible: false,
lang: StyledSelectState::new(
FieldId::Rte(RteField::CodeLang(Box::new(field_id.clone()))),
vec![],
),
},
code_tooltip: StyledRteCodeState::new(field_id),
range: None,
identifier: uuid::Uuid::new_v4(),
}
@ -246,9 +274,48 @@ impl StyledRteState {
RteMsg::InsertCode(b) => {
if *b {
self.store_range();
} else {
self.code_tooltip.reset();
}
self.code_tooltip.visible = *b;
}
RteMsg::CodeChanged(s) => {
self.code_tooltip.code = s.to_string();
}
RteMsg::InjectCode => {
let lang = match self
.code_tooltip
.lang
.values
.get(0)
.cloned()
.and_then(|idx| self.code_tooltip.languages.get(idx as usize))
{
Some(v) => v.to_string(),
_ => return,
};
let doc = seed::html_document();
let r = match self.range.as_ref() {
Some(r) => r,
_ => return,
};
let code = self.code_tooltip.code.to_string();
let view = match doc.create_element("jirs-code-view") {
Ok(t) => t,
_ => return,
};
if let Err(err) = view.set_attribute("lang", lang.as_str()) {
error!(err);
}
view.set_inner_html(code.as_str());
if let Err(e) = r.insert_node(&view) {
log!(e);
}
self.code_tooltip.reset();
self.schedule_focus(orders);
}
// table
RteMsg::TableSetRows(n) => {
self.table_tooltip.rows = *n;
@ -327,7 +394,7 @@ impl StyledRteState {
fn schedule_focus(&self, orders: &mut impl Orders<Msg>) {
let field_id = self.field_id.clone();
let identifier = self.identifier.clone();
let identifier = self.identifier;
orders.perform_cmd(cmds::timeout(200, move || {
Msg::Rte(RteMsg::RequestFocus(identifier), field_id)
}));
@ -339,7 +406,6 @@ pub struct StyledRte {
table_tooltip: StyledRteTableState,
identifier: Option<uuid::Uuid>,
code_tooltip: StyledRteCodeState,
// value: String,
}
impl StyledRte {
@ -352,10 +418,7 @@ impl StyledRte {
rows: 0,
cols: 0,
},
code_tooltip: StyledRteCodeState {
visible: false,
lang: StyledSelectState::new(field_id.clone(), vec![]),
},
code_tooltip: StyledRteCodeState::new(field_id),
identifier: None,
}
}
@ -387,7 +450,6 @@ impl StyledRteBuilder {
pub fn build(self) -> StyledRte {
StyledRte {
field_id: self.field_id,
// value: self.value,
table_tooltip: self.table_tooltip,
identifier: self.identifier,
code_tooltip: self.code_tooltip,
@ -1008,14 +1070,9 @@ fn styled_rte_button(title: &str, icon: Icon, handler: EventHandler<Msg>) -> Nod
}
fn code_tooltip(values: &StyledRte) -> Node<Msg> {
let StyledRteCodeState { visible, lang } = &values.code_tooltip;
let StyledRteCodeState { visible, lang, .. } = &values.code_tooltip;
let mut languages: Vec<&str> = crate::hi::SYNTAX_SET
.syntaxes()
.iter()
.map(|s| s.name.as_str())
.collect();
languages.sort();
let languages = values.code_tooltip.languages();
let options: Vec<(String, u32)> = languages
.into_iter()
@ -1052,16 +1109,44 @@ fn code_tooltip(values: &StyledRte) -> Node<Msg> {
.into_node()
};
let input = {
let field_id = values.field_id.clone();
let on_change = ev(Ev::Change, move |ev| {
ev.stop_propagation();
let target = ev.target().unwrap();
let textarea = seed::to_textarea(&target);
let code = textarea.value();
Msg::Rte(RteMsg::CodeChanged(code), field_id)
});
seed::textarea![on_change]
};
let actions = {
let field_id = values.field_id.clone();
let on_insert = ev(Ev::Click, move |ev| {
ev.stop_propagation();
ev.prevent_default();
Msg::Rte(RteMsg::InjectCode, field_id)
});
let insert = StyledButton::build()
.on_click(on_insert)
.text("Insert")
.build()
.into_node();
div![insert]
};
StyledTooltip::build()
.code_tooltip()
.visible(*visible)
.add_child(h2!["Insert Code", close_tooltip])
.add_child(select_lang_node)
.add_child(seed::textarea![])
.add_child(input)
.add_child(actions)
.build()
.into_node()
}
pub fn code_to_tag(code: &str) -> Node<Msg> {
custom!["jirs-code-view", code]
}
// pub fn code_to_tag(code: &str) -> Node<Msg> {
// custom!["jirs-code-view", code]
// }

View File

@ -89,7 +89,7 @@ impl StyledSelectChildBuilder {
pub fn match_text(&self, text: &str) -> bool {
self.text
.as_ref()
.map(|t| t.contains(text.to_lowercase().as_str()))
.map(|t| t.to_lowercase().contains(text.to_lowercase().as_str()))
.unwrap_or(true)
}