@fieldia/monaco

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

Fieldia text, one-line and JSON fields in VS Code’s editor (Monaco), coloured in the language a page names, following Fieldia’s themes.

原始使用文档
文档目录4 节
  1. Your bundler
  2. Language services (workers)
  3. One line
  4. Every editor

@fieldia/monaco

Fieldia text, one-line and JSON fields in VS Code's editor, Monaco: find and replace, folding, several cursors, moving lines, VS Code's keys — coloured in the language a page names, and wearing the form's theme.

npm install @fieldia/monaco
import { mountViewer } from '@fieldia/viewer';
import { monacoWidgets } from '@fieldia/monaco';

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

A node opts in with "widget": "monaco":

{ "type": "field", "field": "script", "widget": "monaco", "options": { "language": "python" } }
  • text.monaco: a text field, in the language options.language names — every name @fieldia/code takes (python, sql, typescript, bash…) and Monaco's other grammars besides (GraphQL, SCSS, Elixir, F#, HCL…): MONACO_LANGUAGES lists them. One Monaco has no grammar for is plain text.
  • char.monaco: a char field, one line of code in the language options.language names, such as a condition: order.total > 100 && !order.paid. See One line.
  • json.monaco: a json field, edited 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.

Monaco is large (about 3 MB, 800 KB gzipped, with its editing features), so it loads only when a page has such a field, and each language's grammar only when a field uses it. Until it has loaded the field is a plain text box that works as one — and stays one if Monaco cannot be loaded.

Your bundler

The widgets import monaco-editor's ES modules, which import their own CSS. Vite takes them as they are; webpack needs style-loader and css-loader for .css (and asset/resource for .ttf); esbuild writes the CSS beside your bundle, for your page to link, and needs loader: { '.ttf': 'file' }. Build with code splitting (ES modules) so Monaco is a file of its own.

Language services (workers)

With nothing more, code is coloured and no worker is started. For JSON checked as it is typed, and suggestions and checks for TypeScript, JavaScript, CSS and HTML, give the widgets workers as Monaco asks for them:

import { createMonacoWidgets } from '@fieldia/monaco';
// Vite: each worker is a module of monaco-editor's own.
import EditorWorker from 'monaco-editor/editor/editor.worker?worker';
import JsonWorker from 'monaco-editor/languages/features/json/json.worker?worker';
import TsWorker from 'monaco-editor/languages/features/typescript/ts.worker?worker';

const widgets = createMonacoWidgets({
  getWorker: (_id, label) => {
    if (label === 'json') return new JsonWorker();
    if (label === 'typescript' || label === 'javascript') return new TsWorker();
    return new EditorWorker();
  },
  // The services these workers are for; CSS's and HTML's fields ask for none.
  services: ['json', 'typescript'],
});

(css.worker and html.worker sit beside json.worker, under languages/features/css and languages/features/html.) The function becomes MonacoEnvironment.getWorker, unless your app has set a MonacoEnvironment of its own, which is kept.

Each field turns on the service of its language, for every service Monaco has. When you give workers for some of them only, name those, so the others' fields ask for none: createMonacoWidgets({ getWorker, services: ['json'] }) (json, typescript — JavaScript's too —, css — SCSS's and Less's too — and html).

One line

A char field's editor is one line, as @fieldia/code's is:

  • As tall as a text box, in a frame drawn as the form's text boxes are, in either skin and in the dark: no line numbers, no minimap, no folding. A long line scrolls sideways with the caret.
  • Enter adds no line, with Shift, Alt or Ctrl either. Ctrl+Enter (Cmd+Enter on a Mac) saves, as from any field; with the viewer's keys: { enterMovesToNext: true }, Enter moves on to the next field, as from a text box. Otherwise Enter does nothing.
  • Pasted lines become one line, as a text box joins them; a paste of line breaks alone adds nothing. A line break that gets in another way — a line copied down, a suggestion of several lines — is joined at once, and Undo takes the join back with it. A value set from elsewhere is shown whole.
  • Ctrl+F opens the browser's find, as in a text box: VS Code's find has no room in one line.
  • The node's placeholder shows while it is empty. Its input says it is one line (aria-multiline="false").

Every editor

  • 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 on the editor's input, which the field's label names and points at.
  • Text and JSON grow with their text, from a few lines to a screenful; the page scrolls past them under the mouse wheel.
  • Its theme is made from Fieldia's tokens as the page has them (theme, scheme, accent), and made again when they change. Code is coloured as in @fieldia/code, light or dark with the page, readable on every theme's surface; set your own with the --fd-code-* tokens. Monaco has one theme per page: two forms in different looks share the last one drawn.
  • Code reads left to right, on an Arabic page too.

MIT. Monaco is MIT too.

版本与发布记录

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

版本发布日期距今
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/monaco 的常见问题

@fieldia/monaco 是什么?

Fieldia text, one-line and JSON fields in VS Code’s editor (Monaco), coloured in the language a page names, following Fieldia’s themes.

如何安装 @fieldia/monaco?

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

@fieldia/monaco 支持 TypeScript 吗?

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

@fieldia/monaco 支持 ESM 还是 CommonJS?

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

@fieldia/monaco 可以商用吗?

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

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

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

@fieldia/monaco 还在维护吗?

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