Git Product home page Git Product logo

Comments (2)

GeoAstronaute avatar GeoAstronaute commented on August 25, 2024 3

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.

sanjay51 avatar sanjay51 commented on August 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.