Skip to content

节点内容 ​

每个 JmNode 必须携带一个 content: JmNodeContent。当前已发布的只有 text 类型。

类型细节见 节点 API 参考。

1. 类型 ​

ts
const JmNodeContentType = { Text: 'text' } as const;

type 是 'text',value 是 string。value 在类型声明上是 unknown,为将来扩展(图片、富文本等)留余地。遇到不认识的 type,反序列化报错,不静默降级。

2. 创建 ​

ts
// 推荐
const content = JmNodeContent.createText('节点文字');

// 等价但啰嗦
const content = new JmNodeContent(JmNodeContentType.Text, '节点文字');

构造时校验 type:不在 JmNodeContentType 的取值里就抛 Error。

3. 类型判断 ​

ts
content.isText()                    // → true
content.hasType(JmNodeContentType.Text)  // → true,等价
content.getText()                   // → '节点文字',非 text 抛 Error

4. 节点使用 ​

ts
const mind = new JmMind({ name: '我的图' });
mind.root.content = JmNodeContent.createText('根节点');

mind.addNode(
    JmNodeContent.createText('子节点'),
    { parentId: mind.root.id }
);

addChildNode 也接受字符串或 JmNodeContent,是在指定父节点下添加子节点的更短写法。

5. 序列化 ​

json
{
  "id": "jm-a1",
  "content": { "type": "text", "value": "节点文字" }
}

type 和 value 原样写进 JSON,反序列化原样读回。

本文档由 AI 协助生成 / This documentation was prepared with AI assistance