> ## Documentation Index
> Fetch the complete documentation index at: https://docs.twenty.com/llms.txt
> Use this file to discover all available pages before exploring further.

# محرر التعليمات البرمجية

> حرّر الكود واعرضه في محرر Monaco بتنسيق سمة Twenty.

## نظرة عامة

يعرض `CodeEditor` محرر [Monaco](https://microsoft.github.io/monaco-editor/) يتبع سمة Twenty في الوضعين الفاتح والداكن. يضيف `CodeEditorHeader` شريطًا فوقه لعنوان أو علامات تبويب أو إجراءات. كلاهما يأتيان من `twenty-ui/components/code-editor`، وهي نقطة إدخال منفصلة، لذا يعمل باقي Twenty UI دون Monaco.

> [tab] Light
>
> [مثال على محرر الكود (light)](https://storybook.twenty.com/iframe.html?id=ui-components-codeeditor--display\&viewMode=story\&globals=colorScheme:light)

> [tab] Dark
>
> [مثال على محرر الكود (dark)](https://storybook.twenty.com/iframe.html?id=ui-components-codeeditor--display\&viewMode=story\&globals=colorScheme:dark)

[Open in Storybook](https://storybook.twenty.com/?path=/story/ui-components-codeeditor--display)

## استخدام

```tsx theme={null}
import { CodeEditor, CodeEditorHeader } from 'twenty-ui/components/code-editor';

export const WorkspaceEditor = () => (
  <>
    <CodeEditorHeader title="workspace.json" />
    <CodeEditor
      variant="with-header"
      language="json"
      defaultValue={JSON.stringify(
        { name: 'Acme', plan: 'Pro', seats: 12 },
        null,
        2,
      )}
      height={160}
    />
  </>
);
```

## إعداد

ثبّت التبعيات الاختيارية النظيرة للمحرر:

```bash theme={null}
yarn add @monaco-editor/react monaco-editor
```

يحمّل المحرر حزمة `monaco-editor` المثبّتة لديك بدلًا من نسخة من CDN. يشغّل Monaco ميزات اللغة، مثل التحقق والإكمال التلقائي، في web workers، ويعتمد إنشاء هذه العمال على أداة التجميع لديك. قبل تركيب محرر، عرّف `MonacoEnvironment.getWorker` كما هو موصوف في [دليل تكامل ESM لـ Monaco](https://github.com/microsoft/monaco-editor/blob/main/docs/integrate-esm.md). بدونه، سيظل المحرر معروضًا، لكن ميزات اللغة لن تكون متاحة.

لا يعمل محرر الكود داخل [مكوّنات الواجهة الأمامية](https://docs.twenty.com/ar/developers/extend/apps/layout/front-components.md) بعد.

## محتوى متحكم فيه

```tsx theme={null}
import { useState } from 'react';
import { isDefined } from 'twenty-ui/utilities';
import { CodeEditor } from 'twenty-ui/components/code-editor';

export const PayloadEditor = () => {
  const [payload, setPayload] = useState('{\n  "name": "Acme"\n}');

  return (
    <CodeEditor
      language="json"
      value={payload}
      onChange={(value) => {
        if (isDefined(value)) {
          setPayload(value);
        }
      }}
      height={200}
    />
  );
};
```

يتضمن `CodeEditorProps` عقد محرر Monaco React المثبّت. يتلقى `onChange(value, event)` المحتوى (`string | undefined`) و`IModelContentChangedEvent` الأصلي، بما في ذلك النطاقات التي تم تغييرها والإصدار وتفاصيل التراجع/الإعادة. تحقق من قيمة غير معرّفة عند الحد الذي يتطلب تطبيقك فيه سلسلة نصية.

يعمل `beforeMount(monaco)` بعد تعريف سمة Twenty وقبل إنشاء المحرر. يتلقى `onMount(editor, monaco)` مثيل المحرر بعد تطبيق السمة المحددة. تتلقى `onValidate(markers)` تشخيصات Monaco دون تغيير. يمرر `overrideServices` إلى إنشاء المحرر.

يظهر `Loader` من Twenty إلى أن يتم تحميل Monaco. مرر `loading` لتخصيص ذلك المحتوى، أو عيّن `isLoading` لإبقائه مرئيًا أثناء تحميل بياناتك الخاصة. يقوم `isLoading` بإلغاء تحميل محرر موجود ويتبع قواعد التخلص من النماذج في Monaco React.

## النماذج والقيم

استخدم `defaultValue` و`defaultLanguage` و`defaultPath` لتهيئة نموذج غير متحكم به، أو `value` و`language` و`path` للتحديثات. تحتفظ هذه الخصائص بسلوك Monaco React، بما في ذلك قواعده لاختيار القيم الأولية وإعادة استخدام معرّف URI لنموذج موجود. لا يؤدي تغيير `defaultValue` إلى الكتابة فوق نموذج موجود بالفعل.

يؤدي تغيير `path` إلى تحديد نموذج أو إنشائه. تكون القيمة الافتراضية لـ `saveViewState` هي true، ويستعيد حالة مؤشر النموذج وتمريره عند الرجوع إليه. يكشف `line` عن سطر عند تحميل المحرر أو تغيّر الخاصية. تكون القيمة الافتراضية لـ `keepCurrentModel` هي false؛ وعندما تكون true، يكون المستدعي مسؤولًا عن التخلص من النموذج المحتفظ به.

```tsx theme={null}
import { CodeEditor } from 'twenty-ui/components/code-editor';

export const FileEditor = () => (
  <CodeEditor
    defaultPath="file:///workspace/settings.json"
    defaultLanguage="json"
    defaultValue={'{\n  "name": "Acme"\n}'}
    saveViewState
  />
);
```

## التكامل الأصلي

يشير `ref` إلى div الخارجي لمجموعة Twenty، الذي يستخدم `display: contents` افتراضيًا. يحافظ هذا على قسم Monaco ضمن تخطيط التحجيم وflex الخاص بالعنصر الأب. لا يحتوي div الخارجي على مربع تخطيط قابل للقياس.

استخدم `containerProps` للسمات الأصلية لمعالج الأحداث والفئة والنمط الخاصة بـ div الخارجي. تتطلب الخلفية والحدود والحشو والأبعاد تجاوز `display` الخاص به. فضّل قسم `wrapperProps` في Monaco React لأنماط التخطيط وref أصلي قابل للقياس. يستهدف `className` div حاوية المحرر داخل ذلك القسم؛ استخدم `onMount` لمثيل محرر Monaco.

يحتوي قسم Monaco React على محتوى التحميل الخاص به إلى أن يصبح المحرر جاهزًا. أثناء تهيئة Monaco المضمّنة الأولية أو `isLoading`، يعرض Twenty أداة تحميل منفصلة دون `wrapperProps` أو `className`. يبقى `containerProps` و`ref` الخارجيان متاحين في هذه الحالات.

```tsx theme={null}
import { useRef } from 'react';
import { CodeEditor } from 'twenty-ui/components/code-editor';

export const NamedEditor = () => {
  const containerRef = useRef<HTMLDivElement>(null);

  return (
    <CodeEditor
      ref={containerRef}
      containerProps={{ id: 'payload-editor' }}
      wrapperProps={{ 'data-editor-surface': 'payload' }}
      options={{ ariaLabel: 'Payload code' }}
    />
  );
};
```

## إضافة ترويسة

```tsx theme={null}
import { CodeEditor, CodeEditorHeader } from 'twenty-ui/components/code-editor';
import { Button } from 'twenty-ui/primitives/input';

type ScriptEditorProps = {
  script: string;
  onRun: () => void;
};

export const ScriptEditor = ({ script, onRun }: ScriptEditorProps) => (
  <div>
    <CodeEditorHeader
      title="index.ts"
      endElement={
        <Button size="sm" onClick={onRun}>
          Run
        </Button>
      }
    />
    <CodeEditor language="typescript" value={script} variant="with-header" />
  </div>
);
```

يزيل `variant="with-header"` الحد العلوي للمحرر لكي يتصل بالترويسة. استخدم `borderless` عندما يكون المحرر داخل سطح يحتوي بالفعل على حد.

يقبل الرأس محتوى العقدة في `title` و`startElement` و`endElement`. استخدم الأجزاء لعلامات تبويب أو إجراءات متعددة. تستهدف الخصائص الأصلية و`className` و`style` و`ref` عنصر div الجذر الخاص به. استخدم `render` لاستبدال ذلك الجذر، على سبيل المثال `render={<header />}`. توفر الخاصية `title` محتوى مرئيًا؛ ولتعيين سمة title أصلية، مرّرها على العنصر المزوّد إلى `render`.

## الحجم والمظهر

يبلغ ارتفاع المحرر 450px وعرضه 100% افتراضيًا. يقبل `width` و`height` البكسلات أو طول CSS.

يتبع التحجيم هذا الترتيب:

1. يبدأ `resizable` من الارتفاع المعروض، بما في ذلك أطوال CSS، ضمن حدود مقبض تغيير الحجم من 50px إلى 500px. يضيف مقبضًا بعد تحميل Monaco. يؤدي السحب أو إدخال لوحة المفاتيح بعد ذلك إلى تعيين ارتفاع بالبكسل. يؤدي تغيير `height` إلى إعادة تعيينه ضمن الحدود نفسها.
2. بخلاف ذلك، يستخدم `autoHeight` ارتفاع محتوى Monaco. يُستخدم الارتفاع الصريح أو الافتراضي إلى أن يُقاس المحتوى.
3. بخلاف ذلك، يُطبّق `height` الصريح أو الافتراضي.

يفعّل Monaco React `automaticLayout` افتراضيًا. إذا عطّلته في `options`، فيجب أن يستدعي تطبيقك `editor.layout()` عندما يتغير حجم الحاوية. يتم تمرير `wrapperProps.style` الأصلي دون تغيير ويمكنه تجاوز عرض القسم أو ارتفاعه.

مرّر `resizeLabel` مترجمًا لتسمية مقبض تغيير الحجم لقارئات الشاشة.

يوفر Twenty إعدادات افتراضية للخط والتنسيق وشريط التمرير والخريطة المصغرة والحشو. يوسّع `contentPadding="comfortable"` أيضًا الفجوة بين أرقام الأسطر والرمز البرمجي. تستبدل `options` الخاصة بالمستدعي الإعدادات الافتراضية المقابلة، بما في ذلك كائن `padding` بالكامل و`lineDecorationsWidth`.

تكون لـ `theme` الصريحة الأولوية على `options.theme`؛ وبخلاف ذلك، يتبع المحرر النسق الفاتح أو الداكن لـ Twenty. عرّف سمات Monaco المخصصة في `beforeMount`. سمات Monaco عامة، لذا فإن تغيير إحداها يؤثر في محررات Monaco الأخرى على الصفحة نفسها. يتحكم `transparentBackground` في مظهر غلاف Twenty بشكل مستقل.

## التحقق

يبلّغ Monaco عن تشخيصاته الخاصة، مثل أخطاء صياغة JSON، عبر `onValidate`. لإضافة تشخيصاتك الخاصة، أعد بيانات العلامات من `setMarkers`. يعمل عند التحميل واختيار النموذج وكل تغيير في محتوى النموذج، بما في ذلك تحديثات القيم المتحكم بها، ويستبدل العلامات المخصصة للنموذج الحالي.

## لوحة المفاتيح

أثناء تركيز المحرر، يمنع انتشار أحداث لوحة المفاتيح. لا تتلقى معالجات مفاتيح React في المكوّنات الأصلية والاختصارات على مستوى المستند ضغطات المفاتيح المكتوبة في المحرر.

## الإزاحة

### محرر التعليمات البرمجية

**autoHeight** `boolean` (default: false)

Grows the editor to fit its content instead of using `height`. Ignored when `resizable` is set.

**beforeMount** `BeforeMount`

Called with Monaco before editor creation, after the Twenty theme has been defined. Use it to register custom themes or languages.

**className** `string`

Class applied to the Monaco editor-container div.

**containerProps** `Omit\<Omit\<DetailedHTMLProps\<HTMLAttributes\<HTMLDivElement\>, HTMLDivElement\>, "ref"\>, "children"\>`

Native props, className and style for the outer Twenty group div. Its default `display: contents` preserves the Monaco section's parent sizing and flex layout without creating a layout box. Background, border, padding and dimensions require overriding `display`. Prefer `wrapperProps` for section layout styles. Its keyboard handler runs before focused editor keystrokes stop propagating.

**contentPadding** `"comfortable" | "default"` (default: default)

`default` supplies top and bottom padding. `comfortable` also widens the gap between line numbers and code. Explicit Monaco options override these defaults.

**defaultLanguage** `string`

Initial Monaco language for a newly created model.

**defaultPath** `string`

Initial model URI, used when `path` is omitted.

**defaultValue** `string`

Initial content for a model created by Monaco React.

**height** `string | number` (default: 450)

Editor height in pixels or as a CSS length, defaulting to 450px. Resizing starts from this rendered height and resets when this prop changes. Auto-height uses it until content is measured.

**isLoading** `boolean` (default: false)

Shows a loader at the editor height instead of the editor. A loader also shows while Monaco loads.

**keepCurrentModel** `boolean` (default: false)

Keeps the current model when the editor unmounts. Defaults to false. The caller owns disposal of retained models.

**language** `string`

Monaco language identifier for highlighting and language services, such as `json` or `typescript`.

**line** `number`

Line revealed when the editor mounts or this prop changes.

**loading** `ReactNode` (default: \<Loader />)

Content shown while Monaco loads or `isLoading` is true. Defaults to the Twenty Loader.

**onChange** `OnChange`

Monaco React callback with the full content (string or undefined) and the original IModelContentChangedEvent.

**onMount** `OnMount`

Called when the editor is ready, with the editor instance and the Monaco namespace. The selected theme is already applied.

**onValidate** `OnValidate`

Called with the markers Monaco reports after validating the content, such as JSON syntax errors.

**options** `IStandaloneEditorConstructionOptions`

Monaco editor options merged over Twenty defaults. Explicit values, including padding, replace the corresponding defaults. The `theme` prop takes precedence over `options.theme`.

**overrideServices** `IEditorOverrideServices`

Monaco services passed through when the editor is created.

**path** `string`

URI of the current model. Changing it selects or creates a model using Monaco React behavior.

**ref** `Ref\<HTMLDivElement\>`

Ref to the outer Twenty group div, which has no measurable layout box with its default `display: contents`. Use `wrapperProps` for a measurable section ref or `onMount` for the Monaco editor instance.

**resizable** `boolean` (default: false)

Adds a height resize handle after Monaco mounts, bounded from 50px to 500px. Takes precedence over autoHeight. A changed height prop resets the resized height within these bounds.

**resizeLabel** `string` (default: Resize editor)

Accessible name of the resize handle. Pass a localized label when `resizable` is enabled.

**saveViewState** `boolean` (default: true)

Preserves Monaco React view state between model changes. Defaults to true.

**setMarkers** `((value: string) =\> IMarkerData[])`

Returns custom validation markers for the current model content. Runs on mount, model selection and content changes, including controlled updates.

**theme** `string` (default: Twenty theme)

Monaco theme name. Takes precedence over `options.theme`, which takes precedence over the Twenty theme. Define custom themes in `beforeMount`.

**transparentBackground** `boolean`

Removes the editor background so the surrounding surface shows through.

**value** `string`

Content of the editor. Pair it with `onChange` to control the editor.

**variant** `"borderless" | "default" | "with-header"` (default: default)

Border treatment: `default` draws a rounded border, `with-header` attaches the editor below a `CodeEditorHeader`, and `borderless` removes the border.

**width** `string | number` (default: 100%)

Width of the Monaco React section, in pixels or as a CSS length.

**wrapperProps** `object`

Native props passed unchanged to the Monaco React section around the editor and Monaco React loading content. Use this section for layout styles and a measurable native ref. Not applied to the separate Twenty loader during initial Monaco configuration or `isLoading`. Native styles can override the section sizing.

### رأس محرر التعليمات البرمجية

**className** `string`

Additional CSS class applied to the root element.

**endElement** `ReactNode`

Content shown at the end of the header, such as actions. Use a fragment for multiple elements.

**ref** `Ref\<HTMLDivElement\>`

Ref forwarded to the rendered root element, a div by default.

**render** `ReactElement\<unknown, string | JSXElementConstructor\<any\>\> | ComponentRenderFn\<HTMLProps, \{\}\>`

Replace the root div or compose it with another component. Native props and refs target the rendered root.

**startElement** `ReactNode`

Content shown at the start of the header, before the title, such as tabs. Use a fragment for multiple elements.

**style** `CSSProperties`

Inline styles applied to the root element.

**title** `ReactNode`

Content shown after `startElement`. This is visible header content, not the native HTML title attribute.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.