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(example): add doc uniscript example #1495

Merged
merged 6 commits into from
Mar 6, 2024
Merged
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
8 changes: 6 additions & 2 deletions examples/esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ const ctx = await esbuild[args.watch ? 'context' : 'build']({
// sheets-multi
'./src/sheets-multi/main.tsx',

// sheets-uniscript
'./src/sheets-uniscript/main.ts',

// docs
'./src/docs/main.ts',

// docs-uniscript
'./src/docs-uniscript/main.ts',

// slides
'./src/slides/main.ts',

// uniscript
'./src/uniscript/main.ts',
],
outdir: './local',

Expand Down
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@univerjs/docs-ui": "workspace:*",
"@univerjs/engine-formula": "workspace:*",
"@univerjs/engine-render": "workspace:*",
"@univerjs/facade": "workspace:*",
"@univerjs/find-replace": "workspace:*",
"@univerjs/icons": "^0.1.30",
"@univerjs/rpc": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta content="width=device-width, initial-scale=1" name="viewport" />
<title>Uniscript</title>
<title>Univer Docs Uniscript</title>

<link rel="icon" type="image/x-icon" href="../favicon.svg" />
<link rel="stylesheet" href="./main.css" />
Expand Down
2 changes: 1 addition & 1 deletion examples/public/sheets-multi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta content="width=device-width, initial-scale=1" name="viewport" />
<title>Univer Sheet</title>
<title>Univer Sheets Multi Instance</title>

<link rel="icon" type="image/x-icon" href="../favicon.svg" />
<link rel="stylesheet" href="./main.css" />
Expand Down
31 changes: 31 additions & 0 deletions examples/public/sheets-uniscript/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta content="width=device-width, initial-scale=1" name="viewport" />
<title>Univer Sheets Uniscript</title>

<link rel="icon" type="image/x-icon" href="../favicon.svg" />
<link rel="stylesheet" href="./main.css" />
<style>
html,
body {
height: 100%;
margin: 0;
}
</style>

<script>
new EventSource('/esbuild').addEventListener('change', () => {
console.info('reload--');
location.reload();
});
</script>
</head>

<body style="overflow: hidden">
<div id="app" style="height: 100%"></div>

<script src="./main.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion examples/public/sheets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta content="width=device-width, initial-scale=1" name="viewport" />
<title>Univer Sheet</title>
<title>Univer Sheets</title>

<link rel="icon" type="image/x-icon" href="../favicon.svg" />
<link rel="stylesheet" href="./main.css" />
Expand Down
74 changes: 74 additions & 0 deletions examples/src/docs-uniscript/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* 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.
*/

import { LocaleType, LogLevel, Univer } from '@univerjs/core';
import { defaultTheme } from '@univerjs/design';
import { UniverDocsPlugin } from '@univerjs/docs';
import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { UniverUIPlugin } from '@univerjs/ui';
import { UniverUniscriptPlugin } from '@univerjs/uniscript';

import { UniverSheetsUIPlugin } from '@univerjs/sheets-ui';
import { UniverSheetsPlugin } from '@univerjs/sheets';
import { DEFAULT_DOCUMENT_DATA_CN } from '../data';

// univer
const univer = new Univer({
theme: defaultTheme,
locale: LocaleType.ZH_CN,
logLevel: LogLevel.VERBOSE,
});

// core plugins

univer.registerPlugin(UniverRenderEnginePlugin);
univer.registerPlugin(UniverFormulaEnginePlugin);
univer.registerPlugin(UniverUIPlugin, {
container: 'app',
header: true,
toolbar: true,
});

univer.registerPlugin(UniverDocsPlugin, {
standalone: true,
});
univer.registerPlugin(UniverDocsUIPlugin);

univer.registerPlugin(UniverSheetsPlugin);
univer.registerPlugin(UniverSheetsUIPlugin);

univer.registerPlugin(UniverUniscriptPlugin, {
getWorkerUrl(moduleID: string, label: string) {
if (label === 'typescript' || label === 'javascript') {
return './vs/language/typescript/ts.worker.js';
}

return './vs/editor/editor.worker.js';
},
});

// create univer doc instance
univer.createUniverDoc(DEFAULT_DOCUMENT_DATA_CN);

declare global {
interface Window {

Check warning on line 69 in examples/src/docs-uniscript/main.ts

View workflow job for this annotation

GitHub Actions / eslint

Interface name `Window` must match the RegExp: /^I[A-Z0-9]/u
univer?: Univer;
}
}

window.univer = univer;
11 changes: 8 additions & 3 deletions examples/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
BUILD_TIME: process.env.BUILD_TIME,
});

function Examples() {

Check warning on line 35 in examples/src/main.tsx

View workflow job for this annotation

GitHub Actions / eslint

Fast refresh only works when a file has exports. Move your component(s) to a separate file
return (
<section className={styles.examples}>
<a className={styles.btn} href="./sheets/">
Expand All @@ -51,12 +51,17 @@
{' '}
</a>
<a className={styles.btn} href="./sheets-multi/">
<span> Univer Multi Instance</span>
<span> Univer Sheets Multi Instance</span>
<div className={styles.btnBg}>→</div>
{' '}
</a>
<a className={styles.btn} href="./uniscript/">
<span> Uniscript</span>
<a className={styles.btn} href="./sheets-uniscript/">
<span> Univer Sheets Uniscript</span>
<div className={styles.btnBg}>→</div>
{' '}
</a>
<a className={styles.btn} href="./docs-uniscript/">
<span> Univer Docs Uniscript</span>
<div className={styles.btnBg}>→</div>
{' '}
</a>
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions packages/facade/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
},
"peerDependencies": {
"@univerjs/core": "workspace:*",
"@univerjs/docs": "workspace:*",
"@univerjs/engine-render": "workspace:*",
"@univerjs/network": "workspace:*",
"@univerjs/sheets": "workspace:*",
"@univerjs/sheets-formula": "workspace:*",
Expand All @@ -72,7 +74,9 @@
"dependencies": {},
"devDependencies": {
"@univerjs/core": "workspace:*",
"@univerjs/docs": "workspace:*",
"@univerjs/engine-formula": "workspace:*",
"@univerjs/engine-render": "workspace:*",
"@univerjs/network": "workspace:*",
"@univerjs/shared": "workspace:*",
"@univerjs/sheets": "workspace:*",
Expand Down
111 changes: 111 additions & 0 deletions packages/facade/src/apis/docs/__tests__/create-test-bed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* 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.
*/

import type { DocumentDataModel, IDocumentData } from '@univerjs/core';
import {
ILogService,
IUniverInstanceService,
LocaleService,
LogLevel,
Plugin,
PluginType,
Univer,
} from '@univerjs/core';
import {
enUS,
zhCN,
} from '@univerjs/sheets-formula';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';

import { DocStateChangeManagerService, DocViewModelManagerService, IMEInputManagerService, TextSelectionManagerService } from '@univerjs/docs';

import { ITextSelectionRenderManager, TextSelectionRenderManager } from '@univerjs/engine-render';
import { FUniver } from '../../facade';

function getTestDocumentDataDemo(): IDocumentData {
return {
id: 'test',
body: {
dataStream: 'Hello,\r\n',
},
documentStyle: {
pageSize: {
width: 594.3,
height: 840.51,
},
marginTop: 72,
marginBottom: 72,
marginRight: 90,
marginLeft: 90,
},
};
}

export interface ITestBed {
univer: Univer;
get: Injector['get'];
doc: DocumentDataModel;
univerAPI: FUniver;
}

export function createTestBed(documentConfig?: IDocumentData, dependencies?: Dependency[]): ITestBed {
const univer = new Univer();
const injector = univer.__getInjector();

class TestSpyPlugin extends Plugin {
static override type = PluginType.Univer;

constructor(
_config: undefined,
@Inject(Injector) override readonly _injector: Injector
) {
super('test-plugin');

this._injector = _injector;
}

override onStarting(injector: Injector): void {
injector.add([TextSelectionManagerService]);
injector.add([DocViewModelManagerService]);
injector.add([DocStateChangeManagerService]);
injector.add([IMEInputManagerService]);
injector.add([ITextSelectionRenderManager, { useClass: TextSelectionRenderManager }]);

dependencies?.forEach((d) => injector.add(d));
}
}

injector.get(LocaleService).load({ zhCN, enUS });

univer.registerPlugin(TestSpyPlugin);
const doc = univer.createUniverDoc(documentConfig || getTestDocumentDataDemo());

const univerInstanceService = injector.get(IUniverInstanceService);
univerInstanceService.focusUniverInstance('test');
const logService = injector.get(ILogService);

logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs

const univerAPI = FUniver.newAPI(injector);

return {
univer,
get: injector.get.bind(injector),
doc,
univerAPI,
};
}
49 changes: 49 additions & 0 deletions packages/facade/src/apis/docs/__tests__/f-document.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* 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.
*/

import { ICommandService } from '@univerjs/core';
import type { Injector } from '@wendellhu/redi';
import { beforeEach, describe, expect, it } from 'vitest';

import { InsertCommand, RichTextEditingMutation } from '@univerjs/docs';
import type { FUniver } from '../../facade';
import { createTestBed } from './create-test-bed';

describe('Test FDocument', () => {
let get: Injector['get'];
let commandService: ICommandService;
let univerAPI: FUniver;

beforeEach(() => {
const testBed = createTestBed();
get = testBed.get;
univerAPI = testBed.univerAPI;

commandService = get(ICommandService);
commandService.registerCommand(InsertCommand);
commandService.registerCommand(RichTextEditingMutation as any);
});

it('Document appendText', async () => {
const activeDoc = univerAPI.getActiveDocument();

expect(await activeDoc?.appendText('Univer')).toBeTruthy();

const dataStream = activeDoc?.getSnapshot().body?.dataStream;

expect(dataStream?.substring(0, dataStream.length - 2)).toEqual('Hello,Univer');
});
});
Loading
Loading