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.

perf: refactor local file service (#3479)

Wenzhao Hu23e47c2a6839
diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.ts
index 9820ea6244..dde7696cf1 100644
--- a/packages/ui/src/index.ts
+++ b/packages/ui/src/index.ts
@@ -66,7 +66,8 @@ export { Editor, EditorService, IEditorService } from './services/editor/editor.
export { DesktopGlobalZoneService } from './services/global-zone/desktop-global-zone.service';
export { IGlobalZoneService } from './services/global-zone/global-zone.service';
export { DesktopLayoutService, ILayoutService } from './services/layout/layout.service';
-export { ILocalFileService, type IOpenFileOptions } from './services/local-file/file-opener.service';
+export { DesktopLocalFileService } from './services/local-file/desktop-local-file.service';
+export { ILocalFileService, type IOpenFileOptions } from './services/local-file/local-file.service';
export { DesktopLocalStorageService } from './services/local-storage/local-storage.service';
export {
type ICustomComponentProps,
diff --git a/packages/ui/src/services/local-file/desktop-file-opener.service.tsx b/packages/ui/src/services/local-file/desktop-file-opener.service.tsx
deleted file mode 100644
index 4c57260bb5..0000000000
--- a/packages/ui/src/services/local-file/desktop-file-opener.service.tsx
+++ /dev/null
@@ -1,104 +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.
- */
-
-/* eslint-disable react-refresh/only-export-components */
-
-import { connectInjector, Disposable, Inject, Injector, useDependency, useObservable } from '@univerjs/core';
-
-import React, { useCallback, useEffect, useRef } from 'react';
-import { Subject } from 'rxjs';
-import { BuiltInUIPart, IUIPartsService } from '../parts/parts.service';
-import { ILocalFileService } from './file-opener.service';
-import type { IOpenFileOptions } from './file-opener.service';
-
-interface IOpenFileRequest extends IOpenFileOptions {
- onFileSelected: (files: File[]) => void;
-}
-
-export class DesktopLocalFileService extends Disposable implements ILocalFileService {
- private readonly _fileOpenRequest$ = new Subject<IOpenFileRequest | null>();
- readonly fileOpenRequest$ = this._fileOpenRequest$.asObservable();
-
- constructor(
- @Inject(Injector) injector: Injector,
- @IUIPartsService private readonly _uiPartsService: IUIPartsService
- ) {
- super();
-
- this.disposeWithMe(this._uiPartsService.registerComponent(
- BuiltInUIPart.GLOBAL, () => connectInjector(DesktopFileOpenerWrapper, injector)));
- }
-
- openFile(options?: IOpenFileOptions): Promise<File[]> {
- // eslint-disable-next-line ts/no-this-alias
- const service = this;
-
- return new Promise((resolve) => {
- const request: IOpenFileRequest = {
- ...options,
- onFileSelected(files) {
- resolve(files);
- service._fileOpenRequest$.next(null);
- },
- };
-
- this._fileOpenRequest$.next(request);
- });
- }
-
- downloadFile(data: Blob, fileName: string): void {
- const a = document.createElement('a');
- a.download = fileName;
- a.href = window.URL.createObjectURL(data);
- a.click();
- }
-}
-
-function DesktopFileOpener(props: { request: IOpenFileRequest }) {
- const { request } = props;
- const { accept, onFileSelected, multiple } = request;
- const fileInputRef = useRef<HTMLInputElement>(null);
-
- const handleFileChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
- const fileList = event.target.files;
- onFileSelected(fileList ? Array.from(fileList) : []);
-
- if (fileInputRef.current) fileInputRef.current.value = '';
- }, [onFileSelected]);
-
- useEffect(() => {
- if (fileInputRef.current) fileInputRef.current.click();
- }, []);
-
- return (
- <input
- type="file"
- ref={fileInputRef}
- onChange={handleFileChange}
- accept={accept}
- multiple={multiple}
- />
- );
-}
-
-function DesktopFileOpenerWrapper() {
- const DesktopLocalFileService = useDependency(ILocalFileService) as DesktopLocalFileService;
- const request = useObservable(DesktopLocalFileService.fileOpenRequest$, null);
-
- if (!request) return null;
- return <DesktopFileOpener request={request} />;
-}
-
diff --git a/packages/ui/src/services/local-file/desktop-local-file.service.ts b/packages/ui/src/services/local-file/desktop-local-file.service.ts
new file mode 100644
index 0000000000..60d684c233
--- /dev/null
+++ b/packages/ui/src/services/local-file/desktop-local-file.service.ts
@@ -0,0 +1,44 @@
+/**
+ * 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 { Disposable } from '@univerjs/core';
+
+import type { ILocalFileService, IOpenFileOptions } from './local-file.service';
+
+export class DesktopLocalFileService extends Disposable implements ILocalFileService {
+ openFile(options?: IOpenFileOptions): Promise<File[]> {
+ return new Promise((resolve) => {
+ const inputElement = document.createElement('input');
+ inputElement.type = 'file';
+ inputElement.accept = options?.accept ?? '';
+ inputElement.multiple = options?.multiple ?? false;
+ inputElement.onchange = (event) => {
+ const fileList = (event.target as HTMLInputElement).files;
+ if (fileList) {
+ resolve(Array.from(fileList));
+ }
+ };
+ inputElement.click();
+ });
+ }
+
+ downloadFile(data: Blob, fileName: string): void {
+ const a = document.createElement('a');
+ a.download = fileName;
+ a.href = window.URL.createObjectURL(data);
+ a.click();
+ }
+}
diff --git a/packages/ui/src/services/local-file/file-opener.service.ts b/packages/ui/src/services/local-file/local-file.service.ts
similarity index 99%
rename from packages/ui/src/services/local-file/file-opener.service.ts
rename to packages/ui/src/services/local-file/local-file.service.ts
index 90cad81813..b985e26d94 100644
--- a/packages/ui/src/services/local-file/file-opener.service.ts
+++ b/packages/ui/src/services/local-file/local-file.service.ts
@@ -21,13 +21,11 @@ export interface IOpenFileOptions {
multiple?: boolean;
}
-export interface ILocalFileService {
- openFile(options?: IOpenFileOptions): Promise<File[]>;
- downloadFile(data: Blob, fileName: string): void;
-}
-
/**
* This service is used to upload files.
*/
export const ILocalFileService = createIdentifier<ILocalFileService>('univer-ui.local-file.service');
-
+export interface ILocalFileService {
+ openFile(options?: IOpenFileOptions): Promise<File[]>;
+ downloadFile(data: Blob, fileName: string): void;
+}
diff --git a/packages/ui/src/ui-plugin.ts b/packages/ui/src/ui-plugin.ts
index 08a324ef72..9c9235edf3 100644
--- a/packages/ui/src/ui-plugin.ts
+++ b/packages/ui/src/ui-plugin.ts
@@ -38,8 +38,8 @@ import { EditorService, IEditorService } from './services/editor/editor.service'
import { DesktopGlobalZoneService } from './services/global-zone/desktop-global-zone.service';
import { IGlobalZoneService } from './services/global-zone/global-zone.service';
import { DesktopLayoutService, ILayoutService } from './services/layout/layout.service';
-import { DesktopLocalFileService } from './services/local-file/desktop-file-opener.service';
-import { ILocalFileService } from './services/local-file/file-opener.service';
+import { DesktopLocalFileService } from './services/local-file/desktop-local-file.service';
+import { ILocalFileService } from './services/local-file/local-file.service';
import { DesktopLocalStorageService } from './services/local-storage/local-storage.service';
import { IMenuService, MenuService } from './services/menu/menu.service';
import { IMenuManagerService, MenuManagerService } from './services/menu/menu-manager.service';