Comments (2)
Hi @sanjay51,
I looked a bit on possible solution for you. For your use case, I recommand you to retrieve the editor instance (https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.istandalonecodeeditor.html) as shown in the readme:
// Template
<ngx-monaco-editor [options]="editorOptions" [(ngModel)]="code" (init)="editorInit($event)"></ngx-monaco-editor>
// TS
import { Component } from '@angular/core';
import { MonacoStandaloneCodeEditor } from '@materia-ui/ngx-monaco-editor';
...
export class AppComponent {
editorOptions = {theme: 'vs-dark', language: 'javascript'};
code: string= 'function x() {\nconsole.log("Hello world!");\n}';
editorInit(editor: MonacoStandaloneCodeEditor) {
// Here you can access editor instance
this.editor = editor;
}
}
You will be able with the editor instance to set your cursor at a specific position, or even better save the view state of the editor and reset it after external update (this allow to conserve scroll position as well) :
// saving view state
const state = this.editor.saveViewState();
// restoring view state
this.editor.restoreViewState(state);
Here are some usefull issues I looked at: microsoft/monaco-editor#1397, microsoft/monaco-editor#194.
I think the best way to achieve your goal is to use rxjs streams to restore the state view when it is needed, something like that:
externalUpdate$ = new BehaviorSubject();
editorModelContentChange$ = new BehaviorSubject();
// this.editor retrieved on init
this.editor.onDidChangeModelContent((event) => this.editorModelContentChange.next(event));
this.externalUpdate$.pipe(
skip(1),
map((code) => ({ code, viewState: this.editor.saveViewState() })),
tap(({ code }) => {
this.code = code
}),
switchMap(({ viewState }) =>
this.editorModelContentChange$.asObservable()
.pipe(
delay(1),
tap(() => this.editor.restoreViewState(viewState)),
take(1)
)
)
).subscribe();
Of course, it is just a short example, and you will have to manage the observables unsubscriptions and the code merging logic, when an external update occured (to not lost the current change in the editor).
Here is a brief stackblitz example of this solution: https://stackblitz.com/edit/materia-ngx-monaco-editor-example-1bgg2k.
Let us know if you find out a better approach. You seems to have a very interesting project!
Have a nice day!
from ngx-monaco-editor.
Hi @GeoAstronaute ,
Thank you so much for looking into it. I couldn't get it running though especially with the remote synchronization that I'm doing simultaneously, let me try again this weekend. Thanks a lot! Feel free to close this though, I think either way it's gonna be tricky.
from ngx-monaco-editor.
Related Issues (20)
- Changes to `original` property not detected properly HOT 1
- Angular 14 support? HOT 3
- Provide APIs for testing
- Followed the steps provided but the monaco editor is not showing HOT 3
- Angular 15 support?
- how to change language mode to sql ?
- How to add new language support HOT 1
- Change monaco editor context menu labels
- Angular 13 support HOT 5
- Create more than one editor on the same page. Is it possible in this library ? HOT 2
- Syntax highlightning not working for diff editor HOT 1
- Monaco loader fails when Electron Node integration is off HOT 1
- both ngx-monaco-editor and monaco-editor define MonacoEnvironment HOT 6
- Integration with monaco-yaml doesn't work
- Demo project doesn't work HOT 1
- Interface error after installation
- this.onTouched is not a function
- [DOC] EditorOptions link from README is broken
- overflow causes markers to not work well
- use in electron, Error: Invalid package HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngx-monaco-editor.