GithubHelp home page GithubHelp logo

yusei-0 / ngx-mat-codemirror Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zeljkoantich/ngx-mat-codemirror

0.0 0.0 0.0 1.03 MB

Code editor input for Angular Material 2+ based on Codemirror

Home Page: https://smnbbrv.github.io/ngx-mat-codemirror/

License: MIT License

JavaScript 8.80% TypeScript 77.61% CSS 0.76% HTML 12.83%

ngx-mat-codemirror's Introduction

ngx-mat-codemirror

Build Status

A code editor input for Angular Material 2+ based on codemirror.

Demo here.

Installation

npm i -S ngx-mat-codemirror codemirror

and typings for codemirror:

npm i -D @types/codemirror

Configuring styles

First, import node_modules/codemirror/lib/codemirror.css into your project.

Then in order to use standard codemirror themes you need to include the corresponding CSS files. All themes are located at node_modules/codemirror/theme folder.

A preview for the themes could be found here.

For example, if you use angular-cli, add the to your

{
  ...
  "styles": [
    ...,
    "../node_modules/codemirror/lib/codemirror.css",
    "../node_modules/codemirror/theme/neat.css",
    ...,
  ],
  ...
}

This will import the neat codemirror theme and you can use it across the project.

Configuring modes (languages)

Import all the necessary codemirror modes (the languages that you are going to use) in the main.ts or some barrel file that is going to be imported into main.ts.

All the modes could be found at node_modules/codemirror/mode;

For example if we are going to use SASS and YAML code editors then we import the following:

import 'codemirror/mode/sass/sass';
import 'codemirror/mode/yaml/yaml';

Note: it is not enough to import the modes into scripts section of .angular-cli.json.

Usage

Import module whenever you need to use the code editor:

import { NgModule } from '@angular/core';
import { MatCodemirrorModule } from 'ngx-mat-codemirror';

@NgModule({
  // ...
  imports: [
    // ...
    MatCodemirrorModule,
    // ...
  ],
  // ...
})
export class MyModule { }

Then use it:

<mat-card-content>
  <mat-form-field>
    <mat-codemirror formControlName="yaml" required [options]="{ theme: 'neat', mode: 'yaml' }" placeholder="YAML"></mat-codemirror>
    <mat-error *ngIf="form.get('yaml').hasError('required')">required</mat-error>
    <mat-hint>YAML config sample</mat-hint>
  </mat-form-field>
</mat-card-content>

API

The mat-codemirror input implements both ControlValueAccessor and MatFormFieldControl. That means all the properties that are used by / available for

  • ngModel
  • formControl / formControlName
  • material inputs

are also supported here.

Additionally the following properties / events are available:

  • [options] - options passed to the CodeMirror instance. The lineNumbers option will be ignored by design.
  • [name] - name applied to the created textarea
  • [autoFocus] - setting applied to the created textarea
  • [preserveScrollPosition] - preserve previous scroll position after updating value
  • (focusChange) - called when the editor is focused or loses focus
  • (scroll) - called when the editor is scrolled
  • (cursorActivity) - called when the text cursor is moved

Note: the line numbers are automatically shown / hidden depending on the content of the input. If there is more than one line, they are shown, otherwise hidden.

Credits

Credits to @TypeCtrl for initial implementation of ControlValueAccessor

License

MIT

ngx-mat-codemirror's People

Contributors

zeljkoantich avatar smnbbrv avatar angular-cli avatar

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.