@advanced-rest-client/code-mirror

中文文档
3.1.5发布于 2021/03/12Apache-2.0ESM + CJS近 30 天下载 3377

CodeMirror as a web component

原始使用文档
文档目录11 节
  1. Required imports
  2. Accessing options
  3. Rendering hidden element
  4. Styling
  5. Usage
  6. Installation
  7. In an html file
  8. In a LitElement
  9. Development
  10. Running the demo locally
  11. Running the tests

Code mirror web component

Tests and publishing

Published on NPM

This is a wrapper for CodeMirror library. It has been adjusted to work inside shadow DOM. The component is used by Advanced REST Client and API Console.

Required imports

CodeMirror library has to be included into the document before inserting this element to the DOM. CodeMirror 6 possibly will be working with ES imports but this is not set in stone at the moment.

Below is the default set of scripts to be added to the document.

<script src="node_modules/codemirror/lib/codemirror.js"></script>
<script src="node_modules/codemirror/addon/mode/loadmode.js"></script>
<script src="node_modules/codemirror/mode/meta.js"></script>
<!--Default set of parsers -->
<script src="node_modules/codemirror/mode/javascript/javascript.js"></script>
<script src="node_modules/codemirror/mode/xml/xml.js"></script>
<script src="node_modules/codemirror/mode/htmlmixed/htmlmixed.js"></script>

If you are using JSON linter

<script src="node_modules/jsonlint/lib/jsonlint.js"></script>
<script src="node_modules/codemirror/addon/lint/lint.js"></script>
<script src="node_modules/codemirror/addon/lint/json-lint.js"></script>
<link rel="stylesheet" href="node_modules/codemirror/addon/lint/lint.css" />

Finally, if your application will use modes that aren't included in the document, you should set import URI. This will be used to resolve modes dependencies.

<script>
CodeMirror.modeURL = 'node_modules/codemirror/mode/%N/%N.js';
</script>

Accessing options

The element exposes setOption() function that should be used to directly set editor option.

cm.setOption('extraKeys', {
 'Ctrl-Space': (cm) => {
   CodeMirror.showHint(cm, CodeMirror.hint['http-headers'], {
     container: containerRef
   });
 }
});

Additionally, the element has the editor property which is a reference to the CodeMirror instance.

Rendering hidden element

If the element is active but not visible then you may need to call refresh() function on a CodeMirror instance after showing the element.

parent.style.display = 'block';
cm.editor.refresh();

Styling

See codemirror-styles.js file for CSS variables definition.

Usage

Installation

npm install --save @advanced-rest-client/code-mirror

In an html file

<html>
  <head>
    <script type="module">
      import '@advanced-rest-client/code-mirror/code-mirror.js';
    </script>
  </head>
  <body>
    <code-mirror mode="javascript">
      function myScript() {
        return 100;
      }
    </code-mirror>
  </body>
</html>

In a LitElement

import { LitElement, html } from 'lit-element';
import '@advanced-rest-client/code-mirror/code-mirror.js';

class SampleElement extends PolymerElement {
  render() {
    return html`
    <code-mirror @value-changed="${this._valueHandler}"></code-mirror>
    `;
  }

  _valueHandler(e) {
    this.value = e.detail.value;
  }
}
customElements.define('sample-element', SampleElement);

Development

git clone https://github.com/advanced-rest-client/code-mirror
cd code-mirror
npm install

Running the demo locally

npm start

Running the tests

npm test

版本与发布记录

@advanced-rest-client/code-mirror 共发布 13 个版本,首次发布于 2018/05/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.5latest2021/03/125 年前
3.1.42020/11/265 年前
3.1.32020/10/046 年前
3.1.22020/10/046 年前
3.1.12020/10/046 年前
3.1.02020/10/026 年前

关于 @advanced-rest-client/code-mirror 的常见问题

@advanced-rest-client/code-mirror 是什么?

CodeMirror as a web component

如何安装 @advanced-rest-client/code-mirror?

在项目目录运行 npm install @advanced-rest-client/code-mirror,或使用 pnpm add / yarn add / bun add。

@advanced-rest-client/code-mirror 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/advanced-rest-client__code-mirror 是否存在。

@advanced-rest-client/code-mirror 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@advanced-rest-client/code-mirror 可以商用吗?

@advanced-rest-client/code-mirror 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@advanced-rest-client/code-mirror 的下载量有多少?

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

@advanced-rest-client/code-mirror 还在维护吗?

最新版本 3.1.5 发布于 2021/03/12(5 年前);至今共发布 13 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/05/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。