Skip to content

Commit 0884c30

Browse files
committed
Replace FileSystemAdapter with Vault API for folder creation. Edit UI text a bit. Set default embed note name format.
1 parent 567e73d commit 0884c30

File tree

5 files changed

+23
-28
lines changed

5 files changed

+23
-28
lines changed

main.ts

+17-23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import {
22
App,
33
ButtonComponent,
4-
// Component,
54
Editor,
6-
// FileManager,
7-
FileSystemAdapter,
85
MarkdownView,
96
moment,
107
Modal,
@@ -13,14 +10,13 @@ import {
1310
PluginSettingTab,
1411
sanitizeHTMLToDom,
1512
Setting,
16-
// setIcon,
1713
TFile,
1814
} from 'obsidian';
1915

2016
const path = require('path'); // eslint-disable-line
2117

2218
interface PasteAsEmbedSettings {
23-
// TODO: replace with Map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
19+
// TODO: replace Record with Map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
2420
userRules: Record<string, PasteRule>;
2521
datetimeFormat: string;
2622
}
@@ -32,7 +28,6 @@ const DEFAULT_SETTINGS: PasteAsEmbedSettings = {
3228

3329
export default class PasteAsEmbed extends Plugin {
3430
settings: PasteAsEmbedSettings;
35-
adapter: FileSystemAdapter;
3631

3732
async pasteFilter(
3833
evt: ClipboardEvent | null,
@@ -78,14 +73,14 @@ export default class PasteAsEmbed extends Plugin {
7873
const embedFolder = this.getEmbedFolder(matchingRule, view.file)
7974
const embedFilePath = path.join(embedFolder, embedNoteName + ".md");
8075

81-
if (!await this.adapter.exists(embedFolder))
82-
await this.adapter.mkdir(embedFolder);
76+
if (!this.app.vault.getFolderByPath(embedFolder))
77+
await this.app.vault.createFolder(embedFolder);
8378

8479
this.app.vault.create(embedFilePath, txt)
8580

8681
editor.replaceSelection(`![[${embedNoteName}]]\n`);
8782

88-
new Notice('Pasted contents of clipboard into embedded note');
83+
new Notice('Pasted contents of clipboard into embedded note.');
8984

9085
}
9186
}
@@ -129,9 +124,7 @@ export default class PasteAsEmbed extends Plugin {
129124
await this.loadSettings();
130125
// This adds a settings tab so the user can configure various aspects of the plugin
131126
this.addSettingTab(new PasteAsEmbedSettingTab(this.app, this));
132-
133-
this.adapter = this.app.vault.adapter as FileSystemAdapter;
134-
127+
135128
this.registerEvent(
136129
this.app.workspace.on(
137130
'editor-paste',
@@ -350,11 +343,11 @@ class ConfirmDeleteModal extends Modal {
350343

351344
class SettingsModal extends Modal {
352345
name: string; // Identifies the rule
353-
folder: string; // Where to create the new file
354346
pattern: string; // Clipboard text pattern that triggers the rule
355347
template?: string; // Insert the pasted text into this template, when writing to new file (e.g. code fence)
356348
desc?: string;
357-
filenameFmt: string;
349+
folder: string; // Where to create the new file
350+
filenameFmt: string = "${date}";
358351
editing = false;
359352
saved = false; // Whether the user clicked "Save"
360353

@@ -394,7 +387,8 @@ class SettingsModal extends Modal {
394387
);
395388

396389
new Setting(settingDiv)
397-
.setName('Description (optional)')
390+
.setName('Description')
391+
.setDesc('Optional text to be displayed with the rule, in the Settings pane.')
398392
.addText(text => text
399393
.setPlaceholder('')
400394
.setValue(this.desc ?? "")
@@ -404,7 +398,7 @@ class SettingsModal extends Modal {
404398
);
405399

406400
new Setting(settingDiv)
407-
.setName('Pattern (optional)')
401+
.setName('Pattern')
408402
.setDesc('Regex pattern that triggers the rule on pasted clipboard text. Leave empty to trigger on all pasted text.')
409403
.addText(text => text
410404
.setPlaceholder('')
@@ -415,8 +409,8 @@ class SettingsModal extends Modal {
415409
);
416410

417411
new Setting(settingDiv)
418-
.setName('Embedded note folder')
419-
.setDesc('Where to save the embedded notes. Start with "./" for path relative to the folder of the current note. Use ${notename} for the name of the current note.')
412+
.setName('Folder')
413+
.setDesc('Where to save the embedded notes. Leave empty to save in the active folder. Start with "./" for a path relative to the active folder. Use ${notename} for the name of the active note. ')
420414
.addText(text => text
421415
.setPlaceholder('')
422416
.setValue(this.folder ?? "")
@@ -426,8 +420,8 @@ class SettingsModal extends Modal {
426420
);
427421

428422
new Setting(settingDiv)
429-
.setName('Name format for embedded note')
430-
.setDesc('Use ${notename} for the name of the current note, and ${date} for a datetime string.')
423+
.setName('Embedded note name format')
424+
.setDesc('Use ${notename} for the name of the active note, and ${date} for a datetime string.')
431425
.addText(text => text
432426
.setPlaceholder('')
433427
.setValue(this.filenameFmt ?? "")
@@ -437,8 +431,8 @@ class SettingsModal extends Modal {
437431
);
438432

439433
new Setting(settingDiv)
440-
.setName('Template (optional)')
441-
.setDesc('Use ${content} to indicate where pasted text should be inserted, before pasting into embedded file.')
434+
.setName('Template')
435+
.setDesc('Use ${content} to indicate where pasted text should be inserted, before pasting into embedded file. Leave empty to insert as-is.')
442436
.addTextArea(textArea => {
443437
textArea.inputEl.rows = 5;
444438
textArea
@@ -447,7 +441,7 @@ class SettingsModal extends Modal {
447441
.onChange(async (value) => {
448442
this.template = value;
449443
});
450-
});
444+
}); File
451445

452446
const footerEl = contentEl.createDiv();
453447
const footerButtons = new Setting(footerEl);

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "paste-as-embed",
33
"name": "Paste as Embed",
4-
"version": "0.1.2",
4+
"version": "0.1.3",
55
"minAppVersion": "0.15.0",
66
"description": "Paste text into a separate note, and embed the note.",
77
"author": "Matt Laporte",

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-paste-as-embed",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "Paste text into a separate note, and embed the note.",
55
"main": "main.js",
66
"scripts": {

versions.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"0.0.1": "0.15.0",
33
"0.1.0": "0.15.0",
44
"0.1.1": "0.15.0",
5-
"0.1.2": "0.15.0"
5+
"0.1.2": "0.15.0",
6+
"0.1.3": "0.15.0"
67
}

0 commit comments

Comments
 (0)