Skip to content

Commit

Permalink
fix: fix the xlsx View & make the plugin desktoponly (#55)
Browse files Browse the repository at this point in the history
* fix: fix the xlsx View & make the plugin desktoponly

* feat: add the xls & xlsm type
  • Loading branch information
karlsbeard authored Apr 16, 2024
1 parent 3f26159 commit 4d65d89
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 50 deletions.
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": false
"isDesktopOnly": true
}
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Type as UDocType, UDocView } from './views/udoc'
import { ChooseTypeModal } from './modals/chooseType'
import { SettingTab } from './modals/settingTab'
import { univerIconSvg } from './utils/common'
import { injectWasm } from './utils/wasm'
import type { UniverPluginSettings } from '@/types/setting'

export type ViewType = typeof USheetType | typeof UDocType
Expand All @@ -16,6 +17,7 @@ export default class UniverPlugin extends Plugin {

async onload() {
await this.loadSettings()
await injectWasm()

addIcon('univer', univerIconSvg)

Expand All @@ -36,7 +38,7 @@ export default class UniverPlugin extends Plugin {

this.registerExtensions(['usheet'], USheetType)
this.registerExtensions(['udoc'], UDocType)
this.registerExtensions(['xlsx'], XlsxType)
this.registerExtensions(['xlsx', 'xls', 'xlsm'], XlsxType)
}

async loadSettings() {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function transformSheetBlockMetaToBuffer(sheetBlocks: ISheetBlockJson): I
*/
export function transformSnapshotJsonToWorkbookData(snapshot: ISnapshotJson, sheetBlocks: ISheetBlockJson): Nullable<IWorkbookData> {
const snapshotData = transformSnapshotMetaToBuffer(snapshot)
if (!snapshotData)
if (!snapshotData || !sheetBlocks)
return null

const blocks = transformSheetBlockMetaToBuffer(sheetBlocks)
Expand Down
21 changes: 21 additions & 0 deletions src/utils/wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import exec from '@univerjs-pro/exchange-wasm/wasm_exec?raw'
import init from '@univerjs-pro/exchange-wasm/exchange.wasm?init'

export async function injectWasm() {
try {
// eslint-disable-next-line no-new-func
new Function(exec)()

// @ts-expect-error
const go = new window.Go()

init(go.importObject).then((instance) => {
go.run(instance)
}).catch((err) => {
console.error(err)
})
}
catch (err) {
console.error(err)
}
}
53 changes: 9 additions & 44 deletions src/views/xlsx.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,33 @@
import type { IWorkbookData, Univer, Workbook } from '@univerjs/core'
import type { IWorkbookData, Nullable, Univer, Workbook } from '@univerjs/core'
import type { WorkspaceLeaf } from 'obsidian'
import { TextFileView } from 'obsidian'
import { FUniver } from '@univerjs/facade'
import { UniverSheetsConditionalFormattingUIPlugin } from '@univerjs/sheets-conditional-formatting-ui'
import exec from '@univerjs-pro/exchange-wasm/wasm_exec?raw'
import init from '@univerjs-pro/exchange-wasm/exchange.wasm?init'
import type { UniverPluginSettings } from '@/types/setting'
import { sheetInit } from '@/univer/sheets'
import { transformSnapshotJsonToWorkbookData } from '@/utils/snapshot'

async function injectWasm() {
try {
// eslint-disable-next-line no-new-func
new Function(exec)()

// @ts-expect-error
const go = new window.Go()

init(go.importObject).then((instance) => {
go.run(instance)
}).catch((err) => {
console.error(err)
})
}
catch (err) {
console.error(err)
}
}

function readFile(file: ArrayBuffer): Promise<string> {
return new Promise((resolve) => {
const reader = new FileReader()
reader.onload = function () {
// @ts-expect-error
const result = window.univerProExchangeImport(reader.result)
resolve(result)
}

reader.readAsArrayBuffer(new Blob([file]))
})
}

export const Type = 'univer-xlsx'

export class XlsxTypeView extends TextFileView {
rootContainer: HTMLDivElement
univer: Univer
workbook: Workbook
FUniver: FUniver
sheetData: IWorkbookData | object
settings: UniverPluginSettings
workbookData: Nullable<IWorkbookData>

constructor(leaf: WorkspaceLeaf, settings: UniverPluginSettings) {
super(leaf)
this.settings = settings
}

getViewData(): string {
return JSON.stringify(this.workbook.save())
// return JSON.stringify(this.workbook.save())
return ''
}

async setViewData(_data: string, _: boolean) {
await injectWasm()

this.domInit()
this.univer?.dispose()
this.workbook?.dispose()
Expand All @@ -82,13 +47,13 @@ export class XlsxTypeView extends TextFileView {
this.univer.registerPlugin(UniverSheetsConditionalFormattingUIPlugin)

const raw = await this.app.vault.readBinary(this.file)
const result = await readFile(raw)
// @ts-expect-error
const transformData = await window.univerProExchangeImport(raw)
const json = JSON.parse(transformData)

const json = JSON.parse(result)
const workbookData = transformSnapshotJsonToWorkbookData(json.snapshot, json.sheetBlocks)
this.workbookData = transformSnapshotJsonToWorkbookData(json.snapshot, json.sheetBlocks)

if (workbookData)
this.workbook = this.univer.createUniverSheet(workbookData)
this.workbook = this.univer.createUniverSheet(this.workbookData ?? {})

// this.FUniver.onCommandExecuted(() => {
// this.requestSave()
Expand Down
4 changes: 2 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { copyFile, rename, writeFile } from 'node:fs/promises'
import { join, resolve } from 'node:path'
import process from 'node:process'
import { existsSync, readdirSync } from 'node:fs'
import { existsSync } from 'node:fs'
import { defineConfig } from 'vite'
import { univerPlugin } from '@univerjs/vite-plugin'
import builtins from 'builtin-modules'
Expand All @@ -27,7 +27,7 @@ function generate(isDev?: boolean) {
author: pkg.author,
authorUrl: 'https://github.com/dream-num',
fundingUrl: 'https://opencollective.com/univer',
isDesktopOnly: false,
isDesktopOnly: true,
}, null, 2)}\n`)
await copyFile(resolve(buildDir, 'manifest.json'), join(process.cwd(), 'manifest.json'))
rename(resolve(buildDir, 'style.css'), resolve(buildDir, 'styles.css'))
Expand Down

0 comments on commit 4d65d89

Please sign in to comment.