npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@1xtend/ng-monaco-editor

v1.0.16

Published

Monaco Editor for Angular 17+

Downloads

34

Readme

📚 Monaco Editor for Angular 17+

This library wraps the well-known Monaco Editor for easy use in Angular 17+. Feel free to contribute. Let's make it better!

🎯 Versions

It only supports Angular 17 and above.

⚡ Setup

📦 Install Required Dependencies

First, install the necessary package:

npm i @1xtend/ng-monaco-editor

monaco-editor package will be installed automatically, because it is specified in "dependencies".

📁 Import Monaco Editor Assets

Your application must have access to Monaco Editor assets to work properly. To provide this access, you need to make the following changes to the angular.json file:

{
  "app-name": {
    ...
    "architect": {
      ...
      "build": {
        ...
        "options": {
          ...
          "assets": [
            "src/favicon.ico",
            "src/assets",
            {
              "glob": "**/*",
              "input": "node_modules/monaco-editor/min",
              "output": "assets/monaco-editor/min"
            },
            {
              "glob": "**/*",
              "input": "node_modules/monaco-editor/min-maps",
              "output": "assets/monaco-editor/min-maps"
            }
          ]
        }
      }
    }
  }
}

⚙️ Provide config

You need to define and provide an NG_MONACO_EDITOR_CONFIG in your app.config.ts.

const config: NgMonacoEditorConfig = {
  /**
   * Optional.
   *
   * Base URL to Monaco Editor assets via AMD. By default, it's "/assets", but you can change the path to assets in the previous step.
   */
  baseUrl: "/lib/assets",

  /**
   * Optional.
   *
   * Provide editor default options.
   */
  defaultOptions: {
    theme: "vs-dark",
    fontSize: 20,
  },

  /**
   * Optional.
   *
   * This function is called when Monaco is loaded.
   */
  onMonacoLoad: () => {
    /**
     * You can use it to set various settings.
     */
    monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
      noSyntaxValidation: true,
      noSemanticValidation: true,
    });

    console.log("Monaco has been loaded successfully!");
  },

  /**
   * Optional.
   *
   * The interval for auto re-layout. Defaul value is 100ms.
   */
  autoLayoutInterval: 500,
};

export const appConfig: ApplicationConfig = {
  providers: [
    {
      provide: NG_MONACO_EDITOR_CONFIG,
      useValue: config,
    },
  ],
};

✨ Usage

For both ng-monaco-editor and ng-monaco-diff-editor, you need to create options. You can use the NgEditorOptions interface.

Basic Example:

@Component({
  selector: "my-editors",
  standalone: true,
  imports: [MonacoEditorComponent, MonacoDiffEditorComponent],
})
export class MyEditor {
  options: NgEditorOptions = {
    language: "typescript",
    theme: "vs-dark",
  };

  diffOptions: NgEditorOptions = {
    language: "html",
  };
}
<ng-monaco-editor [options]="options"></ng-monaco-editor>
<ng-monaco-diff-editor [options]="diffOptions"></ng-monaco-diff-editor>

You can use both Reactive and Template Driven forms to control the editor's value.

Reactive forms example:

@Component({
  selector: "my-editor",
  standalone: true,
  imports: [MonacoEditorComponent, ReactiveFormsModule],
})
export class MyEditor {
  options: NgEditorOptions = {
    language: "typescript",
    theme: "vs-dark",
  };
  valueControl = new FormControl("const age: number = 100");
}
<ng-monaco-editor [options]="options" [formControl]="valueControl"></ng-monaco-editor>

Template Driven forms example:

@Component({
  selector: "my-editor",
  standalone: true,
  imports: [MonacoEditorComponent, FormsModule],
})
export class MyEditor {
  options: NgEditorOptions = {
    language: "typescript",
    theme: "vs-dark",
  };
  value: string = "const age: number = 100";
}
<ng-monaco-editor [options]="options" [(ngModel)]="value"></ng-monaco-editor>

Additionally, ng-monaco-diff-editor has an originalValue input to display its original value.

@Component({
  selector: "my-editor",
  standalone: true,
  imports: [MonacoDiffEditorComponent, FormsModule],
})
export class MyEditor {
  options: NgEditorOptions = {
    language: "typescript",
    theme: "vs-dark",
  };
  value: string = "const age: number = 100";
  originalValue: string = "const age: number = 99";
}
<ng-monaco-diff-editor [options]="options" [(ngModel)]="value" [originalValue]="originalValue"></ng-monaco-diff-editor>

🎨 Styling

Both ng-monaco-editor and ng-monaco-diff-editor have the same ng-monaco-editor-wrapper class. You can style the component by using ::ng-deep.

<ng-monaco-editor [options]="options" class="my-editor"></ng-monaco-editor>
.my-editor ::ng-deep .ng-monaco-editor-wrapper {
  display: block;
  width: 50%;
  border: 1px solid red;
}

Also, the editors have their own classes like ng-monaco-editor and ng-monaco-diff-editor.

🔗 Useful links

📃 License

MIT @1xtend