moduforge-collaboration-client 0.4.14

moduforge 协作系统
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use mf_transform::{
    attr_step::AttrStep,
    mark_step::{AddMarkStep, RemoveMarkStep},
    node_step::{AddNodeStep, RemoveNodeStep},
    step::Step,
};
use mf_model::{node::Node, tree::Tree};
use std::{any::TypeId};

use crate::{types::StepResult, utils::Utils};
use yrs::{
    types::{array::ArrayRef, map::MapRef, Value},
    Array, ArrayPrelim, Map, MapPrelim, ReadTxn, Transact, TransactionMut,
    WriteTxn,
};

/// 将 `Step` 转换为 Yrs 事务的 Trait
/// 这个 Trait 是动态安全的
pub trait StepConverter: Send + Sync {
    /// 将步骤应用到 Yrs 文档事务中
    fn apply_to_yrs_txn(
        &self,
        step: &dyn Step,
        txn: &mut TransactionMut,
    ) -> Result<StepResult, Box<dyn std::error::Error>>;
    /// 将 Yrs 变更转换成 ModuForge step
    fn apply_yrs_to_step(&self) {}

    /// 返回转换器的名称
    fn name(&self) -> &'static str;

    /// 检查此转换器是否支持给定的步骤类型
    fn supports(
        &self,
        step: &dyn Step,
    ) -> bool;

    /// 获取步骤的操作描述
    fn get_description(
        &self,
        step: &dyn Step,
    ) -> String {
        format!("Executing operation: {} ({})", step.name(), self.name())
    }
}

/// 默认的步骤转换器,用于不支持的步骤
pub struct DefaultStepConverter;

impl StepConverter for DefaultStepConverter {
    fn apply_to_yrs_txn(
        &self,
        step: &dyn Step,
        txn: &mut TransactionMut,
    ) -> Result<StepResult, Box<dyn std::error::Error>> {
        Ok(StepResult {
            step_id: uuid::Uuid::new_v4().to_string(),
            step_name: step.name().to_string(),
            description: format!("默认处理程序处理未知步骤: {}", step.name()),
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_millis() as u64,
            client_id: txn
                .origin()
                .as_ref()
                .map(|s| s.to_string())
                .unwrap_or_default(),
        })
    }

    fn name(&self) -> &'static str {
        "DefaultStepConverter"
    }

    fn supports(
        &self,
        _step: &dyn Step,
    ) -> bool {
        true // Default converter supports all types as a fallback.
    }
}

/// 节点相关步骤的转换器
pub struct NodeStepConverter;

impl NodeStepConverter {
    fn insert_node_data(
        &self,
        txn: &mut TransactionMut,
        nodes_map: &MapRef,
        node: &Node,
    ) {
        let node_data_map =
            Utils::get_or_create_node_data_map(nodes_map, txn, &node.id);
        // 插入节点类型
        node_data_map.insert(txn, "type", node.r#type.clone());
        // 插入节点属性
        let attrs_map =
            node_data_map.insert(txn, "attrs", MapPrelim::<yrs::Any>::new());
        for (key, value) in node.attrs.iter() {
            attrs_map.insert(
                txn,
                key.clone(),
                Utils::json_value_to_yrs_any(value),
            );
        }
        // 插入节点内容
        let content_array = node_data_map.insert(
            txn,
            "content",
            ArrayPrelim::from(Vec::<yrs::Any>::new()),
        );
        for child_id in &node.content {
            content_array
                .push_back(txn, yrs::Any::String(child_id.clone().into()));
        }
        // 插入节点标记
        let marks_array = node_data_map.insert(
            txn,
            "marks",
            ArrayPrelim::from(Vec::<yrs::Any>::new()),
        );
        for mark in &node.marks {
            Utils::add_mark_to_array(&marks_array, txn, mark);
        }
    }

    /// 递归插入节点及其所有子节点
    fn insert_node_enum_recursive(
        &self,
        txn: &mut TransactionMut,
        nodes_map: &MapRef,
        node_enum: &mf_model::node_type::NodeEnum,
    ) {
        // 插入当前节点
        self.insert_node_data(txn, nodes_map, &node_enum.0);

        // 递归插入所有子节点
        for child_enum in &node_enum.1 {
            self.insert_node_enum_recursive(txn, nodes_map, child_enum);
        }
    }
}

impl StepConverter for NodeStepConverter {
    fn apply_to_yrs_txn(
        &self,
        step: &dyn Step,
        txn: &mut TransactionMut,
    ) -> Result<StepResult, Box<dyn std::error::Error>> {
        let client_id =
            txn.origin().as_ref().map(|s| s.to_string()).unwrap_or_default();

        if let Some(add_step) = step.downcast_ref::<AddNodeStep>() {
            let nodes_map = txn.get_or_insert_map("nodes");
            let all_nodes = &add_step.nodes;
            let parent_id = add_step.parent_id.clone();

            // 如果parent_id 不是根节点需要 修改 parent_id节点的 content 数组
            // 获取根节点 id 从 meta 区域
            let meta_map = txn.get_or_insert_map("meta");
            let root_id = meta_map.get(txn, "root_id");
            if let Some(root_id) = root_id
                && root_id.to_string(txn) != parent_id
            {
                let node_data_map = Utils::get_or_create_node_data_map(
                    &nodes_map, txn, &parent_id,
                );
                let content_array =
                    Utils::get_or_create_content_array(&node_data_map, txn);
                for node_enum in all_nodes {
                    content_array.push_back(
                        txn,
                        yrs::Any::String(node_enum.0.id.clone().into()),
                    );
                }
            }

            for node_enum in all_nodes {
                self.insert_node_enum_recursive(txn, &nodes_map, node_enum);
            }

            return Ok(StepResult {
                step_id: uuid::Uuid::new_v4().to_string(),
                step_name: step.name().to_string(),
                description: format!("添加 {} 个节点", all_nodes.len()),
                timestamp: std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .as_millis() as u64,
                client_id,
            });
        } else if let Some(remove_step) = step.downcast_ref::<RemoveNodeStep>()
        {
            let nodes_map = txn.get_or_insert_map("nodes");
            for node_id in &remove_step.node_ids {
                nodes_map.remove(txn, &node_id.to_string());
            }

            return Ok(StepResult {
                step_id: uuid::Uuid::new_v4().to_string(),
                step_name: step.name().to_string(),
                description: format!(
                    "Removed {} nodes",
                    remove_step.node_ids.len()
                ),
                timestamp: std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .as_millis() as u64,
                client_id,
            });
        }

        Err("不支持的节点操作".into())
    }

    fn name(&self) -> &'static str {
        "NodeStepConverter"
    }

    fn supports(
        &self,
        step: &dyn Step,
    ) -> bool {
        step.type_id() == TypeId::of::<AddNodeStep>()
            || step.type_id() == TypeId::of::<RemoveNodeStep>()
    }
}

/// 属性相关步骤的转换器
pub struct AttrStepConverter;

impl StepConverter for AttrStepConverter {
    fn apply_to_yrs_txn(
        &self,
        step: &dyn Step,
        txn: &mut TransactionMut,
    ) -> Result<StepResult, Box<dyn std::error::Error>> {
        let client_id =
            txn.origin().as_ref().map(|s| s.to_string()).unwrap_or_default();
        if let Some(attr_step) = step.downcast_ref::<AttrStep>() {
            let nodes_map = txn.get_or_insert_map("nodes");
            let node_data_map = Utils::get_or_create_node_data_map(
                &nodes_map,
                txn,
                &attr_step.id,
            );
            // 获取或创建节点属性映射
            let attrs_map =
                Utils::get_or_create_node_attrs_map(&node_data_map, txn);
            // 更新节点属性
            for (key, value) in attr_step.values.iter() {
                attrs_map.insert(
                    txn,
                    key.clone(),
                    Utils::json_value_to_yrs_any(value),
                );
            }

            Ok(StepResult {
                step_id: uuid::Uuid::new_v4().to_string(),
                step_name: step.name().to_string(),
                description: format!(
                    "更新 {} 个属性 for node {}",
                    attr_step.values.len(),
                    attr_step.id
                ),
                timestamp: std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .as_millis() as u64,
                client_id,
            })
        } else {
            Err("不支持的属性操作".into())
        }
    }

    fn name(&self) -> &'static str {
        "AttrStepConverter"
    }

    fn supports(
        &self,
        step: &dyn Step,
    ) -> bool {
        step.type_id() == TypeId::of::<AttrStep>()
    }
}

/// 标记相关步骤的转换器
pub struct MarkStepConverter;

impl MarkStepConverter {
    /// 从 Yrs 数组中删除标记
    fn remove_mark_from_array(
        &self,
        marks_array: &ArrayRef,
        txn: &mut TransactionMut,
        mark_type_to_remove: &str,
    ) {
        let len = marks_array.len(txn);
        for i in (0..len).rev() {
            if let Some(Value::YMap(mark_map)) = marks_array.get(txn, i) {
                if let Some(mark_type_value) = mark_map.get(txn, "type") {
                    let mark_type = match mark_type_value {
                        Value::YText(_text_ref) => {
                            // For now, skip text refs as we can't easily extract string
                            continue;
                        },
                        Value::Any(any) => any.to_string(),
                        _ => continue,
                    };
                    if mark_type == mark_type_to_remove {
                        marks_array.remove(txn, i);
                        return;
                    }
                }
            }
        }
    }
}

impl StepConverter for MarkStepConverter {
    fn apply_to_yrs_txn(
        &self,
        step: &dyn Step,
        txn: &mut TransactionMut,
    ) -> Result<StepResult, Box<dyn std::error::Error>> {
        let client_id =
            txn.origin().as_ref().map(|s| s.to_string()).unwrap_or_default();

        if let Some(add_mark_step) = step.downcast_ref::<AddMarkStep>() {
            let nodes_map = txn.get_or_insert_map("nodes");
            // 获取或创建节点数据映射
            let node_data_map = Utils::get_or_create_node_data_map(
                &nodes_map,
                txn,
                &add_mark_step.id,
            );
            // 获取或创建标记数组
            let marks_array =
                Utils::get_or_create_marks_array(&node_data_map, txn);
            // 添加标记
            for mark in &add_mark_step.marks {
                Utils::add_mark_to_array(&marks_array, txn, mark);
            }

            return Ok(StepResult {
                step_id: uuid::Uuid::new_v4().to_string(),
                step_name: step.name().to_string(),
                description: format!(
                    "添加 {} 个标记 to node {}",
                    add_mark_step.marks.len(),
                    add_mark_step.id
                ),
                timestamp: std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .as_millis() as u64,
                client_id,
            });
        } else if let Some(remove_mark_step) =
            step.downcast_ref::<RemoveMarkStep>()
        {
            let nodes_map = txn.get_or_insert_map("nodes");
            let node_data_map = Utils::get_or_create_node_data_map(
                &nodes_map,
                txn,
                &remove_mark_step.id,
            );
            let marks_array =
                Utils::get_or_create_marks_array(&node_data_map, txn);
            // 删除标记
            for mark_type in &remove_mark_step.mark_types {
                self.remove_mark_from_array(&marks_array, txn, mark_type);
            }

            return Ok(StepResult {
                step_id: uuid::Uuid::new_v4().to_string(),
                step_name: step.name().to_string(),
                description: format!(
                    "Removed {} marks from node {}",
                    remove_mark_step.mark_types.len(),
                    remove_mark_step.id
                ),
                timestamp: std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .as_millis() as u64,
                client_id,
            });
        }

        Err("不支持的标记操作".into())
    }

    fn name(&self) -> &'static str {
        "MarkStepConverter"
    }

    fn supports(
        &self,
        step: &dyn Step,
    ) -> bool {
        step.type_id() == TypeId::of::<AddMarkStep>()
            || step.type_id() == TypeId::of::<RemoveMarkStep>()
    }
}

/// 所有步骤转换器的注册表
pub struct StepConverterRegistry {
    converters: Vec<Box<dyn StepConverter>>,
}

impl Default for StepConverterRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl StepConverterRegistry {
    /// 创建一个新的注册表,包含所有默认的转换器
    pub fn new() -> Self {
        let mut registry = Self { converters: Vec::new() };

        registry.register(Box::new(NodeStepConverter));
        registry.register(Box::new(AttrStepConverter));
        registry.register(Box::new(MarkStepConverter));
        registry.register(Box::new(DefaultStepConverter));

        registry
    }

    /// 注册一个新的转换器
    pub fn register(
        &mut self,
        converter: Box<dyn StepConverter>,
    ) {
        tracing::info!("🔄 注册步骤转换器: {}", converter.name());
        self.converters.push(converter);
    }

    /// 查找支持给定步骤的转换器
    pub fn find_converter(
        &self,
        step: &dyn Step,
    ) -> Option<&(dyn StepConverter)> {
        for converter in &self.converters {
            if converter.supports(step) {
                return Some(converter.as_ref());
            }
        }
        None
    }
}

/// 全局映射器,用于处理转换
#[derive(Debug)]
pub struct Mapper;

impl Mapper {
    /// Gets the global singleton instance of the converter registry.
    pub fn global_registry() -> &'static StepConverterRegistry {
        use std::sync::OnceLock;
        static REGISTRY: OnceLock<StepConverterRegistry> = OnceLock::new();
        REGISTRY.get_or_init(StepConverterRegistry::new)
    }


    /// 获取 Yrs 文档的版本信息
    pub fn get_yrs_doc_version(doc: &yrs::Doc) -> u64 {
        let txn = doc.transact();
        txn.state_vector().len() as u64
    }

    /// 检查 Yrs 文档是否为空
    pub fn is_yrs_doc_empty(doc: &yrs::Doc) -> bool {
        let txn = doc.transact();
        let nodes_map = txn.get_map("nodes");
        nodes_map.map_or(true, |map| map.len(&txn) == 0)
    }
}