diff --git a/README.md b/README.md index 2ab56d9..edefc48 100644 --- a/README.md +++ b/README.md @@ -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 } 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.) diff --git a/manifest.json b/manifest.json index f871b3f..7bc8cb2 100644 --- a/manifest.json +++ b/manifest.json @@ -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 diff --git a/src/plugin.js b/src/plugin.js index 1840237..5f6550d 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -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(){ diff --git a/versions.json b/versions.json index 820c542..01cfab1 100644 --- a/versions.json +++ b/versions.json @@ -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",