@ks89/ngx-codemirror6
Version:
Codemirror 6 library for Angular
148 lines (142 loc) • 6.15 kB
JavaScript
import * as i0 from '@angular/core';
import { forwardRef, Component, Input, ViewChild, NgModule } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { lineNumbers, EditorView } from '@codemirror/view';
import { EditorState } from '@codemirror/state';
import { html } from '@codemirror/lang-html';
import { css } from '@codemirror/lang-css';
import { javascript } from '@codemirror/lang-javascript';
import { sass } from '@codemirror/lang-sass';
import { basicSetup } from 'codemirror';
/**
* The MIT License (MIT)
*
* Copyright (c) 2023 Stefano Cappa (Ks89)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* CodeMirror component
*/
class CodemirrorComponent {
constructor() {
this.content = '';
this.appendExtensions = [];
this.language = '';
}
ngAfterViewInit() {
let lang;
switch (this.language) {
case 'html':
lang = html();
break;
case 'javascript':
lang = javascript({ typescript: false, jsx: false });
break;
case 'typescript':
lang = javascript({ typescript: true, jsx: false });
break;
case 'jsx':
lang = javascript({ typescript: false, jsx: true });
break;
case 'css':
lang = css();
break;
case 'scss':
lang = sass();
break;
case 'sass':
lang = sass({ indented: true });
break;
default:
throw new Error('Internal ngx-codemirror6 error - unrecognized language');
}
const extensions = [lineNumbers(), lang, basicSetup, EditorState.readOnly.of(true), ...this.appendExtensions];
const config = {
doc: this.content,
extensions: extensions
};
this.codemirrorInit(config);
}
codemirrorInit(config) {
if (!this.host) {
throw new Error('Internal ngx-codemirror6 error - host must be defined');
}
new EditorView({
parent: this.host.nativeElement,
state: EditorState.create(config)
});
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.1", ngImport: i0, type: CodemirrorComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.0.1", type: CodemirrorComponent, selector: "ks-codemirror", inputs: { content: "content", appendExtensions: "appendExtensions", language: "language" }, providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CodemirrorComponent),
multi: true
}
], viewQueries: [{ propertyName: "host", first: true, predicate: ["host"], descendants: true }], ngImport: i0, template: `<div #host></div>`, isInline: true }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.1", ngImport: i0, type: CodemirrorComponent, decorators: [{
type: Component,
args: [{
selector: 'ks-codemirror',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CodemirrorComponent),
multi: true
}
],
template: `<div #host></div>`
}]
}], propDecorators: { content: [{
type: Input
}], appendExtensions: [{
type: Input
}], language: [{
type: Input
}], host: [{
type: ViewChild,
args: ['host']
}] } });
class CodemirrorModule {
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.1", ngImport: i0, type: CodemirrorModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.0.1", ngImport: i0, type: CodemirrorModule, declarations: [CodemirrorComponent], exports: [CodemirrorComponent] }); }
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.0.1", ngImport: i0, type: CodemirrorModule }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.1", ngImport: i0, type: CodemirrorModule, decorators: [{
type: NgModule,
args: [{
declarations: [
CodemirrorComponent
],
imports: [],
exports: [
CodemirrorComponent
]
}]
}] });
/*
* Public API Surface of ngx-codemirror6
*/
/**
* Generated bundle index. Do not edit.
*/
export { CodemirrorComponent, CodemirrorModule };
//# sourceMappingURL=ks89-ngx-codemirror6.mjs.map