feat: 新增创建文件方式

This commit is contained in:
雷军 2023-09-21 16:25:56 +08:00
parent a2b28ff136
commit ecc6a4a876
4 changed files with 71 additions and 65 deletions

View file

@ -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 }) => {

View file

@ -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

View file

@ -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<TFile> {
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
);
}
}

View file

@ -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"
);
}