Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Parameterized Buttons in Template Queries to Trigger Actions based on the Row #1140

Closed
simone-viozzi opened this issue Nov 6, 2024 · 2 comments

Comments

@simone-viozzi
Copy link

Hi, my use case is:

Currently, SilverBullet does not have a robust attachment management system ( see #72 ).

To address this, I created a space script that allows querying for orphaned attachments:

silverbullet.registerFunction({ name: "getOrphanAttachments" }, async () => {
  // List of folders to exclude
  const excludeFolders = ["_plugs", "silverbullet_utils"];

  // Get all attachments in the space, filtering out excluded folders
  const allAttachments = await space.listAttachments();
  const filteredAttachments = allAttachments.filter(attachment => {
    return !excludeFolders.some(folder => attachment.name.startsWith(`${folder}/`));
  });

  // Get all links in notes pointing to files
  const allLinks = await datastore.query({ type: "link" });

  // Filter links to include only those with the "link" tag and a non-empty `toFile`
  const linkedFiles = allLinks
    .filter(entry => entry.value.tag === "link" && entry.value.toFile && entry.value.toFile.trim() !== "")
    .map(entry => entry.value.toFile);

  // Find attachments that are not present in links
  const orphanAttachments = filteredAttachments.filter(attachment => {
    return !linkedFiles.includes(attachment.name);
  });

  return orphanAttachments;
});

Now I would like to create a query from this list where each row of the query has a delete button.

Something like:

{{#each getOrphanAttachments()}}
- [[{{ref}}]] - [Delete](deleteFile "{{name}}")
{{/each}}

or

{{#each getOrphanAttachments()}}
- [[{{ref}}]] - {[deleteFile "{{name}}"]}
{{/each}}

something like this:

image

Request:

What type of space script do I need to implement so that clicking the button/link would trigger the space.deleteAttachment(fileName) syscall?

Side Issue:

When clicking on the link of the file reference in a query result, I get an error: Page name cannot end with a file extension. However, when the result of the query is embedded, clicking the file reference opens the file in a new tab, as expected.

Thank you

@simone-viozzi simone-viozzi changed the title Support for Parameterized Buttons in Template Queries to Trigger specific Actions based on the row Support for Parameterized Buttons in Template Queries to Trigger Actions based on the Row Nov 6, 2024
@wunter8
Copy link

wunter8 commented Dec 30, 2024

I found a "Delete Page" function on the community forum. It seems like you could do something similar for your "Delete Attachment" function: https://community.silverbullet.md/t/my-first-hours-with-sb/743

@simone-viozzi
Copy link
Author

silverbullet.registerCommand({ name: "Delete It" }, async (name) => {
  if (name) {
    try {
      // Add ./ in front of the name
      name = `./${name}`;
      
      // Delete the file
      await syscall("space.deleteFile", name);
      await syscall("editor.flashNotification", `File Name ${name} Deleted!`);
      await syscall("codeWidget.refreshAll");
    
    } catch (error) {
      await syscall("editor.flashNotification", `An error occurred: ${error.message}`);
    }
  } else {
    await syscall("editor.flashNotification", "No file name provided!");
  }
});
```template
{{#each getOrphanAttachments()}}
- [[{{ref}}]] - {[Delete It]("{{ref}}")}
{{/each}}
```

Done it!!

this will render like:

image

if you add a ! in the query (like ![[{{ref}}]]) it will also render each attachment, so you can see them right away.

Thank you @wunter8!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants