الانتقال إلى المحتوى الرئيسي

نظرة عامة

يعرض CodeEditor محرر Monaco يتبع سمة Twenty في الوضعين الفاتح والداكن. يضيف CodeEditorHeader شريطًا فوقه لعنوان أو علامات تبويب أو إجراءات. كلاهما يأتيان من twenty-ui/components/code-editor، وهي نقطة إدخال منفصلة، لذا يعمل باقي Twenty UI دون Monaco.

استخدام

إعداد

ثبّت التبعيات الاختيارية النظيرة للمحرر:
يحمّل المحرر حزمة monaco-editor المثبّتة لديك بدلًا من نسخة من CDN. يشغّل Monaco ميزات اللغة، مثل التحقق والإكمال التلقائي، في web workers، ويعتمد إنشاء هذه العمال على أداة التجميع لديك. قبل تركيب محرر، عرّف MonacoEnvironment.getWorker كما هو موصوف في دليل تكامل ESM لـ Monaco. بدونه، سيظل المحرر معروضًا، لكن ميزات اللغة لن تكون متاحة. لا يعمل محرر الكود داخل مكوّنات الواجهة الأمامية بعد.

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

يتضمن 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، يكون المستدعي مسؤولًا عن التخلص من النموذج المحتفظ به.

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

يشير 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 الخارجيان متاحين في هذه الحالات.

إضافة ترويسة

يزيل 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 في المكوّنات الأصلية والاختصارات على مستوى المستند ضغطات المفاتيح المكتوبة في المحرر.

الإزاحة

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

boolean
افتراضي:"false"
Grows the editor to fit its content instead of using height. Ignored when resizable is set.
BeforeMount
Called with Monaco before editor creation, after the Twenty theme has been defined. Use it to register custom themes or languages.
string
Class applied to the Monaco editor-container div.
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.
"comfortable" | "default"
افتراضي:"default"
default supplies top and bottom padding. comfortable also widens the gap between line numbers and code. Explicit Monaco options override these defaults.
string
Initial Monaco language for a newly created model.
string
Initial model URI, used when path is omitted.
string
Initial content for a model created by Monaco React.
string | number
افتراضي:"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.
boolean
افتراضي:"false"
Shows a loader at the editor height instead of the editor. A loader also shows while Monaco loads.
boolean
افتراضي:"false"
Keeps the current model when the editor unmounts. Defaults to false. The caller owns disposal of retained models.
string
Monaco language identifier for highlighting and language services, such as json or typescript.
number
Line revealed when the editor mounts or this prop changes.
ReactNode
افتراضي:"<Loader />"
Content shown while Monaco loads or isLoading is true. Defaults to the Twenty Loader.
OnChange
Monaco React callback with the full content (string or undefined) and the original IModelContentChangedEvent.
OnMount
Called when the editor is ready, with the editor instance and the Monaco namespace. The selected theme is already applied.
OnValidate
Called with the markers Monaco reports after validating the content, such as JSON syntax errors.
IStandaloneEditorConstructionOptions
Monaco editor options merged over Twenty defaults. Explicit values, including padding, replace the corresponding defaults. The theme prop takes precedence over options.theme.
IEditorOverrideServices
Monaco services passed through when the editor is created.
string
URI of the current model. Changing it selects or creates a model using Monaco React behavior.
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.
boolean
افتراضي:"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.
string
افتراضي:"Resize editor"
Accessible name of the resize handle. Pass a localized label when resizable is enabled.
boolean
افتراضي:"true"
Preserves Monaco React view state between model changes. Defaults to true.
((value: string) => IMarkerData[])
Returns custom validation markers for the current model content. Runs on mount, model selection and content changes, including controlled updates.
string
افتراضي:"Twenty theme"
Monaco theme name. Takes precedence over options.theme, which takes precedence over the Twenty theme. Define custom themes in beforeMount.
boolean
Removes the editor background so the surrounding surface shows through.
string
Content of the editor. Pair it with onChange to control the editor.
"borderless" | "default" | "with-header"
افتراضي:"default"
Border treatment: default draws a rounded border, with-header attaches the editor below a CodeEditorHeader, and borderless removes the border.
string | number
افتراضي:"100%"
Width of the Monaco React section, in pixels or as a CSS length.
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.

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

string
Additional CSS class applied to the root element.
ReactNode
Content shown at the end of the header, such as actions. Use a fragment for multiple elements.
Ref<HTMLDivElement>
Ref forwarded to the rendered root element, a div by default.
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.
ReactNode
Content shown at the start of the header, before the title, such as tabs. Use a fragment for multiple elements.
CSSProperties
Inline styles applied to the root element.
ReactNode
Content shown after startElement. This is visible header content, not the native HTML title attribute.