mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-29 19:18:13 +08:00
Additional WASM translation layer cleanup
This commit is contained in:
@@ -36,7 +36,6 @@ export function registerResponseHandler(responseType: ResponseType, callback: Re
|
|||||||
state.responseMap[responseType] = callback;
|
state.responseMap[responseType] = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
export function handleResponse(responseType: string, responseData: any) {
|
export function handleResponse(responseType: string, responseData: any) {
|
||||||
const callback = state.responseMap[responseType];
|
const callback = state.responseMap[responseType];
|
||||||
const data = parseResponse(responseType, responseData);
|
const data = parseResponse(responseType, responseData);
|
||||||
@@ -50,7 +49,6 @@ export function handleResponse(responseType: string, responseData: any) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
function parseResponse(responseType: string, data: any): Response {
|
function parseResponse(responseType: string, data: any): Response {
|
||||||
switch (responseType) {
|
switch (responseType) {
|
||||||
case "DocumentChanged":
|
case "DocumentChanged":
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use crate::dispatch;
|
||||||
use crate::shims::Error;
|
use crate::shims::Error;
|
||||||
use crate::wrappers::{translate_key, translate_tool, Color};
|
use crate::wrappers::{translate_key, translate_tool, Color};
|
||||||
use editor::input::input_preprocessor::ModifierKeys;
|
use editor::input::input_preprocessor::ModifierKeys;
|
||||||
@@ -9,20 +10,14 @@ use editor::LayerId;
|
|||||||
use graphene::layers::BlendMode;
|
use graphene::layers::BlendMode;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
fn convert_error(err: editor::EditorError) -> JsValue {
|
|
||||||
Error::new(&err.to_string()).into()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn dispatch<T: Into<Message>>(message: T) {
|
|
||||||
let messages = crate::EDITOR_STATE.with(|state| state.borrow_mut().handle_message(message.into()));
|
|
||||||
crate::handle_responses(messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Modify the currently selected tool in the document state store
|
/// Modify the currently selected tool in the document state store
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn select_tool(tool: String) -> Result<(), JsValue> {
|
pub fn select_tool(tool: String) -> Result<(), JsValue> {
|
||||||
match translate_tool(&tool) {
|
match translate_tool(&tool) {
|
||||||
Some(tool) => Ok(dispatch(ToolMessage::ActivateTool(tool))),
|
Some(tool) => {
|
||||||
|
dispatch(ToolMessage::ActivateTool(tool));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
None => Err(Error::new(&format!("Couldn't select {} because it was not recognized as a valid tool", tool)).into()),
|
None => Err(Error::new(&format!("Couldn't select {} because it was not recognized as a valid tool", tool)).into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,7 +27,10 @@ pub fn select_tool(tool: String) -> Result<(), JsValue> {
|
|||||||
pub fn set_tool_options(tool: String, options: &JsValue) -> Result<(), JsValue> {
|
pub fn set_tool_options(tool: String, options: &JsValue) -> Result<(), JsValue> {
|
||||||
match options.into_serde::<ToolOptions>() {
|
match options.into_serde::<ToolOptions>() {
|
||||||
Ok(options) => match translate_tool(&tool) {
|
Ok(options) => match translate_tool(&tool) {
|
||||||
Some(tool) => Ok(dispatch(ToolMessage::SetToolOptions(tool, options))),
|
Some(tool) => {
|
||||||
|
dispatch(ToolMessage::SetToolOptions(tool, options));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
None => Err(Error::new(&format!("Couldn't set options for {} because it was not recognized as a valid tool", tool)).into()),
|
None => Err(Error::new(&format!("Couldn't set options for {} because it was not recognized as a valid tool", tool)).into()),
|
||||||
},
|
},
|
||||||
Err(err) => Err(Error::new(&format!("Invalid JSON for ToolOptions: {}", err)).into()),
|
Err(err) => Err(Error::new(&format!("Invalid JSON for ToolOptions: {}", err)).into()),
|
||||||
@@ -53,7 +51,10 @@ pub fn send_tool_message(tool: String, message: &JsValue) -> Result<(), JsValue>
|
|||||||
None => Err(Error::new(&format!("Couldn't send message for {} because it was not recognized as a valid tool", tool)).into()),
|
None => Err(Error::new(&format!("Couldn't send message for {} because it was not recognized as a valid tool", tool)).into()),
|
||||||
};
|
};
|
||||||
match tool_message {
|
match tool_message {
|
||||||
Ok(tool_message) => Ok(dispatch(tool_message)),
|
Ok(tool_message) => {
|
||||||
|
dispatch(tool_message);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -256,10 +257,11 @@ pub fn set_blend_mode_for_selected_layers(blend_mode_svg_style_name: String) ->
|
|||||||
"saturation" => BlendMode::Saturation,
|
"saturation" => BlendMode::Saturation,
|
||||||
"color" => BlendMode::Color,
|
"color" => BlendMode::Color,
|
||||||
"luminosity" => BlendMode::Luminosity,
|
"luminosity" => BlendMode::Luminosity,
|
||||||
_ => return Err(convert_error(EditorError::Misc("UnknownBlendMode".to_string()))),
|
_ => return Err(Error::new(&EditorError::Misc("UnknownBlendMode".to_string()).to_string()).into()),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(dispatch(DocumentMessage::SetBlendModeForSelectedLayers(blend_mode)))
|
dispatch(DocumentMessage::SetBlendModeForSelectedLayers(blend_mode));
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the opacity for the selected layers
|
/// Set the opacity for the selected layers
|
||||||
|
|||||||
+17
-15
@@ -1,7 +1,6 @@
|
|||||||
pub mod document;
|
pub mod document;
|
||||||
mod shims;
|
mod shims;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
pub mod window;
|
|
||||||
pub mod wrappers;
|
pub mod wrappers;
|
||||||
|
|
||||||
use editor::{message_prelude::*, Editor};
|
use editor::{message_prelude::*, Editor};
|
||||||
@@ -9,7 +8,7 @@ use std::cell::RefCell;
|
|||||||
use utils::WasmLog;
|
use utils::WasmLog;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
// the thread_local macro provides a way to initialize static variables with non-constant functions
|
// The thread_local macro provides a way to initialize static variables with non-constant functions
|
||||||
thread_local! {
|
thread_local! {
|
||||||
pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new());
|
pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new());
|
||||||
}
|
}
|
||||||
@@ -22,24 +21,27 @@ pub fn init() {
|
|||||||
log::set_max_level(log::LevelFilter::Debug);
|
log::set_max_level(log::LevelFilter::Debug);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_responses(responses: Vec<FrontendMessage>) {
|
// Sends FrontendMessages to JavaScript
|
||||||
for response in responses.into_iter() {
|
pub fn dispatch<T: Into<Message>>(message: T) {
|
||||||
handle_response(response)
|
let messages = EDITOR_STATE.with(|state| state.borrow_mut().handle_message(message.into()));
|
||||||
|
|
||||||
|
for message in messages.into_iter() {
|
||||||
|
let message_type = message.to_discriminant().local_name();
|
||||||
|
let message_data = JsValue::from_serde(&message).expect("Failed to serialize response");
|
||||||
|
|
||||||
|
let _ = handleResponse(message_type, message_data).map_err(|error| {
|
||||||
|
log::error!(
|
||||||
|
"While handling FrontendMessage \"{:?}\", JavaScript threw an error: {:?}",
|
||||||
|
message.to_discriminant().local_name(),
|
||||||
|
error
|
||||||
|
)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The JavaScript function to call into
|
||||||
#[wasm_bindgen(module = "/../src/utilities/response-handler-binding.ts")]
|
#[wasm_bindgen(module = "/../src/utilities/response-handler-binding.ts")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#[wasm_bindgen(catch)]
|
#[wasm_bindgen(catch)]
|
||||||
fn handleResponse(responseType: String, responseData: JsValue) -> Result<(), JsValue>;
|
fn handleResponse(responseType: String, responseData: JsValue) -> Result<(), JsValue>;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_response(response: FrontendMessage) {
|
|
||||||
let response_type = response.to_discriminant().local_name();
|
|
||||||
send_response(response_type, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn send_response(response_type: String, response_data: FrontendMessage) {
|
|
||||||
let response_data = JsValue::from_serde(&response_data).expect("Failed to serialize response");
|
|
||||||
let _ = handleResponse(response_type, response_data).map_err(|error| log::error!("javascript threw an error: {:?}", error));
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -41,5 +41,6 @@ impl log::Log for WasmLog {
|
|||||||
let msg = &format!("%c{}\t{}", name, record.args());
|
let msg = &format!("%c{}\t{}", name, record.args());
|
||||||
log(msg, color)
|
log(msg, color)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&self) {}
|
fn flush(&self) {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
use wasm_bindgen::prelude::*;
|
|
||||||
|
|
||||||
type DocumentId = u32;
|
|
||||||
|
|
||||||
/// Modify the active Document in the editor state store
|
|
||||||
#[wasm_bindgen]
|
|
||||||
pub fn set_active_document(document_id: DocumentId) {
|
|
||||||
todo!("set_active_document {}", document_id)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Query the name of a specific document
|
|
||||||
#[wasm_bindgen]
|
|
||||||
pub fn get_document_name(document_id: DocumentId) -> String {
|
|
||||||
todo!("get_document_name {}", document_id)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Query the id of the most recently interacted with document
|
|
||||||
#[wasm_bindgen]
|
|
||||||
pub fn get_active_document() -> DocumentId {
|
|
||||||
todo!("get_active_document")
|
|
||||||
}
|
|
||||||
|
|
||||||
type PanelId = u32;
|
|
||||||
/// Notify the editor that the mouse hovers above a panel
|
|
||||||
#[wasm_bindgen]
|
|
||||||
pub fn panel_hover_enter(panel_id: PanelId) {
|
|
||||||
todo!("panel_hover_enter {}", panel_id)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Query a list of currently available operations
|
|
||||||
#[wasm_bindgen]
|
|
||||||
pub fn get_available_operations() -> Vec<JsValue> {
|
|
||||||
todo!("get_available_operations")
|
|
||||||
// vec!["example1", "example2"].into_iter().map(JsValue::from).collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
/// Load a new .gdd file into the editor
|
|
||||||
/// Returns a unique document identifier
|
|
||||||
#[wasm_bindgen]
|
|
||||||
pub fn load_document(raw_data: &[u8]) -> DocumentId {
|
|
||||||
todo!()
|
|
||||||
}*/
|
|
||||||
Reference in New Issue
Block a user