Skip to content

Commit

Permalink
Document and improve async tag page creation
Browse files Browse the repository at this point in the history
  • Loading branch information
pjeby committed Jun 5, 2023
1 parent 907ca4d commit 87e0215
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,23 @@ This event allows other plugins to take over creation of tag pages. You can reg
```typescript
type tagPageEvent = {
tag: string
file?: TFile
file?: TFile | Promise<TFile>
}

this.registerEvent(app.workspace.on("tag-page:will-create", (evt: tagPageEvent) => {
if (!evt.file) {
// create the file here, then save it in the event
evt.file = // the new file to use
evt.file = someAsynFunctionReturningaTFilePromise();
}
}));
```

Note that if `evt.file` exists, your callback should not do anything, as the file has already been created. If you want to modify an already-created tag page file, use the `tag-page:did-create` event instead. (See below.)
You can set the file to the result of calling an async function (as shown), but the event handler itself must **not** be async nor should it await anything. If it's async, you will most likely end up with multiple files created because Tag Wrangler and any other event handlers for the event will think no other plugin has created one.

Note that if `evt.file` is anything but `undefined`, your callback *must not do anything*, as the file has already been created or is in the process of being created. If you want to modify an already-created tag page file, use the `tag-page:did-create` event instead. (See below.)

For users of Quickadd and other plugins that allow user-defined Javascript, note that the `this.registerEvent()` call may need to be replaced with something like `app.plugins.plugins['quickadd'].registerEvent()`. You should also only run this code *once*, when the plugin is initialized, and not as part of a command or template.

### `tag-page:did-create`

This event allows other plugins to modify or rename a newly-created tag page. It has the same callback signature as `tag-page:will-create`, except the `file` field will always contain a file. (The one created by Tag Wrangler or by a callback to `tag-page:will-create`.) You should use the `app.vault.process()` method to do any changes, to prevent accidental file overwrites and data loss. (It should also be safe to `app.vault.rename()` it to change its name or location.)
This event allows other plugins to modify or rename a newly-created tag page. It has the same callback signature as `tag-page:will-create`, except the `file` field will always contain a TFile. (The one created by Tag Wrangler or by a callback to `tag-page:will-create`.) You should use the `app.vault.process()` method to do any changes, to prevent accidental file overwrites and data loss. (It should also be safe to `app.vault.rename()` it to change its name or location.)
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Tag Wrangler",
"author": "PJ Eby",
"authorUrl": "https://github.com/pjeby",
"version": "0.5.9",
"version": "0.5.10",
"minAppVersion": "0.15.9",
"description": "Rename, merge, toggle, and search tags from the tag pane",
"isDesktopOnly": false
Expand Down
9 changes: 5 additions & 4 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,21 @@ export default class TagWrangler extends Plugin {
const tag = new Tag(tagName);
const tp_evt = { tag: tag.canonical, file: undefined };
app.workspace.trigger("tag-page:will-create", tp_evt);
if (!tp_evt.file) {
let file = tp_evt.file && await tp_evt.file;
if (!file) {
const baseName = new Tag(tagName).name.split("/").join(" ");
const folder = this.app.fileManager.getNewFileParent(this.app.workspace.getActiveFile()?.path || "");
const path = this.app.vault.getAvailablePath(folder.getParentPrefix()+baseName, "md");
const file = await this.app.vault.create(path, [
file = await this.app.vault.create(path, [
"---",
`Aliases: [ ${JSON.stringify(Tag.toTag(tagName))} ]`,
"---",
""
].join("\n"));
tp_evt.file = file;
}
tp_evt.file = file;
app.workspace.trigger("tag-page:did-create", tp_evt);
this.openTagPage(tp_evt.file, true, newLeaf);
this.openTagPage(file, true, newLeaf);
}

async onload(){
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"0.5.9": "0.15.9",
"0.5.10": "0.15.9",
"0.5.5": "0.15.9",
"0.5.3": "0.14.5",
"0.5.2": "0.13.19",
Expand Down

0 comments on commit 87e0215

Please sign in to comment.