Skip to content

Commit

Permalink
feat: add the new excel feature (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
karlsbeard authored Apr 23, 2024
1 parent 137f7db commit 436c15f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
21 changes: 17 additions & 4 deletions src/modals/chooseType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface ModalText {
title: string
docBtn: string
sheetBtn: string
excelBtn: string
}

export class ChooseTypeModal extends Modal {
Expand All @@ -33,6 +34,11 @@ export class ChooseTypeModal extends Modal {
cls: 'univer-mdal-btn',
})

const excelBtn = btnContainer.createEl('button', {
text: 'Excel',
cls: 'univer-mdal-btn',
})

docBtn.onclick = () => {
createNewFile(this.app, 'udoc')
this.close()
Expand All @@ -42,6 +48,11 @@ export class ChooseTypeModal extends Modal {
createNewFile(this.app, 'usheet')
this.close()
}

excelBtn.onclick = () => {
createNewFile(this.app, 'xlsx')
this.close()
}
}

onClose(): void {
Expand All @@ -52,15 +63,17 @@ export class ChooseTypeModal extends Modal {
if (this.settings.language === 'EN') {
return {
title: 'Please choose the type of file you want to create',
docBtn: 'univer doc',
sheetBtn: 'univer sheet',
docBtn: 'Univer Doc',
sheetBtn: 'Univer Sheet',
excelBtn: 'Excel',
}
}
else {
return {
title: '请选择您要创建的文件类型',
docBtn: 'univer 文档',
sheetBtn: 'univer 表格',
docBtn: 'Univer 文档',
sheetBtn: 'Univer 表格',
excelBtn: 'Excel',
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import JSZip from 'jszip'
import { type App, Notice } from 'obsidian'
import { Type as DocType } from '@/views/udoc'
import { Type as SheetType } from '@/views/usheet'
import { Type as XlsxType } from '@/views/xlsx'

export async function createNewFile(app: App, suffix: string, folderPath?: string, fileNum?: number): Promise<void> {
if (folderPath) {
Expand All @@ -17,7 +18,7 @@ export async function createNewFile(app: App, suffix: string, folderPath?: strin
try {
await app.vault.create(filePath, '')
await app.workspace.getLeaf(true).setViewState({
type: suffix === 'udoc' ? DocType : SheetType,
type: getFileType(suffix),
active: true,
state: {
file: filePath,
Expand Down Expand Up @@ -65,3 +66,14 @@ function readFileHandler(file: Blob) {
reader.readAsArrayBuffer(file)
})
}

function getFileType(suffix: string) {
switch (suffix) {
case 'udoc':
return DocType
case 'xlsx':
return XlsxType
default:
return SheetType
}
}
18 changes: 9 additions & 9 deletions src/views/xlsx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IWorkbookData, Univer, Workbook } from '@univerjs/core'
import type { IWorkbookData, Nullable, Univer, Workbook } from '@univerjs/core'
import type { TFile, WorkspaceLeaf } from 'obsidian'
import { TextFileView } from 'obsidian'
import { FUniver } from '@univerjs/facade'
Expand All @@ -13,8 +13,8 @@ export class XlsxTypeView extends TextFileView {
rootContainer: HTMLDivElement
univer: Univer
workbook: Workbook
workbookData: Nullable<IWorkbookData>
FUniver: FUniver
sheetData: IWorkbookData | object
settings: UniverPluginSettings
legacyFile: TFile

Expand Down Expand Up @@ -55,12 +55,14 @@ export class XlsxTypeView extends TextFileView {
this.univer.registerPlugin(UniverSheetsConditionalFormattingUIPlugin)

const raw = await this.app.vault.readBinary(this.legacyFile)
// @ts-expect-error
const transformData = await window.univerProExchangeImport(raw)
const jsonData = JSON.parse(transformData)
let workbookData = transformSnapshotJsonToWorkbookData(jsonData.snapshot, jsonData.sheetBlocks)
if (raw.byteLength !== 0) {
// @ts-expect-error
const transformData = await window.univerProExchangeImport(raw)
const jsonData = JSON.parse(transformData)
this.workbookData = transformSnapshotJsonToWorkbookData(jsonData.snapshot, jsonData.sheetBlocks)
}

workbookData = workbookData || {} as IWorkbookData
const workbookData = this.workbookData || {} as IWorkbookData

if (workbookData.sheets) {
const sheets = workbookData.sheets
Expand All @@ -73,7 +75,6 @@ export class XlsxTypeView extends TextFileView {
sheet.rowCount = Math.max(99, sheet.rowCount)
})
}

this.workbook = this.univer.createUniverSheet(workbookData)
}

Expand Down Expand Up @@ -103,7 +104,6 @@ export class XlsxTypeView extends TextFileView {
async saveToExcel(file: TFile, workbook: Workbook) {
if (!file || !workbook)
return

const saveWorkbookData = workbook.save()
const snapshotJSON = await transformWorkbookDataToSnapshotJson(saveWorkbookData)
const snapshot = JSON.stringify(snapshotJSON)
Expand Down

0 comments on commit 436c15f

Please sign in to comment.