Add include command to pull in scripts.
This commit is contained in:
parent
849d9b4c8a
commit
c5115993cb
@ -15,6 +15,7 @@ use yew::{
|
||||
UseStateHandle,
|
||||
};
|
||||
|
||||
pub use self::storage::load_extant_file_contents;
|
||||
use self::storage::load_file_contents;
|
||||
|
||||
mod create_script_dialog;
|
||||
@ -62,6 +63,7 @@ pub fn try_run_script(script: &str, global_memo: &GlobalMemoCell) -> anyhow::Res
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Only intended for use from the editor, since it borrows from global_memo.
|
||||
fn run_script<F>(script: &str, global_memo: &GlobalMemoCell, set_err: Rc<F>, frame: &FrameId)
|
||||
where
|
||||
F: Fn(Option<&str>),
|
||||
|
@ -75,3 +75,15 @@ pub fn load_file_contents(file: &str) -> anyhow::Result<String> {
|
||||
.map_err(|_| Error::msg("Error retrieving localStorage"))?
|
||||
.unwrap_or_else(|| "".to_owned()))
|
||||
}
|
||||
|
||||
pub fn load_extant_file_contents(file: &str) -> anyhow::Result<String> {
|
||||
let win = window().ok_or_else(|| Error::msg("Can't get window"))?;
|
||||
let local = win
|
||||
.local_storage()
|
||||
.map_err(|_| Error::msg("Error retrieving localStorage"))?
|
||||
.ok_or_else(|| Error::msg("Local storage not available"))?;
|
||||
Ok(local
|
||||
.get(&format!("scriptfile_{}", file))
|
||||
.map_err(|_| Error::msg("Error retrieving localStorage"))?
|
||||
.ok_or_else(|| Error::msg("File not found"))?)
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ use piccolo::{
|
||||
use yew::UseStateSetter;
|
||||
|
||||
use crate::{
|
||||
editor_view::load_extant_file_contents,
|
||||
id_intern::intern_id,
|
||||
match_table::{
|
||||
create_match_table, match_table_add, match_table_lua_table, match_table_remove,
|
||||
@ -190,6 +191,7 @@ pub fn install_lua_globals(
|
||||
register_command!(echo_frame_raw);
|
||||
register_command!(editor);
|
||||
register_command!(hsplit);
|
||||
register_stateless_command!(cmd_include, "include");
|
||||
register_command!(cmd_list_logs, "listlogs");
|
||||
register_command!(mud_log, "log");
|
||||
register_command!(panel_merge);
|
||||
@ -419,7 +421,7 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn command<'gc>(
|
||||
fn command<'gc>(
|
||||
ctx: Context<'gc>,
|
||||
global_memo: &GlobalMemoCell,
|
||||
_global_layout: &UseStateSetter<GlobalLayoutCell>,
|
||||
@ -444,3 +446,19 @@ pub fn command<'gc>(
|
||||
Ok(CallbackReturn::Return)
|
||||
})
|
||||
}
|
||||
|
||||
fn cmd_include<'gc>(ctx: Context<'gc>) -> Callback<'gc> {
|
||||
Callback::from_fn(&ctx, move |ctx, _ex, mut stack| {
|
||||
let file: String = stack.consume(ctx).map_err(|_| {
|
||||
anyhow::Error::msg(
|
||||
"include takes only one argument, a string containing a Lua file to run",
|
||||
)
|
||||
})?;
|
||||
let contents = load_extant_file_contents(&file)?;
|
||||
let function: Function<'gc> = Closure::load(ctx, None, contents.as_bytes())?.into();
|
||||
Ok(CallbackReturn::Call {
|
||||
function,
|
||||
then: None,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user