Adding an "enabled" attribute to templates
This commit is contained in:
parent
154d9a8ce9
commit
509ece91f0
@ -1,4 +1,5 @@
|
|||||||
import { editor, space } from "$sb/syscalls.ts";
|
import { editor, space } from "$sb/syscalls.ts";
|
||||||
|
import { isFederationPath } from "$sb/lib/resolve.ts";
|
||||||
|
|
||||||
export async function deletePage() {
|
export async function deletePage() {
|
||||||
const pageName = await editor.getCurrentPage();
|
const pageName = await editor.getCurrentPage();
|
||||||
@ -13,9 +14,15 @@ export async function deletePage() {
|
|||||||
await space.deletePage(pageName);
|
await space.deletePage(pageName);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function copyPage() {
|
export async function copyPage(_def: any, predefinedNewName: string) {
|
||||||
const oldName = await editor.getCurrentPage();
|
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) {
|
if (!newName) {
|
||||||
return;
|
return;
|
||||||
@ -26,7 +33,7 @@ export async function copyPage() {
|
|||||||
await space.getPageMeta(newName);
|
await space.getPageMeta(newName);
|
||||||
// So when we get to this point, we error out
|
// So when we get to this point, we error out
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Page ${newName} already exists, cannot rename to existing page.`,
|
`"${newName}" already exists, cannot copy to existing page.`,
|
||||||
);
|
);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
if (e.message === "Not found") {
|
if (e.message === "Not found") {
|
||||||
|
@ -84,6 +84,7 @@ export const builtins: Record<string, Record<string, string>> = {
|
|||||||
trigger: "string",
|
trigger: "string",
|
||||||
where: "string",
|
where: "string",
|
||||||
priority: "number",
|
priority: "number",
|
||||||
|
enabled: "boolean",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -31,7 +31,12 @@ export async function renderTemplateWidgets(side: "top" | "bottom"): Promise<
|
|||||||
const allFrontMatterTemplates = await queryObjects<TemplateObject>(
|
const allFrontMatterTemplates = await queryObjects<TemplateObject>(
|
||||||
"template",
|
"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 }],
|
orderBy: [{ expr: ["attr", "priority"], desc: false }],
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -326,12 +326,17 @@ function render(
|
|||||||
};
|
};
|
||||||
case "CommandLink": {
|
case "CommandLink": {
|
||||||
// Child 0 is CommandLinkMark, child 1 is CommandLinkPage
|
// 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 {
|
return {
|
||||||
name: "button",
|
name: "button",
|
||||||
attrs: {
|
attrs: {
|
||||||
"data-onclick": JSON.stringify(["command", commandText]),
|
"data-onclick": JSON.stringify(["command", command]),
|
||||||
},
|
},
|
||||||
body: commandText,
|
body: commandText,
|
||||||
};
|
};
|
||||||
|
@ -51,8 +51,11 @@ export async function templateSlashComplete(
|
|||||||
completeEvent: CompleteEvent,
|
completeEvent: CompleteEvent,
|
||||||
): Promise<SlashCompletion[]> {
|
): Promise<SlashCompletion[]> {
|
||||||
const allTemplates = await queryObjects<TemplateObject>("template", {
|
const allTemplates = await queryObjects<TemplateObject>("template", {
|
||||||
// Only return templates that have a trigger
|
// Only return templates that have a trigger and are not expliclty disabled
|
||||||
filter: ["attr", "trigger"],
|
filter: ["and", ["attr", "trigger"], ["!=", ["attr", "enabled"], [
|
||||||
|
"boolean",
|
||||||
|
false,
|
||||||
|
]]],
|
||||||
});
|
});
|
||||||
return allTemplates.map((template) => ({
|
return allTemplates.map((template) => ({
|
||||||
label: template.trigger!,
|
label: template.trigger!,
|
||||||
|
@ -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
|
* `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.
|
* `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.
|
* `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
|
# 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.
|
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.
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
Slash templates allow you to define custom [[Slash Commands]] that expand “snippet style” templates inline.
|
Slash templates allow you to define custom [[Slash Commands]] that expand “snippet style” templates inline.
|
||||||
|
|
||||||
# Definition
|
# 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 you’d like to (temporarily) disable the template, you can set the `enabled` attribute to `false`.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ This is an attempt at collecting useful, reusable templates so you don’t 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.
|
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
|
```yaml
|
||||||
federate:
|
federate:
|
||||||
@ -56,7 +56,7 @@ render [[template/documented-template]]
|
|||||||
```
|
```
|
||||||
|
|
||||||
# Live Widget Templates
|
# 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
|
```query
|
||||||
template
|
template
|
||||||
|
10
website/template/widget/federated-template-copy.md
Normal file
10
website/template/widget/federated-template-copy.md
Normal 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)
|
Loading…
Reference in New Issue
Block a user