From 50d6d06dd42de0a5d3eb2756050a8ccc37e7773f Mon Sep 17 00:00:00 2001 From: ljcoder Date: Sat, 14 Oct 2023 13:47:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8E=BB=E6=8E=89=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.ts | 78 ++++-------------------------------------- src/utils/FileUtils.ts | 3 +- 2 files changed, 7 insertions(+), 74 deletions(-) diff --git a/src/main.ts b/src/main.ts index eae2318..82d62d1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -26,7 +26,6 @@ import { FRONTMATTER_KEY, FRONTMATTER, VIEW_TYPE_EXCEL } from "src/constants"; export default class ExcelPlugin extends Plugin { public settings: ExcelSettings; - public excelFileModes: { [file: string]: string } = {}; private _loaded: boolean = false; async onload() { @@ -42,7 +41,7 @@ export default class ExcelPlugin extends Plugin { this.registerExtensions(["sheet"], VIEW_TYPE_EXCEL); // This creates an icon in the left ribbon. - this.addRibbonIcon("table", "Excel", (e: MouseEvent) => { + this.addRibbonIcon("table", t("CREATE_EXCEL"), (e: MouseEvent) => { // Called when the user clicks the icon. this.createAndOpenExcel(getExcelFilename(this.settings), undefined, this.getBlackData()); }); @@ -98,9 +97,6 @@ export default class ExcelPlugin extends Plugin { leaf.view.file && self.isExcelFile(leaf.view.file) ) { - self.excelFileModes[ - (leaf as any).id || leaf.view.file?.path - ] = VIEW_TYPE_EXCEL; self.setExcelView(leaf); } } @@ -183,56 +179,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(t("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(t("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 this.register( @@ -241,15 +187,6 @@ export default class ExcelPlugin extends Plugin { // while the file is open. When the file closes, we no longer need to keep track of it. detach(next) { return function () { - const state = this.view?.getState(); - // console.log('state--', state.file) - if ( - state?.file && - self.excelFileModes[this.id || state.file] - ) { - delete self.excelFileModes[this.id || state.file]; - } - return next.apply(this); }; }, @@ -261,29 +198,25 @@ export default class ExcelPlugin extends Plugin { self._loaded && // If we have a markdown file state.type === "markdown" && - state.state?.file && - // And the current mode of the file is not set to markdown - self.excelFileModes[this.id || state.state.file] !== - "markdown" + state.state?.file ) { // Then check for the excalidraw frontMatterKey const cache = this.app.metadataCache.getCache( state.state.file ); + // console.log("setViewState", cache) if ( cache?.frontmatter && cache?.frontmatter[FRONTMATTER_KEY] ) { + // console.log("setViewState --", cache) // If we have it, force the view type to excalidraw const newState = { ...state, type: VIEW_TYPE_EXCEL, }; - self.excelFileModes[state.state.file] = - VIEW_TYPE_EXCEL; - return next.apply(this, [newState, ...rest]); } } @@ -311,7 +244,7 @@ export default class ExcelPlugin extends Plugin { const folderpath = normalizePath( foldername ? foldername : this.settings.folder, ); - await checkAndCreateFolder(folderpath) + await checkAndCreateFolder(this.app.vault, folderpath) const fname = getNewUniqueFilepath(this.app.vault, filename, folderpath); const file = await this.app.vault.create(fname, initData ?? this.getBlackData()); @@ -373,6 +306,7 @@ export default class ExcelPlugin extends Plugin { return true; } const fileCache = f ? this.app.metadataCache.getFileCache(f) : null; + // console.log("isExcelFile", fileCache) return ( !!fileCache?.frontmatter && !!fileCache?.frontmatter[FRONTMATTER_KEY] ); diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index d0361e2..9ddfda7 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -110,8 +110,7 @@ export function getExcelFilename(settings: ExcelSettings): string { * Open or create a folderpath if it does not exist * @param folderpath */ -export async function checkAndCreateFolder(folderpath: string) { - const vault = app.vault; +export async function checkAndCreateFolder(vault: Vault, folderpath: string) { folderpath = normalizePath(folderpath); //https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/658 //@ts-ignore