Skip to content

Commit

Permalink
Allow editor to start from a certain line
Browse files Browse the repository at this point in the history
This allows prepending of content for the code editor that cannot be edited by the end-user.
  • Loading branch information
bennothommo committed Jan 3, 2024
1 parent fc6c6cd commit 3ba08c2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.

Large diffs are not rendered by default.

50 changes: 43 additions & 7 deletions modules/backend/formwidgets/codeeditor/assets/js/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import { parse as parseXml } from 'fast-plist';
import constrainedEditor from 'constrained-editor-plugin';

((Snowboard) => {
/**
Expand Down Expand Up @@ -49,6 +50,7 @@ import { parse as parseXml } from 'fast-plist';
visibilityChange: () => this.onVisibilityChange(),
};
this.keybindings = [];
this.customLineNumbering = null;

this.observeElement();
}
Expand Down Expand Up @@ -244,7 +246,9 @@ import { parse as parseXml } from 'fast-plist';
},
insertSpaces: this.config.get('useSoftTabs'),
language: this.config.get('language'),
lineNumbers: this.config.get('showGutter') ? 'on' : 'off',
lineNumbers: (this.customLineNumbering)
? this.customLineNumbering
: this.config.get('showGutter') ? 'on' : 'off',
minimap: {
enabled: this.config.get('showMinimap'),
},
Expand Down Expand Up @@ -1049,14 +1053,46 @@ import { parse as parseXml } from 'fast-plist';
}

/**
* Allows parts of the code to be hidden from editing.
* Allows the editor to start from a specific line.
*
* @param {Array|String} range
* This acts as a way to define prepended content for the editor that cannot be seen or
* edited. For example, the "Code" view in the CMS editor has a line to define the opening
* "<?php" tag, but this is hidden from the user.
*
* @param {Number} line
*/
setHiddenRange(range) {
const ranges = (!Array.isArray(range)) ? [range] : range;
const processed = ranges.map((item) => new monaco.Range(item.startLineNumber, item.startColumn, item.endLineNumber, item.endColumn));
this.editor.setHiddenAreas(processed);
fromLine(line) {
if (line <= 1) {
this.customLineNumbering = null;
return;
}

const modelLines = this.getModel().getFullModelRange().endLineNumber;
const modelEndColumn = this.getModel().getFullModelRange().endColumn;

if (line > modelLines) {
this.customLineNumbering = null;
throw new Error('The line number is greater than the number of lines in the editor.');
}

const hiddenRange = new monaco.Range(1, 1, line - 1, modelEndColumn);
const editableRange = new monaco.Range(line, 1, modelLines, modelEndColumn);

const constrainedInstance = constrainedEditor(monaco);
constrainedInstance.initializeIn(this.editor);
constrainedInstance.addRestrictionsTo(this.getModel(), [{
range: [editableRange.startLineNumber, editableRange.startColumn, editableRange.endLineNumber, editableRange.endColumn],
allowMultiline: true,
}]);

this.editor.setHiddenAreas([hiddenRange]);

if (this.config.get('showGutter')) {
this.customLineNumbering = (number) => {
return (number >= line) ? String((number - line) + 1) : '';
};
this.editor.updateOptions(this.getConfigOptions());
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions modules/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"fast-plist": "^0.1.3",
"jquery-events-to-dom-events": "^1.1.0",
"monaco-editor": "^0.34.1",
"constrained-editor-plugin": "^1.3.0",
"vue": "^3.2.45",
"@simonwep/pickr": "^1.8.2"
},
Expand Down
2 changes: 1 addition & 1 deletion modules/cms/assets/js/winter.cmspage.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
}
}

widget.setHiddenRange(match.range);
widget.fromLine(2);
}

CmsPage.prototype.onAfterAllTabsClosed = function(ev) {
Expand Down

0 comments on commit 3ba08c2

Please sign in to comment.