import { codeWidget, editor, events } from "$sb/syscalls.ts"; import { parseQuery } from "$sb/lib/parse-query.ts"; import { loadPageObject, replaceTemplateVars } from "../template/page.ts"; import { resolvePath } from "$sb/lib/resolve.ts"; import { CodeWidgetContent } from "../../type/types.ts"; import { jsonToMDTable, renderQueryTemplate } from "../template/util.ts"; import { renderQuery } from "./api.ts"; export async function widget( bodyText: string, pageName: string, ): Promise { const pageObject = await loadPageObject(pageName); try { let resultMarkdown = ""; const parsedQuery = await parseQuery( await replaceTemplateVars(bodyText, pageObject), ); const results = await renderQuery(parsedQuery, { page: pageObject }); if (Array.isArray(results)) { resultMarkdown = jsonToMDTable(results); } else { resultMarkdown = results; } return { markdown: resultMarkdown, buttons: [ { description: "Edit", svg: ``, invokeFunction: "query.editButton", }, { description: "Reload", svg: ``, invokeFunction: "query.refreshAllWidgets", }, ], }; } catch (e: any) { return { markdown: `**Error:** ${e.message}` }; } } export function refreshAllWidgets() { codeWidget.refreshAll(); } export async function editButton(pos: number) { await editor.moveCursor(pos); }