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

Commit 49982eb

Browse files
committed
EditorAppBase no longer inherits code from derived classes
1 parent 9c4bff5 commit 49982eb

File tree

11 files changed

+101
-122
lines changed

11 files changed

+101
-122
lines changed

Diff for: packages/monaco-editor-react/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import { MonacoEditorReactComp } from '@typefox/monaco-editor-react/bundle';
6464

6565
These are the examples specifically for `@typefox/monaco-editor-react` that you can find in the repository:
6666

67-
- TypeScript editor worker using classical configuration [see](../examples/react_ts.html)
67+
- TypeScript editor worker using classic configuration [see](../examples/react_ts.html)
6868
- Langium statemachine language client and web worker based language server using the exact same user configuration as [wrapper statemachine](../examples/wrapper_statemachine.html), [see](../examples/react_statemachine.html)
6969
- Langium grammar language client and web worker based language server using vscode-api configuration [see](../examples/react_langium.html)
7070

Diff for: packages/monaco-editor-react/src/index.tsx

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EditorAppClassic, MonacoEditorLanguageClientWrapper, UserConfig, WorkerConfigDirect, WorkerConfigOptions, isAppConfigDifferent } from 'monaco-editor-wrapper';
1+
import { EditorAppClassic, EditorAppExtended, MonacoEditorLanguageClientWrapper, UserConfig, WorkerConfigDirect, WorkerConfigOptions } from 'monaco-editor-wrapper';
22
import { IDisposable } from 'monaco-editor';
33
import * as vscode from 'vscode';
44
import React, { CSSProperties } from 'react';
@@ -41,6 +41,8 @@ export class MonacoEditorReactComp extends React.Component<MonacoEditorProps> {
4141
}
4242

4343
let mustReInit = false;
44+
const prevConfig = prevProps.userConfig.wrapperConfig.editorAppConfig;
45+
const config = userConfig.wrapperConfig.editorAppConfig;
4446
const prevWorkerOptions = prevProps.userConfig.languageClientConfig?.options;
4547
const currentWorkerOptions = userConfig.languageClientConfig?.options;
4648
const prevIsWorker = (prevWorkerOptions?.$type === 'WorkerDirect');
@@ -59,26 +61,21 @@ export class MonacoEditorReactComp extends React.Component<MonacoEditorProps> {
5961
mustReInit = true;
6062
}
6163

64+
if (prevConfig.$type === 'classic' && config.$type === 'classic') {
65+
mustReInit = (wrapper?.getMonacoEditorApp() as EditorAppClassic).isAppConfigDifferent(prevConfig, config, false) === true;
66+
} else if (prevConfig.$type === 'extended' && config.$type === 'extended') {
67+
mustReInit = (wrapper?.getMonacoEditorApp() as EditorAppExtended).isAppConfigDifferent(prevConfig, config, false) === true;
68+
}
69+
6270
if (mustReInit) {
6371
await this.handleReinit();
6472
} else {
65-
if (wrapper !== null) {
66-
const prevConfig = prevProps.userConfig.wrapperConfig.editorAppConfig;
67-
const config = userConfig.wrapperConfig.editorAppConfig;
68-
const appConfigDifferent = isAppConfigDifferent(prevConfig, config, false, false);
69-
70-
// we need to restart if the editor wrapper config changed
71-
if (appConfigDifferent) {
72-
await this.handleReinit();
73-
} else {
74-
// the function now ensure a model update is only required if something else than the code changed
75-
this.wrapper.updateModel(userConfig.wrapperConfig.editorAppConfig);
76-
77-
if (prevConfig.$type === 'classic' && config.$type === 'classic') {
78-
if (prevConfig.editorOptions !== config.editorOptions) {
79-
(wrapper.getMonacoEditorApp() as EditorAppClassic).updateMonacoEditorOptions(config.editorOptions ?? {});
80-
}
81-
}
73+
// the function now ensure a model update is only required if something else than the code changed
74+
this.wrapper.updateModel(userConfig.wrapperConfig.editorAppConfig);
75+
76+
if (prevConfig.$type === 'classic' && config.$type === 'classic') {
77+
if (prevConfig.editorOptions !== config.editorOptions) {
78+
(wrapper.getMonacoEditorApp() as EditorAppClassic).updateMonacoEditorOptions(config.editorOptions ?? {});
8279
}
8380
}
8481
}

Diff for: packages/monaco-editor-wrapper/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ All notable changes to npm module [monaco-editor-wrapper](https://www.npmjs.com/
7171
- Use global configuration object that is passed to the wrapper on start
7272
- The `monaco-editor-wrapper` and the new `@typefox/monaco-editor-react` component use the same configuration
7373
- The underlying monaco-editor can be configured in two ways now (wrapperConfig):
74-
- Classical: As before, but with one config object
74+
- Classic: As before, but with one config object
7575
- Extension like: Using the extension based mechanism supplied by `monaco-vscode-api`
7676
- `monaco-languageclient` no longer exposes its own service. Now, we fully rely on services supplied by `monaco-vscode-api`
7777
- This means even if you decide to configure monaco-editor the classical way, you still require some basic services. This configuration is made inside `MonacoEditorLanguageClientWrapper`. Potential serviceConfig supplied when using vscode-api extension config is taken into account and combined then.

Diff for: packages/monaco-editor-wrapper/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The `UserConfig` now contains everything and is passed to the `start` function o
2424
[@codingame/monaco-vscode-api](https://github.com/CodinGame/monaco-vscode-api) implements the VSCode api and redirects calls to `monaco-editor`. It allows to add serivccs that are usually only available in VSCode and not with pure `monaco-editor`.
2525
`UserConfig` allows two possible configuration modes:
2626

27-
- **Classical**: Configure `monaco-editor` as you would when using it directly, [see](./src/editorAppClassic.ts)
27+
- **Classic**: Configure `monaco-editor` as you would when using it directly, [see](./src/editorAppClassic.ts)
2828
- **Extended**: Configure `monaco-editor` like a VSCode extension, [see](./src/editorAppExtended.ts)
2929

3030
[This](https://github.com/CodinGame/monaco-vscode-api#monaco-standalone-services) is the list of services defined by [@codingame/monaco-vscode-api](https://github.com/CodinGame/monaco-vscode-api).
@@ -46,7 +46,7 @@ The following services are enabled by default in both editor modes:
4646

4747
If you want any further services than the ones initialized by default, you should use the **extended** mode as some service (like *theme* and *textmate*) are incompatible with the **classic** mode.
4848

49-
Monarch grammars and themes can only be used in **classical** mode and textmate grammars and themes can only be used in **extended** mode.
49+
Monarch grammars and themes can only be used in **classic** mode and textmate grammars and themes can only be used in **extended** mode.
5050

5151
## Usage
5252

@@ -87,8 +87,8 @@ const run = async () => {
8787

8888
These are the examples specifically for `monaco-editor-wrapper` you find in the repository:
8989

90-
- TypeScript editor worker using classical mode, [see](../examples/wrapper_ts.html)
90+
- TypeScript editor worker using classic mode, [see](../examples/wrapper_ts.html)
9191
- Language client & web socket language server example using extended mode [see](../examples/wrapper_ws.html) It requires the json language server to run. Use `start:server:json` from [here](../examples/package.json)
92-
- Multiple editors using classical mode [see](../examples/wrapper_adv.html)
92+
- Multiple editors using classic mode [see](../examples/wrapper_adv.html)
9393
- Langium statemachine language client and web worker based language server using extended mode [see](../examples/wrapper_statemachine.html)
94-
- Langium grammar language client and web worker based language server allowing to choose classical or extended mode [see](../examples/wrapper_langium.html)
94+
- Langium grammar language client and web worker based language server allowing to choose classic or extended mode [see](../examples/wrapper_langium.html)

Diff for: packages/monaco-editor-wrapper/src/editorAppBase.ts

+20-55
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { editor, Uri } from 'monaco-editor';
22
import { createConfiguredEditor, createConfiguredDiffEditor, createModelReference, ITextFileEditorModel } from 'vscode/monaco';
33
import { IReference } from 'vscode/service-override/editor';
4-
import { WrapperConfig } from './wrapper.js';
54
import { updateUserConfiguration as vscodeUpdateUserConfiguration } from 'vscode/service-override/configuration';
6-
import { EditorAppConfigClassic } from './editorAppClassic.js';
7-
import { EditorAppConfigExtended } from './editorAppExtended.js';
85

96
export type ModelUpdate = {
107
languageId: string;
@@ -14,14 +11,21 @@ export type ModelUpdate = {
1411
codeOriginalUri?: string;
1512
}
1613

17-
export type EditorAppBaseConfig = ModelUpdate & {
14+
export type EditorAppType = 'extended' | 'classic';
15+
16+
export type EditorAppConfigBase = ModelUpdate & {
17+
$type: EditorAppType;
1818
useDiffEditor: boolean;
1919
domReadOnly?: boolean;
2020
readOnly?: boolean;
2121
awaitExtensionReadiness?: Array<() => Promise<void>>;
2222
}
2323

24-
export type EditorAppType = 'extended' | 'classic';
24+
export enum ModelUpdateType {
25+
none,
26+
code,
27+
model
28+
}
2529

2630
/**
2731
* This is the base class for both Monaco Ediotor Apps:
@@ -44,8 +48,9 @@ export abstract class EditorAppBase {
4448
this.id = id;
4549
}
4650

47-
protected buildConfig(userAppConfig: EditorAppConfigExtended | EditorAppConfigClassic): EditorAppBaseConfig {
51+
protected buildConfig(userAppConfig: EditorAppConfigBase): EditorAppConfigBase {
4852
return {
53+
$type: userAppConfig.$type,
4954
languageId: userAppConfig.languageId,
5055
code: userAppConfig.code ?? '',
5156
codeOriginal: userAppConfig.codeOriginal ?? '',
@@ -214,31 +219,31 @@ export abstract class EditorAppBase {
214219
return Promise.resolve();
215220
}
216221

217-
protected async updateUserConfiguration(json?: string) {
222+
updateMonacoEditorOptions(options: editor.IEditorOptions & editor.IGlobalEditorOptions) {
223+
this.getEditor()?.updateOptions(options);
224+
}
225+
226+
async updateUserConfiguration(json?: string) {
218227
if (json) {
219228
return vscodeUpdateUserConfiguration(json);
220229
}
221230
return Promise.resolve();
222231
}
223232

224-
abstract getAppType(): string;
225233
abstract init(): Promise<void>;
226234
abstract specifyService(): editor.IEditorOverrideServices;
227235
abstract createEditors(container: HTMLElement): Promise<void>;
228-
abstract getConfig(): EditorAppConfigClassic | EditorAppConfigExtended;
236+
abstract getConfig(): EditorAppConfigBase;
229237
abstract disposeApp(): void;
238+
abstract isAppConfigDifferent(orgConfig: EditorAppConfigBase, config: EditorAppConfigBase, includeModelData: boolean): boolean;
230239
}
231240

232-
export const isExtendedEditorApp = (wrapperConfig: WrapperConfig) => {
233-
return wrapperConfig.editorAppConfig?.$type === 'extended';
234-
};
235-
236-
export const isCodeUpdateRequired = (config: EditorAppBaseConfig, modelUpdate: ModelUpdate) => {
241+
export const isCodeUpdateRequired = (config: EditorAppConfigBase, modelUpdate: ModelUpdate) => {
237242
const updateRequired = (modelUpdate.code !== undefined && modelUpdate.code !== config.code) || modelUpdate.codeOriginal !== config.codeOriginal;
238243
return updateRequired ? ModelUpdateType.code : ModelUpdateType.none;
239244
};
240245

241-
export const isModelUpdateRequired = (config: EditorAppBaseConfig, modelUpdate: ModelUpdate): ModelUpdateType => {
246+
export const isModelUpdateRequired = (config: EditorAppConfigBase, modelUpdate: ModelUpdate): ModelUpdateType => {
242247
const codeUpdate = isCodeUpdateRequired(config, modelUpdate);
243248

244249
type ModelUpdateKeys = keyof typeof modelUpdate;
@@ -249,43 +254,3 @@ export const isModelUpdateRequired = (config: EditorAppBaseConfig, modelUpdate:
249254
const updateRequired = propsModelUpdate.some(propCompare);
250255
return updateRequired ? ModelUpdateType.model : codeUpdate;
251256
};
252-
253-
export enum ModelUpdateType {
254-
none,
255-
code,
256-
model
257-
}
258-
259-
export const isAppConfigDifferent = (orgConfig: EditorAppConfigClassic | EditorAppConfigExtended,
260-
config: EditorAppConfigClassic | EditorAppConfigExtended, includeModelData: boolean, includeEditorOptions: boolean): boolean => {
261-
262-
let different = includeModelData ? isModelUpdateRequired(orgConfig, config) !== ModelUpdateType.none : false;
263-
if (orgConfig.$type === config.$type) {
264-
265-
type ClassicKeys = keyof typeof orgConfig;
266-
const propsClassic = ['useDiffEditor', 'readOnly', 'domReadOnly', 'awaitExtensionReadiness', 'automaticLayout', 'languageDef', 'languageExtensionConfig', 'theme', 'themeData'];
267-
const propsClassicEditorOptions = ['editorOptions', 'diffEditorOptions'];
268-
269-
const propCompareClassic = (name: string) => {
270-
return orgConfig[name as ClassicKeys] !== config[name as ClassicKeys];
271-
};
272-
273-
const propsVscode = ['useDiffEditor', 'readOnly', 'domReadOnly', 'awaitExtensionReadiness', 'userConfiguration', 'extensions'];
274-
type ExtendedKeys = keyof typeof orgConfig;
275-
const propCompareExtended = (name: string) => {
276-
return orgConfig[name as ExtendedKeys] !== config[name as ExtendedKeys];
277-
};
278-
279-
if (orgConfig.$type === 'classic' && config.$type === 'classic') {
280-
different = different || propsClassic.some(propCompareClassic);
281-
if (includeEditorOptions) {
282-
different = different || propsClassicEditorOptions.some(propCompareClassic);
283-
}
284-
} else if (orgConfig.$type === 'extended' && config.$type === 'extended') {
285-
different = different || propsVscode.some(propCompareExtended);
286-
}
287-
} else {
288-
throw new Error('Provided configurations are not of the same type.');
289-
}
290-
return different;
291-
};

Diff for: packages/monaco-editor-wrapper/src/editorAppClassic.ts

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { editor, languages } from 'monaco-editor';
2-
import { EditorAppBase, EditorAppBaseConfig, EditorAppType } from './editorAppBase.js';
2+
import { EditorAppBase, EditorAppConfigBase, ModelUpdateType, isModelUpdateRequired } from './editorAppBase.js';
33
import { UserConfig } from './wrapper.js';
44
import { Logger } from './logger.js';
55

6-
export type EditorAppConfigClassic = EditorAppBaseConfig & {
6+
export type EditorAppConfigClassic = EditorAppConfigBase & {
77
$type: 'classic';
88
automaticLayout?: boolean;
99
theme?: editor.BuiltinTheme | string;
@@ -43,10 +43,6 @@ export class EditorAppClassic extends EditorAppBase {
4343
this.config.themeData = userAppConfig.themeData ?? undefined;
4444
}
4545

46-
getAppType(): EditorAppType {
47-
return 'classic';
48-
}
49-
5046
getConfig(): EditorAppConfigClassic {
5147
return this.config;
5248
}
@@ -102,12 +98,26 @@ export class EditorAppClassic extends EditorAppBase {
10298
this.logger?.info('Init of Classic App was completed.');
10399
}
104100

105-
updateMonacoEditorOptions(options: editor.IEditorOptions & editor.IGlobalEditorOptions) {
106-
this.getEditor()?.updateOptions(options);
107-
}
108-
109101
disposeApp(): void {
110102
this.disposeEditor();
111103
this.disposeDiffEditor();
112104
}
105+
106+
isAppConfigDifferent(orgConfig: EditorAppConfigClassic, config: EditorAppConfigClassic, includeModelData: boolean): boolean {
107+
let different = false;
108+
if (includeModelData) {
109+
different = isModelUpdateRequired(orgConfig, config) !== ModelUpdateType.none;
110+
}
111+
type ClassicKeys = keyof typeof orgConfig;
112+
const propsClassic = ['useDiffEditor', 'readOnly', 'domReadOnly', 'awaitExtensionReadiness', 'automaticLayout', 'languageDef', 'languageExtensionConfig', 'theme', 'themeData'];
113+
const propsClassicEditorOptions = ['editorOptions', 'diffEditorOptions'];
114+
115+
const propCompareClassic = (name: string) => {
116+
return orgConfig[name as ClassicKeys] !== config[name as ClassicKeys];
117+
};
118+
119+
different = different || propsClassic.some(propCompareClassic);
120+
different = different || propsClassicEditorOptions.some(propCompareClassic);
121+
return different;
122+
}
113123
}

Diff for: packages/monaco-editor-wrapper/src/editorAppExtended.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IDisposable, editor } from 'monaco-editor';
33
import getThemeServiceOverride from '@codingame/monaco-vscode-theme-service-override';
44
import getTextmateServiceOverride from '@codingame/monaco-vscode-textmate-service-override';
55
import { whenReady as whenReadyTheme } from '@codingame/monaco-vscode-theme-defaults-default-extension';
6-
import { EditorAppBase, EditorAppBaseConfig, EditorAppType } from './editorAppBase.js';
6+
import { EditorAppBase, EditorAppConfigBase, ModelUpdateType, isModelUpdateRequired } from './editorAppBase.js';
77
import { registerExtension, IExtensionManifest, ExtensionHostKind } from 'vscode/extensions';
88
import { UserConfig } from './wrapper.js';
99
import { verifyUrlorCreateDataUrl } from './utils.js';
@@ -18,7 +18,7 @@ export type UserConfiguration = {
1818
json?: string;
1919
}
2020

21-
export type EditorAppConfigExtended = EditorAppBaseConfig & {
21+
export type EditorAppConfigExtended = EditorAppConfigBase & {
2222
$type: 'extended';
2323
extensions?: ExtensionConfig[];
2424
userConfiguration?: UserConfiguration;
@@ -57,10 +57,6 @@ export class EditorAppExtended extends EditorAppBase {
5757
this.config.userConfiguration = userAppConfig.userConfiguration ?? undefined;
5858
}
5959

60-
getAppType(): EditorAppType {
61-
return 'extended';
62-
}
63-
6460
getConfig(): EditorAppConfigExtended {
6561
return this.config;
6662
}
@@ -116,4 +112,18 @@ export class EditorAppExtended extends EditorAppBase {
116112
this.disposeDiffEditor();
117113
this.extensionRegisterResults.forEach((k) => k?.dispose());
118114
}
115+
116+
isAppConfigDifferent(orgConfig: EditorAppConfigExtended, config: EditorAppConfigExtended, includeModelData: boolean): boolean {
117+
let different = false;
118+
if (includeModelData) {
119+
different = isModelUpdateRequired(orgConfig, config) !== ModelUpdateType.none;
120+
}
121+
const propsExtended = ['useDiffEditor', 'readOnly', 'domReadOnly', 'awaitExtensionReadiness', 'userConfiguration', 'extensions'];
122+
type ExtendedKeys = keyof typeof orgConfig;
123+
const propCompareExtended = (name: string) => {
124+
return orgConfig[name as ExtendedKeys] !== config[name as ExtendedKeys];
125+
};
126+
different = different || propsExtended.some(propCompareExtended);
127+
return different;
128+
}
119129
}

Diff for: packages/monaco-editor-wrapper/src/index.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import {
22
EditorAppBase,
3-
isExtendedEditorApp,
43
isCodeUpdateRequired,
54
isModelUpdateRequired,
6-
isAppConfigDifferent,
75
ModelUpdateType
86
} from './editorAppBase.js';
97

108
import type {
11-
EditorAppBaseConfig,
9+
EditorAppConfigBase,
1210
EditorAppType,
1311
ModelUpdate
1412
} from './editorAppBase.js';
@@ -18,7 +16,7 @@ import type {
1816
} from './editorAppClassic.js';
1917

2018
import {
21-
EditorAppClassic,
19+
EditorAppClassic
2220
} from './editorAppClassic.js';
2321

2422
import type {
@@ -67,7 +65,7 @@ import {
6765

6866
export type {
6967
WrapperConfig,
70-
EditorAppBaseConfig,
68+
EditorAppConfigBase,
7169
EditorAppType,
7270
EditorAppConfigClassic,
7371
ExtensionConfig,
@@ -92,10 +90,8 @@ export {
9290
MonacoEditorLanguageClientWrapper,
9391
LanguageClientWrapper,
9492
EditorAppBase,
95-
isExtendedEditorApp,
9693
isCodeUpdateRequired,
9794
isModelUpdateRequired,
98-
isAppConfigDifferent,
9995
ModelUpdateType,
10096
EditorAppClassic,
10197
EditorAppExtended,

0 commit comments

Comments
 (0)