Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add json worker and language implement in dashboards #3121

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* [Multi DataSource] UX enhancement on Data source management stack ([#2521](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2521))
* [Multi DataSource] UX enhancement on Update stored password modal for Data source management stack ([#2532](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2532))
* [Viz Builder] Create a new wizard directly on a dashboard ([#2384](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2384))
- [Monaco editor] add json worker and language implement ([#3121](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3121))

### 🐛 Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dashboarding"
],
"private": true,
"version": "2.5.0",
"version": "2.4.2",
"branch": "2.x",
"types": "./opensearch_dashboards.d.ts",
"tsdocMetadata": "./build/tsdoc-metadata.json",
Expand Down
1 change: 1 addition & 0 deletions packages/osd-monaco/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

export { monaco } from './monaco';
export { XJsonLang } from './xjson';
import './json';

/* eslint-disable-next-line @osd/eslint/module_migration */
import * as BarePluginApi from 'monaco-editor/esm/vs/editor/editor.api';
Expand Down
85 changes: 85 additions & 0 deletions packages/osd-monaco/src/json/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
* monaco-editor under v0.20.0 has the bug that use the global monaco uncorrectly in ESM mode. https://github.com/microsoft/monaco-editor/issues/1974
* copy the language implement and import by our self.
* */
// @ts-ignore
import * as mode from 'monaco-editor/esm/vs/language/json/jsonMode.js'; // eslint-disable-line
import { monaco } from '../monaco';

const Emitter = monaco.Emitter;
class LanguageServiceDefaultsImpl {
_onDidChange: monaco.Emitter<any>;
_languageId: string;
_diagnosticsOptions!: monaco.languages.json.DiagnosticsOptions;
constructor(languageId: string, diagnosticsOptions: monaco.languages.json.DiagnosticsOptions) {
this._onDidChange = new Emitter();
this._languageId = languageId;
this.setDiagnosticsOptions(diagnosticsOptions);
}
public get languageId() {
return this._languageId;
}
public get onDidChange() {
return this._onDidChange.event;
}
public get diagnosticsOptions() {
return this._diagnosticsOptions;
}
setDiagnosticsOptions(options: monaco.languages.json.DiagnosticsOptions) {
this._diagnosticsOptions = options || Object.create(null);
this._onDidChange.fire(this);
}
}
export { LanguageServiceDefaultsImpl };
const diagnosticDefault = {
validate: true,
allowComments: true,
schemas: [],
enableSchemaRequest: false,
};
const jsonDefaults = new LanguageServiceDefaultsImpl('json', diagnosticDefault);
function createAPI() {
return {
jsonDefaults,
};
}
monaco.languages.json = createAPI();
monaco.languages.register({
id: 'json',
extensions: ['.json', '.bowerrc', '.jshintrc', '.jscsrc', '.eslintrc', '.babelrc'],
aliases: ['JSON', 'json'],
mimetypes: ['application/json'],
});
monaco.languages.onLanguage('json', function () {
mode.setupMode(jsonDefaults);
});
33 changes: 33 additions & 0 deletions packages/osd-monaco/src/json/worker/json.worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/* eslint-disable */
// @ts-ignore
import 'monaco-editor/esm/vs/language/json/json.worker.js';
9 changes: 7 additions & 2 deletions packages/osd-monaco/src/xjson/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import { registerLexerRules } from './lexer_rules';
import { ID } from './constants';
// @ts-ignore
import workerSrc from '!!raw-loader!../../target/public/xjson.editor.worker.js';
// @ts-ignore
import jsonWorkerSrc from '!!raw-loader!../../target/public/json.editor.worker.js';

const wps = new WorkerProxyService();

Expand All @@ -48,8 +50,11 @@ registerLexerRules(monaco);
// @ts-ignore
window.MonacoEnvironment = {
getWorker: (module: string, languageId: string) => {
if (languageId === ID) {
// In OpenSearch Dashboards we will probably build this once and then load with raw-loader
// In OpenSearch Dashboards we will probably build this once and then load with raw-loader
if (languageId === 'json') {
const blob = new Blob([jsonWorkerSrc], { type: 'application/javascript' });
return new Worker(URL.createObjectURL(blob));
} else if (languageId === ID) {
const blob = new Blob([workerSrc], { type: 'application/javascript' });
return new Worker(URL.createObjectURL(blob));
}
Expand Down
2 changes: 1 addition & 1 deletion packages/osd-monaco/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ const createLangWorkerConfig = (lang) => ({
},
});

module.exports = [createLangWorkerConfig('xjson')];
module.exports = [createLangWorkerConfig('xjson'), createLangWorkerConfig('json')];