Skip to content

Commit 037b78b

Browse files
committed
polishing for release
1 parent 9a01428 commit 037b78b

File tree

1 file changed

+38
-112
lines changed

1 file changed

+38
-112
lines changed

main.ts

Lines changed: 38 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
1+
// ----------------------------------------------------------------------------------------
2+
// File : main.ts
3+
// Author : Stefan Wolfrum (@metawops)
4+
// Date : 2022-05-27
5+
// Last Update: 2022-05-31
6+
// Description: Implementation of my very first Obsidian plugin.
7+
// It allows to export rendered HTML tables (i.e. from a pane in reading mode)
8+
// to be exported to a CSV file and optionally to the clipboard, too.
9+
// Purely based on the Obsidian sample plugin.
10+
// ----------------------------------------------------------------------------------------
211

3-
// Remember to rename these classes and interfaces!
12+
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
413

514
interface Table2CSVSettings {
615
exportPath: string;
@@ -24,7 +33,7 @@ export default class Table2CSVPlugin extends Plugin {
2433
settings: Table2CSVSettings;
2534

2635
async onload() {
27-
console.log("In onload().");
36+
2837
await this.loadSettings();
2938

3039
this.addCommand({
@@ -34,56 +43,49 @@ export default class Table2CSVPlugin extends Plugin {
3443

3544
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
3645

37-
// const markdownView = this.app.workspace.activeLeaf.view as MarkdownView;
38-
// const livePreviewActive: boolean = markdownView.getState().field(editorLivePreviewField);
39-
4046
if (view) {
4147
if (!checking) {
4248
// Here we can actually start with our work
43-
console.log("table-to-csv-export command triggered.")
4449
const viewMode = view.getMode();
45-
console.log("viewMode =", viewMode);
4650
if (viewMode=="preview") {
47-
console.log("We're in reading mode and can now work on the HTML tables! :-)");
48-
console.log("Here's the HTML of the active pane in reading mode:");
49-
console.log(view.previewMode.containerEl);
50-
5151
// Now convert the tables
5252
const csvString = htmlToCSV(view.previewMode.containerEl, this.settings.sepChar, this.settings.quoteData);
53-
console.log("And here's the HTML tables converted to CSV:");
54-
console.log(csvString);
5553

5654
const filename = `${this.settings.exportPath}${this.settings.baseFilename}-${this.settings.fileNumber}.csv`;
5755
this.app.vault.create(filename, csvString)
58-
5956
.then( () => {
57+
// increment the file number addition string
58+
// first, convert the current string to a number:
6059
let fn: number = +this.settings.fileNumber;
60+
// then increment the number:
6161
fn++;
62+
// don't allow more that 999; restart with 001 in that case:
63+
if (fn==1000) fn = 1;
64+
// convert the number to a string again:
6265
let newFileNumberString: string = fn + "";
66+
// add leading zeroes to the string:
6367
while (newFileNumberString.length < 3) newFileNumberString = "0" + newFileNumberString;
6468
this.settings.fileNumber = newFileNumberString;
6569
if (this.settings.saveToClipboardToo) {
6670
navigator.clipboard
6771
.writeText(csvString)
68-
.then(() => {
69-
console.log(`"${csvString}" was copied to clipboard.`);
72+
.then(() => {
73+
new Notice(`The file ${filename} was successfully created in your vault. The contents was also copied to the clipboard.`);
7074
})
7175
.catch((err) => {
72-
console.log(`Error copying text to clipboard: ${err}`);
76+
new Notice('There was an error with copying the contents to the clipboard.');
7377
});
74-
new Notice(`The file ${filename} was successfully created in your vault and the contents was copied to the clipboard.`)
78+
7579
} else {
7680
new Notice(`The file ${filename} was successfully created in your vault.`)
7781
}
7882
})
7983

8084
.catch( (error) => {
81-
console.log(error.message);
8285
const errorMessage = `Error: ${error.message}`;
8386
new Notice(errorMessage);
8487
})
8588

86-
8789
}
8890
else {
8991
new Notice('This command only works on panes in reading mode! – No CSV files were written.');
@@ -98,79 +100,20 @@ export default class Table2CSVPlugin extends Plugin {
98100
});
99101

100102

101-
// This creates an icon in the left ribbon.
102-
//const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
103-
// Called when the user clicks the icon.
104-
//new Notice('This is a notice!');
105-
//});
106-
// Perform additional things with the ribbon
107-
//ribbonIconEl.addClass('my-plugin-ribbon-class');
108-
109-
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
110-
// const statusBarItemEl = this.addStatusBarItem();
111-
// statusBarItemEl.setText('Status Bar Text');
112-
113-
// This adds a simple command that can be triggered anywhere
114-
// this.addCommand({
115-
// id: 'open-sample-modal-simple',
116-
// name: 'Open sample modal (simple)',
117-
// callback: () => {
118-
// new SampleModal(this.app).open();
119-
// }
120-
// });
121-
// This adds an editor command that can perform some operation on the current editor instance
122-
// this.addCommand({
123-
// id: 'sample-editor-command',
124-
// name: 'Sample editor command',
125-
// editorCallback: (editor: Editor, view: MarkdownView) => {
126-
// console.log(editor.getSelection());
127-
// editor.replaceSelection('Sample Editor Command');
128-
// }
129-
// });
130-
// This adds a complex command that can check whether the current state of the app allows execution of the command
131-
// this.addCommand({
132-
// id: 'open-sample-modal-complex',
133-
// name: 'Open sample modal (complex)',
134-
// checkCallback: (checking: boolean) => {
135-
// // Conditions to check
136-
// const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
137-
// if (markdownView) {
138-
// // If checking is true, we're simply "checking" if the command can be run.
139-
// // If checking is false, then we want to actually perform the operation.
140-
// if (!checking) {
141-
// new SampleModal(this.app).open();
142-
// }
143-
144-
// // This command will only show up in Command Palette when the check function returns true
145-
// return true;
146-
// }
147-
// }
148-
// });
149-
150103
// This adds a settings tab so the user can configure various aspects of the plugin
151104
this.addSettingTab(new Table2CSVSettingTab(this.app, this));
152-
153-
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
154-
// Using this function will automatically remove the event listener when this plugin is disabled.
155-
// this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
156-
// console.log('click', evt);
157-
// });
158-
159-
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
160-
//this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
161105
}
162106

163-
onunload() {
164-
console.log("In onunload().");
165-
}
107+
onunload() {
108+
}
166109

167-
async loadSettings() {
168-
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
169-
}
110+
async loadSettings() {
111+
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
112+
}
170113

171-
async saveSettings() {
172-
await this.saveData(this.settings);
173-
}
114+
async saveSettings() {
115+
await this.saveData(this.settings);
116+
}
174117
}
175118

176119

@@ -195,22 +138,6 @@ function htmlToCSV(html: HTMLElement, sep: string, quote: boolean) {
195138
return data.join("\n");
196139
}
197140

198-
// class SampleModal extends Modal {
199-
// constructor(app: App) {
200-
// super(app);
201-
// }
202-
203-
// onOpen() {
204-
// const {contentEl} = this;
205-
// contentEl.setText('Woah!');
206-
// }
207-
208-
// onClose() {
209-
// const {contentEl} = this;
210-
// contentEl.empty();
211-
// }
212-
// }
213-
214141
class Table2CSVSettingTab extends PluginSettingTab {
215142
plugin: Table2CSVPlugin;
216143

@@ -228,6 +155,8 @@ class Table2CSVSettingTab extends PluginSettingTab {
228155
containerEl.createEl('p', {text: 'NOTE #1: Currently, this plugin will only work reliably when there is only one table in a note.'});
229156
containerEl.createEl('p', {text: 'NOTE #2: Currently, the exported CSV files are saved inside your vault main folder.'});
230157

158+
// Being able to set a path for the exports will be a future addition
159+
// ------------------------------------------------------------------
231160
// new Setting(containerEl)
232161
// .setName('CSV file export path')
233162
// .setDesc('Enter the path where the exported CSV file should be saved. If no path is set the CSV file will be saved into your vault folder.')
@@ -247,7 +176,7 @@ class Table2CSVSettingTab extends PluginSettingTab {
247176
.setPlaceholder('<enter a base filename')
248177
.setValue(this.plugin.settings.baseFilename)
249178
.onChange(async (value) => {
250-
console.log('base filename: ' + value);
179+
//console.log('base filename: ' + value);
251180
this.plugin.settings.baseFilename = value;
252181
await this.plugin.saveSettings();
253182
}));
@@ -259,7 +188,7 @@ class Table2CSVSettingTab extends PluginSettingTab {
259188
.setPlaceholder('')
260189
.setValue(this.plugin.settings.fileNumber)
261190
.onChange(async (value) => {
262-
console.log('fileNumber: ' + value);
191+
//console.log('fileNumber: ' + value);
263192
this.plugin.settings.fileNumber = value;
264193
await this.plugin.saveSettings();
265194
}));
@@ -271,7 +200,7 @@ class Table2CSVSettingTab extends PluginSettingTab {
271200
.setPlaceholder('<enter a separation character or string>')
272201
.setValue(this.plugin.settings.sepChar)
273202
.onChange(async (value) => {
274-
console.log('sepChar: ' + value);
203+
//console.log('sepChar: ' + value);
275204
this.plugin.settings.sepChar = value;
276205
await this.plugin.saveSettings();
277206
}));
@@ -282,7 +211,7 @@ class Table2CSVSettingTab extends PluginSettingTab {
282211
.addToggle( toggle => toggle
283212
.setValue(this.plugin.settings.quoteData)
284213
.onChange(async (value) => {
285-
console.log('quote data toggle: ' + value);
214+
//console.log('quote data toggle: ' + value);
286215
this.plugin.settings.quoteData = value;
287216
await this.plugin.saveSettings();
288217
}));
@@ -293,12 +222,9 @@ class Table2CSVSettingTab extends PluginSettingTab {
293222
.addToggle( toggle => toggle
294223
.setValue(this.plugin.settings.saveToClipboardToo)
295224
.onChange(async (value) => {
296-
console.log('save to clipboard, too: ' + value);
225+
//console.log('save to clipboard, too: ' + value);
297226
this.plugin.settings.saveToClipboardToo = value;
298227
await this.plugin.saveSettings();
299228
}));
300-
301-
302-
303229
}
304230
}

0 commit comments

Comments
 (0)