Fixed ace/keyboard loading

This commit is contained in:
Spxg
2025-05-18 02:07:13 +08:00
parent e6d17b1b54
commit e84d5af619
6 changed files with 40 additions and 41 deletions

View File

@@ -8,6 +8,5 @@ js-sys = "0.3.77"
serde = { version = "1.0.219", features = ["derive"] }
serde-wasm-bindgen = "0.6.5"
thiserror = "2.0.12"
tokio = { version = "1.45.0", features = ["sync"] }
wasm-bindgen = "0.2.100"
web-sys = { version = "0.3.77", features = ["HtmlElement"] }

View File

@@ -1,5 +1,3 @@
use std::sync::Arc;
use js_sys::{Object, Reflect};
use serde::{Deserialize, Serialize};
use wasm_bindgen::{JsCast, JsValue, prelude::Closure};
@@ -7,19 +5,12 @@ use wasm_bindgen::{JsCast, JsValue, prelude::Closure};
// <https://ajaxorg.github.io/ace-api-docs/index.html>
mod bindgen {
use js_sys::Object;
use wasm_bindgen::{
JsValue,
prelude::{Closure, wasm_bindgen},
};
use wasm_bindgen::{JsValue, prelude::wasm_bindgen};
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = ace, js_name = edit, catch)]
pub fn edit(element: &str, options: Option<Object>) -> Result<Editor, JsValue>;
#[wasm_bindgen(js_namespace = ["ace", "config"], js_name = loadModule)]
pub fn load_module(module: &str, callback: &Closure<dyn FnMut(JsValue)>);
#[wasm_bindgen(js_namespace = ace, js_name = require, catch)]
pub fn require(module: &str) -> Result<JsValue, JsValue>;
}
@@ -32,7 +23,7 @@ mod bindgen {
pub fn set_theme(this: &Editor, theme: &str) -> Result<(), JsValue>;
#[wasm_bindgen(method, js_name = setKeyboardHandler, catch)]
pub fn set_keyboard_handler(this: &Editor, handler: &str) -> Result<(), JsValue>;
pub fn set_keyboard_handler(this: &Editor, handler: JsValue) -> Result<(), JsValue>;
#[wasm_bindgen(method, js_name = getValue)]
pub fn get_value(this: &Editor) -> String;
@@ -67,8 +58,13 @@ impl EditorOptionsBuilder {
self
}
pub fn keyboard(mut self, value: &str) -> Self {
self.0.keyboard_handler = value.into();
pub fn keyboard(mut self, value: Option<&str>) -> Self {
let value = if let Some(value) = value {
JsValue::from(value)
} else {
JsValue::null()
};
self.0.keyboard_handler = value;
self
}
@@ -87,7 +83,8 @@ impl EditorOptionsBuilder {
pub struct EditorOptions {
pub mode: String,
pub theme: String,
pub keyboard_handler: String,
#[serde(with = "serde_wasm_bindgen::preserve")]
pub keyboard_handler: JsValue,
pub value: String,
}
@@ -158,23 +155,19 @@ impl Editor {
self.js.set_theme(theme).map_err(EditorError::SetTheme)
}
pub fn set_keyboard_handler(&self, handler: &str) -> Result<()> {
pub fn set_keyboard_handler(&self, handler: Option<&str>) -> Result<()> {
let handler = if let Some(handler) = handler {
JsValue::from(handler)
} else {
JsValue::null()
};
self.js
.set_keyboard_handler(handler)
.map_err(EditorError::SetKeyboardHandler)
}
pub async fn define_vim_w(callback: Box<dyn Fn() + 'static>) -> Result<()> {
let notify = Arc::new(tokio::sync::Notify::new());
let waiter = Arc::clone(&notify);
let load_module = Closure::once(move |_: JsValue| {
notify.notify_waiters();
});
bindgen::load_module("ace/keyboard/vim", &load_module);
waiter.notified().await;
pub fn define_vim_w(callback: Box<dyn Fn() + 'static>) -> Result<()> {
let value = bindgen::require("ace/keyboard/vim").map_err(EditorError::DefineEx)?;
let define_ex = Reflect::get(&value, &JsValue::from("CodeMirror"))