# @covalent/code-editor 中文文档

规范地址：https://npmcn.com/package/%40covalent/code-editor/

版本：11\.23\.0
npm 元数据更新时间：2026-10-02T15:32:37.692Z
中文译文更新时间：2026-11-10T22:27:56.310Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40covalent/code-editor
原文对照：https://npmcn.com/package/%40covalent/code-editor/?lang=original
项目主页：https://github.com/teradata/covalent#readme
源码仓库：https://github.com/teradata/covalent

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：137 kB，5 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：1 个
- peer 依赖：@angular/core 20\.x\.x，monaco-editor ^0\.55\.1，@angular/common 20\.x\.x
- 首次发布：2017-06-22T23:24:13.242Z
- 最新版本发布：2026-10-02T15:32:37.476Z
- 近 30 天下载量：2555（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：422（近 12 个月 74 个）

## 中文简介

Teradata UI 平台代码编辑器模块

## 安装

```sh
npm install @covalent/code-editor
```

也可以使用：`pnpm add @covalent/code-editor`、`yarn add @covalent/code-editor`、`bun add @covalent/code-editor`

## README

## TdCode编辑器 组件: td-code-编辑器

`<td-code-editor>` 用于基于 Covalent 和 Monaco 编辑的代码编辑组件。 该组件可以在Electron和Web浏览器中运行.

## API 总结

#### 投入

-   值 ?: 字符串
    -   代码编辑器实例的值
-   语言 ? : 字符串
    -   编辑器实例中用于语法的语言
-   注册语言?: 函数( )
    -   在编辑器中注册自定义语言
-   编辑器Style?: 字符串
    -   对编辑容器应用了额外的样式
-   主题? : 字符串
    -   用于编辑器样式的主题
-   编辑选项? : 对象
    -   在此列出的有效配置对象 : [https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.ieditoropulations.html 互联网档案馆的存檔,存档日期2013-12-22](https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.ieditoroptions.html)
-   布局 ?: 函数( )
    -   指示编辑器重新测量容器

#### 属性

-   是福尔斯克林吗
    -   编辑器目前为全屏模式
-   完全ScreenKeyBinding?:数字
    -   将短切的密钥代码设置为全屏。 列出的选项参见这里 : [https://microsoft.github.io/monaco-editor/api/enums/monaco.keycode.html 互联网档案馆的存檔,存档日期2013-12-22](https://microsoft.github.io/monaco-editor/api/enums/monaco.keycode.html)

#### 活动

-   编辑器已初始化吗 ?: 函数( $event)
    -   编辑器初始化完成后发送 。 事件传递一个引用到实际编辑器实例,可用于调用Angular组件外的额外操作.
-   编辑器已更改 ?: 函数( $event)
    -   当编辑器的配置改变时发送
-   编辑器变换?: 函数( $event)
    -   当编辑器语言发生变化时被发送

## 安装

此组件可以被安装为npm包.

```bash
npm install @covalent/code-editor
```

## 设置

由于摩纳哥已知的一期 编辑版0.2.0 [https://github.com/microsoft/monaco-editor/issues/1842 (中文(简体) )](https://github.com/microsoft/monaco-editor/issues/1842) 使用 monaco 编辑器的0.17.1 版本.

如果您注意到图标存在不加载的问题, Css- loader 有已知的问题, 它无法正确加载文件字体 [https://github.com/microsoft/monaco-editor/issues/2742 (英语)](https://github.com/microsoft/monaco-editor/issues/2742) 降级为 `css-loader@5.2.7`。 。 。 .

我们利用摩纳哥编辑的无害环境管理。 要包含此构件, 您必须使用自定义的网页包 。 见 [https://github.com/Microsoft/monaco-editor/blob/master/docs/integrate-em.md 互联网档案馆的存檔,存档日期2013-12-21](https://github.com/Microsoft/monaco-editor/blob/master/docs/integrate-esm.md) 更多信息.

安装 Webpack 自定义构建器 .

```bash
npm install --save-dev @angular-builders/custom-webpack
```

安装摩纳哥编辑器 Webpack 扩展插件 .

```bash
npm install --save-dev monaco-editor-webpack-plugin
```

使用 Monaco Editor Webpack 插件创建 Webpack 配置文件 。 语言和特性可以包含/排除,以控制由此而来的图像大小.

```javascript
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');

module.exports = {
  // target should only be specified when including component in Electron app
  target: 'electron-renderer',
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader'],
      },
      {
        test: /\.ttf$/,
        type: 'asset/resource',
      },
    ],
  },
  plugins: [
    new MonacoWebpackPlugin({
      languages: ['css', 'html', 'javascript', 'sql', 'typescript'],
      features: ['contextmenu', 'clipboard', 'find'],
    }),
  ],
};
```

如果使用Webpack 4或更低,则需要使用文件装入器,而不是像下面的代码那样使用资产模块:

```javascript
{
  test: /\.ttf$/,
  use: ['file-loader']
}
```

注意:如果将这个组件纳入到电子应用程序中,请定义电子还原器目标. 参见这里的电子示例: [https://github.com/Teradata/covalent-electron/blob/main/monaco-webpack.config.js 互联网档案馆的存檔,存档日期2014-12-22](https://github.com/Teradata/covalent-electron/blob/main/monaco-webpack.config.js)

引用您的 angular.json 构建配置中的 Webpack 文件 .

```json
...
"build": {
  "builder": "@angular-builders/custom-webpack:browser",
  "options": {
    "customWebpackConfig": {
      "path": "./monaco-webpack.config.js",
      "mergeStrategies": {
        "module.rules": "prepend"
      }
    },
...
```

导入 **共价代码编辑器模块** 在您的 NGMudule 中:

```typescript
import { CovalentCodeEditorModule } from '@covalent/code-editor';
@NgModule({
  imports: [
    CovalentCodeEditorModule,
    ...
  ],
  ...
})
export class MyModule {}
```

## 示例

```html
<td-code-editor [style.height.px]="200" editorStyle="border:0;" flex theme="vs" language="sql" [editorOptions]="{readOnly:true, fontSize:20}" [(ngModel)]="model" (ngModelChange)="callBackFunc()"> </td-code-editor>
```

## 运行单元测试

运行 `nx test angular-code-editor` 执行单元测试.

## 常见问题

### @covalent/code-editor 是什么？

Teradata UI 平台代码编辑器模块

### 如何安装 @covalent/code-editor？

在项目目录运行 npm install @covalent/code-editor，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@angular/core（20\.x\.x）、monaco-editor（^0\.55\.1）、@angular/common（20\.x\.x）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @covalent/code-editor 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @covalent/code-editor 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @covalent/code-editor 可以商用吗？

@covalent/code-editor 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @covalent/code-editor 的下载量有多少？

根据 npm 官方下载统计，@covalent/code-editor 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,555 次，日均约 85 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @covalent/code-editor 还在维护吗？

最新版本 11\.23\.0 发布于 2026/10/02（1 个月前）；至今共发布 422 个版本，其中过去 12 个月发布了 74 个；首次发布于 2017/06/23。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

