نظرة عامة
يعرض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.
يتبع التحجيم هذا الترتيب:
- يبدأ
resizableمن الارتفاع المعروض، بما في ذلك أطوال CSS، ضمن حدود مقبض تغيير الحجم من 50px إلى 500px. يضيف مقبضًا بعد تحميل Monaco. يؤدي السحب أو إدخال لوحة المفاتيح بعد ذلك إلى تعيين ارتفاع بالبكسل. يؤدي تغييرheightإلى إعادة تعيينه ضمن الحدود نفسها. - بخلاف ذلك، يستخدم
autoHeightارتفاع محتوى Monaco. يُستخدم الارتفاع الصريح أو الافتراضي إلى أن يُقاس المحتوى. - بخلاف ذلك، يُطبّق
heightالصريح أو الافتراضي.
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 في المكوّنات الأصلية والاختصارات على مستوى المستند ضغطات المفاتيح المكتوبة في المحرر.الإزاحة
محرر التعليمات البرمجية
Grows the editor to fit its content instead of using
height. Ignored when resizable is set.Called with Monaco before editor creation, after the Twenty theme has been defined. Use it to register custom themes or languages.
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.default supplies top and bottom padding. comfortable also widens the gap between line numbers and code. Explicit Monaco options override these defaults.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.
Shows a loader at the editor height instead of the editor. A loader also shows while Monaco loads.
Keeps the current model when the editor unmounts. Defaults to false. The caller owns disposal of retained models.
Monaco language identifier for highlighting and language services, such as
json or typescript.Content shown while Monaco loads or
isLoading is true. Defaults to the Twenty Loader.Monaco React callback with the full content (string or undefined) and the original IModelContentChangedEvent.
Called when the editor is ready, with the editor instance and the Monaco namespace. The selected theme is already applied.
Called with the markers Monaco reports after validating the content, such as JSON syntax errors.
Monaco editor options merged over Twenty defaults. Explicit values, including padding, replace the corresponding defaults. The
theme prop takes precedence over options.theme.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.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.
Accessible name of the resize handle. Pass a localized label when
resizable is enabled.Returns custom validation markers for the current model content. Runs on mount, model selection and content changes, including controlled updates.
Monaco theme name. Takes precedence over
options.theme, which takes precedence over the Twenty theme. Define custom themes in beforeMount.Border treatment:
default draws a rounded border, with-header attaches the editor below a CodeEditorHeader, and borderless removes the border.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.رأس محرر التعليمات البرمجية
Content shown at the end of the header, such as actions. Use a fragment for multiple elements.
Replace the root div or compose it with another component. Native props and refs target the rendered root.
Content shown at the start of the header, before the title, such as tabs. Use a fragment for multiple elements.