ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
2 lines • 10.6 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/cdk/platform"),require("@angular/core"),require("@angular/forms"),require("ng-zorro-antd/core/logger"),require("ng-zorro-antd/core/util"),require("rxjs"),require("rxjs/operators"),require("@angular/common"),require("ng-zorro-antd/core/config"),require("ng-zorro-antd/icon"),require("ng-zorro-antd/spin")):"function"==typeof define&&define.amd?define("ng-zorro-antd/code-editor",["exports","@angular/cdk/platform","@angular/core","@angular/forms","ng-zorro-antd/core/logger","ng-zorro-antd/core/util","rxjs","rxjs/operators","@angular/common","ng-zorro-antd/core/config","ng-zorro-antd/icon","ng-zorro-antd/spin"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self)["ng-zorro-antd"]=t["ng-zorro-antd"]||{},t["ng-zorro-antd"]["code-editor"]={}),t.ng.cdk.platform,t.ng.core,t.ng.forms,t["ng-zorro-antd"].core.logger,t["ng-zorro-antd"].core.util,t.rxjs,t.rxjs.operators,t.ng.common,t["ng-zorro-antd"].core.config,t["ng-zorro-antd"].icon,t["ng-zorro-antd"].spin)}(this,(function(t,e,o,n,i,r,s,a,d,c,u,l){"use strict";var p;t.NzCodeEditorLoadingStatus=void 0,(p=t.NzCodeEditorLoadingStatus||(t.NzCodeEditorLoadingStatus={})).UNLOAD="unload",p.LOADING="loading",p.LOADED="LOADED";function h(t,e,o,n){var i,r=arguments.length,s=r<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,o):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,o,n);else for(var a=t.length-1;a>=0;a--)(i=t[a])&&(s=(r<3?i(s):r>3?i(e,o,s):i(e,o))||s);return r>3&&s&&Object.defineProperty(e,o,s),s}function g(t,e){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(t,e)}Object.create;function f(t,e){var o="function"==typeof Symbol&&t[Symbol.iterator];if(!o)return t;var n,i,r=o.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(n=r.next()).done;)s.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(o=r.return)&&o.call(r)}finally{if(i)throw i.error}}return s}function m(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(f(arguments[e]));return t}Object.create;var z="codeEditor";function v(t){return function(){for(var e=[],o=0;o<arguments.length;o++)e[o]=arguments[o];t&&t.apply(void 0,m(e))}}var y=function(){function e(e,o){var n=this;this.nzConfigService=e,this.firstEditorInitialized=!1,this.loaded$=new s.Subject,this.loadingStatus=t.NzCodeEditorLoadingStatus.UNLOAD,this.option={},this.option$=new s.BehaviorSubject(this.option);var i=this.nzConfigService.getConfigForComponent(z);this.document=o,this.config=Object.assign({},i),this.option=this.config.defaultEditorOption||{},this.nzConfigService.getConfigChangeEventForComponent(z).subscribe((function(){var t=n.nzConfigService.getConfigForComponent(z);t&&n._updateDefaultOption(t.defaultEditorOption)}))}return e.prototype._updateDefaultOption=function(t){this.option=Object.assign(Object.assign({},this.option),t),this.option$.next(this.option),t.theme&&monaco.editor.setTheme(t.theme)},e.prototype.requestToInit=function(){var e=this;return this.loadingStatus===t.NzCodeEditorLoadingStatus.LOADED?(this.onInit(),s.of(this.getLatestOption())):(this.loadingStatus===t.NzCodeEditorLoadingStatus.UNLOAD&&(this.config.useStaticLoading&&"undefined"==typeof monaco?i.warn("You choose to use static loading but it seems that you forget to config webpack plugin correctly. Please refer to our official websitefor more details about static loading."):this.loadMonacoScript()),this.loaded$.asObservable().pipe(a.tap((function(){return e.onInit()})),a.map((function(){return e.getLatestOption()}))))},e.prototype.loadMonacoScript=function(){var e=this;if(this.config.useStaticLoading)Promise.resolve().then((function(){return e.onLoad()}));else if(this.loadingStatus!==t.NzCodeEditorLoadingStatus.LOADING){this.loadingStatus=t.NzCodeEditorLoadingStatus.LOADING;var o=this.config.assetsRoot,n=o?o+"/vs":"assets/vs",r=window,s=this.document.createElement("script");s.type="text/javascript",s.src=n+"/loader.js",s.onload=function(){r.require.config({paths:{vs:n}}),r.require(["vs/editor/editor.main"],(function(){e.onLoad()}))},s.onerror=function(){throw new Error(i.PREFIX+' cannot load assets of monaco editor from source "'+n+'".')},this.document.documentElement.appendChild(s)}},e.prototype.onLoad=function(){this.loadingStatus=t.NzCodeEditorLoadingStatus.LOADED,this.loaded$.next(!0),this.loaded$.complete(),v(this.config.onLoad)()},e.prototype.onInit=function(){this.firstEditorInitialized||(this.firstEditorInitialized=!0,v(this.config.onFirstEditorInit)()),v(this.config.onInit)()},e.prototype.getLatestOption=function(){return Object.assign({},this.option)},e}();y.ɵprov=o.ɵɵdefineInjectable({factory:function(){return new y(o.ɵɵinject(c.NzConfigService),o.ɵɵinject(d.DOCUMENT))},token:y,providedIn:"root"}),y.decorators=[{type:o.Injectable,args:[{providedIn:"root"}]}],y.ctorParameters=function(){return[{type:c.NzConfigService},{type:void 0,decorators:[{type:o.Inject,args:[d.DOCUMENT]}]}]};var O=function(){function t(t,e,n,i){this.nzCodeEditorService=t,this.ngZone=e,this.platform=i,this.nzEditorMode="normal",this.nzOriginalText="",this.nzLoading=!1,this.nzFullControl=!1,this.nzEditorInitialized=new o.EventEmitter,this.editorOptionCached={},this.destroy$=new s.Subject,this.resize$=new s.Subject,this.editorOption$=new s.BehaviorSubject({}),this.value="",this.modelSet=!1,this.onChange=function(t){},this.onTouch=function(){},this.el=n.nativeElement,this.el.classList.add("ant-code-editor")}return Object.defineProperty(t.prototype,"nzEditorOption",{set:function(t){this.editorOption$.next(t)},enumerable:!1,configurable:!0}),t.prototype.ngAfterViewInit=function(){var t=this;this.platform.isBrowser&&this.nzCodeEditorService.requestToInit().subscribe((function(e){return t.setup(e)}))},t.prototype.ngOnDestroy=function(){this.editorInstance&&this.editorInstance.dispose(),this.destroy$.next(),this.destroy$.complete()},t.prototype.writeValue=function(t){this.value=t,this.setValue()},t.prototype.registerOnChange=function(t){this.onChange=t},t.prototype.registerOnTouched=function(t){this.onTouch=t},t.prototype.layout=function(){this.resize$.next()},t.prototype.setup=function(t){var e=this;r.inNextTick().subscribe((function(){e.editorOptionCached=t,e.registerOptionChanges(),e.initMonacoEditorInstance(),e.registerResizeChange(),e.setValue(),e.nzFullControl||e.setValueEmitter(),e.nzEditorInitialized.emit(e.editorInstance)}))},t.prototype.registerOptionChanges=function(){var t=this;s.combineLatest([this.editorOption$,this.nzCodeEditorService.option$]).pipe(a.takeUntil(this.destroy$)).subscribe((function(e){var o=f(e,2),n=o[0],i=o[1];t.editorOptionCached=Object.assign(Object.assign(Object.assign({},t.editorOptionCached),i),n),t.updateOptionToMonaco()}))},t.prototype.initMonacoEditorInstance=function(){var t=this;this.ngZone.runOutsideAngular((function(){t.editorInstance="normal"===t.nzEditorMode?monaco.editor.create(t.el,Object.assign({},t.editorOptionCached)):monaco.editor.createDiffEditor(t.el,Object.assign({},t.editorOptionCached))}))},t.prototype.registerResizeChange=function(){var t=this;this.ngZone.runOutsideAngular((function(){s.fromEvent(window,"resize").pipe(a.debounceTime(300),a.takeUntil(t.destroy$)).subscribe((function(){t.layout()})),t.resize$.pipe(a.takeUntil(t.destroy$),a.filter((function(){return!!t.editorInstance})),a.map((function(){return{width:t.el.clientWidth,height:t.el.clientHeight}})),a.distinctUntilChanged((function(t,e){return t.width===e.width&&t.height===e.height})),a.debounceTime(50)).subscribe((function(){t.editorInstance.layout()}))}))},t.prototype.setValue=function(){var t=this;if(this.editorInstance)if(this.nzFullControl&&this.value)i.warn("should not set value when you are using full control mode! It would result in ambiguous data flow!");else if("normal"===this.nzEditorMode)if(this.modelSet){var e=this.editorInstance.getModel();this.preservePositionAndSelections((function(){return e.setValue(t.value)}))}else this.editorInstance.setModel(monaco.editor.createModel(this.value,this.editorOptionCached.language)),this.modelSet=!0;else if(this.modelSet){var o=this.editorInstance.getModel();this.preservePositionAndSelections((function(){o.modified.setValue(t.value),o.original.setValue(t.nzOriginalText)}))}else{var n=this.editorOptionCached.language;this.editorInstance.setModel({original:monaco.editor.createModel(this.nzOriginalText,n),modified:monaco.editor.createModel(this.value,n)}),this.modelSet=!0}},t.prototype.preservePositionAndSelections=function(t){if(this.editorInstance){var e=this.editorInstance.getPosition(),o=this.editorInstance.getSelections();t(),e&&this.editorInstance.setPosition(e),o&&this.editorInstance.setSelections(o)}else t()},t.prototype.setValueEmitter=function(){var t=this,e="normal"===this.nzEditorMode?this.editorInstance.getModel():this.editorInstance.getModel().modified;e.onDidChangeContent((function(){t.ngZone.run((function(){t.emitValue(e.getValue())}))}))},t.prototype.emitValue=function(t){this.value!==t&&(this.value=t,this.onChange(t))},t.prototype.updateOptionToMonaco=function(){this.editorInstance&&this.editorInstance.updateOptions(Object.assign({},this.editorOptionCached))},t}();O.decorators=[{type:o.Component,args:[{changeDetection:o.ChangeDetectionStrategy.OnPush,encapsulation:o.ViewEncapsulation.None,selector:"nz-code-editor",exportAs:"nzCodeEditor",template:'\n <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 ',providers:[{provide:n.NG_VALUE_ACCESSOR,useExisting:o.forwardRef((function(){return O})),multi:!0}]}]}],O.ctorParameters=function(){return[{type:y},{type:o.NgZone},{type:o.ElementRef},{type:e.Platform}]},O.propDecorators={nzEditorMode:[{type:o.Input}],nzOriginalText:[{type:o.Input}],nzLoading:[{type:o.Input}],nzFullControl:[{type:o.Input}],nzToolkit:[{type:o.Input}],nzEditorOption:[{type:o.Input}],nzEditorInitialized:[{type:o.Output}]},h([r.InputBoolean(),g("design:type",Object)],O.prototype,"nzLoading",void 0),h([r.InputBoolean(),g("design:type",Object)],O.prototype,"nzFullControl",void 0);var C=function(){};C.decorators=[{type:o.NgModule,args:[{declarations:[O],imports:[d.CommonModule,u.NzIconModule,l.NzSpinModule],exports:[O]}]}],t.NzCodeEditorComponent=O,t.NzCodeEditorModule=C,t.NzCodeEditorService=y,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=ng-zorro-antd-code-editor.umd.min.js.map