UNPKG

ng-zorro-antd

Version:

An enterprise-class UI components based on Ant Design and Angular

631 lines (623 loc) 18.4 kB
import { InjectionToken, Injectable, Inject, ɵɵdefineInjectable, ɵɵinject, EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, forwardRef, NgZone, ElementRef, Input, Output, NgModule } from '@angular/core'; import { __decorate, __metadata } from 'tslib'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { Subject, BehaviorSubject, of, combineLatest, fromEvent } from 'rxjs'; import { tap, map, takeUntil, debounceTime, filter, distinctUntilChanged } from 'rxjs/operators'; import { PREFIX, inNextTick, warn, InputBoolean } from 'ng-zorro-antd/core'; import { DOCUMENT, CommonModule } from '@angular/common'; import { NzIconModule } from 'ng-zorro-antd/icon'; import { NzSpinModule } from 'ng-zorro-antd/spin'; /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @enum {string} */ const NzCodeEditorLoadingStatus = { UNLOAD: 'unload', LOADING: 'loading', LOADED: 'LOADED', }; /** * @record */ function NzCodeEditorConfig() { } if (false) { /** @type {?|undefined} */ NzCodeEditorConfig.prototype.assetsRoot; /** @type {?|undefined} */ NzCodeEditorConfig.prototype.defaultEditorOption; /** * @return {?} */ NzCodeEditorConfig.prototype.onLoad = function () { }; /** * @return {?} */ NzCodeEditorConfig.prototype.onFirstEditorInit = function () { }; /** * @return {?} */ NzCodeEditorConfig.prototype.onInit = function () { }; } /** @type {?} */ const NZ_CODE_EDITOR_CONFIG = new InjectionToken('nz-code-editor-config', { providedIn: 'root', factory: NZ_CODE_EDITOR_CONFIG_FACTORY }); /** * @return {?} */ function NZ_CODE_EDITOR_CONFIG_FACTORY() { return {}; } /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ // tslint:disable no-any /** * @param {?=} fn * @return {?} */ function tryTriggerFunc(fn) { return (/** * @param {...?} args * @return {?} */ (...args) => { if (fn) { fn(...args); } }); } // tslint:enable no-any class NzCodeEditorService { /** * @param {?} config * @param {?} _document */ constructor(config, _document // tslint:disable-line no-any ) { this.config = config; this.firstEditorInitialized = false; this.loaded$ = new Subject(); this.loadingStatus = NzCodeEditorLoadingStatus.UNLOAD; this.option$ = new BehaviorSubject(this.option); this.document = _document; this.option = this.config.defaultEditorOption || {}; } // TODO: use config service later. /** * @param {?} option * @return {?} */ updateDefaultOption(option) { this.option = Object.assign({}, this.option, option); this.option$.next(this.option); if (option.theme) { monaco.editor.setTheme(option.theme); } } /** * @return {?} */ requestToInit() { if (this.loadingStatus === NzCodeEditorLoadingStatus.LOADED) { this.onInit(); return of(this.getLatestOption()); } if (this.loadingStatus === NzCodeEditorLoadingStatus.UNLOAD) { this.loadMonacoScript(); } return this.loaded$.asObservable().pipe(tap((/** * @return {?} */ () => this.onInit())), map((/** * @return {?} */ () => this.getLatestOption()))); } /** * @private * @return {?} */ loadMonacoScript() { if (this.loadingStatus === NzCodeEditorLoadingStatus.LOADING) { return; } this.loadingStatus = NzCodeEditorLoadingStatus.LOADING; /** @type {?} */ const assetsRoot = this.config.assetsRoot; /** @type {?} */ const vs = assetsRoot ? `${assetsRoot}/vs` : 'assets/vs'; /** @type {?} */ const windowAsAny = (/** @type {?} */ (window)); // tslint:disable-line no-any /** @type {?} */ const loadScript = this.document.createElement('script'); loadScript.type = 'text/javascript'; loadScript.src = `${vs}/loader.js`; loadScript.onload = (/** * @return {?} */ () => { windowAsAny.require.config({ paths: { vs } }); windowAsAny.require(['vs/editor/editor.main'], (/** * @return {?} */ () => { this.loadingStatus = NzCodeEditorLoadingStatus.LOADED; this.loaded$.next(true); this.loaded$.complete(); this.onLoad(); })); }); loadScript.onerror = (/** * @return {?} */ () => { throw new Error(`${PREFIX} cannot load assets of monaco editor from source "${vs}".`); }); this.document.documentElement.appendChild(loadScript); } /** * @private * @return {?} */ onInit() { if (!this.firstEditorInitialized) { this.firstEditorInitialized = true; tryTriggerFunc(this.config.onFirstEditorInit)(); } tryTriggerFunc(this.config.onInit)(); } /** * @private * @return {?} */ onLoad() { tryTriggerFunc(this.config.onLoad)(); } /** * @private * @return {?} */ getLatestOption() { return Object.assign({}, this.option); } } NzCodeEditorService.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ NzCodeEditorService.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [NZ_CODE_EDITOR_CONFIG,] }] }, { type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] }] } ]; /** @nocollapse */ NzCodeEditorService.ngInjectableDef = ɵɵdefineInjectable({ factory: function NzCodeEditorService_Factory() { return new NzCodeEditorService(ɵɵinject(NZ_CODE_EDITOR_CONFIG), ɵɵinject(DOCUMENT)); }, token: NzCodeEditorService, providedIn: "root" }); if (false) { /** * @type {?} * @private */ NzCodeEditorService.prototype.document; /** * @type {?} * @private */ NzCodeEditorService.prototype.firstEditorInitialized; /** * @type {?} * @private */ NzCodeEditorService.prototype.loaded$; /** * @type {?} * @private */ NzCodeEditorService.prototype.loadingStatus; /** * @type {?} * @private */ NzCodeEditorService.prototype.option; /** @type {?} */ NzCodeEditorService.prototype.option$; /** * @type {?} * @private */ NzCodeEditorService.prototype.config; } /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class NzCodeEditorComponent { /** * @param {?} nzCodeEditorService * @param {?} ngZone * @param {?} elementRef */ constructor(nzCodeEditorService, ngZone, elementRef) { this.nzCodeEditorService = nzCodeEditorService; this.ngZone = ngZone; this.nzEditorMode = 'normal'; this.nzOriginalText = ''; this.nzLoading = false; this.nzFullControl = false; this.nzEditorInitialized = new EventEmitter(); this.editorOptionCached = {}; this.destroy$ = new Subject(); this.resize$ = new Subject(); this.editorOption$ = new BehaviorSubject({}); this.value = ''; this.modelSet = false; this.el = elementRef.nativeElement; } /** * @param {?} value * @return {?} */ set nzEditorOption(value) { this.editorOption$.next(value); } /** * Initialize a monaco editor instance. * @return {?} */ ngAfterViewInit() { this.nzCodeEditorService.requestToInit().subscribe((/** * @param {?} option * @return {?} */ option => this.setup(option))); } /** * @return {?} */ ngOnDestroy() { if (this.editorInstance) { this.editorInstance.dispose(); } this.destroy$.next(); this.destroy$.complete(); } /** * @param {?} value * @return {?} */ writeValue(value) { this.value = value; this.setValue(); } // tslint:disable-next-line no-any /** * @param {?} fn * @return {?} */ registerOnChange(fn) { this.onChange = fn; } // tslint:disable-next-line no-any /** * @param {?} fn * @return {?} */ registerOnTouched(fn) { this.onTouch = fn; } /** * @param {?} _value * @return {?} */ onChange(_value) { } /** * @return {?} */ onTouch() { } /** * @return {?} */ layout() { this.resize$.next(); } /** * @private * @param {?} option * @return {?} */ setup(option) { inNextTick().subscribe((/** * @return {?} */ () => { this.editorOptionCached = option; this.registerOptionChanges(); this.initMonacoEditorInstance(); this.registerResizeChange(); this.setValue(); if (!this.nzFullControl) { this.setValueEmitter(); } this.nzEditorInitialized.emit(this.editorInstance); })); } /** * @private * @return {?} */ registerOptionChanges() { combineLatest([this.editorOption$, this.nzCodeEditorService.option$]) .pipe(takeUntil(this.destroy$)) .subscribe((/** * @param {?} __0 * @return {?} */ ([selfOpt, defaultOpt]) => { this.editorOptionCached = Object.assign({}, this.editorOptionCached, defaultOpt, selfOpt); this.updateOptionToMonaco(); })); } /** * @private * @return {?} */ initMonacoEditorInstance() { this.ngZone.runOutsideAngular((/** * @return {?} */ () => { this.editorInstance = this.nzEditorMode === 'normal' ? monaco.editor.create(this.el, Object.assign({}, this.editorOptionCached)) : monaco.editor.createDiffEditor(this.el, Object.assign({}, ((/** @type {?} */ (this.editorOptionCached))))); })); } /** * @private * @return {?} */ registerResizeChange() { this.ngZone.runOutsideAngular((/** * @return {?} */ () => { fromEvent(window, 'resize') .pipe(debounceTime(300), takeUntil(this.destroy$)) .subscribe((/** * @return {?} */ () => { this.layout(); })); this.resize$ .pipe(takeUntil(this.destroy$), filter((/** * @return {?} */ () => !!this.editorInstance)), map((/** * @return {?} */ () => ({ width: this.el.clientWidth, height: this.el.clientHeight }))), distinctUntilChanged((/** * @param {?} a * @param {?} b * @return {?} */ (a, b) => a.width === b.width && a.height === b.height)), debounceTime(50)) .subscribe((/** * @return {?} */ () => { this.editorInstance.layout(); })); })); } /** * @private * @return {?} */ setValue() { if (!this.editorInstance) { return; } if (this.nzFullControl && this.value) { warn(`should not set value when you are using full control mode! It would result in ambiguous data flow!`); return; } if (this.nzEditorMode === 'normal') { if (this.modelSet) { ((/** @type {?} */ (this.editorInstance.getModel()))).setValue(this.value); } else { ((/** @type {?} */ (this.editorInstance))).setModel(monaco.editor.createModel(this.value, ((/** @type {?} */ (this.editorOptionCached))).language)); this.modelSet = true; } } else { if (this.modelSet) { /** @type {?} */ const model = (/** @type {?} */ (((/** @type {?} */ (this.editorInstance))).getModel())); model.modified.setValue(this.value); model.original.setValue(this.nzOriginalText); } else { /** @type {?} */ const language = ((/** @type {?} */ (this.editorOptionCached))).language; ((/** @type {?} */ (this.editorInstance))).setModel({ original: monaco.editor.createModel(this.value, language), modified: monaco.editor.createModel(this.nzOriginalText, language) }); } } } /** * @private * @return {?} */ setValueEmitter() { /** @type {?} */ const model = (/** @type {?} */ ((this.nzEditorMode === 'normal' ? ((/** @type {?} */ (this.editorInstance))).getModel() : (/** @type {?} */ (((/** @type {?} */ (this.editorInstance))).getModel())).modified))); model.onDidChangeContent((/** * @return {?} */ () => { this.emitValue(model.getValue()); })); } /** * @private * @param {?} value * @return {?} */ emitValue(value) { this.value = value; this.onChange(value); } /** * @private * @return {?} */ updateOptionToMonaco() { if (this.editorInstance) { this.editorInstance.updateOptions(Object.assign({}, this.editorOptionCached)); } } } NzCodeEditorComponent.decorators = [ { type: Component, args: [{ changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, selector: 'nz-code-editor', exportAs: 'nzCodeEditor', template: "<div class=\"ant-code-editor-loading\" *ngIf=\"nzLoading\">\n <nz-spin></nz-spin>\n</div>\n\n<div class=\"ant-code-editor-toolkit\" *ngIf=\"nzToolkit\">\n <ng-template [ngTemplateOutlet]=\"nzToolkit\"></ng-template>\n</div>\n", host: { '[class.ant-code-editor]': 'true' }, providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef((/** * @return {?} */ () => NzCodeEditorComponent)), multi: true } ] }] } ]; /** @nocollapse */ NzCodeEditorComponent.ctorParameters = () => [ { type: NzCodeEditorService }, { type: NgZone }, { type: ElementRef } ]; NzCodeEditorComponent.propDecorators = { nzEditorMode: [{ type: Input }], nzOriginalText: [{ type: Input }], nzLoading: [{ type: Input }], nzFullControl: [{ type: Input }], nzToolkit: [{ type: Input }], nzEditorOption: [{ type: Input }], nzEditorInitialized: [{ type: Output }] }; __decorate([ InputBoolean(), __metadata("design:type", Object) ], NzCodeEditorComponent.prototype, "nzLoading", void 0); __decorate([ InputBoolean(), __metadata("design:type", Object) ], NzCodeEditorComponent.prototype, "nzFullControl", void 0); if (false) { /** @type {?} */ NzCodeEditorComponent.prototype.nzEditorMode; /** @type {?} */ NzCodeEditorComponent.prototype.nzOriginalText; /** @type {?} */ NzCodeEditorComponent.prototype.nzLoading; /** @type {?} */ NzCodeEditorComponent.prototype.nzFullControl; /** @type {?} */ NzCodeEditorComponent.prototype.nzToolkit; /** @type {?} */ NzCodeEditorComponent.prototype.nzEditorInitialized; /** @type {?} */ NzCodeEditorComponent.prototype.editorOptionCached; /** * @type {?} * @private */ NzCodeEditorComponent.prototype.el; /** * @type {?} * @private */ NzCodeEditorComponent.prototype.destroy$; /** * @type {?} * @private */ NzCodeEditorComponent.prototype.resize$; /** * @type {?} * @private */ NzCodeEditorComponent.prototype.editorOption$; /** * @type {?} * @private */ NzCodeEditorComponent.prototype.editorInstance; /** * @type {?} * @private */ NzCodeEditorComponent.prototype.value; /** * @type {?} * @private */ NzCodeEditorComponent.prototype.modelSet; /** * @type {?} * @private */ NzCodeEditorComponent.prototype.nzCodeEditorService; /** * @type {?} * @private */ NzCodeEditorComponent.prototype.ngZone; } /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class NzCodeEditorModule { } NzCodeEditorModule.decorators = [ { type: NgModule, args: [{ declarations: [NzCodeEditorComponent], imports: [CommonModule, NzIconModule, NzSpinModule], exports: [NzCodeEditorComponent] },] } ]; /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { NZ_CODE_EDITOR_CONFIG, NZ_CODE_EDITOR_CONFIG_FACTORY, NzCodeEditorComponent, NzCodeEditorLoadingStatus, NzCodeEditorModule, NzCodeEditorService }; //# sourceMappingURL=ng-zorro-antd-code-editor.js.map