Skip to content

[6.x] UI Code Editor #11856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 10, 2025
30 changes: 0 additions & 30 deletions resources/css/components/fieldtypes/code.css

This file was deleted.

1 change: 0 additions & 1 deletion resources/css/cp.css
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
@import './components/fieldtypes/bard.css';
@import './components/fieldtypes/button-group.css';
@import './components/fieldtypes/checkboxes.css';
@import './components/fieldtypes/code.css';
@import './components/fieldtypes/dictionary-fields.css';
@import './components/fieldtypes/environment.css';
@import './components/fieldtypes/grid.css';
Expand Down
17 changes: 3 additions & 14 deletions resources/css/vendors/codemirror.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@

.CodeMirror {
/* Set height, width, borders, and global font properties here */
@apply py-3 text-base text-gray-800 dark:text-gray-300 font-normal leading-normal min-h-20;
@apply text-gray-800 dark:text-gray-300 font-normal leading-normal min-h-20;
}

/* PADDING */

.CodeMirror-lines {
padding: 4px 0; /* Vertical padding around content */
padding: 16px 0; /* Vertical padding around content */
}
.CodeMirror pre {
padding: 0 16px; /* Horizontal padding of content */
}
.CodeMirror-wrap {
@apply rounded-b;
}

.CodeMirror-scrollbar-filler,
.CodeMirror-gutter-filler {
Expand Down Expand Up @@ -480,7 +477,7 @@ span.CodeMirror-selectedtext {
}

.CodeMirror-fullscreen {
@apply fixed inset-0 h-auto rounded-none pt-14;
@apply fixed inset-0 h-auto rounded-none pt-13;
}

.CodeMirror-rulers {
Expand All @@ -493,14 +490,6 @@ span.CodeMirror-selectedtext {
@apply bottom-0 top-0;
}

/* Light Theme
========================================================================== */
.code-fieldtype .theme-light {
.CodeMirror {
@apply border p-0;
}
}

/* Material Theme
========================================================================== */

Expand Down
231 changes: 31 additions & 200 deletions resources/js/components/fieldtypes/CodeFieldtype.vue
Original file line number Diff line number Diff line change
@@ -1,238 +1,69 @@
<template>
<portal name="code-fullscreen" :disabled="!fullScreenMode" target-class="code-fieldtype">
<element-container @resized="refresh">
<div class="code-fieldtype-container" :class="[themeClass, { 'code-fullscreen': fullScreenMode }]">
<publish-field-fullscreen-header
v-if="fullScreenMode"
:title="config.display"
:field-actions="fieldActions"
@close="toggleFullscreen"
>
<div class="code-fieldtype-toolbar-fullscreen">
<div>
<select-input
v-if="config.mode_selectable"
:options="modes"
v-model="mode"
:is-read-only="isReadOnly"
class="text-xs leading-none"
/>
<div v-else v-text="modeLabel" class="font-mono text-xs text-gray-700"></div>
</div>
</div>
</publish-field-fullscreen-header>
<div class="code-fieldtype-toolbar" v-if="!fullScreenMode">
<div>
<select-input
v-if="config.mode_selectable"
:options="modes"
v-model="mode"
:is-read-only="isReadOnly"
class="text-xs leading-none"
/>
<div v-else v-text="modeLabel" class="font-mono text-xs text-gray-700"></div>
</div>
</div>
<div ref="codemirror"></div>
</div>
</element-container>
</portal>
<CodeEditor
ref="codeEditor"
:theme="config.theme"
:rulers="config.rulers"
:disabled="isReadOnly"
:key-map="config.key_map"
:tab-size="config.indent_size"
:indent-type="config.indent_type"
:line-numbers="config.line_numbers"
:line-wrapping="config.line_wrapping"
:allow-mode-selection="config.mode_selectable"
:mode="mode"
:model-value="value.code"
:title="config.display"
:field-actions="fieldActions"
@update:mode="modeUpdated"
@update:model-value="codeUpdated"
/>
</template>

<script>
import Fieldtype from './Fieldtype.vue';
import CodeMirror from 'codemirror';
import { markRaw } from 'vue';

// Addons
import 'codemirror/addon/edit/matchbrackets';
import 'codemirror/addon/display/fullscreen';
import 'codemirror/addon/display/rulers';

// Keymaps
import 'codemirror/keymap/sublime';
import 'codemirror/keymap/vim';

// Modes
import 'codemirror/mode/css/css';
import 'codemirror/mode/clike/clike';
import 'codemirror/mode/diff/diff';
import 'codemirror/mode/go/go';
import 'codemirror/mode/gfm/gfm';
import 'codemirror/mode/handlebars/handlebars';
import 'codemirror/mode/haml/haml';
import 'codemirror/mode/htmlmixed/htmlmixed';
import 'codemirror/mode/javascript/javascript';
import 'codemirror/mode/markdown/markdown';
import 'codemirror/mode/nginx/nginx';
import 'codemirror/mode/php/php';
import 'codemirror/mode/python/python';
import 'codemirror/mode/ruby/ruby';
import 'codemirror/mode/shell/shell';
import 'codemirror/mode/sql/sql';
import 'codemirror/mode/twig/twig';
import 'codemirror/mode/vue/vue';
import 'codemirror/mode/xml/xml';
import 'codemirror/mode/yaml/yaml';
import 'codemirror/mode/yaml-frontmatter/yaml-frontmatter';
import { CodeEditor } from '@statamic/ui';

export default {
mixins: [Fieldtype],

data() {
return {
codemirror: null,
modes: [
{ value: 'clike', label: 'C-Like' },
{ value: 'css', label: 'CSS' },
{ value: 'diff', label: 'Diff' },
{ value: 'go', label: 'Go' },
{ value: 'haml', label: 'HAML' },
{ value: 'handlebars', label: 'Handlebars' },
{ value: 'htmlmixed', label: 'HTML' },
{ value: 'less', label: 'LESS' },
{ value: 'markdown', label: 'Markdown' },
{ value: 'gfm', label: 'Markdown (GHF)' },
{ value: 'nginx', label: 'Nginx' },
{ value: 'text/x-java', label: 'Java' },
{ value: 'javascript', label: 'JavaScript' },
{ value: 'jsx', label: 'JSX' },
{ value: 'text/x-objectivec', label: 'Objective-C' },
{ value: 'php', label: 'PHP' },
{ value: 'python', label: 'Python' },
{ value: 'ruby', label: 'Ruby' },
{ value: 'scss', label: 'SCSS' },
{ value: 'shell', label: 'Shell' },
{ value: 'sql', label: 'SQL' },
{ value: 'twig', label: 'Twig' },
{ value: 'vue', label: 'Vue' },
{ value: 'xml', label: 'XML' },
{ value: 'yaml-frontmatter', label: 'YAML' },
],
mode: this.value.mode || this.config.mode,
fullScreenMode: false,
};
},
components: { CodeEditor },

computed: {
modeLabel() {
return this.modes.find((m) => m.value === this.mode).label || this.mode;
},
exactTheme() {
return this.config.theme === 'light' ? 'default' : 'material';
},
themeClass() {
return 'theme-' + this.config.theme;
mode() {
return this.value.mode || this.config.mode;
},

replicatorPreview() {
if (!this.showFieldPreviews || !this.config.replicator_preview) return;

return this.value.code ? truncate(this.value.code, 60) : '';
},
readOnlyOption() {
return this.isReadOnly ? 'nocursor' : false;
},
rulers() {
if (!this.config.rulers) {
return [];
}

let rulerColor = this.config.theme === 'light' ? '#d1d5db' : '#546e7a';

return Object.entries(this.config.rulers).map(([column, style]) => {
let lineStyle = style === 'dashed' ? 'dashed' : 'solid';

return {
column: parseInt(column),
lineStyle: lineStyle,
color: rulerColor,
};
});
},
internalFieldActions() {
return [
{
title: __('Toggle Fullscreen Mode'),
icon: ({ vm }) => (vm.fullScreenMode ? 'shrink-all' : 'expand-bold'),
icon: ({ vm }) => (vm.$refs.codeEditor.fullScreenMode ? 'shrink-all' : 'expand-bold'),
quick: true,
visibleWhenReadOnly: true,
run: this.toggleFullscreen,
run: ({ vm }) => vm.$refs.codeEditor.toggleFullscreen(),
},
];
},
},

watch: {
value(value, oldValue) {
if (value.code == this.codemirror.doc.getValue()) return;
if (!value.code) value.code = '';

this.codemirror.doc.setValue(value.code);
},
readOnlyOption(val) {
this.codemirror.setOption('readOnly', val);
},
mode(mode) {
this.codemirror.setOption('mode', mode);
this.updateDebounced({ code: this.value.code, mode: this.mode });
},
},

mounted() {
this.$nextTick(() => this.initCodeMirror());
// CodeMirror needs to be manually refreshed when made visible in the DOM.
this.$events.$on('tab-switched', () => this.$refs.codeEditor?.refresh());
},

methods: {
focus() {
this.codemirror.focus();
},
refresh() {
this.$nextTick(function () {
this.codemirror.refresh();
});
modeUpdated(mode) {
this.updateDebounced({ code: this.value.code, mode });
},
initCodeMirror() {
this.codemirror = markRaw(
CodeMirror(this.$refs.codemirror, {
value: this.value.code || '',
mode: this.mode,
direction: document.querySelector('html').getAttribute('dir') ?? 'ltr',
addModeClass: true,
keyMap: this.config.key_map,
tabSize: this.config.indent_size,
indentWithTabs: this.config.indent_type !== 'spaces',
lineNumbers: this.config.line_numbers,
lineWrapping: this.config.line_wrapping,
matchBrackets: true,
readOnly: this.readOnlyOption,
theme: this.exactTheme,
inputStyle: 'contenteditable',
rulers: this.rulers,
}),
);

this.codemirror.on('change', (cm) => {
this.updateDebounced({ code: cm.doc.getValue(), mode: this.mode });
});

this.codemirror.on('focus', () => this.$emit('focus'));
this.codemirror.on('blur', () => this.$emit('blur'));

// Refresh to ensure CodeMirror visible and the proper size
// Most applicable when loaded by another field like Bard
this.refresh();

this.codemirror.setOption('fullScreen', this.fullScreenMode);

if (this.fullScreenMode === false) {
document.documentElement.removeAttribute('style');
}

// CodeMirror also needs to be manually refreshed when made visible in the DOM
this.$events.$on('tab-switched', this.refresh);
},
toggleFullscreen() {
this.fullScreenMode = !this.fullScreenMode;
codeUpdated(code) {
this.updateDebounced({ code, mode: this.mode });
},
},
};
Expand Down
Loading
Loading