-
I have written a number of small, JavaScript-based, Full House Templates. I'm impressed with the power of this plugin. My main objective is to modify existing blocks, with the use of keyboard shortcuts. However, I would like to reduce code duplication in my templates. Here are a couple of examples: Assign this block to a Project: ``{
// do not modify block content
blocks.skip()
const selected = (await logseq.Editor.getSelectedBlocks())[0]
if (selected) {
const content = block.content
const newContent = content + !content.toLowerCase().includes('project::') ? `\nproject:: [[>/Or/Projects/${cursor()}]]` : ''
// Update the block with the new content
await logseq.Editor.updateBlock(block.uuid, newContent)
}
}`` Convert this block to an Action and add an ``{
// do not modify block content
blocks.skip()
const selected = (await logseq.Editor.getSelectedBlocks())[0]
if (selected) {
let content = selected.content
content = !content.startsWith('TODO ') ? `TODO ${content}` : content
const newContent = content + !content.toLowerCase().includes('action-type::') ? `\naction-type:: [[to/${cursor()}]]` : ''
// Update the block with the new content
await logseq.Editor.updateBlock(selected.uuid, newContent)
}
}`` As you can see, both of these templates add a property to the block. The corresponding code differs only in the property name and property value. I would like to extract a function that would look something like this, and could be called from both templates: function addPropertyToContent(content, propertyName, propertyValue) {
const newContent = content + !content.toLowerCase().includes(`${propertyName}::`) ? `\n${propertyName}:: ${propertyValue}` : ''
return newContent
} I have read the JavaScript Environment section of the documentation, which suggests that I should be able do what I've described above, but I haven't been able to get it to work. I've tried adding a block with the following code to the same page, but my templates complain that ``{
function fnAddPropertyToContent(content, propertyName, propertyValue) {
const newContent = content + !content.toLowerCase().includes(`${propertyName}::`) ? `\n${propertyName}:: ${propertyValue}` : ''
return newContent
}
var addPropertyToContent = fnAddPropertyToContent
}`` I'd be grateful if you could give me step-by-step instructions to achieve what I've decribed. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hello, @mjkaye! Thank you ❤️
Quick answer: JS env works only inside the particular template. There is no cross-templated js-functions. Long answer: I'll provide step-by-step instructions after weekend. |
Beta Was this translation helpful? Give feedback.
Here is the solution:
Notes