-
-
Notifications
You must be signed in to change notification settings - Fork 665
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(example): add doc uniscript example (#1495)
* feat(example): add doc uniscript demo * feat(example): use univer api in doc uniscript * feat(facade): add appendText api * test(facade): document appendText test * fix(facade): remove unused comment * fix(facade): remove unused mock file
- Loading branch information
Showing
29 changed files
with
466 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
univer?: Univer; | ||
} | ||
} | ||
|
||
window.univer = univer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
packages/facade/src/apis/docs/__tests__/create-test-bed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
packages/facade/src/apis/docs/__tests__/f-document.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
Oops, something went wrong.