Skip to content

Commit

Permalink
feat: add the file exchange function (#73)
Browse files Browse the repository at this point in the history
* feat: add the service & command of the exchange plugin

* feat: complete the basic import feature

* feat: complete the export feature of excel and usheet

* fix: remove unnecessary code

* feat: add the filter feature & update univer version to 0.1.8

* feat: revert the import feature

* feat: add the incremental update feature

* feat: add wasm inject

* chore: update readme

* fix: remove unnecessary files & fix the vite config & make the plugin support the mobile device

* fix: fix the exchange function bugs

* feat: complete the base function of exchange

* fix: enhance the import function, make it quickly as it can

* fix: fix the mutation observer function

* fix: optimize the file name of save function

* chore: remove uncessary css style and ts file

* fix: use univerInstanceService instead of mitt
  • Loading branch information
karlsbeard committed May 22, 2024
1 parent 436c15f commit 344c583
Show file tree
Hide file tree
Showing 27 changed files with 5,703 additions and 4,396 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ dist

.env

.npmrc
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@univerjs-pro:registry="https://verdaccio.univer.work/"
@univerjs:registry="https://verdaccio.univer.work/"
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ Univer supports the English and Chinese. Now you can choose the language in the

![img](assets/language.gif)

## Xlsx format support feature

Univer default supports xls、xlsm、xlsx in three formats to read and save. You can close this feature when you do not want to use it.

![img](assets/excel.gif)

## How to install and use

Text tutorial:
Expand Down
Binary file added assets/excel.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/language.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/use.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export default antfu({
rules: {
'no-new': 'off',
'ts/ban-ts-comment': 'off',
'unused-imports/no-unused-vars': 'off',
},
})
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"author": "DreamNum",
"authorUrl": "https://github.com/dream-num",
"fundingUrl": "https://opencollective.com/univer",
"isDesktopOnly": true
"isDesktopOnly": false
}
53 changes: 29 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,42 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@univerjs/core": "^0.1.7",
"@univerjs/data-validation": "^0.1.7",
"@univerjs/design": "^0.1.7",
"@univerjs/docs": "^0.1.7",
"@univerjs/docs-ui": "^0.1.7",
"@univerjs/engine-formula": "^0.1.7",
"@univerjs/engine-render": "^0.1.7",
"@univerjs/facade": "^0.1.7",
"@univerjs/find-replace": "^0.1.7",
"@univerjs/protocol": "^0.1.18",
"@univerjs/rpc": "^0.1.7",
"@univerjs/sheets": "^0.1.7",
"@univerjs/sheets-conditional-formatting-ui": "^0.1.7",
"@univerjs/sheets-data-validation": "^0.1.7",
"@univerjs/sheets-find-replace": "^0.1.7",
"@univerjs/sheets-formula": "^0.1.7",
"@univerjs/sheets-numfmt": "^0.1.7",
"@univerjs/sheets-ui": "^0.1.7",
"@univerjs/sheets-zen-editor": "^0.1.7",
"@univerjs/ui": "^0.1.7",
"@univerjs/core": "0.1.11",
"@univerjs/data-validation": "0.1.11",
"@univerjs/design": "0.1.11",
"@univerjs/docs": "0.1.11",
"@univerjs/docs-ui": "0.1.11",
"@univerjs/engine-formula": "0.1.11",
"@univerjs/engine-render": "0.1.11",
"@univerjs/facade": "0.1.11",
"@univerjs/find-replace": "0.1.11",
"@univerjs/icons": "^0.1.44",
"@univerjs/protocol": "^0.1.29",
"@univerjs/rpc": "0.1.11",
"@univerjs/sheets": "0.1.11",
"@univerjs/sheets-conditional-formatting-ui": "0.1.11",
"@univerjs/sheets-data-validation": "0.1.11",
"@univerjs/sheets-filter": "^0.1.11",
"@univerjs/sheets-find-replace": "0.1.11",
"@univerjs/sheets-formula": "0.1.11",
"@univerjs/sheets-numfmt": "0.1.11",
"@univerjs/sheets-ui": "0.1.11",
"@univerjs/sheets-zen-editor": "0.1.11",
"@univerjs/ui": "0.1.11",
"@wendellhu/redi": "^0.15.1",
"defu": "^6.1.4",
"jszip": "^3.10.1"
"jszip": "^3.10.1",
"rxjs": "^7.8.1",
"tslib": "^2.6.2"
},
"optionalDependencies": {
"@univerjs-pro/exchange-wasm": "^1.1.0"
},
"devDependencies": {
"@antfu/eslint-config": "^2.13.3",
"@release-it/conventional-changelog": "^8.0.1",
"@types/node": "^20.12.7",
"@univerjs/vite-plugin": "^0.3.2",
"@types/node": "^20.12.12",
"@univerjs/vite-plugin": "^0.3.3",
"builtin-modules": "^3.3.0",
"dotenv": "^16.4.5",
"eslint": "^8.57.0",
Expand All @@ -60,7 +65,7 @@
"release-it": "^17.2.0",
"simple-git-hooks": "^2.11.1",
"typescript": "^5.4.5",
"vite": "^5.2.8"
"vite": "^5.2.11"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged"
Expand Down
9,473 changes: 5,194 additions & 4,279 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import { injectWasm } from './utils/wasm'
import type { UniverPluginSettings } from '@/types/setting'

export type ViewType = typeof USheetType | typeof UDocType

export default class UniverPlugin extends Plugin {
settings: UniverPluginSettings

async onload() {
await this.loadSettings()
await injectWasm()
Expand All @@ -29,7 +27,6 @@ export default class UniverPlugin extends Plugin {

// add the setting tab
this.addSettingTab(new SettingTab(this.app, this))

// register view
this.registerView(USheetType, leaf => new USheetView(leaf, this.settings))
this.registerExtensions(['usheet'], USheetType)
Expand Down
42 changes: 42 additions & 0 deletions src/plugins/ExchangePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
LocaleService,

Plugin,
UniverInstanceType,
} from '@univerjs/core'

import type { Dependency } from '@wendellhu/redi'
import { Inject, Injector } from '@wendellhu/redi'
import { ExchangeController } from './controllers/exchange.controller'
import { ExchangeService, IExchangeService } from './services/exchange.service'
import zhCN from './locale/zh-CN'
import enUS from './locale/en-US'

export class ExchangePlugin extends Plugin {
static override type = UniverInstanceType.UNIVER_SHEET
static override pluginName = 'exchange-client'
constructor(
@Inject(Injector) override readonly _injector: Injector,
@Inject(LocaleService) private readonly _localeService: LocaleService,
) {
super()
}

initialize() {
this._localeService.load({
zhCN,
enUS,
})

const dependencies: Dependency[] = [
[ExchangeController],
[IExchangeService, { useClass: ExchangeService }],
] as Dependency[]

dependencies.forEach(dependency => this._injector.add(dependency))
}

override onReady(): void {
this.initialize()
}
}
47 changes: 47 additions & 0 deletions src/plugins/commands/exchange.operation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* 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 { ICommand } from '@univerjs/core'
import { CommandType } from '@univerjs/core'
import type { IAccessor } from '@wendellhu/redi'
import { getUploadXlsxFile } from '@/utils/file'
import { IExchangeService } from '@/plugins/services/exchange.service'

export const ExchangeClientUploadJsonOperation: ICommand = {
id: 'exchange-client.operation.upload-json',
type: CommandType.OPERATION,
handler: async (accessor: IAccessor) => {
const exchangeService = accessor.get(IExchangeService)

const file = await getUploadXlsxFile()

if (!file)
return false

exchangeService.uploadJson(file)
return true
},
}

export const ExchangeClientDownloadJsonOperation: ICommand = {
id: 'exchange-client.operation.download-json',
type: CommandType.OPERATION,
handler: async (accessor: IAccessor) => {
const exchangeService = accessor.get(IExchangeService)
exchangeService.downloadJson()
return true
},
}
38 changes: 38 additions & 0 deletions src/plugins/controllers/exchange.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Disposable, ICommandService, LifecycleStages, OnLifecycle } from '@univerjs/core'
import type { IMenuItemFactory } from '@univerjs/ui'
import { IMenuService } from '@univerjs/ui'
import { Inject, Injector } from '@wendellhu/redi'
import { ExchangeClientDownloadJsonOperation, ExchangeClientUploadJsonOperation } from '../commands/exchange.operation'
import { ExchangeDownloadJsonMenuItemFactory, ExchangeMenuItemFactory, ExchangeUploadJsonMenuItemFactory } from './menu'

@OnLifecycle(LifecycleStages.Steady, ExchangeController)
export class ExchangeController extends Disposable {
constructor(
@Inject(Injector) private readonly _injector: Injector,
@ICommandService private readonly _commandService: ICommandService,
@IMenuService private readonly _menuService: IMenuService,
) {
super()
this._initCommands()
this._initMenus()
}

private _initCommands() {
[
ExchangeClientUploadJsonOperation,
ExchangeClientDownloadJsonOperation,
].forEach((command) => {
this.disposeWithMe(this._commandService.registerCommand(command))
})
}

private _initMenus() {
([
ExchangeMenuItemFactory,
ExchangeUploadJsonMenuItemFactory,
ExchangeDownloadJsonMenuItemFactory,
] as IMenuItemFactory[]).forEach((factory) => {
this.disposeWithMe(this._menuService.addMenuItem(this._injector.invoke(factory)))
})
}
}
37 changes: 37 additions & 0 deletions src/plugins/controllers/menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { IMenuButtonItem, IMenuSelectorItem } from '@univerjs/ui'
import { MenuGroup, MenuItemType, MenuPosition } from '@univerjs/ui'
import type { IAccessor } from '@wendellhu/redi'
import { ExchangeClientDownloadJsonOperation, ExchangeClientUploadJsonOperation } from '@/plugins/commands/exchange.operation'

const EXCHANGE_OPERATION_ID = 'exchange-client.operation.exchange'

export function ExchangeMenuItemFactory(): IMenuSelectorItem<string> {
return {
id: EXCHANGE_OPERATION_ID,
group: MenuGroup.TOOLBAR_OTHERS,
type: MenuItemType.SUBITEMS,
icon: 'DirectExportSingle',
tooltip: 'exchange.file',
positions: [MenuPosition.TOOLBAR_START],
}
}

export function ExchangeUploadJsonMenuItemFactory(accessor: IAccessor): IMenuButtonItem<string> {
return {
id: ExchangeClientUploadJsonOperation.id,
type: MenuItemType.BUTTON,
title: 'exchange.uploadJson',
icon: 'FolderSingle',
positions: [EXCHANGE_OPERATION_ID],
}
}

export function ExchangeDownloadJsonMenuItemFactory(accessor: IAccessor): IMenuButtonItem<string> {
return {
id: ExchangeClientDownloadJsonOperation.id,
type: MenuItemType.BUTTON,
title: 'exchange.downloadJson',
icon: 'ExportSingle',
positions: [EXCHANGE_OPERATION_ID],
}
}
9 changes: 9 additions & 0 deletions src/plugins/locale/en-US.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
exchange: {
uploadJson: 'Open(File)',
downloadJson: 'Save As',
uploadError: 'Open Failed',
uploading: 'Uploading, please wait...',
file: 'File',
},
}
9 changes: 9 additions & 0 deletions src/plugins/locale/zh-CN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
exchage: {
uploadJson: '打开(文件)',
downloadJson: '保存到本地',
uploadError: '打开失败',
uploading: '正在上传,请稍后...',
file: '文件',
},
}
Loading

0 comments on commit 344c583

Please sign in to comment.