{"version":3,"file":"ng-zorro-antd-code-editor.mjs","sources":["../../components/code-editor/typings.ts","../../components/code-editor/code-editor.service.ts","../../components/code-editor/code-editor.component.ts","../../components/code-editor/code-editor.module.ts","../../components/code-editor/public-api.ts","../../components/code-editor/ng-zorro-antd-code-editor.ts"],"sourcesContent":["/**\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE\n */\n\nimport { editor, Environment } from 'monaco-editor';\n\nimport IStandAloneEditorConstructionOptions = editor.IStandaloneEditorConstructionOptions;\nimport IDiffEditorConstructionOptions = editor.IDiffEditorConstructionOptions;\n\ndeclare global {\n interface Window {\n MonacoEnvironment?: Environment | undefined;\n }\n}\n\nexport type EditorOptions = IStandAloneEditorConstructionOptions;\nexport type DiffEditorOptions = IDiffEditorConstructionOptions;\nexport type JoinedEditorOptions = EditorOptions | DiffEditorOptions;\n\nexport type NzEditorMode = 'normal' | 'diff';\n\nexport const enum NzCodeEditorLoadingStatus {\n UNLOAD = 'unload',\n LOADING = 'loading',\n LOADED = 'LOADED'\n}\n","/**\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE\n */\n\nimport { DOCUMENT } from '@angular/common';\nimport { Injectable, OnDestroy, inject } from '@angular/core';\nimport { BehaviorSubject, Observable, ReplaySubject, Subscription, of } from 'rxjs';\nimport { map, tap } from 'rxjs/operators';\n\nimport { CodeEditorConfig, NzConfigService } from 'ng-zorro-antd/core/config';\nimport { PREFIX, warn } from 'ng-zorro-antd/core/logger';\nimport { NzSafeAny } from 'ng-zorro-antd/core/types';\n\nimport { JoinedEditorOptions, NzCodeEditorLoadingStatus } from './typings';\n\ndeclare const monaco: NzSafeAny;\n\nconst NZ_CONFIG_MODULE_NAME = 'codeEditor';\n\nfunction tryTriggerFunc(fn?: (...args: NzSafeAny[]) => NzSafeAny): (...args: NzSafeAny) => void {\n return (...args: NzSafeAny[]) => {\n if (fn) {\n fn(...args);\n }\n };\n}\n\n// Caretaker note: previously, these were `NzCodeEditorService` properties.\n// They're kept as static variables because this will allow loading Monaco only once.\n// This applies to micro frontend apps with multiple Angular apps or a single Angular app\n// that can be bootstrapped and destroyed multiple times (e.g. using Webpack module federation).\n// Root providers are re-initialized each time the app is bootstrapped. Platform providers aren't.\n// We can't make the `NzCodeEditorService` to be a platform provider (`@Injectable({ providedIn: 'platform' })`)\n// since it depends on other root providers.\nconst loaded$ = new ReplaySubject(1);\nlet loadingStatus = NzCodeEditorLoadingStatus.UNLOAD;\n\n@Injectable({\n providedIn: 'root'\n})\nexport class NzCodeEditorService implements OnDestroy {\n private document: Document = inject(DOCUMENT);\n private firstEditorInitialized = false;\n private option: JoinedEditorOptions = {};\n private config: CodeEditorConfig;\n private subscription: Subscription | null;\n\n option$ = new BehaviorSubject(this.option);\n\n constructor(private readonly nzConfigService: NzConfigService) {\n const globalConfig = this.nzConfigService.getConfigForComponent(NZ_CONFIG_MODULE_NAME);\n\n this.config = { ...globalConfig };\n if (this.config.monacoEnvironment) {\n window.MonacoEnvironment = { ...this.config.monacoEnvironment };\n }\n this.option = this.config.defaultEditorOption || {};\n\n this.subscription = this.nzConfigService.getConfigChangeEventForComponent(NZ_CONFIG_MODULE_NAME).subscribe(() => {\n const newGlobalConfig: NzSafeAny = this.nzConfigService.getConfigForComponent(NZ_CONFIG_MODULE_NAME);\n if (newGlobalConfig) {\n this._updateDefaultOption(newGlobalConfig.defaultEditorOption);\n }\n });\n }\n\n ngOnDestroy(): void {\n this.subscription!.unsubscribe();\n this.subscription = null;\n }\n\n private _updateDefaultOption(option: JoinedEditorOptions): void {\n this.option = { ...this.option, ...option };\n this.option$.next(this.option);\n\n if ('theme' in option && option.theme) {\n monaco.editor.setTheme(option.theme);\n }\n }\n\n requestToInit(): Observable {\n if (loadingStatus === NzCodeEditorLoadingStatus.LOADED) {\n this.onInit();\n return of(this.getLatestOption());\n }\n\n if (loadingStatus === NzCodeEditorLoadingStatus.UNLOAD) {\n if (this.config.useStaticLoading && typeof monaco === 'undefined') {\n warn(\n 'You choose to use static loading but it seems that you forget ' +\n 'to config webpack plugin correctly. Please refer to our official website' +\n 'for more details about static loading.'\n );\n } else {\n this.loadMonacoScript();\n }\n }\n\n return loaded$.pipe(\n tap(() => this.onInit()),\n map(() => this.getLatestOption())\n );\n }\n\n private loadMonacoScript(): void {\n if (this.config.useStaticLoading) {\n Promise.resolve().then(() => this.onLoad());\n return;\n }\n\n if (loadingStatus === NzCodeEditorLoadingStatus.LOADING) {\n return;\n }\n\n loadingStatus = NzCodeEditorLoadingStatus.LOADING;\n\n const assetsRoot = this.config.assetsRoot;\n const vs = assetsRoot ? `${assetsRoot}/vs` : 'assets/vs';\n const windowAsAny = window as NzSafeAny;\n const loadScript = this.document.createElement('script');\n\n loadScript.type = 'text/javascript';\n loadScript.src = `${vs}/loader.js`;\n\n const onLoad = (): void => {\n cleanup();\n windowAsAny.require.config({\n paths: { vs },\n ...this.config.extraConfig\n });\n windowAsAny.require(['vs/editor/editor.main'], () => {\n this.onLoad();\n });\n };\n\n const onError = (): void => {\n cleanup();\n throw new Error(`${PREFIX} cannot load assets of monaco editor from source \"${vs}\".`);\n };\n\n const cleanup = (): void => {\n // Caretaker note: we have to remove these listeners once the `