-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Provide ability to block editing specific lines #953
Comments
Thank you for the reply. I have seen the previous issue, and this request is essentially another plea to reconsider adding back the feature (or providing some sort of native alternative). Providing editable/read-only ranges is really useful, and the hack suggested is rather dirty/doesn't make for a good experience. For example, consider an educational software where the following template is created, and the user/student has to enter their code in between: function foo(arg1, arg2) {
// your code goes here
} |
This would be very useful for me as well. |
Yes, this would be amazingly useful for me as well, I am even considering to switch from Ace editor, if this feature can be implemented in Monaco. |
Agree this would be indeed very helpful for a scenario I'm having with a current app. Based on the comment in the first line (which contains meta) additional rules are applied. So having the first line locked down in order to prevent the user from unintentionally deleting it (CTRL+A DEL) would be awesome. |
yes please lets make this feature happen! |
SetEditableRange Function is really useful i think.... |
Is this possible? |
@virus2016 nope... but if you use monaco editor version 0.4.X ... you can do that... |
Yeah, that would be really useful for me as well, I am thinking to use monaco editor for our production |
Is there any plan to add this feature in future releases? |
@alexandrudima is it something that can be implemented elegantly with the editor's current design? will you accept a PR adding back this functionality, assuming you answered yes on the former question? I'm not sure from reading these discussions if this is a hopeless cause or simply lacking interest and manpower. Perhaps this can be solved by a different kind of solution, like adding a before-change event which offers a canceling mechanism for changes... Any alternative which isn't a hack would be a blessing.. |
Another use case is wrapping code in a function when the context is different from function onCreate(this: Copy) {
// User-written code
} |
I would personally appreciate a little more granularity than just 'lines', possibly a character range, with helper methods for doing lines? |
Is there any update on this issue? Any possible workarounds for non-js files meanwhile? |
@mehulmpt probably... there was no update for this issues for about 2 year... maybe? m.m |
Is there any update on this issue? |
@alexdima Any updates on this? Or any alternative ? |
Still no updates |
@rcjsuen How to achive this using existing monaco api? |
@GNSubrahmanyam Sorry, I have no idea. This is not something I have ever tried to investigate as I do not have this use case in my own use of the Monaco Editor. |
Indeed my comment is a little naive. There are many special cases to deal with. I have not tested it in real cases but you can try it in the playground: |
I developed an application based on Monaco in which the user can only edit the last line (like a terminal). There are a lot of cases to test out like pasting in content with newlines, pressing backspace on boundaries, and so on. I found it very reliable to simply call I also added an editor.onDidChangeModelContent((e) => {
if (e.isUndoing || editor.ignoreChange) { return; }
for (const ch of e.changes) {
if (<some test of ch.range to check if it is invalid>) {
editor.getModel().undo();
return;
}
}
} |
Oh I'm crushed this isn't a feature. |
Wow a lot need this feature. I also need it for my final year project, I guess I'd have to fork and try to hack around until it works. |
It appears Microsoft won't implement anything that isn't directly useful to VS Code |
I tried this way,but need to pay special attention to that when formatting the code, it may be misjudged instance.onDidChangeModelContent((e) => {
const model = instance.getModel();
let validatePass = false;
if (frozenHeaderLine && model) {
const headerContent = model.getLineContent(1);
if (frozenHeaderLine === headerContent) { // here use ===
validatePass = true;
} else {
instance.trigger('undo', 'undo', '');
onWarning?.('can't edit headerline! ');
return;
}
}
} |
I have developed a plugin named DemoScreen.Recording.2021-11-03.at.1.49.33.PM.movLinks for Playground and Sample code is available in API Documentation |
This is the only thing stopping me to move from Ace Editor to Monaco Editor. |
@Progyan1997 Hi were you able to find any solution for this -- #874 (comment) |
@pawansingh00 a person right above you mentions releasing an NPM package to do it... |
@dustinlacewell Saw it, and other solutions mentioned too (Yours one too -- #953 (comment) ) and already trying it out too. So, was looking for any other alternative solution. |
@pawansingh00 The performance problem which I told was about the older version of that restricted editor, in which I used regex recursively to validate the changes in the content. The npm package works in a different way. The latest one uses range values and compares the position of the entered content with the set of allowable ranges. If the current position of the content is not in the allowed ranges it will undo the changes. I am using this package for quite a while now and I haven't felt any performance issues till now. |
@Pranomvignesh can you please help me figure out a way to integrate this with https://www.npmjs.com/package/@monaco-editor/react ? |
The hack that was proposed earlier with Undo does not work if user does Redo operation. I solved it with another hack, which removes future stack from undoRedoService in monaco-editor@0.34.0 // usually here goes editor.onDidChangeModelContent callback with content validation....
// ....
editor.trigger("undo", "undo", null);
(
model as unknown as {
_undoRedoService: {
_editStacks: Map<string, { _future: [] }>;
};
}
)._undoRedoService._editStacks
.get(model.uri.toString())
?._future.pop() This code is not recommended to run in next versions of Monaco, as it is not guaranteed to have same properties |
Any progress on this feature? For me, I need the ability to make a property readonly in the monaco editor JSON language. See https://stackoverflow.com/questions/75033605/how-do-i-make-a-property-readonly-in-monaco-editor-json-language |
👋 I'm using a mixture of JS + CSS to disable editing a specific set of lines. Sharing in case anyone is interested. // Disable editing specification_id, version, and app values
editor.createDecorationsCollection([
{
range: new monaco.Range(1, 0, 3, 0), // Block lines 1 to 3
options: {
isWholeLine: true,
className: 'editor-line-readonly',
},
},
]);
// Disable line editing (handled from the CSS side but here as well to be safe)
editor.onKeyDown(e => {
const isInBlockedRange = editor
.getSelections()
?.findIndex(range => new monaco.Range(1, 0, 4, 0).intersectRanges(range)) !== -1; // Block lines 1 to 3
if (isInBlockedRange) {
e.stopPropagation();
e.preventDefault();
}
}); /* ⚠️ HACK
* The Monaco editor doesn't provide a way to disable editing specific lines.
* As a workaround, from the JS side we apply a `.editor-line-readonly` classname decoration to the lines we want
* to disable and make them non-editable from the CSS side.
* The decoration elements are layered underneath the code lines and are absolutely placed within a parent `div`, so
* to target such `div` we use the `:has` pseudoclass (notice it's not available in Firefox yet).
* DOM example for reference:
* - div class="view-overlays"
* - div (one for each code line)
* - div class="editor-line-readonly" (positioned absolutely)
* - div class="view-lines"
* - div (one for each code line)
* - span (the code line text)
*/
div:has(>.editor-line-readonly) {
background-color: #00000005;
cursor: not-allowed;
z-index: 999; /* Moves this div above the code line making it not selectable */
} |
@Pranomvignesh thank you for creating this plugin https://github.com/Pranomvignesh/constrained-editor-plugin |
Inspired by @mmazzarolo's example above I tried setting the entire editor to readonly when a selection intersects a locked line. private lockLinesViaReadOnlyEditor(editor: MonacoEditor.IStandaloneCodeEditor, lockedLineNumbers: number[]) {
editor.onDidChangeCursorSelection(_ => {
const selectionInLockedRange = editor.getSelections()?.some(selection => {
return lockedLineNumbers.some(lockedLineNumber => {
return selection.intersectRanges(new Range(lockedLineNumber, 0, lockedLineNumber + 1, 0));
});
});
editor.updateOptions({readOnly: selectionInLockedRange, readOnlyMessage: {value: 'Cannot edit locked lines.'}});
});
} I'm using individual line numbers in this example, but you can change it to use ranges if you like. |
very nice, this plugin just cover my needs. thx |
This Can be possible as Monaco is not Supporting ReadOnly Ranges but it is still supporting ReadOnly so we can get the cursor position and change the editor options accordingly |
Related on Stack Overflow: How to set read only for certain lines in VS Code? |
can i use in 0.34.1 ? |
Does ace editor provide this feature? |
Monaco is a great editor for providing programmatic interfaces for users to an application.
One feature that'd significantly help with the user experience is to make specific lines read-only, such that user can enter their code within a specific block (e.g., between a function block).
Would it be possible to include this as a core features/API of the editor?
The text was updated successfully, but these errors were encountered: