use std::sync::Arc;
use crate::{
event::EventBus, extension_manager::ExtensionManager,
history_manager::HistoryManager, types::EditorOptions,
};
use async_trait::async_trait;
use moduforge_state::{
state::State,
transaction::{Command, Transaction},
};
use moduforge_model::{node_pool::NodePool, schema::Schema};
#[async_trait]
pub trait EditorCore {
type Error;
fn doc(&self) -> Arc<NodePool>;
fn get_options(&self) -> &EditorOptions;
fn get_state(&self) -> &Arc<State>;
fn get_schema(&self) -> Arc<Schema>;
fn get_event_bus(&self) -> &EventBus;
fn get_tr(&self) -> Transaction;
async fn command(
&mut self,
command: Arc<dyn Command>,
) -> Result<(), Self::Error>;
async fn dispatch(
&mut self,
transaction: Transaction,
) -> Result<(), Self::Error>;
async fn register_plugin(&mut self) -> Result<(), Self::Error>;
async fn unregister_plugin(
&mut self,
plugin_key: String,
) -> Result<(), Self::Error>;
fn undo(&mut self);
fn redo(&mut self);
}
pub struct EditorBase {
pub event_bus: EventBus,
pub state: Arc<State>,
pub extension_manager: ExtensionManager,
pub history_manager: HistoryManager<Arc<State>>,
pub options: EditorOptions,
}
impl EditorBase {
pub fn doc(&self) -> Arc<NodePool> {
self.state.doc()
}
pub fn get_options(&self) -> &EditorOptions {
&self.options
}
pub fn get_state(&self) -> &Arc<State> {
&self.state
}
pub fn get_schema(&self) -> Arc<Schema> {
self.extension_manager.get_schema()
}
pub fn get_event_bus(&self) -> &EventBus {
&self.event_bus
}
pub fn get_tr(&self) -> Transaction {
let tr = self.get_state().tr();
tr
}
pub fn undo(&mut self) {
self.history_manager.jump(-1);
self.state = self.history_manager.get_present();
}
pub fn redo(&mut self) {
self.history_manager.jump(1);
self.state = self.history_manager.get_present();
}
}