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(uniscript): implement uniscript POC (#570)

Wenzhao Hu4f72d773244c
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 1219e2fae8..4eaa417c3b 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -60,6 +60,7 @@
"undoredo",
"undos",
"unhine",
+ "Uniscript",
"univer",
"univerdoc",
"univerjs",
diff --git a/docs/zh/sheet-architecture.md b/docs/zh/sheet-architecture.md
index 2a0b2b358d..c17cd7de7c 100644
--- a/docs/zh/sheet-architecture.md
+++ b/docs/zh/sheet-architecture.md
@@ -32,7 +32,13 @@ Univer Sheet 的架构主要由以下几个部分组成:
3. sheet 数据验证
4. sheet 数据透视表
-等等
+等等。
+
+**何时使用 SheetInterceptorService,何时不要使用?**
+
+当各个功能需要操作同一块数据或者状态,但是它们之前并没有明确的依赖关系时使用 SheetInterceptorService,例如:数据透视表、公式、条件格式、数据验证、单元格原始数据都可能影响其他功能对单元格内容的取值结果,但是它们之间并没有明确的依赖关系,因此使用 SheetInterceptorService 来实现这些功能是比较合适的。
+
+如果一个功能明确地依赖其他功能,例如公式需要在下拉填充或复制粘贴功能被调用时做一些操作,那么公式模块应该直接依赖下拉填充和复制粘贴模块,而不是通过 SheetInterceptorService 来实现。
### base-sheets
diff --git a/examples/univer-sheet-ts/esbuild.config.mjs b/examples/univer-sheet-ts/esbuild.config.mjs
index db06e1710b..5816108155 100644
--- a/examples/univer-sheet-ts/esbuild.config.mjs
+++ b/examples/univer-sheet-ts/esbuild.config.mjs
@@ -10,15 +10,32 @@ const nodeModules = path.resolve(process.cwd(), './node_modules');
const args = minimist(process.argv.slice(2));
-const ctx = await esbuild[args.watch ? 'context' : 'build']({
+// User should also config their bunlder to build monaco editor's resources for web worker.
+const monacoEditorEntryPoints = [
+ 'vs/language/typescript/ts.worker.js',
+ 'vs/editor/editor.worker.js',
+];
+
+const monacoBuildTask = () => esbuild.build({
+ entryPoints: monacoEditorEntryPoints.map(entry => `./node_modules/monaco-editor/esm/${entry}`),
bundle: true,
color: true,
- loader: { '.svg': 'file' },
- sourcemap: true,
+ format: 'iife',
+ outbase: './node_modules/monaco-editor/esm/',
+ outdir: './local',
plugins: [
cleanPlugin({
patterns: ['./local'],
}),
+ ]
+});
+
+const ctx = await esbuild[args.watch ? 'context' : 'build']({
+ bundle: true,
+ color: true,
+ loader: { '.svg': 'file', '.ttf': 'file' },
+ sourcemap: true,
+ plugins: [
copyPlugin({
assets: {
from: ['./public/*'],
@@ -43,6 +60,7 @@ const ctx = await esbuild[args.watch ? 'context' : 'build']({
});
if (args.watch) {
+ await monacoBuildTask();
await ctx.watch();
const { host, port } = await ctx.serve({
@@ -52,4 +70,4 @@ if (args.watch) {
const url = `http://localhost:${port}`;
console.log(`Local server: ${url}`);
-}
+}
\ No newline at end of file
diff --git a/examples/univer-sheet-ts/package.json b/examples/univer-sheet-ts/package.json
index 76dc962c5c..669f54979f 100644
--- a/examples/univer-sheet-ts/package.json
+++ b/examples/univer-sheet-ts/package.json
@@ -31,7 +31,9 @@
"@univerjs/sheets-plugin-numfmt": "workspace:*",
"@univerjs/ui-plugin-docs": "workspace:*",
"@univerjs/ui-plugin-sheets": "workspace:*",
- "@wendellhu/redi": "^0.12.11"
+ "@univerjs/uniscript": "workspace:*",
+ "@wendellhu/redi": "^0.12.11",
+ "monaco-editor": "0.44.0"
},
"devDependencies": {
"esbuild": "^0.19.7",
@@ -43,4 +45,4 @@
"ts-node": "^10.9.1",
"typescript": "^5.3.2"
}
-}
+}
\ No newline at end of file
diff --git a/examples/univer-sheet-ts/src/main.tsx b/examples/univer-sheet-ts/src/main.tsx
index 59543817e5..c21fc58797 100644
--- a/examples/univer-sheet-ts/src/main.tsx
+++ b/examples/univer-sheet-ts/src/main.tsx
@@ -11,6 +11,7 @@ import { FormulaPlugin } from '@univerjs/sheets-plugin-formula';
import { FormulaUIPlugin } from '@univerjs/sheets-plugin-formula-ui';
import { NumfmtPlugin } from '@univerjs/sheets-plugin-numfmt';
import { SheetUIPlugin } from '@univerjs/ui-plugin-sheets';
+import { IUniscriptPluginConfig, UniscriptPlugin } from '@univerjs/uniscript';
import { locales } from './locales';
import { DebuggerPlugin } from './sheets-plugin-debugger';
@@ -51,6 +52,16 @@ univer.registerPlugin(UniverRPCMainThreadPlugin, {
unsyncMutations: new Set([RichTextEditingMutation.id]),
} as IUniverRPCMainThreadPluginConfig);
+univer.registerPlugin(UniscriptPlugin, {
+ getWorkerUrl(moduleID, label) {
+ if (label === 'typescript' || label === 'javascript') {
+ return './vs/language/typescript/ts.worker.js';
+ }
+
+ return './vs/editor/editor.worker.js';
+ },
+} as IUniscriptPluginConfig);
+
// create univer sheet instance
univer.createUniverSheet(DEFAULT_WORKBOOK_DATA_DEMO);
diff --git a/packages/base-ui/package.json b/packages/base-ui/package.json
index 9a819b6905..d71edb8f5d 100644
--- a/packages/base-ui/package.json
+++ b/packages/base-ui/package.json
@@ -53,7 +53,7 @@
"@univerjs/base-render": "workspace:*",
"@univerjs/core": "workspace:*",
"@univerjs/design": "workspace:*",
- "@univerjs/icons": "^0.1.12",
+ "@univerjs/icons": "^0.1.14",
"@wendellhu/redi": "^0.12.11",
"clsx": "^2.0.0",
"localforage": "^1.10.0",
diff --git a/packages/base-ui/src/common/component-manager.ts b/packages/base-ui/src/common/component-manager.ts
index a4734712b6..2a3ff6b9ca 100644
--- a/packages/base-ui/src/common/component-manager.ts
+++ b/packages/base-ui/src/common/component-manager.ts
@@ -14,6 +14,8 @@ import {
CancelMergeSingle,
ClearFormat,
CntSingle,
+ CodeSingle,
+ ContentSingle16,
Copy,
DeleteCellMoveDown,
DeleteCellShiftLeft,
@@ -99,6 +101,7 @@ export class ComponentManager {
InnerBorder,
ItalicSingle,
KeyboardSingle,
+ ContentSingle16,
LeftBorder,
LeftJustifyingSingle,
LeftRotationFortyFiveDegreesSingle,
@@ -153,6 +156,7 @@ export class ComponentManager {
FreezeColumnSingle,
FreezeRowSingle,
FreezeToSelectedSingle,
+ CodeSingle,
};
for (const k in iconList) {
diff --git a/packages/design/package.json b/packages/design/package.json
index 02877558cb..f63bf3022d 100644
--- a/packages/design/package.json
+++ b/packages/design/package.json
@@ -51,7 +51,7 @@
"dependencies": {
"@rc-component/color-picker": "^1.4.1",
"@rc-component/trigger": "^1.18.2",
- "@univerjs/icons": "^0.1.12",
+ "@univerjs/icons": "^0.1.14",
"clsx": "^2.0.0",
"rc-dialog": "^9.3.4",
"rc-dropdown": "^4.1.0",
diff --git a/packages/sheets-plugin-formula-ui/package.json b/packages/sheets-plugin-formula-ui/package.json
index 19fcd5ffa9..a3881187f9 100644
--- a/packages/sheets-plugin-formula-ui/package.json
+++ b/packages/sheets-plugin-formula-ui/package.json
@@ -52,7 +52,7 @@
"@univerjs/base-ui": "workspace:*",
"@univerjs/core": "workspace:*",
"@univerjs/design": "workspace:*",
- "@univerjs/icons": "^0.1.12",
+ "@univerjs/icons": "^0.1.14",
"@univerjs/ui-plugin-sheets": "workspace:*",
"@wendellhu/redi": "^0.12.11",
"rxjs": "^7.8.1"
diff --git a/packages/sheets-plugin-numfmt/package.json b/packages/sheets-plugin-numfmt/package.json
index 0896967f2f..2126c97020 100644
--- a/packages/sheets-plugin-numfmt/package.json
+++ b/packages/sheets-plugin-numfmt/package.json
@@ -52,7 +52,7 @@
"@univerjs/base-sheets": "workspace:*",
"@univerjs/base-ui": "workspace:*",
"@univerjs/core": "workspace:*",
- "@univerjs/icons": "^0.1.12",
+ "@univerjs/icons": "^0.1.14",
"@univerjs/design": "workspace:*",
"@univerjs/ui-plugin-sheets": "workspace:*",
"@wendellhu/redi": "^0.12.11",
diff --git a/packages/ui-plugin-sheets/package.json b/packages/ui-plugin-sheets/package.json
index 769d7dee53..b1736622e7 100644
--- a/packages/ui-plugin-sheets/package.json
+++ b/packages/ui-plugin-sheets/package.json
@@ -57,7 +57,7 @@
"@univerjs/base-ui": "workspace:*",
"@univerjs/core": "workspace:*",
"@univerjs/design": "workspace:*",
- "@univerjs/icons": "^0.1.12",
+ "@univerjs/icons": "^0.1.14",
"@wendellhu/redi": "^0.12.11",
"clsx": "^2.0.0",
"rxjs": "^7.8.1"
diff --git a/packages/uniscript/README.md b/packages/uniscript/README.md
new file mode 100644
index 0000000000..9199a5e6f7
--- /dev/null
+++ b/packages/uniscript/README.md
@@ -0,0 +1,17 @@
+# @univerjs/uniscript
+
+## Introduction
+
+Univer uniscript package. This package add scripting feature to univer.
+
+## Usage
+
+### Installation
+
+```shell
+npm i @univerjs/uniscript
+```
+
+### API
+
+Check [Univer](https://github.com/dream-num/univer/)
diff --git a/packages/uniscript/package.json b/packages/uniscript/package.json
new file mode 100644
index 0000000000..c6d28c94c9
--- /dev/null
+++ b/packages/uniscript/package.json
@@ -0,0 +1,31 @@
+{
+ "name": "@univerjs/uniscript",
+ "private": true,
+ "author": "DreamNum Inc. <[email protected]>",
+ "main": "./src/index.ts",
+ "module": "./src/index.ts",
+ "dependencies": {
+ "@univerjs/core": "workspace:*",
+ "@univerjs/base-ui": "workspace:*",
+ "@univerjs/design": "workspace:*",
+ "@univerjs/base-sheets": "workspace:*",
+ "@univerjs/ui-plugin-sheets": "workspace:*",
+ "@wendellhu/redi": "^0.12.10",
+ "rxjs": "^7.8.1"
+ },
+ "devDependencies": {
+
+ "@types/react": "^18.2.38",
+ "@types/react-dom": "^18.2.15",
+ "@types/node": "^20.9.0",
+ "@types/numeral": "^2.0.5",
+ "@vitest/coverage-istanbul": "^0.34.6",
+ "esbuild": "^0.19.5",
+ "less": "^4.2.0",
+ "ts-node": "^10.9.1",
+ "vitest": "^0.34.6"
+ },
+ "peerDependencies": {
+ "monaco-editor": "0.44.0"
+ }
+}
\ No newline at end of file
diff --git a/packages/uniscript/src/commands/operations/panel.operation.ts b/packages/uniscript/src/commands/operations/panel.operation.ts
new file mode 100644
index 0000000000..0b1fadbbc1
--- /dev/null
+++ b/packages/uniscript/src/commands/operations/panel.operation.ts
@@ -0,0 +1,32 @@
+import { ISidebarService } from '@univerjs/base-ui';
+import { CommandType, IOperation } from '@univerjs/core';
+import { IAccessor } from '@wendellhu/redi';
+
+import { ScriptPanelService } from '../../services/script-panel.service';
+
+export const ScriptPanelComponentName = 'ScriptPanel';
+
+export const ToggleScriptPanelOperation: IOperation = {
+ type: CommandType.OPERATION,
+ id: 'univer.operation.toggle-sscript-panel',
+ handler: (accessor: IAccessor) => {
+ const scriptPanelService = accessor.get(ScriptPanelService);
+ const sidebarService = accessor.get(ISidebarService);
+
+ const isOpen = scriptPanelService.isOpen;
+
+ if (isOpen) {
+ scriptPanelService.close();
+ sidebarService.close();
+ } else {
+ scriptPanelService.open();
+ sidebarService.open({
+ header: { title: 'script-panel.title' },
+ children: { label: ScriptPanelComponentName },
+ width: 600,
+ });
+ }
+
+ return true;
+ },
+};
diff --git a/packages/uniscript/src/controllers/menu.ts b/packages/uniscript/src/controllers/menu.ts
new file mode 100644
index 0000000000..a347f1c652
--- /dev/null
+++ b/packages/uniscript/src/controllers/menu.ts
@@ -0,0 +1,14 @@
+import { IMenuButtonItem, MenuItemType, MenuPosition } from '@univerjs/base-ui';
+
+import { ToggleScriptPanelOperation } from '../commands/operations/panel.operation';
+
+export function UniscriptMenuItemFactory(): IMenuButtonItem {
+ return {
+ id: ToggleScriptPanelOperation.id,
+ title: 'toggle-script-panel',
+ tooltip: 'script-panel.tooltip.menu-button',
+ icon: 'CodeSingle',
+ type: MenuItemType.BUTTON,
+ positions: [MenuPosition.TOOLBAR_START],
+ };
+}
diff --git a/packages/uniscript/src/controllers/uniscript.controller.ts b/packages/uniscript/src/controllers/uniscript.controller.ts
new file mode 100644
index 0000000000..fbba554104
--- /dev/null
+++ b/packages/uniscript/src/controllers/uniscript.controller.ts
@@ -0,0 +1,22 @@
+import { ComponentManager, IMenuService } from '@univerjs/base-ui';
+import { Disposable, ICommandService, LifecycleStages, OnLifecycle } from '@univerjs/core';
+import { Inject } from '@wendellhu/redi';
+
+import { ScriptPanelComponentName, ToggleScriptPanelOperation } from '../commands/operations/panel.operation';
+import { ScriptEditorPanel } from '../views/components/ScriptEditorPanel';
+import { UniscriptMenuItemFactory } from './menu';
+
+@OnLifecycle(LifecycleStages.Steady, UniscriptController)
+export class UniscriptController extends Disposable {
+ constructor(
+ @IMenuService menuService: IMenuService,
+ @ICommandService commandService: ICommandService,
+ @Inject(ComponentManager) componentManager: ComponentManager
+ ) {
+ super();
+
+ this.disposeWithMe(menuService.addMenuItem(UniscriptMenuItemFactory()));
+ this.disposeWithMe(componentManager.register(ScriptPanelComponentName, ScriptEditorPanel));
+ this.disposeWithMe(commandService.registerCommand(ToggleScriptPanelOperation));
+ }
+}
diff --git a/packages/uniscript/src/facade/README.md b/packages/uniscript/src/facade/README.md
new file mode 100644
index 0000000000..1604f98bfc
--- /dev/null
+++ b/packages/uniscript/src/facade/README.md
@@ -0,0 +1,3 @@
+# Facade
+
+Facade works as an simplified interface for using Univer, especially via Uniscript.
\ No newline at end of file
diff --git a/packages/uniscript/src/facade/facade.ts b/packages/uniscript/src/facade/facade.ts
new file mode 100644
index 0000000000..033482210c
--- /dev/null
+++ b/packages/uniscript/src/facade/facade.ts
@@ -0,0 +1 @@
+export class FUniver {}
diff --git a/packages/uniscript/src/facade/sheet/range.ts b/packages/uniscript/src/facade/sheet/range.ts
new file mode 100644
index 0000000000..b9aa16567b
--- /dev/null
+++ b/packages/uniscript/src/facade/sheet/range.ts
@@ -0,0 +1 @@
+export class FRange {}
diff --git a/packages/uniscript/src/facade/sheet/sheet-facade.ts b/packages/uniscript/src/facade/sheet/sheet-facade.ts
new file mode 100644
index 0000000000..8caab8cbb7
--- /dev/null
+++ b/packages/uniscript/src/facade/sheet/sheet-facade.ts
@@ -0,0 +1 @@
+export class FSheet {}
diff --git a/packages/uniscript/src/facade/sheet/workbook.ts b/packages/uniscript/src/facade/sheet/workbook.ts
new file mode 100644
index 0000000000..416096e8a4
--- /dev/null
+++ b/packages/uniscript/src/facade/sheet/workbook.ts
@@ -0,0 +1 @@
+export class FWorkbook {}
diff --git a/packages/uniscript/src/facade/sheet/worksheet.ts b/packages/uniscript/src/facade/sheet/worksheet.ts
new file mode 100644
index 0000000000..4f8bce7170
--- /dev/null
+++ b/packages/uniscript/src/facade/sheet/worksheet.ts
@@ -0,0 +1 @@
+export class FWorksheet {}
diff --git a/packages/uniscript/src/index.ts b/packages/uniscript/src/index.ts
new file mode 100644
index 0000000000..81be4732e5
--- /dev/null
+++ b/packages/uniscript/src/index.ts
@@ -0,0 +1 @@
+export { type IUniscriptPluginConfig, UniscriptPlugin } from './plugin';
diff --git a/packages/uniscript/src/locale/en-US.ts b/packages/uniscript/src/locale/en-US.ts
new file mode 100644
index 0000000000..2e3e495243
--- /dev/null
+++ b/packages/uniscript/src/locale/en-US.ts
@@ -0,0 +1,11 @@
+export default {
+ 'script-panel': {
+ title: 'Uniscript',
+ tooltip: {
+ 'menu-button': 'Toggle Uniscript Panel',
+ },
+ panel: {
+ execute: 'Execute Script',
+ },
+ },
+};
diff --git a/packages/uniscript/src/locale/index.ts b/packages/uniscript/src/locale/index.ts
new file mode 100644
index 0000000000..b0b1d48d5d
--- /dev/null
+++ b/packages/uniscript/src/locale/index.ts
@@ -0,0 +1,2 @@
+export { default as enUS } from './en-US';
+export { default as zhCN } from './zh-CN';
diff --git a/packages/uniscript/src/locale/zh-CN.ts b/packages/uniscript/src/locale/zh-CN.ts
new file mode 100644
index 0000000000..ff8b4c5632
--- /dev/null
+++ b/packages/uniscript/src/locale/zh-CN.ts
@@ -0,0 +1 @@
+export default {};
diff --git a/packages/uniscript/src/models/model.ts b/packages/uniscript/src/models/model.ts
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/packages/uniscript/src/plugin.ts b/packages/uniscript/src/plugin.ts
new file mode 100644
index 0000000000..85bb08c2eb
--- /dev/null
+++ b/packages/uniscript/src/plugin.ts
@@ -0,0 +1,42 @@
+import { LocaleService, Plugin, PluginType } from '@univerjs/core';
+import { Dependency, Inject, Injector } from '@wendellhu/redi';
+
+import { UniscriptController } from './controllers/uniscript.controller';
+import { enUS } from './locale';
+import { IScriptEditorServiceConfig, ScriptEditorService } from './services/script-editor.service';
+import { UniscriptExecutionService } from './services/script-execution.service';
+import { ScriptPanelService } from './services/script-panel.service';
+
+const PLUGIN_NAME = 'uniscript';
+
+export interface IUniscriptPluginConfig extends IScriptEditorServiceConfig {}
+
+export class UniscriptPlugin extends Plugin {
+ static override type = PluginType.Univer;
+
+ constructor(
+ private readonly _config: IUniscriptPluginConfig,
+ @Inject(Injector) protected override _injector: Injector,
+ @Inject(LocaleService) private readonly _localeService: LocaleService
+ ) {
+ super(PLUGIN_NAME);
+ }
+
+ override onStarting(injector: Injector): void {
+ const dependencies: Dependency[] = [
+ // controllers
+ [UniscriptController],
+
+ // services
+ [ScriptEditorService, { useFactory: () => injector.createInstance(ScriptEditorService, this._config) }],
+ [ScriptPanelService],
+ [UniscriptExecutionService],
+ ];
+
+ dependencies.forEach((d) => injector.add(d));
+
+ this._localeService.load({
+ enUS,
+ });
+ }
+}
diff --git a/packages/uniscript/src/services/script-editor.service.ts b/packages/uniscript/src/services/script-editor.service.ts
new file mode 100644
index 0000000000..d93560b44c
--- /dev/null
+++ b/packages/uniscript/src/services/script-editor.service.ts
@@ -0,0 +1,22 @@
+import { Disposable } from '@univerjs/core';
+
+export interface IScriptEditorServiceConfig {
+ getWorkerUrl(moduleID: string, label: string): string;
+}
+
+/**
+ * This service is for loading monaco editor and its resources.
+ */
+export class ScriptEditorService extends Disposable {
+ constructor(private readonly _config: IScriptEditorServiceConfig) {
+ super();
+ }
+
+ requireVscodeEditor(): void {
+ if (!window.MonacoEnvironment) {
+ window.MonacoEnvironment = {
+ getWorkerUrl: this._config.getWorkerUrl,
+ };
+ }
+ }
+}
diff --git a/packages/uniscript/src/services/script-execution.service.ts b/packages/uniscript/src/services/script-execution.service.ts
new file mode 100644
index 0000000000..d5e85ec69a
--- /dev/null
+++ b/packages/uniscript/src/services/script-execution.service.ts
@@ -0,0 +1,10 @@
+import { Disposable } from '@univerjs/core';
+
+/**
+ * This service is to
+ */
+export class UniscriptExecutionService extends Disposable {
+ async execute(code: string): Promise<boolean> {
+ return true;
+ }
+}
diff --git a/packages/uniscript/src/services/script-panel.service.ts b/packages/uniscript/src/services/script-panel.service.ts
new file mode 100644
index 0000000000..c56ecaed5e
--- /dev/null
+++ b/packages/uniscript/src/services/script-panel.service.ts
@@ -0,0 +1,23 @@
+import { Disposable } from '@univerjs/core';
+import { BehaviorSubject, distinctUntilChanged } from 'rxjs';
+
+export class ScriptPanelService extends Disposable {
+ private _open$ = new BehaviorSubject<boolean>(false);
+ open$ = this._open$.pipe(distinctUntilChanged());
+ get isOpen(): boolean {
+ return this._open$.getValue();
+ }
+
+ override dispose(): void {
+ this._open$.next(false);
+ this._open$.complete();
+ }
+
+ open(): void {
+ this._open$.next(true);
+ }
+
+ close(): void {
+ this._open$.next(false);
+ }
+}
diff --git a/packages/uniscript/src/views/components/ScriptEditorPanel.tsx b/packages/uniscript/src/views/components/ScriptEditorPanel.tsx
new file mode 100644
index 0000000000..f41b08f0eb
--- /dev/null
+++ b/packages/uniscript/src/views/components/ScriptEditorPanel.tsx
@@ -0,0 +1,108 @@
+import { IMessageService, IShortcutService } from '@univerjs/base-ui';
+import { DisposableCollection, LocaleService, Nullable, toDisposable } from '@univerjs/core';
+import { Button, MessageType } from '@univerjs/design';
+import { IDisposable } from '@wendellhu/redi';
+import { useDependency } from '@wendellhu/redi/react-bindings';
+import { editor } from 'monaco-editor';
+import React, { useCallback, useEffect, useRef } from 'react';
+
+import { ScriptEditorService } from '../../services/script-editor.service';
+import { UniscriptExecutionService } from '../../services/script-execution.service';
+import styles from './index.module.less';
+
+export function ScriptEditorPanel() {
+ const editorContentRef = useRef<HTMLDivElement | null>(null);
+ const editorContainerRef = useRef<HTMLDivElement | null>(null);
+
+ const monacoEditorRef = useRef<editor.IStandaloneCodeEditor | null>(null);
+
+ const localeService = useDependency(LocaleService);
+ const scriptService = useDependency(UniscriptExecutionService);
+ const shortcutService = useDependency(IShortcutService);
+ const editorService = useDependency(ScriptEditorService);
+ const messageService = useDependency(IMessageService);
+
+ useEffect(() => {
+ const containerElement = editorContainerRef.current;
+ const contentElement = editorContentRef.current;
+
+ let disposableCollection: Nullable<DisposableCollection> = null;
+ let resizeObserver: Nullable<ResizeObserver> = null;
+
+ if (containerElement && contentElement) {
+ editorService.requireVscodeEditor();
+ const monacoEditor = (monacoEditorRef.current = editor.create(containerElement, {
+ value: '',
+ language: 'javascript',
+ }));
+
+ resizeObserver = new ResizeObserver(() => {
+ let timer: number | undefined = requestIdleCallback(() => {
+ if (!timer) return;
+
+ const { height, width } = contentElement.getBoundingClientRect();
+ monacoEditor.layout({ width, height });
+
+ timer = undefined;
+ });
+ });
+ resizeObserver.observe(contentElement);
+
+ let terminateEscaping: IDisposable | undefined;
+ disposableCollection = new DisposableCollection();
+ disposableCollection.add(
+ monacoEditor.onDidFocusEditorWidget(() => {
+ terminateEscaping = shortcutService.forceEscape();
+ })
+ );
+ disposableCollection.add(
+ monacoEditor.onDidBlurEditorWidget(() => {
+ terminateEscaping?.dispose();
+ terminateEscaping = undefined;
+ })
+ );
+ disposableCollection.add(toDisposable(() => terminateEscaping?.dispose()));
+ }
+
+ return () => {
+ if (resizeObserver && contentElement) {
+ resizeObserver.unobserve(contentElement);
+ }
+
+ disposableCollection?.dispose();
+ };
+ }, []);
+
+ const startExecution = useCallback(() => {
+ const model = monacoEditorRef.current?.getModel();
+ if (model) {
+ scriptService
+ .execute(model.getValue())
+ .then(() => {
+ messageService.show({
+ content: 'Execution completed',
+ type: MessageType.Success,
+ });
+ })
+ .catch(() => {
+ messageService.show({
+ content: 'Execution failed',
+ type: MessageType.Error,
+ });
+ });
+ }
+ }, [scriptService]);
+
+ return (
+ <div className={styles.scriptEditorPanel}>
+ <div className={styles.scriptEditorContent} ref={editorContentRef}>
+ <div className={styles.scriptEditorContainer} ref={editorContainerRef}></div>
+ </div>
+ <div className={styles.scriptEditorActions}>
+ <Button type="primary" size="small" onClick={startExecution}>
+ {localeService.t('script-panel.panel.execute')}
+ </Button>
+ </div>
+ </div>
+ );
+}
diff --git a/packages/uniscript/src/views/components/index.module.less b/packages/uniscript/src/views/components/index.module.less
new file mode 100644
index 0000000000..b787fbef86
--- /dev/null
+++ b/packages/uniscript/src/views/components/index.module.less
@@ -0,0 +1,11 @@
+.script-editor {
+ &-panel {
+ height: 100%;
+ }
+
+ &-content {
+ overflow: hidden;
+ width: 100%;
+ height: calc(100% - 80px);
+ }
+}
\ No newline at end of file
diff --git a/packages/uniscript/src/views/parts/render.part.ts b/packages/uniscript/src/views/parts/render.part.ts
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 7c523c2c2b..e9f6d6f508 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -249,9 +249,15 @@ importers:
'@univerjs/ui-plugin-sheets':
specifier: workspace:*
version: link:../../packages/ui-plugin-sheets
+ '@univerjs/uniscript':
+ specifier: workspace:*
+ version: link:../../packages/uniscript
'@wendellhu/redi':
specifier: ^0.12.11
version: 0.12.11
+ monaco-editor:
+ specifier: 0.44.0
+ version: 0.44.0
devDependencies:
esbuild:
specifier: ^0.19.7
@@ -543,8 +549,8 @@ importers:
specifier: workspace:*
version: link:../design
'@univerjs/icons':
- specifier: ^0.1.12
+ specifier: ^0.1.14
'@wendellhu/redi':
specifier: ^0.12.11
version: 0.12.11
@@ -706,8 +712,8 @@ importers:
specifier: ^1.18.2
'@univerjs/icons':
- specifier: ^0.1.12
+ specifier: ^0.1.14
clsx:
specifier: ^2.0.0
version: 2.0.0
@@ -927,8 +933,8 @@ importers:
specifier: workspace:*
version: link:../design
'@univerjs/icons':
- specifier: ^0.1.12
+ specifier: ^0.1.14
'@univerjs/ui-plugin-sheets':
specifier: workspace:*
version: link:../ui-plugin-sheets
@@ -1031,8 +1037,8 @@ importers:
specifier: workspace:*
version: link:../design
'@univerjs/icons':
- specifier: ^0.1.12
+ specifier: ^0.1.14
'@univerjs/ui-plugin-sheets':
specifier: workspace:*
version: link:../ui-plugin-sheets
@@ -1144,8 +1150,8 @@ importers:
specifier: workspace:*
version: link:../design
'@univerjs/icons':
- specifier: ^0.1.12
+ specifier: ^0.1.14
'@wendellhu/redi':
specifier: ^0.12.11
version: 0.12.11
@@ -1227,6 +1233,61 @@ importers:
specifier: ^0.34.6
+ packages/uniscript:
+ dependencies:
+ '@univerjs/base-sheets':
+ specifier: workspace:*
+ version: link:../base-sheets
+ '@univerjs/base-ui':
+ specifier: workspace:*
+ version: link:../base-ui
+ '@univerjs/core':
+ specifier: workspace:*
+ version: link:../core
+ '@univerjs/design':
+ specifier: workspace:*
+ version: link:../design
+ '@univerjs/ui-plugin-sheets':
+ specifier: workspace:*
+ version: link:../ui-plugin-sheets
+ '@wendellhu/redi':
+ specifier: ^0.12.10
+ version: 0.12.11
+ monaco-editor:
+ specifier: 0.44.0
+ version: 0.44.0
+ rxjs:
+ specifier: ^7.8.1
+ version: 7.8.1
+ devDependencies:
+ '@types/node':
+ specifier: ^20.9.0
+ version: 20.10.0
+ '@types/numeral':
+ specifier: ^2.0.5
+ version: 2.0.5
+ '@types/react':
+ specifier: ^18.2.38
+ version: 18.2.38
+ '@types/react-dom':
+ specifier: ^18.2.15
+ version: 18.2.17
+ '@vitest/coverage-istanbul':
+ specifier: ^0.34.6
+ version: 0.34.6([email protected])
+ esbuild:
+ specifier: ^0.19.5
+ version: 0.19.7
+ less:
+ specifier: ^4.2.0
+ version: 4.2.0
+ ts-node:
+ specifier: ^10.9.1
+ vitest:
+ specifier: ^0.34.6
+
packages:
/@aashutoshrathi/[email protected]:
@@ -5996,8 +6057,8 @@ packages:
resolution: {integrity: sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==}
dev: true
- resolution: {integrity: sha512-1EN0Ce85NIFQCycT/3BmLUzIdQKRz7nLYLyjb6IYXlLf7tlDp478GhqRiE7jVZNHsfziLt7VeKY7kIqdTixsaA==}
+ resolution: {integrity: sha512-FW1AOvZ+VnejiGiOiO9iaOAharpHG8x2ofpSRnlj1SN8+2daN+7b1dae6pKSqZzAJKFiQOyb1KGY95+ADYFGsw==}
peerDependencies:
react: '*'
react-dom: '*'
@@ -10306,6 +10367,10 @@ packages:
ufo: 1.3.2
dev: true
+ resolution: {integrity: sha512-5SmjNStN6bSuSE5WPT2ZV+iYn1/yI9sd4Igtk23ChvqB7kDk9lZbB9F5frsuvpB+2njdIeGGFf2G4gbE6rCC9Q==}
+ dev: false
+
resolution: {integrity: sha512-tzzskb3bG8LvYGFF/mDTpq3jpI6Q9wc3LEmBaghu+DdCssd1FakN7Bc0hVNmEyGq1bq3RgfkCb3cmQLpNPOroA==}
engines: {node: '>=4'}