|
1 | | -import { App, Editor, FileView, MarkdownPreviewView, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile, View, renderResults } from 'obsidian'; |
2 | | -// Remember to rename these classes and interfaces! |
3 | | - |
| 1 | +import { Notice, Plugin, TFile } from 'obsidian'; |
4 | 2 |
|
5 | 3 | export default class MarkmapToCsvPlugin extends Plugin { |
6 | 4 |
|
7 | 5 | async onload() { |
8 | | - // This creates an icon in the left ribbon. |
9 | | - const markMapToCsvIconE1 = this.addRibbonIcon('dice', 'Markmap to csv Plugin', (evt: MouseEvent) => { |
10 | | - // Called when the user clicks the icon. |
11 | | - new Notice('This is a notice!'); |
12 | | - }); |
13 | | - // Perform additional things with the ribbon |
14 | | - markMapToCsvIconE1.addClass('markmap2csv-plugin-ribbon-class'); |
15 | | - |
16 | | - // This adds a status bar item to the bottom of the app. Does not work on mobile apps. |
17 | | - const statusBarItemEl = this.addStatusBarItem(); |
18 | | - statusBarItemEl.setText('Status Bar Text'); |
19 | | - |
20 | 6 | console.log('loading MarkmapToCSVPlugin'); |
21 | 7 |
|
22 | 8 | this.addCommand({ |
23 | 9 | id: 'convert-markmap-to-csv', |
24 | 10 | name: 'Convert Markmap to CSV', |
25 | | - callback: async () => { |
| 11 | + checkCallback: (checking) => { |
26 | 12 | const file = this.app.workspace.getActiveFile() |
27 | | - if (!file) { |
28 | | - new Notice('ERROR: Please open a Markmap file to convert.'); |
29 | | - return; |
| 13 | + if (!file || file.extension !== "md") { |
| 14 | + return false; |
30 | 15 | } |
31 | 16 |
|
32 | | - if (file.extension !== "md") { |
33 | | - new Notice('ERROR: Please open markdown file to convert.'); |
34 | | - return; |
| 17 | + if(!checking) { |
| 18 | + this.app.vault.read(file).then( |
| 19 | + async (markdownData) => { |
| 20 | + const csvData = this.convertMarkmapToCSV(markdownData); |
| 21 | + await this.saveCsvToFile(file, csvData); |
| 22 | + } |
| 23 | + ).catch((err) => { |
| 24 | + new Notice(`ERROR: failed to read file ${file.basename}`) |
| 25 | + throw err |
| 26 | + }); |
35 | 27 | } |
36 | 28 |
|
37 | | - const markdownData = await this.app.vault.read(file); |
38 | | - const csvData = this.convertMarkmapToCSV(markdownData); |
39 | | - await this.saveCsvToFile(file, csvData); |
| 29 | + return true; |
| 30 | + |
40 | 31 | } |
41 | 32 | }); |
42 | | - |
43 | | - // If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin) |
44 | | - // Using this function will automatically remove the event listener when this plugin is disabled. |
45 | | - this.registerDomEvent(document, 'click', (evt: MouseEvent) => { |
46 | | - console.log('click', evt); |
47 | | - }); |
48 | | - |
49 | | - // When registering intervals, this function will automatically clear the interval when the plugin is disabled. |
50 | | - this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000)); |
51 | 33 | } |
52 | 34 |
|
53 | 35 | onunload() { |
|
0 commit comments