When open the file, how to make the code readonly in the code-editor

In theia, that i can open a file in code editor use

`import { open, OpenerService } from ‘@theia/core/lib/browser’

open(this.openservice, uri)`

Let the file be displayed in the code editor in the middle.

but now, I want to make the code only readable in the code editor. how can i do it

Hi @ayuantao,

looking quickly through the code, I was able to come up with this solution. I’m sure there are better ones, but they are probably a bit more involved:

const editorWidget = open(this.openService, uri);
if (editorWidget instanceof EditorWidget) { // You could also cast here, but using instanceof is safer
    const editor = editorWidget.editor;
    if (editor instanceof MonacoEditor) { // You could also cast here, but using instanceof is safer
         const standaloneMonacoEditor = (editor as any).editor // Gets the actual monaco editor. It is protected, so we have to cast to any beforehand
         standaloneMonacoEditor.updateOptions({ readOnly: true })
    }
}
1 Like

HI@ msujew, i have try it, but it seems no use for me.

@ayuantao, I made a slight mistake, open always returns a promise, so you have to put await in front of it:

const editorWidget = await open(this.openerService, uri);
1 Like

@msujew,I tried your method, it works, thank you very much for your help