forked from dream-num/obsidian-univer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45e56dc
commit ad11046
Showing
6 changed files
with
65 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
--ctx_menu_y: 0px; | ||
} | ||
|
||
|
||
.uproduct-container { | ||
margin: 0; | ||
padding: 0; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |