2020-10-30 17:08:52 -04:00
---
2020-12-10 19:09:41 -05:00
stage: Create
group: Editor
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-10-30 17:08:52 -04:00
---
2021-06-18 17:10:06 -04:00
# Source Editor **(FREE)**
2020-10-30 14:08:56 -04:00
2021-06-18 17:10:06 -04:00
**Source Editor** provides the editing experience at GitLab. This thin wrapper around
2021-02-23 04:10:45 -05:00
[the Monaco editor ](https://microsoft.github.io/monaco-editor/ ) provides necessary
helpers and abstractions, and extends Monaco [using extensions ](#extensions ). Multiple
GitLab features use it, including:
2020-10-30 14:08:56 -04:00
2021-02-23 04:10:45 -05:00
- [Web IDE ](../../user/project/web_ide/index.md )
- [CI Linter ](../../ci/lint.md )
- [Snippets ](../../user/snippets.md )
- [Web Editor ](../../user/project/repository/web_editor.md )
2021-09-07 17:10:59 -04:00
- [Security Policies ](../../user/application_security/policies/index.md )
2020-10-30 14:08:56 -04:00
2021-06-18 17:10:06 -04:00
## How to use Source Editor
2020-10-30 14:08:56 -04:00
2021-06-18 17:10:06 -04:00
Source Editor is framework-agnostic and can be used in any application, including both
Rails and Vue. To help with integration, we have the dedicated `<source-editor>`
Vue component, but the integration of Source Editor is generally straightforward:
2020-10-30 14:08:56 -04:00
2021-06-18 17:10:06 -04:00
1. Import Source Editor:
2020-10-30 14:08:56 -04:00
2021-02-23 04:10:45 -05:00
```javascript
2021-06-18 17:10:06 -04:00
import SourceEditor from '~/editor/source_editor';
2021-02-23 04:10:45 -05:00
```
2020-10-30 14:08:56 -04:00
1. Initialize global editor for the view:
2021-02-23 04:10:45 -05:00
```javascript
2021-06-18 17:10:06 -04:00
const editor = new SourceEditor({
2021-02-23 04:10:45 -05:00
// Editor Options.
// The list of all accepted options can be found at
2022-03-30 08:09:14 -04:00
// https://microsoft.github.io/monaco-editor/api/enums/monaco.editor.EditorOption.html
2021-02-23 04:10:45 -05:00
});
```
2020-10-30 14:08:56 -04:00
1. Create an editor's instance:
2021-02-23 04:10:45 -05:00
```javascript
editor.createInstance({
2021-06-18 17:10:06 -04:00
// Source Editor configuration options.
2021-02-23 04:10:45 -05:00
})
```
2020-10-30 14:08:56 -04:00
2021-06-18 17:10:06 -04:00
An instance of Source Editor accepts the following configuration options:
2020-10-30 14:08:56 -04:00
| Option | Required? | Description |
2021-02-23 04:10:45 -05:00
| -------------- | ------- | ---- |
| `el` | `true` | `HTML Node` : The element on which to render the editor. |
| `blobPath` | `false` | `String` : The name of a file to render in the editor, used to identify the correct syntax highlighter to use with that file, or another file type. Can accept wildcards like `*.js` when the actual filename isn't known or doesn't play any role. |
| `blobContent` | `false` | `String` : The initial content to render in the editor. |
| `extensions` | `false` | `Array` : Extensions to use in this instance. |
| `blobGlobalId` | `false` | `String` : An auto-generated property.< br > **Note:** This property may go away in the future. Do not pass `blobGlobalId` unless you know what you're doing.|
2022-03-30 08:09:14 -04:00
| Editor Options | `false` | `Object(s)` : Any property outside of the list above is treated as an Editor Option for this particular instance. Use this field to override global Editor Options on the instance level. A full [index of Editor Options ](https://microsoft.github.io/monaco-editor/api/enums/monaco.editor.EditorOption.html ) is available. |
2020-10-30 14:08:56 -04:00
## API
2021-02-23 04:10:45 -05:00
The editor uses the same public API as
2022-03-30 08:09:14 -04:00
[provided by Monaco editor ](https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.IStandaloneCodeEditor.html )
2021-02-23 04:10:45 -05:00
with additional functions on the instance level:
2020-10-30 14:08:56 -04:00
2021-02-23 04:10:45 -05:00
| Function | Arguments | Description
| --------------------- | ----- | ----- |
| `updateModelLanguage` | `path` : String | Updates the instance's syntax highlighting to follow the extension of the passed `path` . Available only on the instance level.|
| `use` | Array of objects | Array of extensions to apply to the instance. Accepts only the array of _objects_ . You must fetch the extensions' ES6 modules must be fetched and resolved in your views or components before they are passed to `use` . This property is available on _instance_ (applies extension to this particular instance) and _global editor_ (applies the same extension to all instances) levels. |
2022-03-30 08:09:14 -04:00
| Monaco Editor options | See [documentation ](https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.IStandaloneCodeEditor.html ) | Default Monaco editor options |
2020-10-30 14:08:56 -04:00
## Tips
1. Editor's loading state.
2021-06-18 17:10:06 -04:00
The loading state is built in to Source Editor, making spinners and loaders
2021-02-23 04:10:45 -05:00
rarely needed in HTML. To benefit the built-in loading state, set the `data-editor-loading`
property on the HTML element that should contain the editor. When bootstrapping,
2021-06-18 17:10:06 -04:00
Source Editor shows the loader automatically.
2021-02-23 04:10:45 -05:00
2021-06-18 17:10:06 -04:00
![Source Editor: loading state ](img/editor_lite_loading.png )
2020-10-30 14:08:56 -04:00
2020-12-11 01:10:17 -05:00
1. Update syntax highlighting if the filename changes.
2020-10-30 14:08:56 -04:00
2021-02-23 04:10:45 -05:00
```javascript
// fileNameEl here is the HTML input element that contains the file name
fileNameEl.addEventListener('change', () => {
this.editor.updateModelLanguage(fileNameEl.value);
});
```
2020-10-30 14:08:56 -04:00
1. Get the editor's content.
2021-02-23 04:10:45 -05:00
We may set up listeners on the editor for every change, but it rapidly can become
an expensive operation. Instead, get the editor's content when it's needed.
For example, on a form's submission:
2020-10-30 14:08:56 -04:00
2021-02-23 04:10:45 -05:00
```javascript
form.addEventListener('submit', () => {
my_content_variable = this.editor.getValue();
});
```
2020-10-30 14:08:56 -04:00
1. Performance
2021-06-18 17:10:06 -04:00
Even though Source Editor itself is extremely slim, it still depends on Monaco editor,
which adds weight. Every time you add Source Editor to a view, the JavaScript bundle's
2021-02-23 04:10:45 -05:00
size significantly increases, affecting your view's loading performance. We recommend
you import the editor on demand if either:
2020-10-30 14:08:56 -04:00
2021-02-23 04:10:45 -05:00
- You're uncertain if the view needs the editor.
- The editor is a secondary element of the view.
2021-06-18 17:10:06 -04:00
Loading Source Editor on demand is handled like loading any other module:
2021-02-23 04:10:45 -05:00
```javascript
someActionFunction() {
2021-06-18 17:10:06 -04:00
import(/* webpackChunkName: 'SourceEditor' */ '~/editor/source_editor').
then(({ default: SourceEditor }) => {
const editor = new SourceEditor();
2021-02-23 04:10:45 -05:00
...
});
...
}
```
2020-10-30 14:08:56 -04:00
## Extensions
2021-06-18 17:10:06 -04:00
Source Editor provides a universal, extensible editing tool to the whole product,
and doesn't depend on any particular group. Even though the Source Editor's core is owned by
2021-01-14 10:10:46 -05:00
[Create::Editor FE Team ](https://about.gitlab.com/handbook/engineering/development/dev/create-editor/ ),
2021-02-23 04:10:45 -05:00
any group can own the extensions—the main functional elements. The goal of
2021-06-18 17:10:06 -04:00
Source Editor extensions is to keep the editor's core slim and stable. Any
2021-02-23 04:10:45 -05:00
needed features can be added as extensions to this core. Any group can
2021-06-18 17:10:06 -04:00
build and own new editing features without worrying about changes to Source Editor
2021-02-23 04:10:45 -05:00
breaking or overriding them.
2020-10-30 14:08:56 -04:00
2021-02-23 04:10:45 -05:00
You can depend on other modules in your extensions. This organization helps keep
2021-06-18 17:10:06 -04:00
the size of Source Editor's core at bay by importing dependencies only when needed.
2021-02-23 04:10:45 -05:00
2021-06-18 17:10:06 -04:00
Structurally, the complete implementation of Source Editor can be presented as this diagram:
2020-10-30 14:08:56 -04:00
```mermaid
graph TD;
2021-06-18 17:10:06 -04:00
B[Extension 1]---A[Source Editor]
C[Extension 2]---A[Source Editor]
D[Extension 3]---A[Source Editor]
E[...]---A[Source Editor]
F[Extension N]---A[Source Editor]
A[Source Editor]---Z[Monaco]
2020-10-30 14:08:56 -04:00
```
2021-02-23 04:10:45 -05:00
An extension is an ES6 module that exports a JavaScript object:
2020-10-30 14:08:56 -04:00
```javascript
import { Position } from 'monaco-editor';
export default {
navigateFileStart() {
this.setPosition(new Position(1, 1));
},
};
```
2021-06-18 17:10:06 -04:00
In the extension's functions, `this` refers to the current Source Editor instance.
2021-02-23 04:10:45 -05:00
Using `this` , you get access to the complete instance's API, such as the
`setPosition()` method in this particular case.
2020-10-30 14:08:56 -04:00
### Using an existing extension
2021-06-18 17:10:06 -04:00
Adding an extension to Source Editor's instance requires the following steps:
2020-10-30 14:08:56 -04:00
```javascript
2021-06-18 17:10:06 -04:00
import SourceEditor from '~/editor/source_editor';
2020-10-30 14:08:56 -04:00
import MyExtension from '~/my_extension';
2021-06-18 17:10:06 -04:00
const editor = new SourceEditor().createInstance({
2020-10-30 14:08:56 -04:00
...
});
editor.use(MyExtension);
```
### Creating an extension
2021-06-18 17:10:06 -04:00
Let's create our first Source Editor extension. Extensions are
2021-02-23 04:10:45 -05:00
[ES6 modules ](https://hacks.mozilla.org/2015/08/es6-in-depth-modules/ ) exporting a
2021-06-18 17:10:06 -04:00
basic `Object` , used to extend Source Editor's features. As a test, let's
create an extension that extends Source Editor with a new function that, when called,
2021-02-23 04:10:45 -05:00
outputs the editor's content in `alert` .
2020-10-30 14:08:56 -04:00
`~/my_folder/my_fancy_extension.js:`
```javascript
export default {
throwContentAtMe() {
alert(this.getValue());
},
};
```
2021-02-23 04:10:45 -05:00
In the code example, `this` refers to the instance. By referring to the instance,
we can access the complete underlying
2022-03-30 08:09:14 -04:00
[Monaco editor API ](https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.IStandaloneCodeEditor.html ),
2021-02-23 04:10:45 -05:00
which includes functions like `getValue()` .
2020-10-30 14:08:56 -04:00
Now let's use our extension:
`~/my_folder/component_bundle.js` :
```javascript
2021-06-18 17:10:06 -04:00
import SourceEditor from '~/editor/source_editor';
2020-10-30 14:08:56 -04:00
import MyFancyExtension from './my_fancy_extension';
2021-06-18 17:10:06 -04:00
const editor = new SourceEditor().createInstance({
2020-10-30 14:08:56 -04:00
...
});
editor.use(MyFancyExtension);
...
someButton.addEventListener('click', () => {
editor.throwContentAtMe();
});
```
2021-06-18 17:10:06 -04:00
First of all, we import Source Editor and our new extension. Then we create the
editor and its instance. By default Source Editor has no `throwContentAtMe` method.
2021-02-23 04:10:45 -05:00
But the `editor.use(MyFancyExtension)` line brings that method to our instance.
After that, we can use it any time we need it. In this case, we call it when some
theoretical button has been clicked.
2020-10-30 14:08:56 -04:00
This script would result in an alert containing the editor's content when `someButton` is clicked.
2021-06-18 17:10:06 -04:00
![Source Editor new extension's result ](img/editor_lite_create_ext.png )
2020-10-30 14:08:56 -04:00
### Tips
1. Performance
2021-06-18 17:10:06 -04:00
Just like Source Editor itself, any extension can be loaded on demand to not harm
2021-02-23 04:10:45 -05:00
loading performance of the views:
2020-10-30 14:08:56 -04:00
2021-02-23 04:10:45 -05:00
```javascript
const EditorPromise = import(
2021-06-18 17:10:06 -04:00
/* webpackChunkName: 'SourceEditor' */ '~/editor/source_editor'
2021-02-23 04:10:45 -05:00
);
2021-06-18 17:10:06 -04:00
const MarkdownExtensionPromise = import('~/editor/source_editor_markdown_ext');
2021-02-23 04:10:45 -05:00
Promise.all([EditorPromise, MarkdownExtensionPromise])
2021-06-18 17:10:06 -04:00
.then(([{ default: SourceEditor }, { default: MarkdownExtension }]) => {
const editor = new SourceEditor().createInstance({
2021-02-23 04:10:45 -05:00
...
});
editor.use(MarkdownExtension);
});
```
2020-10-30 14:08:56 -04:00
1. Using multiple extensions
2021-02-23 04:10:45 -05:00
Just pass the array of extensions to your `use` method:
2020-10-30 14:08:56 -04:00
2021-02-23 04:10:45 -05:00
```javascript
editor.use([FileTemplateExtension, MyFancyExtension]);
```