import { DOCUMENT, NgTemplateOutlet } from '@angular/common'; import * as i0 from '@angular/core'; import { inject, Injectable, EventEmitter, forwardRef, booleanAttribute, Output, Input, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule } from '@angular/core'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { ReplaySubject, BehaviorSubject, of, Subject, combineLatest } from 'rxjs'; import { tap, map, takeUntil, debounceTime, filter, distinctUntilChanged } from 'rxjs/operators'; import { warn, PREFIX } from 'ng-zorro-antd/core/logger'; import { inNextTick, fromEventOutsideAngular } from 'ng-zorro-antd/core/util'; import { NzSpinComponent } from 'ng-zorro-antd/spin'; import * as i1 from 'ng-zorro-antd/core/config'; import * as i2 from '@angular/cdk/platform'; /** * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE */ /** * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE */ const NZ_CONFIG_MODULE_NAME = 'codeEditor'; function tryTriggerFunc(fn) { return (...args) => { if (fn) { fn(...args); } }; } // Caretaker note: previously, these were `NzCodeEditorService` properties. // They're kept as static variables because this will allow loading Monaco only once. // This applies to micro frontend apps with multiple Angular apps or a single Angular app // that can be bootstrapped and destroyed multiple times (e.g. using Webpack module federation). // Root providers are re-initialized each time the app is bootstrapped. Platform providers aren't. // We can't make the `NzCodeEditorService` to be a platform provider (`@Injectable({ providedIn: 'platform' })`) // since it depends on other root providers. const loaded$ = new ReplaySubject(1); let loadingStatus = "unload" /* NzCodeEditorLoadingStatus.UNLOAD */; class NzCodeEditorService { nzConfigService; document = inject(DOCUMENT); firstEditorInitialized = false; option = {}; config; subscription; option$ = new BehaviorSubject(this.option); constructor(nzConfigService) { this.nzConfigService = nzConfigService; const globalConfig = this.nzConfigService.getConfigForComponent(NZ_CONFIG_MODULE_NAME); this.config = { ...globalConfig }; if (this.config.monacoEnvironment) { window.MonacoEnvironment = { ...this.config.monacoEnvironment }; } this.option = this.config.defaultEditorOption || {}; this.subscription = this.nzConfigService.getConfigChangeEventForComponent(NZ_CONFIG_MODULE_NAME).subscribe(() => { const newGlobalConfig = this.nzConfigService.getConfigForComponent(NZ_CONFIG_MODULE_NAME); if (newGlobalConfig) { this._updateDefaultOption(newGlobalConfig.defaultEditorOption); } }); } ngOnDestroy() { this.subscription.unsubscribe(); this.subscription = null; } _updateDefaultOption(option) { this.option = { ...this.option, ...option }; this.option$.next(this.option); if ('theme' in option && option.theme) { monaco.editor.setTheme(option.theme); } } requestToInit() { if (loadingStatus === "LOADED" /* NzCodeEditorLoadingStatus.LOADED */) { this.onInit(); return of(this.getLatestOption()); } if (loadingStatus === "unload" /* NzCodeEditorLoadingStatus.UNLOAD */) { if (this.config.useStaticLoading && typeof monaco === 'undefined') { warn('You choose to use static loading but it seems that you forget ' + 'to config webpack plugin correctly. Please refer to our official website' + 'for more details about static loading.'); } else { this.loadMonacoScript(); } } return loaded$.pipe(tap(() => this.onInit()), map(() => this.getLatestOption())); } loadMonacoScript() { if (this.config.useStaticLoading) { Promise.resolve().then(() => this.onLoad()); return; } if (loadingStatus === "loading" /* NzCodeEditorLoadingStatus.LOADING */) { return; } loadingStatus = "loading" /* NzCodeEditorLoadingStatus.LOADING */; const assetsRoot = this.config.assetsRoot; const vs = assetsRoot ? `${assetsRoot}/vs` : 'assets/vs'; const windowAsAny = window; const loadScript = this.document.createElement('script'); loadScript.type = 'text/javascript'; loadScript.src = `${vs}/loader.js`; const onLoad = () => { cleanup(); windowAsAny.require.config({ paths: { vs }, ...this.config.extraConfig }); windowAsAny.require(['vs/editor/editor.main'], () => { this.onLoad(); }); }; const onError = () => { cleanup(); throw new Error(`${PREFIX} cannot load assets of monaco editor from source "${vs}".`); }; const cleanup = () => { // Caretaker note: we have to remove these listeners once the `