API 与插件开发

开发文档2026-05-15

codes.js.cn 提供丰富的 API 接口,支持开发者扩展编辑器功能。

插件结构

// 插件示例
{
  "name": "my-plugin",
  "version": "1.0.0",
  "main": "index.js",
  "commands": [...],
  "languages": [...]
}

核心 API

codes.editor.getValue()

获取编辑器当前内容

const content = codes.editor.getValue();

codes.editor.setValue(text)

设置编辑器内容

codes.editor.setValue('Hello World');

codes.editor.insertText(pos, text)

在指定位置插入文本

codes.editor.insertText({line: 1, col: 5}, '插入文本');

codes.editor.registerCommand(id, handler)

注册自定义命令

codes.editor.registerCommand('my-command', () => {
  console.log('命令执行');
});

事件监听

codes.editor.on('change', (e) => {
  console.log('内容变更', e);
});

codes.editor.on('cursorChange', (e) => {
  console.log('光标移动', e);
});
← 返回技术文章