From a2b28ff1365d8c885b54ad54984ac9029de0dcc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E5=86=9B?= Date: Tue, 19 Sep 2023 18:30:24 +0800 Subject: [PATCH] =?UTF-8?q?=20feat:=20embed=20link=20=E9=AB=98=E5=BA=A6?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=20&=20=E9=85=8D=E7=BD=AE=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ExcelSettingTab.ts | 101 +++++++++++++++++++++++++++++++++++ src/MarkdownPostProcessor.ts | 22 ++++++-- src/main.ts | 6 +++ src/utils/Settings.ts | 12 +++-- styles.css | 1 - 5 files changed, 132 insertions(+), 10 deletions(-) create mode 100644 src/ExcelSettingTab.ts diff --git a/src/ExcelSettingTab.ts b/src/ExcelSettingTab.ts new file mode 100644 index 0000000..b6c51fd --- /dev/null +++ b/src/ExcelSettingTab.ts @@ -0,0 +1,101 @@ +import { App, PluginSettingTab, Setting } from "obsidian"; +import ExcelPlugin from "./main"; + +export class ExcelSettingTab extends PluginSettingTab { + plugin: ExcelPlugin; + + constructor(app: App, plugin: ExcelPlugin) { + super(app, plugin); + this.plugin = plugin; + } + + display() { + let { containerEl } = this + + containerEl.empty() + + containerEl.createEl("h1", { text: "File Setting / 文件设置" }); + + new Setting(containerEl) + .setName("Folder") + .setDesc("Create files in this folder by default / 默认在此文件夹下创建文件") + .addText((text) => + text + .setPlaceholder("/") + .setValue(this.plugin.settings.folder) + .onChange(async (value) => { + this.plugin.settings.folder = value + this.plugin.saveSettings() + }) + ) + + new Setting(containerEl) + .setName("Filename Prefix / 文件名前缀") + .setDesc("filename prefi / 设置文件名前缀") + .addText((text) => + text + .setPlaceholder("Excel") + .setValue(this.plugin.settings.excelFilenamePrefix) + .onChange(async (value) => { + this.plugin.settings.excelFilenamePrefix = value + this.plugin.saveSettings() + }) + ) + + new Setting(containerEl) + .setName("Filename Date Time/ 文件名时间格式") + .setDesc("filename date time / 文件名时间格式") + .addText((text) => + text + .setPlaceholder("YYYY-MM-DD HH.mm.ss") + .setValue(this.plugin.settings.excelFilenameDateTime) + .onChange(async (value) => { + this.plugin.settings.excelFilenameDateTime = value + this.plugin.saveSettings() + }) + ) + + containerEl.createEl("h1", { text: "Embed Link Setting / 嵌入链接设置" }); + + new Setting(containerEl) + .setName("Sheet Height/ 表格高度") + .setDesc("default height for rendering spreadsheets / 渲染表格的默认高度") + .addText((text) => + text + .setPlaceholder("300") + .setValue(this.plugin.settings.sheetHeight) + .onChange(async (value) => { + this.plugin.settings.sheetHeight = value + this.plugin.saveSettings() + }) + ) + + containerEl.createEl("h1", { text: "Sheet Setting / 表格设置" }); + + new Setting(containerEl) + .setName("Row Height/ 行高度") + .setDesc("default row height / 默认行高") + .addText((text) => + text + .setPlaceholder("25") + .setValue(this.plugin.settings.rowHeight) + .onChange(async (value) => { + this.plugin.settings.rowHeight = value + this.plugin.saveSettings() + }) + ) + + new Setting(containerEl) + .setName("Column Width/ 列宽度") + .setDesc("default column width / 默认列的宽度") + .addText((text) => + text + .setPlaceholder("25") + .setValue(this.plugin.settings.colWidth) + .onChange(async (value) => { + this.plugin.settings.colWidth = value + this.plugin.saveSettings() + }) + ) + } +} diff --git a/src/MarkdownPostProcessor.ts b/src/MarkdownPostProcessor.ts index ab5acf0..fd8a6c5 100644 --- a/src/MarkdownPostProcessor.ts +++ b/src/MarkdownPostProcessor.ts @@ -115,18 +115,31 @@ const tmpObsidianWYSIWYG = async ( const data = await vault.read(file); const src = internalEmbedDiv.getAttribute("src") ?? ""; const alt = internalEmbedDiv.getAttribute("alt") ?? ""; + var range = alt + + var heigh = 300 + const matchResult = alt.match(/<(\d+)>/); + + if (matchResult && matchResult.length > 1) { + const extractedValue = matchResult[1]; // 获取匹配到的数字 + // console.log("Extracted value:", extractedValue); + heigh = parseInt(extractedValue) + range = range.replace(/<\d+>/, ''); + } else { + // console.log("No match found."); + } const split = src.split("#"); var excelData = getExcelData(data); if (split.length > 1) { excelData = getExcelAreaData( data, split[1], - alt + range ); } // console.log('internalEmbedDiv', excelData, src, alt) - const sheetDiv = createSheetEl(excelData, file, internalEmbedDiv.clientWidth); + const sheetDiv = createSheetEl(excelData, file, internalEmbedDiv.clientWidth, heigh); if (markdownEmbed) { //display image on canvas without markdown frame internalEmbedDiv.removeClass("markdown-embed"); @@ -135,7 +148,7 @@ const tmpObsidianWYSIWYG = async ( internalEmbedDiv.appendChild(sheetDiv); }; -const createSheetEl = (data: string, file: TFile, width: number): HTMLDivElement => { +const createSheetEl = (data: string, file: TFile, width: number, height: number = 300): HTMLDivElement => { const sheetDiv = createDiv() @@ -161,6 +174,7 @@ const createSheetEl = (data: string, file: TFile, width: number): HTMLDivElement cls: "sheet-iframe", attr: { id: `x-spreadsheet-${new Date().getTime()}`, + style: `height: ${height}px` }, }); @@ -172,7 +186,7 @@ const createSheetEl = (data: string, file: TFile, width: number): HTMLDivElement showToolbar: false, showBottomBar: true, view: { - height: () => 300, + height: () => height, width: () => width, }, }).loadData(jsonData); // load data diff --git a/src/main.ts b/src/main.ts index c2efb81..c824c99 100644 --- a/src/main.ts +++ b/src/main.ts @@ -12,6 +12,7 @@ import { PaneTarget } from "./utils/ModifierkeyHelper"; import { ExcelView } from "./ExcelView"; import { getExcelFilename } from "./utils/FileUtils"; import { around, dedupe } from "monkey-around"; +import { ExcelSettingTab } from "./ExcelSettingTab" import { initializeMarkdownPostProcessor, @@ -25,6 +26,11 @@ export default class ExcelPlugin extends Plugin { private _loaded: boolean = false; async onload() { + // 加载设置 + await this.loadSettings() + + this.addSettingTab(new ExcelSettingTab(this.app, this)) + this.registerView( VIEW_TYPE_EXCEL, (leaf: WorkspaceLeaf) => new ExcelView(leaf, this) diff --git a/src/utils/Settings.ts b/src/utils/Settings.ts index fde471e..afdccc3 100644 --- a/src/utils/Settings.ts +++ b/src/utils/Settings.ts @@ -1,15 +1,17 @@ export interface ExcelSettings { folder: string; excelFilenamePrefix: string, - excelEmbedPrefixWithFilename: true, - excelFilnameEmbedPostfix: string, excelFilenameDateTime: string, + sheetHeight: string, + rowHeight: string, + colWidth: string } export const DEFAULT_SETTINGS: ExcelSettings = { - folder: "Excel", + folder: "/", excelFilenamePrefix: "Excel ", - excelEmbedPrefixWithFilename: true, - excelFilnameEmbedPostfix: " ", excelFilenameDateTime: "YYYY-MM-DD HH.mm.ss", + sheetHeight: "300", + rowHeight: "25", + colWidth: "100" }; diff --git a/styles.css b/styles.css index d6c6a8a..ac04264 100644 --- a/styles.css +++ b/styles.css @@ -16,7 +16,6 @@ /* Markdown 文档引入样式设置*/ .sheet-iframe { width: 100%; - height: 308px; background-color: var(--background-primary-alt); border: 5px solid #f5f6f7; }