diff --git a/src/app.tsx b/src/app.tsx index 33ce025..e4f9a7b 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -40,7 +40,7 @@ async function onAppSettingsChanged() { dayjs.updateLocale('en', { weekStart: 1, }) - } +} async function init() { if (DEV) { @@ -225,15 +225,15 @@ async function main() { showInsertUI(e.uuid) }) - registerBlockContextCopyCommand('Copy as 🏛template', commandTemplateName) - registerPageContextCopyCommand('Copy as 🏛template', commandTemplateName) + registerBlockContextCopyCommand('Copy as 🏛template', false) + registerPageContextCopyCommand('Copy as 🏛template', false) handleTemplateCommand(commandTemplateName) } const commandTemplateViewName = 'template-view' { - registerBlockContextCopyCommand('Copy as 🏛view', commandTemplateViewName) - registerPageContextCopyCommand('Copy as 🏛view', commandTemplateViewName) + registerBlockContextCopyCommand('Copy as 🏛view', true) + registerPageContextCopyCommand('Copy as 🏛view', true) handleTemplateViewCommand(commandTemplateViewName) } @@ -241,7 +241,10 @@ async function main() { { const commandLabel = 'Insert inline 🏛view' const code = 'c.page.name' - const commandGuide = RendererMacro.command(commandViewName).arg(`"${code}"`).toString() + const commandGuide = RendererMacro + .command(commandViewName) + .arg(`"${code}"`, {raw: true}) + .toString() logseq.App.registerCommandPalette({ key: 'insert-inline-view', @@ -270,10 +273,10 @@ async function main() { await postInit() } -function registerBlockContextCopyCommand(label: string, commandName: string) { +function registerBlockContextCopyCommand(label: string, isView: boolean) { logseq.Editor.registerBlockContextMenuItem( label, async (e) => { - const macro = await templateMacroStringForBlock(e.uuid) + const macro = await templateMacroStringForBlock(e.uuid, isView) if (!macro) { console.debug(p`Assertion error: block should exists`, e.uuid) return @@ -285,11 +288,10 @@ function registerBlockContextCopyCommand(label: string, commandName: string) { await logseq.UI.showMsg('Copied to clipboard', 'success', {timeout: 5000}) }) } - -function registerPageContextCopyCommand(label: string, commandName: string) { +function registerPageContextCopyCommand(label: string, isView: boolean) { logseq.App.registerPageMenuItem( label, async ({ page: pageName }) => { - const command = await templateMacroStringForPage(pageName) + const command = await templateMacroStringForPage(pageName, isView) if (!command) { console.debug(p`Assertion error: page should exists`, pageName) return @@ -310,7 +312,7 @@ async function handleRequiredRef(ref: string, refUserName: string) { } return parseReference(ref)! - } +} async function handleLogicErrors(func: Function) { try { await func() @@ -324,7 +326,7 @@ async function handleLogicErrors(func: Function) { else console.error(p`${(error as Error).stack}`) } - } +} function handleTemplateCommand(commandName: string) { let unload = logseq.App.onMacroRendererSlotted(async ({ slot, payload }) => { @@ -350,7 +352,7 @@ function handleTemplateCommand(commandName: string) { }) }) logseq.beforeunload(unload as unknown as () => Promise) - } +} function handleTemplateViewCommand(commandName: string) { logseq.provideModel({ async editBlock(e: any) { @@ -423,7 +425,7 @@ function handleTemplateViewCommand(commandName: string) { }) }) logseq.beforeunload(unload as unknown as () => Promise) - } +} function handleViewCommand(commandName: string) { const unload = logseq.App.onMacroRendererSlotted(async ({ slot, payload }) => { const uuid = payload.uuid @@ -446,11 +448,11 @@ function handleViewCommand(commandName: string) { console.debug(p`Rendering view`, {slot, uuid, viewBody, args}) await handleLogicErrors(async () => { - await renderView(slot, uuid, viewBody, raw, args) + await renderView(slot, uuid, viewBody, args) }) }) logseq.beforeunload(unload as unknown as () => Promise) - } +} export const App = (logseq: any) => { diff --git a/src/context.ts b/src/context.ts index e74aa3c..aef3041 100644 --- a/src/context.ts +++ b/src/context.ts @@ -54,17 +54,26 @@ dayjs.extend(updateLocale) dayjs.extend(logseqPlugin) -export interface ILogseqContext { +export interface ILogseqCurrentContext { + mode: 'template' | 'view' + currentPage: PageContext + currentBlock: BlockContext +} + +export interface ILogseqCallContext { identity: Context | { slot: string, key: string, } config: ConfigContext + page: PageContext | null + block: BlockContext | null + args: ArgsContext +} + +export interface ILogseqContext extends ILogseqCallContext, ILogseqCurrentContext { page: PageContext - currentPage: PageContext block: BlockContext - currentBlock: BlockContext - args: ArgsContext tags?: Context self?: BlockContext diff --git a/src/extensions/customized_eta.ts b/src/extensions/customized_eta.ts index 4548a9d..76d9b1d 100644 --- a/src/extensions/customized_eta.ts +++ b/src/extensions/customized_eta.ts @@ -194,7 +194,7 @@ function compileBody(buff) { // @ts-expect-error const config = this.config - let returnStr = '' + let returnStr = 'include = undefined; includeAsync = undefined; layout = undefined;\n' for (const currentBlock of buff) { if (typeof currentBlock === 'string') { const str = currentBlock diff --git a/src/logic.ts b/src/logic.ts index 5639dbd..d6df43b 100644 --- a/src/logic.ts +++ b/src/logic.ts @@ -5,7 +5,7 @@ import { LogseqMarkup } from './extensions/mldoc_ast' import { InlineTemplate, ITemplate, Template } from './template' import { ILogseqContext, Context, PageContext, BlockContext, - ArgsContext, ConfigContext, + ArgsContext, ConfigContext, ILogseqCurrentContext, ILogseqCallContext, } from './context' import { p, IBlockNode, lockOn, sleep, LogseqReference, getPage, getBlock, @@ -17,15 +17,10 @@ import { import { RenderError, StateError, StateMessage } from './errors' -/** - * @raises StateError: Arg `:page` doesn't exist or improperly specified - */ async function getCurrentContext( - slot: string, - template: ITemplate, blockUUID: string, - argsContext: ArgsContext, -): Promise { + mode: ILogseqCurrentContext['mode'], +): Promise { if (!blockUUID) { // Where is uuid? It definitely should be here, but this is a bug: // https://github.com/logseq/logseq/issues/8904 @@ -33,6 +28,32 @@ async function getCurrentContext( return null } + const currentBlock = await logseq.Editor.getBlock(blockUUID) + if (!currentBlock) { + console.debug(p`logseq issue → rendering non-existed block / slot`) + return null + } + + const currentPage = await logseq.Editor.getPage(currentBlock.page.id) as PageEntity + const currentPageContext = PageContext.createFromEntity(currentPage) + const currentBlockContext = BlockContext.createFromEntity(currentBlock, { page: currentPageContext }) + + return { + mode, + currentPage: currentPageContext, + currentBlock: currentBlockContext, + } +} + +/** + * @raises StateError: Arg `:page` doesn't exist or improperly specified + * @raises StateError: Arg `:block` doesn't exist or improperly specified + */ +async function getCallContext( + slot: string, + template: ITemplate, + argsContext: ArgsContext, +): Promise { // fulfill args with template arg-props const argsProps = template.getArgProperties() for (const [ key, value ] of Object.entries(argsProps)) @@ -80,35 +101,41 @@ async function getCurrentContext( contextBlock = blockExists } - const currentBlock = await logseq.Editor.getBlock(blockUUID) - if (!currentBlock) { - console.debug(p`logseq issue → rendering non-existed block / slot`) - return null - } - - const currentPage = await logseq.Editor.getPage(currentBlock.page.id) as PageEntity - const currentPageContext = PageContext.createFromEntity(currentPage) - const currentBlockContext = BlockContext.createFromEntity(currentBlock, { page: currentPageContext }) - argsContext._hideUndefinedMode = true return { identity: new Context({ slot, key: slot.split('__', 2)[1].trim() }), config: await ConfigContext.get(), - page: contextPage ? PageContext.createFromEntity(contextPage) : currentPageContext, - currentPage: currentPageContext, + page: contextPage ? PageContext.createFromEntity(contextPage) : null, + block: contextBlock ? BlockContext.createFromEntity(contextBlock) : null, + args: argsContext, + } +} - block: contextBlock ? BlockContext.createFromEntity(contextBlock) : currentBlockContext, - currentBlock: currentBlockContext, +async function getContext( + callContext: ILogseqCallContext, + currentContext: ILogseqCurrentContext, +): Promise { + return { + mode: currentContext.mode, + identity: callContext.identity, + config: callContext.config, - args: argsContext, + page: callContext.page || currentContext.currentPage, + currentPage: currentContext.currentPage, + + block: callContext.block || currentContext.currentBlock, + currentBlock: currentContext.currentBlock, + + args: callContext.args, } - } +} + /** * @raises StateError: template doesn't exist */ -async function getTemplateBlock( +export async function getTemplateBlock( templateRef: LogseqReference ): Promise<[BlockEntity, string | undefined, LogseqReferenceAccessType]> { let templateBlock: BlockEntity | null @@ -144,7 +171,7 @@ async function getTemplateBlock( return [ templateBlock, name, accessedVia ] } -async function getTemplate(ref: LogseqReference): Promise