Compare commits

..

3 Commits

Author SHA1 Message Date
Gregory Schier
badcbc7aef Special case Response()->response() 2024-08-26 15:26:43 -07:00
Gregory Schier
4b91601b98 Clean up code 2024-08-26 15:10:29 -07:00
Gregory Schier
93e0202b86 Default template fn args 2024-08-26 13:10:22 -07:00
2 changed files with 56 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
use std::collections::HashMap;
use tauri::{AppHandle, Manager};
use yaak_plugin_runtime::events::RenderPurpose;
use yaak_plugin_runtime::events::{RenderPurpose, TemplateFunctionArg};
use yaak_plugin_runtime::manager::PluginManager;
use yaak_templates::TemplateCallback;
@@ -12,7 +12,10 @@ pub struct PluginTemplateCallback {
impl PluginTemplateCallback {
pub fn new(app_handle: AppHandle) -> PluginTemplateCallback {
PluginTemplateCallback { app_handle, purpose: RenderPurpose::Preview }
PluginTemplateCallback {
app_handle,
purpose: RenderPurpose::Preview,
}
}
pub fn for_send(&self) -> PluginTemplateCallback {
@@ -24,9 +27,41 @@ impl PluginTemplateCallback {
impl TemplateCallback for PluginTemplateCallback {
async fn run(&self, fn_name: &str, args: HashMap<String, String>) -> Result<String, String> {
// The beta named the function `Response` but was changed in stable.
// Keep this here for a while because there's no easy way to migrate
let fn_name = if fn_name == "Response" {
"response"
} else {
fn_name
};
let plugin_manager = self.app_handle.state::<PluginManager>();
let function = plugin_manager
.get_template_functions()
.await
.map_err(|e| e.to_string())?
.iter()
.flat_map(|f| f.functions.clone())
.find(|f| f.name == fn_name)
.ok_or("")?;
let mut args_with_defaults = args.clone();
// Fill in default values for all args
for a_def in function.args {
let base = match a_def {
TemplateFunctionArg::Text(a) => a.base,
TemplateFunctionArg::Select(a) => a.base,
TemplateFunctionArg::Checkbox(a) => a.base,
TemplateFunctionArg::HttpRequest(a) => a.base,
};
if let None = args_with_defaults.get(base.name.as_str()) {
args_with_defaults.insert(base.name, base.default_value.unwrap_or_default());
}
}
let resp = plugin_manager
.call_template_function(fn_name, args, self.purpose.clone())
.call_template_function(fn_name, args_with_defaults, self.purpose.clone())
.await
.map_err(|e| e.to_string())?;
Ok(resp.unwrap_or_default())

View File

@@ -68,7 +68,7 @@ function templateTags(
syntaxTree(view.state).iterate({
from,
to,
enter: (node) => {
enter(node) {
if (node.name == 'Tag') {
// Don't decorate if the cursor is inside the match
for (const r of view.state.selection.ranges) {
@@ -81,7 +81,14 @@ function templateTags(
// TODO: Search `node.tree` instead of using Regex here
const inner = rawTag.replace(/^\$\{\[\s*/, '').replace(/\s*]}$/, '');
const name = inner.match(/(\w+)[(]/)?.[1] ?? inner;
let name = inner.match(/(\w+)[(]/)?.[1] ?? inner;
// The beta named the function `Response` but was changed in stable.
// Keep this here for a while because there's no easy way to migrate
if (name === 'Response') {
name = 'response';
}
let option = options.find((v) => v.name === name);
if (option == null) {
option = {
@@ -93,6 +100,7 @@ function templateTags(
onClick: () => onClickMissingVariable(name, rawTag, node.from),
};
}
const widget = new TemplateTagWidget(option, rawTag, node.from);
const deco = Decoration.replace({ widget, inclusive: true });
widgets.push(deco.range(node.from, node.to));
@@ -120,14 +128,16 @@ export function templateTagsPlugin(
}
},
{
decorations: (v) => v.decorations,
provide: (plugin) =>
EditorView.atomicRanges.of((view) => {
decorations(v) {
return v.decorations;
},
provide(plugin) {
return EditorView.atomicRanges.of((view) => {
return view.plugin(plugin)?.decorations || Decoration.none;
}),
});
},
eventHandlers: {
mousedown: (e) => {
mousedown(e) {
const target = e.target as HTMLElement;
if (target.classList.contains('template-tag')) console.log('CLICKED TEMPLATE TAG');
// return toggleBoolean(view, view.posAtDOM(target));