-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
158 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
<h2>Monaco Language Client Angular Client Example</h2> | ||
<h2>Monaco Language Client Angular Client Two way data binding Example</h2> | ||
<div> | ||
<div id="monaco-editor-root" class="monaco-editor"></div> | ||
@if(wrapperConfig) { | ||
<monaco-angular-wrapper | ||
[wrapperConfig]="wrapperConfig" | ||
(onTextChanged)="onTextChanged($event)" | ||
></monaco-angular-wrapper> | ||
|
||
} | ||
|
||
{{ codeText() }} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { | ||
AfterViewInit, | ||
Component, | ||
EventEmitter, | ||
Input, | ||
OnDestroy, | ||
Output, | ||
} from '@angular/core'; | ||
|
||
|
||
import * as monaco from 'monaco-editor'; | ||
import { | ||
MonacoEditorLanguageClientWrapper, | ||
WrapperConfig, | ||
} from 'monaco-editor-wrapper'; | ||
|
||
@Component({ | ||
standalone: true, | ||
selector: 'monaco-angular-wrapper', | ||
template: ` | ||
<div> | ||
<div id='monaco-editor-root' class='monaco-editor'></div> | ||
</div> | ||
`, | ||
styles: ` | ||
.monaco-editor { | ||
height: 50vh; | ||
} | ||
`, | ||
}) | ||
export class MonacoAngularWrapperComponent implements AfterViewInit, OnDestroy { | ||
@Output() onTextChanged = new EventEmitter<string>(); | ||
@Input() wrapperConfig: WrapperConfig; | ||
title = 'angluar-lang-client'; | ||
private wrapper: MonacoEditorLanguageClientWrapper = | ||
new MonacoEditorLanguageClientWrapper(); | ||
private containerElement?: HTMLDivElement; | ||
private _subscription: monaco.IDisposable | null = null; | ||
private isRestarting?: Promise<void>; | ||
private started: (value: void | PromiseLike<void>) => void; | ||
|
||
async ngAfterViewInit(): Promise<void> { | ||
|
||
this.containerElement = document.getElementById( | ||
'monaco-editor-root' | ||
) as HTMLDivElement; | ||
// await this.handleReinit(); | ||
try { | ||
await this.wrapper.initAndStart(this.wrapperConfig); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
protected async handleReinit() { | ||
await this.destroyMonaco(); | ||
await this.initMonaco(); | ||
await this.startMonaco(); | ||
} | ||
|
||
protected async destroyMonaco(): Promise<void> { | ||
if (this.wrapper) { | ||
Check failure on line 62 in verify/angular/src/monaco-angular-wrapper.component.ts GitHub Actions / monaco-languageclient
|
||
if (this.isRestarting) { | ||
await this.isRestarting; | ||
} | ||
try { | ||
await this.wrapper.dispose(); | ||
} catch { | ||
// The language client may throw an error during disposal. | ||
// This should not prevent us from continue working. | ||
} | ||
} | ||
if (this._subscription) { | ||
this._subscription.dispose(); | ||
} | ||
} | ||
|
||
async ngOnDestroy() { | ||
this.destroyMonaco(); | ||
} | ||
|
||
protected async initMonaco() { | ||
this.isRestarting = new Promise<void>((resolve) => { | ||
this.started = resolve; | ||
}); | ||
await this.wrapper.init(this.wrapperConfig); | ||
} | ||
|
||
protected async startMonaco() { | ||
if (this.containerElement) { | ||
// exceptions are forwarded to onError callback or the exception is thrown | ||
try { | ||
await this.wrapper.start(); | ||
} catch (e) {} | ||
this.started(); | ||
this.isRestarting = undefined; | ||
|
||
this.handleOnTextChanged(); | ||
} | ||
} | ||
|
||
handleOnTextChanged() { | ||
const textModels = this.wrapper.getTextModels(); | ||
if (textModels) { | ||
const verifyModelContent = () => { | ||
const text = textModels.text?.getValue() ?? ''; | ||
const textOriginal = textModels.textOriginal?.getValue() ?? ''; | ||
const codeResources = | ||
this.wrapperConfig.editorAppConfig.codeResources; | ||
const dirty = text !== codeResources?.main?.text; | ||
const dirtyOriginal = | ||
textOriginal !== codeResources?.original?.text; | ||
this.onTextChanged.emit(text); | ||
console.log('dirty , dirtyOriginal', dirty, dirtyOriginal); | ||
}; | ||
|
||
const newSubscriptions: monaco.IDisposable[] = []; | ||
|
||
if (textModels.text) { | ||
newSubscriptions.push( | ||
textModels.text.onDidChangeContent(() => { | ||
verifyModelContent(); | ||
}) | ||
); | ||
} | ||
} | ||
} | ||
} |