From ecc6a4a876695756948b26c835cec3b3078afe30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E5=86=9B?= Date: Thu, 21 Sep 2023 16:25:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ExcelView.ts | 23 ++++++--- src/MarkdownPostProcessor.ts | 12 ++++- src/main.ts | 95 ++++++++++++++++-------------------- src/utils/FileUtils.ts | 6 ++- 4 files changed, 71 insertions(+), 65 deletions(-) diff --git a/src/ExcelView.ts b/src/ExcelView.ts index c43ab89..f982e9b 100644 --- a/src/ExcelView.ts +++ b/src/ExcelView.ts @@ -112,11 +112,10 @@ export class ExcelView extends TextFileView { const eri = this.cellsSelected.eri; const eci = this.cellsSelected.eci; - // 格式 sri-sci:eri-eci if (this.file && data && sri && sci && eri && eci) { - const link = `![[${this.file.basename}#${data.name}|${sri}-${sci}:${eri}-${eci}]]` - console.log(this.file, link) + const link = `![[${this.file.basename}#${data.name}|${sri}-${sci}:${eri}-${eci}]]`; + console.log(this.file, link); navigator.clipboard.writeText(link); new Notice("Copy embed link to clipboard"); } else { @@ -147,8 +146,6 @@ export class ExcelView extends TextFileView { super.onload(); } - - getViewType(): string { return VIEW_TYPE_EXCEL; } @@ -187,23 +184,33 @@ export class ExcelView extends TextFileView { height: () => this.contentEl.clientHeight, width: () => this.contentEl.clientWidth, }, + row: { + len: 100, + height: parseInt(this.plugin.settings.rowHeight), + }, + col: { + len: 26, + width: parseInt(this.plugin.settings.colWidth), + indexWidth: 60, + minWidth: 60, + }, }) .loadData(jsonData) // load data .change(() => { // save data to db const data = this.sheet.getData(); // console.log("save data to db", data); - this.saveData(JSON.stringify(data)) + this.saveData(JSON.stringify(data)); }) .onAddSheet(() => { const data = this.sheet.getData(); // console.log('onAddSheet', data) - this.saveData(JSON.stringify(data)) + this.saveData(JSON.stringify(data)); }) .onRenameSheet(() => { const data = this.sheet.getData(); // console.log('onRenameSheet', data) - this.saveData(JSON.stringify(data)) + this.saveData(JSON.stringify(data)); }); this.sheet.on("cells-selected", (sheetData, { sri, sci, eri, eci }) => { diff --git a/src/MarkdownPostProcessor.ts b/src/MarkdownPostProcessor.ts index fd8a6c5..6454c76 100644 --- a/src/MarkdownPostProcessor.ts +++ b/src/MarkdownPostProcessor.ts @@ -117,7 +117,7 @@ const tmpObsidianWYSIWYG = async ( const alt = internalEmbedDiv.getAttribute("alt") ?? ""; var range = alt - var heigh = 300 + var heigh = parseInt(plugin.settings.sheetHeight) const matchResult = alt.match(/<(\d+)>/); if (matchResult && matchResult.length > 1) { @@ -189,6 +189,16 @@ const createSheetEl = (data: string, file: TFile, width: number, height: number height: () => height, width: () => width, }, + row: { + len: 100, + height: parseInt(plugin.settings.rowHeight), + }, + col: { + len: 26, + width: parseInt(plugin.settings.colWidth), + indexWidth: 60, + minWidth: 60, + }, }).loadData(jsonData); // load data // @ts-ignore diff --git a/src/main.ts b/src/main.ts index c824c99..36865a2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,11 +6,14 @@ import { ViewState, MarkdownView, Workspace, + MenuItem, + Menu, + MetadataCache } from "obsidian"; import { ExcelSettings, DEFAULT_SETTINGS } from "./utils/Settings"; import { PaneTarget } from "./utils/ModifierkeyHelper"; import { ExcelView } from "./ExcelView"; -import { getExcelFilename } from "./utils/FileUtils"; +import { checkAndCreateFolder, getExcelFilename, getNewUniqueFilepath } from "./utils/FileUtils"; import { around, dedupe } from "monkey-around"; import { ExcelSettingTab } from "./ExcelSettingTab" @@ -52,6 +55,8 @@ export default class ExcelPlugin extends Plugin { this.switchToExcelAfterLoad(); this.registerEventListeners(); + + this.registerCommands() } onunload() {} @@ -113,6 +118,32 @@ export default class ExcelPlugin extends Plugin { await this.saveData(this.settings); } + private registerCommands() { + const fileMenuHandlerCreateNew = (menu: Menu, file: TFile) => { + menu.addItem((item: MenuItem) => { + item + .setTitle("Create Excel File") + .onClick((e) => { + let folderpath = file.path; + if (file instanceof TFile) { + folderpath = normalizePath( + file.path.substr(0, file.path.lastIndexOf(file.name)), + ); + } + this.createAndOpenExcel( + getExcelFilename(this.settings), + folderpath, + ); + }); + }); + }; + + this.registerEvent( + this.app.workspace.on("file-menu", fileMenuHandlerCreateNew), + ); + + } + private registerMonkeyPatches() { const key = "https://github.com/zsviczian/obsidian-excalidraw-plugin/issues"; @@ -150,54 +181,6 @@ export default class ExcelPlugin extends Plugin { }) ); } - this.registerEvent( - this.app.workspace.on("editor-menu", (menu, editor, view) => { - if (!view || !(view instanceof MarkdownView)) return; - const file = view.file; - const leaf = view.leaf; - if (!file) return; - const cache = this.app.metadataCache.getFileCache(file); - if (!cache?.frontmatter || !cache.frontmatter[FRONTMATTER_KEY]) - return; - - menu.addItem((item) => - item - .setTitle("OPEN_AS_EXCEL") - .setIcon("grid") - .setSection("excel") - .onClick(() => { - //@ts-ignore - this.excelFileModes[leaf.id || file.path] = - VIEW_TYPE_EXCEL; - this.setExcelView(leaf); - }) - ); - }) - ); - - this.registerEvent( - this.app.workspace.on("file-menu", (menu, file, source, leaf) => { - if (!leaf || !(leaf.view instanceof MarkdownView)) return; - if (!(file instanceof TFile)) return; - const cache = this.app.metadataCache.getFileCache(file); - if (!cache?.frontmatter || !cache.frontmatter[FRONTMATTER_KEY]) - return; - - menu.addItem((item) => { - item.setTitle("OPEN_AS_EXCEL") - .setIcon("grid") - .setSection("pane") - .onClick(() => { - //@ts-ignore - this.excelFileModes[leaf.id || file.path] = - VIEW_TYPE_EXCEL; - this.setExcelView(leaf); - }); - }); - //@ts-ignore - menu.items.unshift(menu.items.pop()); - }) - ); const self = this; // Monkey patch WorkspaceLeaf to open Excalidraw drawings with ExcalidrawView by default @@ -233,13 +216,13 @@ export default class ExcelPlugin extends Plugin { "markdown" ) { // Then check for the excalidraw frontMatterKey - const cache = app.metadataCache.getCache( + const cache = this.app.metadataCache.getFileCache( state.state.file ); if ( cache?.frontmatter && - cache.frontmatter[FRONTMATTER_KEY] + cache?.frontmatter[FRONTMATTER_KEY] ) { // If we have it, force the view type to excalidraw const newState = { @@ -274,9 +257,13 @@ export default class ExcelPlugin extends Plugin { foldername?: string, initData?: string ): Promise { + const folderpath = normalizePath( + foldername ? foldername : this.settings.folder, + ); + await checkAndCreateFolder(folderpath) - const fname = normalizePath(filename); - const file = await this.app.vault.create(fname, initData ?? "{}"); + const fname = getNewUniqueFilepath(this.app.vault, filename, folderpath); + const file = await this.app.vault.create(fname, initData ?? this.getBlackData()); return file; } @@ -336,7 +323,7 @@ export default class ExcelPlugin extends Plugin { } const fileCache = f ? this.app.metadataCache.getFileCache(f) : null; return ( - !!fileCache?.frontmatter && !!fileCache.frontmatter[FRONTMATTER_KEY] + !!fileCache?.frontmatter && !!fileCache?.frontmatter?.FRONTMATTER_KEY ); } } diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index ed593fa..d0361e2 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -97,8 +97,10 @@ export function getNewUniqueFilepath( export function getExcelFilename(settings: ExcelSettings): string { return ( - "Excel " + - window.moment().format('YYYY-MM-DD HH.mm.ss') + + settings.excelFilenamePrefix + + (settings.excelFilenameDateTime !== "" + ? window.moment().format(settings.excelFilenameDateTime) + : "") + ".sheet.md" ); }