Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat: expose monaco editor instance (#895)

Wenzhao Hu51bbd7cb3622
diff --git a/packages/core/src/types/const/action-names.ts b/packages/core/src/types/const/action-names.ts
deleted file mode 100644
index ed1fbd2d03..0000000000
--- a/packages/core/src/types/const/action-names.ts
+++ /dev/null
@@ -1,66 +0,0 @@
-/**
- * Copyright 2023-present DreamNum Inc.
- *
- * Licensed 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.
- */
-
-/**
- * Name set of all action
- */
-export enum ACTION_NAMES {
- /** @see src/Command/Action/InsertDataRowAction */
- INSERT_ROW_DATA_ACTION = 'InsertRowDataAction',
- REMOVE_ROW_DATA_ACTION = 'RemoveRowDataAction',
- INSERT_ROW_ACTION = 'InsertRowAction',
- REMOVE_ROW_ACTION = 'RemoveRowAction',
- SET_RANGE_STYLE_ACTION = 'SetRangeStyleAction',
- SET_SELECTION_ACTION = 'SetSelectionActivateAction',
- SET_RANGE_DATA_ACTION = 'SetRangeDataAction',
- SET_RANGE_FORMATTED_VALUE_ACTION = 'SetRangeFormattedValueAction',
- SET_RANGE_NOTE_ACTION = 'SetRangeNoteAction',
- DELETE_RANGE_ACTION = 'DeleteRangeAction',
- INSERT_SHEET_ACTION = 'InsertSheetAction',
- REMOVE_SHEET_ACTION = 'RemoveSheetAction',
- CLEAR_RANGE_ACTION = 'ClearRangeAction',
- INSERT_RANGE_ACTION = 'InsertRangeAction',
- SET_TAB_COLOR_ACTION = 'SetTabColorAction',
- HIDE_SHEET_ACTION = 'SetWorkSheetHideAction',
- SET_WORKSHEET_ACTIVATE_ACTION = 'SetWorkSheetActivateAction',
- INSERT_COLUMN_ACTION = 'InsertColumnAction',
- INSERT_COLUMN_DATA_ACTION = 'InsertColumnDataAction',
- REMOVE_COLUMN_ACTION = 'RemoveColumnAction',
- REMOVE_COLUMN_DATA_ACTION = 'RemoveColumnDataAction',
- SET_HIDE_ROW_ACTION = 'SetRowHideAction',
- SET_HIDE_COLUMN_ACTION = 'SetColumnHideAction',
- SET_SHOW_ROW_ACTION = 'SetRowShowAction',
- SET_SHOW_COLUMN_ACTION = 'SetColumnShowAction',
- ADD_MERGE_ACTION = 'AddMergeAction',
- REMOVE_MERGE_ACTION = 'RemoveMergeAction',
- SET_WORKSHEET_NAME_ACTION = 'SetWorkSheetNameAction',
- SET_WORKSHEET_STATUS_ACTION = 'SetWorkSheetStatusAction',
- SET_BORDER_ACTION = 'SetBorderAction',
- SET_COLUMN_WIDTH_ACTION = 'SetColumnWidthAction',
- SET_ROW_HEIGHT_ACTION = 'SetRowHeightAction',
- SET_HIDDEN_GRIDLINES_ACTION = 'SetHiddenGridlinesAction',
- SET_RIGHT_TO_LEFT_ACTION = 'SetRightToLeftAction',
- DELETE_BANDING_ACTION = 'DeleteBandingAction',
- SET_BANDING_ACTION = 'SetBandingAction',
- ADD_BANDING_ACTION = 'AddBandingAction',
- DELETE_NAMED_RANGE_ACTION = 'DeleteNamedRangeAction',
- SET_NAMED_RANGE_ACTION = 'SetNamedRangeAction',
- ADD_NAMED_RANGE_ACTION = 'AddNamedRangeAction',
- SET_SHEET_ORDER_ACTION = 'SetSheetOrderAction',
- SET_ZOOM_RATIO_ACTION = 'SetZoomRatioAction',
- SET_FROZEN_COLUMNS_ACTION = 'SetFrozenColumnsAction',
- SET_FROZEN_ROWS_ACTION = 'SetFrozenRowsAction',
-}
diff --git a/packages/core/src/types/const/doc-action-names.ts b/packages/core/src/types/const/doc-action-names.ts
deleted file mode 100644
index 021034e8d3..0000000000
--- a/packages/core/src/types/const/doc-action-names.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-/**
- * Copyright 2023-present DreamNum Inc.
- *
- * Licensed 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.
- */
-
-export enum DOC_ACTION_NAMES {
- INSERT_ACTION_NAME = 'InsertAction',
- DELETE_ACTION_NAME = 'DeleteAction',
- RETAIN_ACTION_NAME = 'RetainAction',
- UPDATE_DOCUMENT_ACTION_NAME = 'UpdateDocumentAction',
-}
diff --git a/packages/core/src/types/const/index.ts b/packages/core/src/types/const/index.ts
index 73dbccc45f..2fa65b4f56 100644
--- a/packages/core/src/types/const/index.ts
+++ b/packages/core/src/types/const/index.ts
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-export * from './action-names';
export * from './const';
export * from './extension-names';
export * from './theme-color-map';
diff --git a/packages/core/src/types/interfaces/i-workbook-data.ts b/packages/core/src/types/interfaces/i-workbook-data.ts
index 8c503e8e42..46e20a62d8 100644
--- a/packages/core/src/types/interfaces/i-workbook-data.ts
+++ b/packages/core/src/types/interfaces/i-workbook-data.ts
@@ -24,12 +24,21 @@ import type { IWorksheetData } from './i-worksheet-data';
* Properties of a workbook's configuration
*/
export interface IWorkbookData extends IExtraModelData {
- /** unit id */
+ /**
+ * unit id
+ */
id: string;
- /** Revision of this document. Would be used in collaborated editing. Starts from one. */
+ /**
+ * Revision of this spreadsheet. Would be used in collaborated editing. Starts from one.
+ */
rev?: number;
+ /**
+ * Name of the spreadsheet.
+ */
name: string;
- /** Version of Univer model definition. */
+ /**
+ * Version of Univer model definition.
+ */
appVersion: string;
locale: LocaleType;
diff --git a/packages/core/vite.config.ts b/packages/core/vite.config.ts
index 4b42182750..f2295be681 100644
--- a/packages/core/vite.config.ts
+++ b/packages/core/vite.config.ts
@@ -40,6 +40,7 @@ export default defineConfig(({ mode }) => ({
},
test: {
coverage: {
+ reporter: ['html', 'json'],
provider: 'istanbul',
},
},
diff --git a/packages/uniscript/src/services/script-editor.service.ts b/packages/uniscript/src/services/script-editor.service.ts
index ca39207b28..627dbb8856 100644
--- a/packages/uniscript/src/services/script-editor.service.ts
+++ b/packages/uniscript/src/services/script-editor.service.ts
@@ -14,20 +14,34 @@
* limitations under the License.
*/
-import { Disposable } from '@univerjs/core';
+import { Disposable, toDisposable } from '@univerjs/core';
+import type { IDisposable } from '@wendellhu/redi';
+import type { editor } from 'monaco-editor';
export interface IScriptEditorServiceConfig {
getWorkerUrl(moduleID: string, label: string): string;
}
/**
- * This service is for loading monaco editor and its resources.
+ * This service is for loading monaco editor and its resources. It also holds the
+ * monaco editor instance so user can interact with the editor programmatically.
*/
export class ScriptEditorService extends Disposable {
+ private _editorInstance: editor.IStandaloneCodeEditor | null = null;
+
constructor(private readonly _config: IScriptEditorServiceConfig) {
super();
}
+ setEditorInstance(editor: editor.IStandaloneCodeEditor): IDisposable {
+ this._editorInstance = editor;
+ return toDisposable(() => (this._editorInstance = null));
+ }
+
+ getEditorInstance(): editor.IStandaloneCodeEditor | null {
+ return this._editorInstance;
+ }
+
requireVscodeEditor(): void {
if (!window.MonacoEnvironment) {
window.MonacoEnvironment = {
diff --git a/packages/uniscript/src/views/components/ScriptEditorPanel.tsx b/packages/uniscript/src/views/components/ScriptEditorPanel.tsx
index 06de71158c..8ab3f94b9d 100644
--- a/packages/uniscript/src/views/components/ScriptEditorPanel.tsx
+++ b/packages/uniscript/src/views/components/ScriptEditorPanel.tsx
@@ -67,6 +67,7 @@ export function ScriptEditorPanel() {
let terminateEscaping: IDisposable | undefined;
disposableCollection = new DisposableCollection();
+ disposableCollection.add(editorService.setEditorInstance(monacoEditor));
disposableCollection.add(
monacoEditor.onDidFocusEditorWidget(() => {
terminateEscaping = shortcutService.forceEscape();