@fieldia/code

中文文档
0.36.1发布于 2026/10/28MITTypeScriptESM + CJS近 30 天下载 4037

Fieldia fields in a code editor: JSON, and text in 44 languages from Python to SQL. CodeMirror 6, each grammar loaded only when a field uses it.

原始使用文档
文档目录3 节
  1. Code in a language
  2. JSON
  3. Every editor

@fieldia/code

Fieldia fields in a code editor: JSON, and text in any of 44 languages, coloured as you type. CodeMirror 6, installed with the package, so it works offline; each language's grammar loads only when a field uses it.

npm install @fieldia/code
import { mountViewer } from '@fieldia/viewer';
import { codeWidgets } from '@fieldia/code';

mountViewer(host, { page, dataSource, widgets: codeWidgets });

A node opts in with "widget": "code"; others keep their plain box. In React pass widgets={codeWidgets}, in Vue :widgets="codeWidgets", in Angular [widgets]="codeWidgets" (spread it beside gridWidgets to use both).

Code in a language

On a text field (or a char field, kept to one line), name the language:

{ "type": "field", "field": "script", "widget": "code", "options": { "language": "python" } }
  • JavaScript, TypeScript (and either with JSX), Python, SQL (and the PostgreSQL, MySQL, SQLite and SQL Server dialects), HTML, CSS, XML, YAML, Markdown, JSON, Java, C, C++, PHP, Rust and Go; and shell, PowerShell, Ruby, Perl, Lua, R, Swift, Kotlin, C#, Scala, Dart, Dockerfile, TOML, INI, diff, nginx, Protocol Buffers, Haskell, Clojure, Erlang, Groovy and Julia. CODE_LANGUAGES lists them, each with its other names (py, bash, yml, c++…); a name is read in any case.
  • Each grammar is a dynamic import of its own, so your bundler makes it a file of its own: a page with one Python field loads Python's grammar and no other.
  • A language not on the list, or none, is plain text.
  • What is typed reaches the form as text; an empty editor as nothing (null).

JSON

On a json field, "widget": "code" edits the value as JSON: valid JSON reaches the form as it is typed; text that is not valid is said so, and the last good value stays.

Every editor

  • Line numbers, bracket matching, undo.
  • Tab moves to the next field, as in every field; Ctrl+] and Ctrl+[ indent.
  • Read-only when the field is; required, invalid and its messages said to assistive technology, and named by the field's label.
  • Code reads left to right, on an Arabic page too.
  • Coloured in Fieldia's tokens: the box on the form's surface, comments in its quiet words; keywords, strings and the rest in colours that read at 4.5:1 or more on every theme's surface, light and dark. Set your own with the --fd-code-keyword, --fd-code-string, --fd-code-literal, --fd-code-function, --fd-code-type, --fd-code-tag and --fd-code-property tokens.

For VS Code's editor instead, see @fieldia/monaco.

MIT. CodeMirror is MIT too.

版本与发布记录

@fieldia/code 共发布 46 个版本,首次发布于 2026/10/03;过去 12 个月发布了 46 个版本。

版本发布日期距今
0.36.1latest2026/10/2817 天前
0.36.02026/10/2817 天前
0.35.32026/10/2818 天前
0.35.22026/10/2718 天前
0.35.12026/10/2619 天前
0.35.02026/10/2619 天前

关于 @fieldia/code 的常见问题

@fieldia/code 是什么?

Fieldia fields in a code editor: JSON, and text in 44 languages from Python to SQL. CodeMirror 6, each grammar loaded only when a field uses it.

如何安装 @fieldia/code?

在项目目录运行 npm install @fieldia/code,或使用 pnpm add / yarn add / bun add。

@fieldia/code 支持 TypeScript 吗?

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

@fieldia/code 支持 ESM 还是 CommonJS?

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

@fieldia/code 可以商用吗?

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

@fieldia/code 的下载量有多少?

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

@fieldia/code 还在维护吗?

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