moduforge-collaboration-client 0.4.12

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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use std::time::{SystemTime, UNIX_EPOCH};
use mf_model::mark::Mark;
use mf_model::node_type::NodeEnum;
use mf_model::tree::Tree;
use mf_state::Transaction;
use crate::mapping::{NodeStepConverter, StepConverter};
use crate::AwarenessRef;
use serde_json::Value as JsonValue;
use yrs::{Map, ReadTxn, Transact};
use yrs::{
    types::{array::ArrayRef, map::MapRef, Value},
    Array, ArrayPrelim, MapPrelim, TransactionMut, WriteTxn,
};

use crate::{mapping::Mapper, ClientResult};
use mf_model::{node::Node, attrs::Attrs, types::NodeId};
use std::sync::Arc;
use std::collections::HashMap;

/// 获取当前时间戳(毫秒)
pub fn get_unix_time() -> u64 {
    SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis()
        as u64
}

pub struct Utils;
impl Utils {
    /// 初始化树
    /// 将树同步到 Yrs 文档
    pub async fn apply_tree_to_yrs(
        awareness_ref: AwarenessRef,
        tree: &Tree,
    ) -> ClientResult<()> {
        let mut awareness = awareness_ref.write().await;
        let doc = awareness.doc_mut();
        let mut txn = doc.transact_mut_with(doc.client_id().clone());

        // 清空现有数据(如果有的话)
        let nodes_map = txn.get_or_insert_map("nodes");
        nodes_map.clear(&mut txn);

        // 同步 Tree 中的所有节点到 Yrs 文档
        Utils::sync_tree_to_yrs(tree, &mut txn)?;

        // 添加根节点ID到 meta 区域
        let meta_map = txn.get_or_insert_map("meta");
        meta_map.insert(
            &mut txn,
            "root_id",
            yrs::Any::String(tree.root_id.to_string().into()),
        );

        // 提交事务
        txn.commit();

        tracing::info!(
            "成功初始化树,包含 {} 个节点,根节点ID: {}",
            tree.nodes.len(),
            tree.root_id
        );
        Ok(())
    }

    /// 将树同步到 Yrs 文档
    pub fn sync_tree_to_yrs(
        tree: &Tree,
        txn: &mut yrs::TransactionMut,
    ) -> ClientResult<()> {
        use mf_transform::{step::Step, node_step::AddNodeStep};

        let root_node = tree.get_node(&tree.root_id).unwrap();
        // 获取根节点的所有子树
        if let Some(root_tree) = tree.all_children(&tree.root_id, None) {
            // 创建一个 AddNodeStep 来添加整个子树
            let add_step = AddNodeStep {
                parent_id: tree.root_id.clone(),
                nodes: vec![NodeEnum(
                    root_node.as_ref().clone(),
                    vec![root_tree],
                )],
            };
            let node_step_converter = NodeStepConverter;
            if let Err(e) = node_step_converter
                .apply_to_yrs_txn(&add_step as &dyn Step, txn)
            {
                tracing::error!("🔄 同步树节点到 Yrs 失败: {}", e);
                return Err(anyhow::anyhow!(format!(
                    "Failed to sync tree: {}",
                    e
                ),));
            }
        }

        Ok(())
    }

    /// 将事务应用到 Yrs 文档
    pub async fn apply_transaction_to_yrs(
        awareness_ref: AwarenessRef,
        transaction: &Transaction,
    ) -> ClientResult<()> {
        // 使用异步锁获取房间信息

        let mut awareness = awareness_ref.write().await;
        let doc = awareness.doc_mut();
        let mut txn = doc.transact_mut_with(doc.client_id().clone());

        // 使用全局注册表应用所有事务中的步骤
        let registry = Mapper::global_registry();

        
        let steps = &transaction.steps;
        for step in steps {
            if let Some(converter) = registry.find_converter(step.as_ref())
            {
                if let Err(e) =
                    converter.apply_to_yrs_txn(step.as_ref(), &mut txn)
                {
                    tracing::error!("🔄 应用步骤到 Yrs 事务失败: {}", e);
                }
            } else {
                let type_name = std::any::type_name_of_val(step.as_ref());
                tracing::warn!(
                    "🔄 应用步骤到 Yrs 事务失败: 没有找到步骤的转换器: {}",
                    type_name
                );
            }
        }
    
        // 统一提交所有更改
        txn.commit();
        tracing::debug!(
            "🔄 应用 {} 个步骤到文档: {}",
            transaction.steps.len(),
            doc.client_id()
        );

        Ok(())
    }

    /// 将事务应用到 Yrs 文档
    pub async fn apply_transactions_to_yrs(
        awareness_ref: AwarenessRef,
        transactions: &[Transaction],
    ) -> ClientResult<()> {
        // 使用异步锁获取房间信息

        let mut awareness = awareness_ref.write().await;
        let doc = awareness.doc_mut();
        let mut txn = doc.transact_mut_with(doc.client_id().clone());

        // 使用全局注册表应用所有事务中的步骤
        let registry = Mapper::global_registry();

        for tr in transactions {
            let steps = &tr.steps;
            for step in steps {
                if let Some(converter) = registry.find_converter(step.as_ref())
                {
                    if let Err(e) =
                        converter.apply_to_yrs_txn(step.as_ref(), &mut txn)
                    {
                        tracing::error!("🔄 应用步骤到 Yrs 事务失败: {}", e);
                    }
                } else {
                    let type_name = std::any::type_name_of_val(step.as_ref());
                    tracing::warn!(
                        "🔄 应用步骤到 Yrs 事务失败: 没有找到步骤的转换器: {}",
                        type_name
                    );
                }
            }
        }
        // 统一提交所有更改
        txn.commit();
        tracing::debug!(
            "🔄 应用 {} 个事务到文档: {}",
            transactions.len(),
            doc.client_id()
        );

        Ok(())
    }

    // --- 转换器的辅助方法 ---
    /// 将 JSON 值转换为 Yrs 的 Any 类型
    pub fn json_value_to_yrs_any(value: &JsonValue) -> yrs::Any {
        match value {
            JsonValue::Null => yrs::Any::Null,
            JsonValue::Bool(b) => yrs::Any::Bool(*b),
            JsonValue::Number(n) => {
                if let Some(i) = n.as_i64() {
                    yrs::Any::BigInt(i)
                } else if let Some(f) = n.as_f64() {
                    yrs::Any::Number(f)
                } else {
                    yrs::Any::Null
                }
            },
            JsonValue::String(s) => yrs::Any::String(s.clone().into()),
            JsonValue::Array(arr) => {
                let yrs_array: Vec<yrs::Any> =
                    arr.iter().map(Utils::json_value_to_yrs_any).collect();
                yrs::Any::Array(yrs_array.into())
            },
            JsonValue::Object(obj) => {
                let yrs_map: std::collections::HashMap<String, yrs::Any> = obj
                    .iter()
                    .map(|(k, v)| (k.clone(), Utils::json_value_to_yrs_any(v)))
                    .collect();
                yrs::Any::Map(yrs_map.into())
            },
        }
    }

    /// 将标记添加到 Yrs 数组中
    pub fn add_mark_to_array(
        marks_array: &ArrayRef,
        txn: &mut TransactionMut,
        mark: &Mark,
    ) {
        let mark_map = MapPrelim::<yrs::Any>::from([
            ("type".to_string(), yrs::Any::String(mark.r#type.clone().into())),
            ("attrs".to_string(), {
                let attrs_map: std::collections::HashMap<String, yrs::Any> =
                    mark.attrs
                        .iter()
                        .map(|(k, v)| {
                            (k.clone(), Utils::json_value_to_yrs_any(v))
                        })
                        .collect();
                yrs::Any::Map(attrs_map.into())
            }),
        ]);
        marks_array.push_back(txn, mark_map);
    }

    /// 获取或创建节点数据映射
    pub fn get_or_create_node_data_map(
        nodes_map: &MapRef,
        txn: &mut TransactionMut,
        node_id: &str,
    ) -> MapRef {
        if let Some(Value::YMap(map)) = nodes_map.get(txn, node_id) {
            map
        } else {
            nodes_map.insert(
                txn,
                node_id.to_string(),
                MapPrelim::<yrs::Any>::new(),
            )
        }
    }

    /// 获取或创建节点属性映射
    pub fn get_or_create_node_attrs_map(
        node_data_map: &MapRef,
        txn: &mut TransactionMut,
    ) -> MapRef {
        if let Some(Value::YMap(map)) = node_data_map.get(txn, "attrs") {
            map
        } else {
            node_data_map.insert(txn, "attrs", MapPrelim::<yrs::Any>::new())
        }
    }

    /// 获取或创建标记数组
    pub fn get_or_create_content_array(
        node_data_map: &MapRef,
        txn: &mut TransactionMut,
    ) -> ArrayRef {
        if let Some(Value::YArray(array)) = node_data_map.get(txn, "content") {
            array
        } else {
            node_data_map.insert(
                txn,
                "content",
                ArrayPrelim::from(Vec::<yrs::Any>::new()),
            )
        }
    }

    /// 将 Yrs 的 Any 类型转换为 JSON 值
    pub fn yrs_any_to_json_value(value: &yrs::Any) -> Option<JsonValue> {
        match value {
            yrs::Any::Null => Some(JsonValue::Null),
            yrs::Any::Bool(b) => Some(JsonValue::Bool(*b)),
            yrs::Any::Number(n) => {
                Some(JsonValue::Number(serde_json::Number::from_f64(*n)?))
            },
            yrs::Any::BigInt(i) => {
                Some(JsonValue::Number(serde_json::Number::from(*i)))
            },
            yrs::Any::String(s) => Some(JsonValue::String(s.to_string())),
            yrs::Any::Array(arr) => {
                let json_array: Vec<JsonValue> = arr
                    .iter()
                    .filter_map(Utils::yrs_any_to_json_value)
                    .collect();
                Some(JsonValue::Array(json_array))
            },
            yrs::Any::Map(map) => {
                let json_map: std::collections::HashMap<String, JsonValue> =
                    map.iter()
                        .filter_map(|(k, v)| {
                            Utils::yrs_any_to_json_value(v)
                                .map(|json_v| (k.to_string(), json_v))
                        })
                        .collect();
                Some(JsonValue::Object(serde_json::Map::from_iter(json_map)))
            },
            _ => None, // 处理其他类型,如 YText, YMap 等
        }
    }

    /// 获取或创建标记数组
    pub fn get_or_create_marks_array(
        node_data_map: &MapRef,
        txn: &mut TransactionMut,
    ) -> ArrayRef {
        if let Some(Value::YArray(array)) = node_data_map.get(txn, "marks") {
            array
        } else {
            node_data_map.insert(
                txn,
                "marks",
                ArrayPrelim::from(Vec::<yrs::Any>::new()),
            )
        }
    }

    /// 从 Yrs 文档中获取根节点ID
    pub fn get_root_id_from_yrs_doc(
        doc: &yrs::Doc
    ) -> Result<String, Box<dyn std::error::Error>> {
        let txn = doc.transact();
        // 优先从 meta 区域读取
        if let Some(meta_map) = txn.get_map("meta") {
            if let Some(yrs::types::Value::Any(any)) =
                meta_map.get(&txn, "root_id")
            {
                return Ok(any.to_string());
            }
        }
        // fallback: 兼容老数据,取 nodes_map 第一个节点
        let nodes_map =
            txn.get_map("nodes").ok_or("Yrs 文档中没有找到 nodes 映射")?;
        for (key, _) in nodes_map.iter(&txn) {
            return Ok(key.to_string());
        }
        Err("Yrs 文档中没有找到根节点".into())
    }

    /// 从 Yrs 文档的 nodes_map 递归构建所有节点和 parent_map
    pub fn build_tree_nodes_from_yrs(
        node_id: &str,
        nodes_map: &yrs::types::map::MapRef,
        txn: &yrs::Transaction,
        tree_nodes: &mut HashMap<NodeId, Arc<Node>>,
        parent_map: &mut HashMap<NodeId, NodeId>,
        parent_id: Option<&NodeId>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let node_data = nodes_map.get(txn, node_id);
        if node_data.is_none() {
            return Err(format!("节点 {} 在 Yrs 文档中不存在", node_id).into());
        }
        let node_data = node_data.unwrap();
        if let yrs::types::Value::YMap(node_map) = node_data {
            // 提取节点类型
            let node_type = node_map
                .get(txn, "type")
                .and_then(|v| match v {
                    yrs::types::Value::Any(any) => Some(any.to_string()),
                    _ => None,
                })
                .unwrap_or_else(|| "unknown".to_string());

            // 提取属性
            let mut attrs = Attrs::default();
            if let Some(attrs_map) = node_map.get(txn, "attrs") {
                if let yrs::types::Value::YMap(attrs_yrs_map) = attrs_map {
                    for (key, value) in attrs_yrs_map.iter(txn) {
                        if let yrs::types::Value::Any(any_value) = value {
                            if let Some(json_value) =
                                Utils::yrs_any_to_json_value(&any_value)
                            {
                                attrs.insert(key.to_string(), json_value);
                            }
                        }
                    }
                }
            }

            // 提取内容(子节点ID列表)
            let mut content = im::Vector::new();
            if let Some(content_array) = node_map.get(txn, "content") {
                if let yrs::types::Value::YArray(content_yrs_array) =
                    content_array
                {
                    for item in content_yrs_array.iter(txn) {
                        if let yrs::types::Value::Any(any) = item {
                            content.push_back(NodeId::from(any.to_string()));
                        }
                    }
                }
            }

            // 提取标记
            let mut marks = im::Vector::new();
            if let Some(marks_array) = node_map.get(txn, "marks") {
                if let yrs::types::Value::YArray(marks_yrs_array) = marks_array
                {
                    for item in marks_yrs_array.iter(txn) {
                        if let yrs::types::Value::YMap(mark_map) = item {
                            let mark_type = mark_map
                                .get(txn, "type")
                                .and_then(|v| match v {
                                    yrs::types::Value::Any(any) => {
                                        Some(any.to_string())
                                    },
                                    _ => None,
                                })
                                .unwrap_or_else(|| "unknown".to_string());

                            let mut mark_attrs = Attrs::default();
                            if let Some(mark_attrs_map) =
                                mark_map.get(txn, "attrs")
                            {
                                if let yrs::types::Value::YMap(attrs_yrs_map) =
                                    mark_attrs_map
                                {
                                    for (key, value) in attrs_yrs_map.iter(txn)
                                    {
                                        if let yrs::types::Value::Any(
                                            any_value,
                                        ) = value
                                        {
                                            if let Some(json_value) =
                                                Utils::yrs_any_to_json_value(
                                                    &any_value,
                                                )
                                            {
                                                mark_attrs.insert(
                                                    key.to_string(),
                                                    json_value,
                                                );
                                            }
                                        }
                                    }
                                }
                            }

                            marks.push_back(Mark {
                                r#type: mark_type,
                                attrs: mark_attrs,
                            });
                        }
                    }
                }
            }

            // 创建节点
            let content_vec: Vec<String> =
                content.clone().into_iter().collect();
            let marks_vec: Vec<Mark> = marks.clone().into_iter().collect();
            let node =
                Node::new(node_id, node_type, attrs, content_vec, marks_vec);

            let node_id_typed = NodeId::from(node_id);
            tree_nodes.insert(node_id_typed.clone(), Arc::new(node));

            // 记录父子关系
            if let Some(parent) = parent_id {
                parent_map.insert(node_id_typed.clone(), parent.clone());
            }

            // 递归处理子节点
            for child_id in content {
                Utils::build_tree_nodes_from_yrs(
                    &child_id,
                    nodes_map,
                    txn,
                    tree_nodes,
                    parent_map,
                    Some(&node_id_typed),
                )?;
            }
        }
        Ok(())
    }

    /// 递归构建 NodeEnum
    pub fn build_node_enum_from_map(
        node: &Node,
        tree_nodes: &HashMap<NodeId, Arc<Node>>,
    ) -> mf_model::node_type::NodeEnum {
        let mut children = Vec::new();
        for child_id in &node.content {
            if let Some(child_node) = tree_nodes.get(child_id) {
                children.push(Utils::build_node_enum_from_map(
                    child_node, tree_nodes,
                ));
            }
        }
        mf_model::node_type::NodeEnum(node.clone(), children)
    }
}