feat: emved link

This commit is contained in:
ljcoder 2023-08-26 17:04:42 +08:00
parent 6d129ed550
commit 0b6b3d4712
9 changed files with 73 additions and 43 deletions

View file

@ -3,16 +3,16 @@
## Excel ## Excel
The Obsidian-Excel plugin integrates [x-spreadsheet](https://github.com/myliang/x-spreadsheet), a feature sheet tool, into Obsidian. You can store and edit `xlsx` files in your vault. The Obsidian-Excel plugin integrates [x-spreadsheet](https://github.com/myliang/x-spreadsheet), a feature sheet tool, into Obsidian. You can store and edit `xlsx` files in your vault.
![Alt text](./doc/img/image.png) ### create sheet file
![Alt text](./doc/img/create.gif)
### import/export xlsx file ### import/export xlsx file
If you are using Microsoft Office 365 to create xlsx files, you need to import the display. If you are using Microsoft Office 365 to create xlsx files, you need to import the display.
![import](./doc/img/import.png) ![import](./doc/img/import.gif)
## TODO ## TODO
✅ import/export excel - link to markdown
- [X] link to markdown

BIN
doc/img/create.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 252 KiB

BIN
doc/img/import.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 308 KiB

BIN
doc/img/link.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1,020 KiB

View file

@ -25,7 +25,7 @@ export class Excel extends MarkdownRenderChild {
const sheet = new Spreadsheet(sheetEle, { const sheet = new Spreadsheet(sheetEle, {
mode: "read", mode: "read",
showToolbar: false, showToolbar: false,
showBottomBar: false, showBottomBar: true,
view: { view: {
height: () => 300, height: () => 300,
width: () => this.containerEl.clientWidth, width: () => this.containerEl.clientWidth,

View file

@ -12,6 +12,7 @@ export class ExcelView extends TextFileView {
public sheet: Spreadsheet; public sheet: Spreadsheet;
public importEle: HTMLElement; public importEle: HTMLElement;
public exportEle: HTMLElement; public exportEle: HTMLElement;
public embedLinkEle: HTMLElement;
public sheetEle: HTMLElement; public sheetEle: HTMLElement;
constructor(leaf: WorkspaceLeaf, plugin: ExcelPlugin) { constructor(leaf: WorkspaceLeaf, plugin: ExcelPlugin) {
@ -41,14 +42,17 @@ export class ExcelView extends TextFileView {
handleFile(e: Event) { handleFile(e: Event) {
//@ts-ignore //@ts-ignore
const files = e.target?.files; const files = e.target?.files;
if (!files) {
new Notice('Failed to get file')
return
}
const f = files[0]; const f = files[0];
const reader = new FileReader(); const reader = new FileReader();
const instance = this;
reader.onload = (e) => { reader.onload = (e) => {
const data = e.target?.result; const data = e.target?.result;
if (data) { if (data) {
instance.process_wb(XLSX.read(data)); this.process_wb(XLSX.read(data));
} else { } else {
new Notice('Read file error') new Notice('Read file error')
} }
@ -58,7 +62,12 @@ export class ExcelView extends TextFileView {
process_wb(wb: XLSX.WorkBook) { process_wb(wb: XLSX.WorkBook) {
const sheetData = stox(wb); const sheetData = stox(wb);
this.sheet.loadData(sheetData); if (sheetData) {
this.sheet.loadData(sheetData);
this.data = JSON.stringify(sheetData);
} else {
new Notice('Data parsing error')
}
} }
handleExportClick(ev: MouseEvent) { handleExportClick(ev: MouseEvent) {
@ -69,6 +78,17 @@ export class ExcelView extends TextFileView {
XLSX.writeFile(new_wb, title + ".xlsx", {}); XLSX.writeFile(new_wb, title + ".xlsx", {});
} }
handleEmbedLink(e:Event) {
if (this.file) {
navigator.clipboard.writeText(
`![[${this.file.path}]]`,
);
new Notice('Copy embed link to clipboard')
} else {
new Notice('Copy embed link failed')
}
}
onload(): void { onload(): void {
this.ownerWindow = this.containerEl.win; this.ownerWindow = this.containerEl.win;
@ -81,29 +101,9 @@ export class ExcelView extends TextFileView {
this.handleExportClick(ev) this.handleExportClick(ev)
); );
app.workspace.onLayoutReady(async () => { this.embedLinkEle = this.addAction("link", "copy embed link", (ev) =>
this.sheetEle = this.contentEl.createDiv({ this.handleEmbedLink(ev)
attr: { );
id: "x-spreadsheet",
class: "sheet-box",
},
});
// 添加导入input用来选择导入的文件
const importInput = this.contentEl.createEl("input", {
cls: "import-excel",
type: "file",
attr: {
id: "import",
accept: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
},
});
importInput.addEventListener(
"change",
this.handleFile.bind(this),
false
);
});
super.onload(); super.onload();
} }
@ -117,11 +117,34 @@ export class ExcelView extends TextFileView {
} }
refresh() { refresh() {
this.sheetEle.empty(); this.contentEl.empty();
this.sheetEle = this.contentEl.createDiv({
attr: {
id: "x-spreadsheet",
class: "sheet-box",
},
});
// 添加隐藏的导入input用来选择导入的文件
const importInput = this.contentEl.createEl("input", {
cls: "import-excel",
type: "file",
attr: {
id: "import",
accept: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
},
});
importInput.addEventListener(
"change",
this.handleFile.bind(this),
false
);
// 初始化 sheet
const jsonData = JSON.parse(this.data || "{}") || {}; const jsonData = JSON.parse(this.data || "{}") || {};
//@ts-ignore //@ts-ignore
this.sheet = new Spreadsheet(this.sheetEle, { this.sheet = new Spreadsheet(this.sheetEle, {
showBottomBar: false, showBottomBar: true,
view: { view: {
height: () => this.contentEl.clientHeight, height: () => this.contentEl.clientHeight,
width: () => this.contentEl.clientWidth, width: () => this.contentEl.clientWidth,
@ -130,11 +153,9 @@ export class ExcelView extends TextFileView {
.loadData(jsonData) // load data .loadData(jsonData) // load data
.change((data) => { .change((data) => {
// save data to db // save data to db
console.log('save data to db')
this.data = JSON.stringify(data); this.data = JSON.stringify(data);
}) })
.on('cells-selected', (cell, { sri, sci, eri, eci}) => {
console.log(cell, sri, sci, eri, eci)
})
// @ts-ignore // @ts-ignore
this.sheet.validate(); this.sheet.validate();

View file

@ -5,12 +5,6 @@
padding: 0; padding: 0;
} }
.sheet-iframe {
width: 100%;
height: 300px;
background-color: red;
}
.import-excel { .import-excel {
position: absolute; position: absolute;
right: 0; right: 0;
@ -19,6 +13,21 @@
opacity: 0; opacity: 0;
} }
/* Markdown 文档引入样式设置*/
.sheet-iframe {
width: 100%;
height: 300px;
background-color: var(--background-primary-alt);
}
ul.x-spreadsheet-menu {
margin-block-start: 0;
}
.sheet-iframe .x-spreadsheet-menu > li .x-spreadsheet-icon {
display: none;
}
/* node_modules/x-data-spreadsheet/src/index.less */ /* node_modules/x-data-spreadsheet/src/index.less */
body { body {
margin: 0; margin: 0;