Skip to content

Commit

Permalink
feat:complete the new tab function
Browse files Browse the repository at this point in the history
  • Loading branch information
karlsbeard committed Mar 22, 2024
1 parent 45e56dc commit ad11046
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 91 deletions.
11 changes: 4 additions & 7 deletions src/modals/chooseType.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { App } from 'obsidian'
import { Modal } from 'obsidian'
import { createNewFile } from '~/utils/createFile'
import { Type as USheetType } from '~/views/usheet'
import { Type as UDocType } from '~/views/udoc'

export class ChooseTypeModal extends Modal {
constructor(app: App) {
Expand All @@ -20,18 +18,17 @@ export class ChooseTypeModal extends Modal {
const sheetBtn = btnContainer.createEl('button', { text: '表格', cls: 'univer-mdal-btn' })

docBtn.onclick = () => {
createNewFile(this.app, 'udoc', UDocType)
this.onClose()
createNewFile(this.app, 'udoc')
this.close()
}

sheetBtn.onclick = () => {
createNewFile(this.app, 'usheet', USheetType)
this.onClose()
createNewFile(this.app, 'usheet')
this.close()
}
}

onClose(): void {
this.contentEl.empty()
this.close()
}
}
1 change: 1 addition & 0 deletions src/style/univer.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
--ctx_menu_y: 0px;
}


.uproduct-container {
margin: 0;
padding: 0;
Expand Down
34 changes: 7 additions & 27 deletions src/utils/createFile.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,34 @@
import { Notice, type App } from 'obsidian'
import { ViewType } from '~/main'
import { Type as DocType } from '~/views/udoc';
import { Type as SheetType } from '~/views/usheet';

export async function createNewFile(app: App, suffix: string, type: ViewType, folderPath?: string, fileNum?: number): Promise<void> {
console.log("createNewFile------------", folderPath, fileNum)
export async function createNewFile(app: App, suffix: string, folderPath?: string, fileNum?: number): Promise<void> {
if (folderPath) {
try {
await app.vault.createFolder(folderPath)
}
catch (err) {
console.log("issue in making folder");
console.log(err);
console.error(err);
}
}
console.log('11111111111111111')
const fileName = `Untitled${fileNum !== undefined ? `-${fileNum}` : ''}.${suffix}`
const filePath = folderPath !== undefined ? `${folderPath}/${fileName}` : fileName
console.log('22222222222222')
try {
await app.vault.create(filePath, '').then(() => {
console.log('file created')

}).catch((err) => {
console.log('file not created')
})
console.log('333333333333333')
await app.vault.create(filePath, '')
await app.workspace.getLeaf(true).setViewState({
type: type,
type: suffix === 'udoc' ? DocType : SheetType,
active: true,
state: {
file: filePath
}
})
console.log('444444444444444')
new Notice(`Created new ${suffix} file: ${filePath}`)
}
catch (err) {
const error = err
if (error.message.includes('File already exists'))
return await createNewFile(app, suffix, type, folderPath, (fileNum || 0) + 1)
return await createNewFile(app, suffix, folderPath, (fileNum || 0) + 1)
}
}


export function setCtxPos(el: HTMLElement) {
const rect = el.getBoundingClientRect()
const p = {x: rect.left, y:rect.top}

let r = document.querySelector(':root') as HTMLElement
if(p.x) {
r.style.setProperty('--ctx_menu_x', `${p.x}px`)
r.style.setProperty('--ctx_menu_y', (-1*p.y + 50 )+ "px" )
}
}
10 changes: 10 additions & 0 deletions src/utils/resize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function setCtxPos(el: HTMLElement) {
const rect = el.getBoundingClientRect();
const p = { x: rect.left, y: rect.top };

let r = document.querySelector(":root") as HTMLElement;
if (p.x) {
r.style.setProperty("--ctx_menu_x", `${p.x}px`);
r.style.setProperty("--ctx_menu_y", -1 * p.y + 50 + "px");
}
}
14 changes: 5 additions & 9 deletions src/views/udoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { WorkspaceLeaf } from 'obsidian'
import { TextFileView } from 'obsidian'
import { DEFAULT_DOCUMENT_DATA_CN } from '../data/default-document-data-cn'
import { docInit } from '~/utils/univer'
import { setCtxPos } from '~/utils/createFile'
import { setCtxPos } from '~/utils/resize'

export const Type = 'univer-doc'

Expand All @@ -17,20 +17,17 @@ export class UDocView extends TextFileView {
resizeObserver: ResizeObserver | void

constructor(leaf: WorkspaceLeaf) {
super(leaf)
super(leaf);
}

getViewData(): string {
console.log('doc saving')
return JSON.stringify(Tools.deepClone(this.documentData.getSnapshot()))
}

setViewData(data: string): void {
if (this.univer)
this.univer.dispose()

this.univer?.dispose()
this.univer = docInit({
container: 'udoc-app',
container: this.rootContainer,
header: true,
toolbar: true,
})
Expand All @@ -47,7 +44,7 @@ export class UDocView extends TextFileView {
docData = JSON.parse(data)
}
catch {
docData = DEFAULT_DOCUMENT_DATA_CN
docData = Tools.deepClone(DEFAULT_DOCUMENT_DATA_CN)
}

setTimeout(() => {
Expand All @@ -72,7 +69,6 @@ export class UDocView extends TextFileView {
}

async onClose() {
console.log('doc closing')
if (this.resizeObserver)
this.resizeObserver.disconnect()

Expand Down
86 changes: 38 additions & 48 deletions src/views/usheet.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,74 @@
import type { IWorkbookData, Univer, Workbook } from '@univerjs/core'
import type { WorkspaceLeaf } from 'obsidian'
import { TextFileView } from 'obsidian'
import { FUniver } from '@univerjs/facade'
import { sheetInit } from '~/utils/univer'
import { setCtxPos } from '~/utils/createFile'
import type { IWorkbookData, Univer, Workbook } from "@univerjs/core";
import type { WorkspaceLeaf } from "obsidian";
import { TextFileView } from "obsidian";
import { FUniver } from "@univerjs/facade";
import { sheetInit } from "~/utils/univer";
import { setCtxPos } from "~/utils/resize";


export const Type = 'univer-sheet'
export const Type = "univer-sheet";

export class USheetView extends TextFileView {
contentData: string
rootContainer: HTMLDivElement
univer: Univer
workbook: Workbook
FUniver: FUniver
resizeObserver: ResizeObserver | void
contentData: string;
rootContainer: HTMLDivElement;
univer: Univer;
workbook: Workbook;
FUniver: FUniver;
resizeObserver: ResizeObserver | void;

constructor(leaf: WorkspaceLeaf) {
super(leaf)
super(leaf);
}

getViewData(): string {
console.log('saveing')
return JSON.stringify(this.workbook.save())
return JSON.stringify(this.workbook.save());
}

setViewData(data: string, _: boolean): void {
console.log('setting data')
if (this.univer)
this.univer.dispose()
this.univer?.dispose();
this.univer = sheetInit({
container: 'usheet-app',
container: this.rootContainer,
header: true,
toolbar: true,
footer: true,
})
this.FUniver = FUniver.newAPI(this.univer)
let sheetData: IWorkbookData | object
});
this.FUniver = FUniver.newAPI(this.univer);
let sheetData: IWorkbookData | object;

this.resizeObserver = new ResizeObserver(() => {
window.dispatchEvent(new Event('resize'))
setCtxPos(this.rootContainer)
}).observe(this.rootContainer)
window.dispatchEvent(new Event("resize"));
setCtxPos(this.rootContainer);
}).observe(this.rootContainer);

try {
sheetData = JSON.parse(data)
}
catch (err) {
sheetData = {}
sheetData = JSON.parse(data);
} catch (err) {
sheetData = {};
}
setTimeout(() => {
this.workbook = this.univer.createUniverSheet(sheetData)
}, 0)
this.workbook = this.univer.createUniverSheet(sheetData);
}, 0);

this.FUniver.onCommandExecuted(() => {
this.requestSave()
})
this.requestSave();
});
}

getViewType() {
return Type
return Type;
}

clear(): void {

}
clear(): void {}

async onOpen() {
this.rootContainer = this.contentEl as HTMLDivElement
this.rootContainer.id = 'usheet-app'
this.rootContainer.classList.add('usheet-container')
this.rootContainer = this.contentEl as HTMLDivElement;
this.rootContainer.id = "usheet-app";
this.rootContainer.classList.add("uproduct-container");
}

async onClose() {
console.log('doc closing')
if (this.resizeObserver)
this.resizeObserver.disconnect()

this.requestSave()
this.univer.dispose()
if (this.resizeObserver) this.resizeObserver.disconnect();

this.requestSave();
this.univer.dispose();
}
}

0 comments on commit ad11046

Please sign in to comment.