Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
react-editor: Expose MonacoEditorLanguageClientWrappervia getEditorWr…
Browse files Browse the repository at this point in the history
…apper
  • Loading branch information
kaisalmen committed Jan 13, 2023
1 parent e4b1727 commit 0b4a256
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 212 deletions.
378 changes: 197 additions & 181 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
},
"devDependencies": {
"@types/node": "~16.18.3",
"@typescript-eslint/eslint-plugin": "~5.47.0",
"vite": "~4.0.2",
"@typescript-eslint/eslint-plugin": "~5.48.1",
"vite": "~4.0.4",
"path-browserify": "~1.0.1",
"typescript": "~4.9.4",
"eslint": "~8.30.0",
"eslint": "~8.31.0",
"editorconfig": "~1.0.1",
"tslib": "~2.4.1",
"shx": "~0.3.4"
},
"volta": {
"node": "16.18.1",
"node": "16.19.0",
"npm": "8.19.3"
},
"workspaces": [
Expand Down
4 changes: 2 additions & 2 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
},
"devDependencies": {
"@types/react": "~18.0.26",
"@types/react-dom": "~18.0.9"
"@types/react-dom": "~18.0.10"
},
"volta": {
"node": "16.18.1",
"node": "16.19.0",
"npm": "8.19.3"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/monaco-editor-comp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@
"build": "npm run clean && npm run compile && npm run build:bundle"
},
"volta": {
"node": "16.18.1",
"node": "16.19.0",
"npm": "8.19.3"
},
"dependencies": {
"monaco-editor-wrapper": "1.6.0",
"lit": "~2.5.0"
"lit": "~2.6.1"
},
"devDependencies": {
"monaco-editor-workers": "0.34.2"
Expand Down
6 changes: 3 additions & 3 deletions packages/monaco-editor-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typefox/monaco-editor-react",
"version": "1.0.0-next.5",
"version": "1.0.0-next.6",
"license": "MIT",
"description": "React component for Monaco-Editor and Monaco Languageclient",
"keywords": [
Expand Down Expand Up @@ -65,7 +65,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"volta": {
"node": "16.18.1",
"node": "16.19.0",
"npm": "8.19.3"
},
"dependencies": {
Expand All @@ -76,7 +76,7 @@
},
"devDependencies": {
"@types/react": "~18.0.26",
"@types/react-dom": "~18.0.9"
"@types/react-dom": "~18.0.10"
},
"repository": {
"type": "git",
Expand Down
34 changes: 19 additions & 15 deletions packages/monaco-editor-react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type MonacoEditorProps = {
}

export class MonacoEditorReactComp extends React.Component<MonacoEditorProps> {
private editor: MonacoEditorLanguageClientWrapper | null = null;
private wrapper: MonacoEditorLanguageClientWrapper | null = null;

private containerElement?: HTMLDivElement;

Expand All @@ -41,12 +41,12 @@ export class MonacoEditorReactComp extends React.Component<MonacoEditorProps> {
componentDidUpdate(prevProps: MonacoEditorProps) {
const { className, text, webworkerUri, syntax, languageId } = this.props;

const { editor } = this;
const { wrapper } = this;

const editorConfig = editor!.getEditorConfig();
const editorConfig = wrapper!.getEditorConfig();
const innerEditor: monaco.editor.IStandaloneCodeEditor =
// eslint-disable-next-line dot-notation
editor!['editor'];
wrapper!['editor'];

if (prevProps.className !== className && this.containerElement) {
this.containerElement.className = className ?? '';
Expand All @@ -57,7 +57,7 @@ export class MonacoEditorReactComp extends React.Component<MonacoEditorProps> {
editorConfig.setMainLanguageId(languageId);
editorConfig.setMonarchTokensProvider(syntax);
// eslint-disable-next-line dot-notation
editor!['updateMonacoConfig']();
wrapper!['updateMonacoConfig']();
const model = innerEditor.getModel();
if (model && text !== model.getValue()) {
model.setValue(text);
Expand All @@ -74,17 +74,17 @@ export class MonacoEditorReactComp extends React.Component<MonacoEditorProps> {
};

private async destroyMonaco(): Promise<void> {
if (this.editor) {
if (this.wrapper) {
await this.isStarting;
try {
await this.editor.dispose();
await this.wrapper.dispose();
} catch {
// This is fine
// Sometimes the language client throws an error during disposal
// This should not prevent us from continue working
}
// eslint-disable-next-line dot-notation
this.editor['languageClient'] = undefined;
this.wrapper['languageClient'] = undefined;
}
if (this._subscription) {
this._subscription.dispose();
Expand All @@ -110,8 +110,8 @@ export class MonacoEditorReactComp extends React.Component<MonacoEditorProps> {

if (this.containerElement) {
this.containerElement.className = className ?? '';
this.editor = new MonacoEditorLanguageClientWrapper('42');
const editorConfig = this.editor.getEditorConfig();
this.wrapper = new MonacoEditorLanguageClientWrapper('42');
const editorConfig = this.wrapper.getEditorConfig();
editorConfig.setMainLanguageId(languageId);
editorConfig.setMonarchTokensProvider(syntax);
if (languageExtensionConfig) {
Expand All @@ -135,18 +135,18 @@ export class MonacoEditorReactComp extends React.Component<MonacoEditorProps> {
type: workerType ?? 'classic',
name: workerName ?? 'LanguageServerWorker',
});
this.editor.setWorker(lsWorker);
this.wrapper.setWorker(lsWorker);
}

this.isStarting = this.editor.startEditor(this.containerElement);
this.isStarting = this.wrapper.startEditor(this.containerElement);
await this.isStarting;

onLoading && onLoading();
onLoad && this.isStarting.then(() => onLoad());

if (onTextChanged) {
// eslint-disable-next-line dot-notation
const innerEditor: monaco.editor.IStandaloneCodeEditor = this.editor['editor'];
const innerEditor: monaco.editor.IStandaloneCodeEditor = this.wrapper['editor'];
const model = innerEditor.getModel();
if (model) {
this._subscription = model.onDidChangeContent(() => {
Expand All @@ -163,20 +163,24 @@ export class MonacoEditorReactComp extends React.Component<MonacoEditorProps> {
}

updateLayout(): void {
this.editor!.updateLayout();
this.wrapper!.updateLayout();
}

getText(): string {
try {
// eslint-disable-next-line dot-notation
const innerEditor: monaco.editor.IStandaloneCodeEditor = this.editor!['editor'];
const innerEditor: monaco.editor.IStandaloneCodeEditor = this.wrapper!['editor'];
const model = innerEditor.getModel();
return model?.getValue() ?? '';
} catch {
return '';
}
}

getEditorWrapper() {
return this.wrapper;
}

/**
* Executes a custom LSP command by name with args, and returns the result
* @param cmd Command to execute
Expand Down
2 changes: 1 addition & 1 deletion packages/monaco-editor-workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"build": "npm run clean && npm run compile && npm run build:workers:vite"
},
"volta": {
"node": "16.18.1",
"node": "16.19.0",
"npm": "8.19.3"
},
"devDependencies": {
Expand Down
3 changes: 0 additions & 3 deletions packages/monaco-editor-wrapper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ Monaco Editor with JavaScript language support in web worker
// helper function for loading monaco-editor's own workers
import { buildWorkerDefinition } from 'monaco-editor-workers';
buildWorkerDefinition('./node_modules/monaco-editor-workers/dist/workers', import.meta.url, false);

import { MonacoEditorLanguageClientWrapper } from 'monaco-editor-wrapper';
const client = new MonacoEditorLanguageClientWrapper();

const client = new MonacoEditorLanguageClientWrapper();
const editorConfig = client.getEditorConfig();
Expand All @@ -50,7 +48,6 @@ Monaco Editor with language server running in a web worker:
// helper function for loading monaco-editor's own workers
import { buildWorkerDefinition } from 'monaco-editor-workers';
buildWorkerDefinition('./node_modules/monaco-editor-workers/dist/workers', import.meta.url, false);

import { MonacoEditorLanguageClientWrapper } from 'monaco-editor-wrapper';

const client = new MonacoEditorLanguageClientWrapper();
Expand Down
2 changes: 1 addition & 1 deletion packages/monaco-editor-wrapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"build": "npm run clean && npm run compile && npm run build:bundle"
},
"volta": {
"node": "16.18.1",
"node": "16.19.0",
"npm": "8.19.3"
},
"dependencies": {
Expand Down

0 comments on commit 0b4a256

Please sign in to comment.