1
0

Adding an "enabled" attribute to templates

This commit is contained in:
Zef Hemel 2024-01-11 14:06:24 +01:00
parent 154d9a8ce9
commit 509ece91f0
9 changed files with 43 additions and 11 deletions

View File

@ -1,4 +1,5 @@
import { editor, space } from "$sb/syscalls.ts";
import { isFederationPath } from "$sb/lib/resolve.ts";
export async function deletePage() {
const pageName = await editor.getCurrentPage();
@ -13,9 +14,15 @@ export async function deletePage() {
await space.deletePage(pageName);
}
export async function copyPage() {
export async function copyPage(_def: any, predefinedNewName: string) {
const oldName = await editor.getCurrentPage();
const newName = await editor.prompt(`New page title:`, `${oldName} (copy)`);
let suggestedName = predefinedNewName || oldName;
if (isFederationPath(oldName)) {
const pieces = oldName.split("/");
suggestedName = pieces.slice(1).join("/");
}
const newName = await editor.prompt(`Copy to new page:`, suggestedName);
if (!newName) {
return;
@ -26,7 +33,7 @@ export async function copyPage() {
await space.getPageMeta(newName);
// So when we get to this point, we error out
throw new Error(
`Page ${newName} already exists, cannot rename to existing page.`,
`"${newName}" already exists, cannot copy to existing page.`,
);
} catch (e: any) {
if (e.message === "Not found") {

View File

@ -84,6 +84,7 @@ export const builtins: Record<string, Record<string, string>> = {
trigger: "string",
where: "string",
priority: "number",
enabled: "boolean",
},
};

View File

@ -31,7 +31,12 @@ export async function renderTemplateWidgets(side: "top" | "bottom"): Promise<
const allFrontMatterTemplates = await queryObjects<TemplateObject>(
"template",
{
filter: ["=", ["attr", "type"], ["string", `widget:${side}`]],
// where type = "widget:X" and enabled != false
filter: ["and", ["=", ["attr", "type"], ["string", `widget:${side}`]], [
"!=",
["attr", "enabled"],
["boolean", false],
]],
orderBy: [{ expr: ["attr", "priority"], desc: false }],
},
);

View File

@ -326,12 +326,17 @@ function render(
};
case "CommandLink": {
// Child 0 is CommandLinkMark, child 1 is CommandLinkPage
const commandText = t.children![1].children![0].text!;
const command = t.children![1].children![0].text!;
let commandText = command;
const aliasNode = findNodeOfType(t, "CommandLinkAlias");
if (aliasNode) {
commandText = aliasNode.children![0].text!;
}
return {
name: "button",
attrs: {
"data-onclick": JSON.stringify(["command", commandText]),
"data-onclick": JSON.stringify(["command", command]),
},
body: commandText,
};

View File

@ -51,8 +51,11 @@ export async function templateSlashComplete(
completeEvent: CompleteEvent,
): Promise<SlashCompletion[]> {
const allTemplates = await queryObjects<TemplateObject>("template", {
// Only return templates that have a trigger
filter: ["attr", "trigger"],
// Only return templates that have a trigger and are not expliclty disabled
filter: ["and", ["attr", "trigger"], ["!=", ["attr", "enabled"], [
"boolean",
false,
]]],
});
return allTemplates.map((template) => ({
label: template.trigger!,

View File

@ -12,6 +12,7 @@ Live Template Widgets follow the same pattern as other [[Templates]] with a few
* `type`: should be set to `widget:top` or `widget:bottom` depending on where you would like it to appear
* `where`: should contain an [[Live Queries$expression]] that evaluates to true for the _pages_ you would like to apply this template to, usually this checks for a specific tag, but it can be any expression. Think of this as a `where` clause that should match for the pages this template is applied to.
* `priority` (optional): in case you have multiple templates that have matching `where` expression, the one with the priority set to the lowest number wins.
* `enabled` (defaults to `true`): in case you want to (temporarily) disable this template, set this to `false`.
# Example
The following widget template applies to all pages tagged with `person` (see the `where`). It uses the [[!silverbullet.md/template/live/incoming]] template, to show all incomplete tasks that reference this particular page.

View File

@ -1,7 +1,7 @@
Slash templates allow you to define custom [[Slash Commands]] that expand “snippet style” templates inline.
# Definition
You can define a slash template by creating a [[Templates|template page]] with a template tag and `trigger` attribute.
You can define a slash template by creating a [[Templates|template page]] with a template tag and `trigger` attribute. If youd like to (temporarily) disable the template, you can set the `enabled` attribute to `false`.
Example:

View File

@ -5,7 +5,7 @@ This is an attempt at collecting useful, reusable templates so you dont have
While you may just copy & paste these templates to your own space, the most convenient ways to use them is using [[Federation]]. This will synchronize these templates into your space and make them available for use instantly.
To set this up, add the following this to your [[SETTINGS]]:
To set this up, add the following to your [[SETTINGS]]:
```yaml
federate:
@ -56,7 +56,7 @@ render [[template/documented-template]]
```
# Live Widget Templates
Use these to add [[Table of Contents]] and [[Linked Mentions]] to your pages.
Use these to add various useful [[Live Template Widgets]] to your pages.
```query
template

View File

@ -0,0 +1,10 @@
---
description: >
Adds a convenient "Copy to my space" button to any
template found via federation
tags: template
type: widget:bottom
where: 'name =~ /^!/ and tags="template"'
---
# Template actions
* {[Page: Copy|Copy to my space]}: use this template in your space by making a local copy of it (and tweaking it as you like)