Bỏ qua, đến nội dung

@digiforce-nc/plugin-field-code

Plugin cung cấp kiểu field code editor — tích hợp CodeMirror để nhập và hiển thị mã nguồn trực tiếp trong bản ghi, hỗ trợ syntax highlighting cho Java, JavaScript, Python, SQL và nhiều ngôn ngữ khác.

Plugin này làm gì?

Hãy hình dung: bạn cần lưu đoạn SQL query, đoạn script Python, hoặc JSON config trong bản ghi. Textarea thường không đủ — thiếu syntax highlighting, thiếu line numbers, dễ sai cú pháp. Plugin này thay thế bằng CodeMirror editor đầy đủ tính năng, lazy-loaded để không ảnh hưởng hiệu năng trang.

Hai chế độ hoạt động

#Chế độChi tiết
1Edit modeCodeMirror editor với syntax highlighting, line numbers, auto-indent, JS linting
2Display modeRead-only code block với highlighting — không load toàn bộ editor

Kiến trúc

Tổng quan

Plugin này là client-only — server không có logic xử lý. Giá trị code được lưu dạng text thuần trong database. Toàn bộ tính năng nằm ở client: editor, highlighting, linting.

Luồng hoạt động

Ngôn ngữ hỗ trợ

Ngôn ngữExtension CodeMirror
JavaScript@codemirror/lang-javascript + jshint linting
TypeScript@codemirror/lang-javascript (ts mode)
Java@codemirror/lang-java
Python@codemirror/lang-python
SQL@codemirror/lang-sql
JSON@codemirror/lang-json
HTML@codemirror/lang-html
CSS@codemirror/lang-css
Markdown@codemirror/lang-markdown

Settings cấu hình

SettingKiểuMô tả
heightstringChiều cao editor (ví dụ: 200px, 50vh)
indentnumberSố space cho mỗi level indent (mặc định 2)

Components

ComponentChức năng
CodeEditorComponent chính — lazy-loaded CodeMirror wrapper
CodeFieldWidgetWidget cho chế độ edit (đăng ký x-component)
DisplayCodeFieldWidgetWidget cho chế độ display (read-only highlight)

Ví dụ sử dụng

Cấu hình field code editor

typescript
const codeField = {
  name: 'sqlQuery',
  type: 'text',
  interface: 'code',
  uiSchema: {
    type: 'string',
    title: 'SQL Query',
    'x-component': 'CodeEditor',
    'x-component-props': {
      language: 'sql',
      height: '300px',
      indent: 4,
    },
  },
};

Hiển thị code read-only

typescript
const displaySchema = {
  type: 'string',
  'x-component': 'CodeEditor',
  'x-component-props': {
    language: 'javascript',
    readOnly: true,
  },
  'x-read-pretty': true,
};

Thành phần client

Thành phầnMô tả
CodeEditorCodeMirror wrapper, lazy-loaded
CodeFieldWidgetWidget edit mode
DisplayCodeFieldWidgetWidget read-only

Dependencies

PackageVai trò
@digiforce-nc/clientClient framework — đăng ký component
@digiforce-nc/serverServer framework (peer) — plugin class rỗng
@codemirror/*Core editor, ngôn ngữ, theme
@uiw/react-codemirrorReact binding cho CodeMirror
jshintJavaScript linting engine

Mục lục chi tiết