mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-19 02:48:12 +08:00
Use serde to serialize responses (#96)
This commit is contained in:
committed by
Keavon Chambers
parent
f10a3c63d1
commit
f8ebff033d
@@ -4,13 +4,11 @@ pub mod utils;
|
||||
pub mod window;
|
||||
pub mod wrappers;
|
||||
|
||||
use editor_core::{
|
||||
events::{DocumentResponse, Response, ToolResponse},
|
||||
Editor, LayerId,
|
||||
};
|
||||
use editor_core::{events::Response, Editor};
|
||||
use std::cell::RefCell;
|
||||
use utils::WasmLog;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wrappers::WasmResponse;
|
||||
|
||||
// the thread_local macro provides a way to initialize static variables with non-constant functions
|
||||
thread_local! { pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new(Box::new(handle_response))) }
|
||||
@@ -23,37 +21,19 @@ pub fn init() {
|
||||
log::set_max_level(log::LevelFilter::Debug);
|
||||
}
|
||||
|
||||
fn path_to_string(path: Vec<LayerId>) -> String {
|
||||
path.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(",")
|
||||
}
|
||||
|
||||
fn handle_response(response: Response) {
|
||||
let response_type = response.to_string();
|
||||
match response {
|
||||
Response::Document(doc) => match doc {
|
||||
DocumentResponse::ExpandFolder { path, children } => {
|
||||
let children = children
|
||||
.iter()
|
||||
.map(|c| format!("name:{},visible:{},type:{}", c.name, c.visible, c.layer_type))
|
||||
.collect::<Vec<String>>()
|
||||
.join(";");
|
||||
send_response(response_type, &[path_to_string(path), children])
|
||||
}
|
||||
DocumentResponse::CollapseFolder { path } => send_response(response_type, &[path_to_string(path)]),
|
||||
DocumentResponse::DocumentChanged => log::error!("Wasm wrapper got request to update the document"),
|
||||
},
|
||||
Response::Tool(ToolResponse::UpdateCanvas { document }) => send_response(response_type, &[document]),
|
||||
Response::Tool(ToolResponse::SetActiveTool { tool_name }) => send_response(response_type, &[tool_name]),
|
||||
}
|
||||
send_response(response_type, response);
|
||||
}
|
||||
fn send_response(response_type: String, response_data: &[String]) {
|
||||
let data = response_data.iter().map(JsValue::from).collect();
|
||||
handleResponse(response_type, data);
|
||||
|
||||
fn send_response(response_type: String, response_data: Response) {
|
||||
let response_data = JsValue::from_serde(&WasmResponse::new(response_data)).expect("Failed to serialize response");
|
||||
handleResponse(response_type, response_data);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(module = "/../src/response-handler.ts")]
|
||||
extern "C" {
|
||||
fn handleResponse(responseType: String, responseData: Vec<JsValue>);
|
||||
fn handleResponse(responseType: String, responseData: JsValue);
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
|
||||
@@ -2,6 +2,8 @@ use crate::shims::Error;
|
||||
use editor_core::events;
|
||||
use editor_core::tools::{SelectAppendMode, ToolType};
|
||||
use editor_core::Color as InnerColor;
|
||||
use events::Response;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
@@ -24,6 +26,15 @@ impl Color {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct WasmResponse(Response);
|
||||
|
||||
impl WasmResponse {
|
||||
pub fn new(response: Response) -> Self {
|
||||
Self(response)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! match_string_to_enum {
|
||||
(match ($e:expr) {$($var:ident),* $(,)?}) => {
|
||||
match $e {
|
||||
|
||||
Reference in New Issue
Block a user