mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-24 09:48:28 +02:00
Add timestamp() to template fns
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::time::SystemTime;
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
use sqlx::types::{Json, JsonValue};
|
use sqlx::types::{Json, JsonValue};
|
||||||
|
|
||||||
use crate::models::{
|
use crate::models::{
|
||||||
@@ -103,7 +104,20 @@ pub fn variables_from_environment(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(template: &str, vars: &HashMap<String, String>) -> String {
|
pub fn render(template: &str, vars: &HashMap<String, String>) -> String {
|
||||||
parse_and_render(template, vars, None)
|
parse_and_render(template, vars, Some(template_callback))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn template_callback(name: &str, _args: HashMap<String, String>) -> String {
|
||||||
|
match name {
|
||||||
|
"timestamp" => {
|
||||||
|
let now = SystemTime::now();
|
||||||
|
let now: DateTime<Utc> = now.into();
|
||||||
|
now.to_rfc3339()
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
"".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_variable_to_map(
|
fn add_variable_to_map(
|
||||||
|
|||||||
@@ -3,21 +3,32 @@ import { Decoration, EditorView, ViewPlugin, WidgetType } from '@codemirror/view
|
|||||||
import { BetterMatchDecorator } from '../BetterMatchDecorator';
|
import { BetterMatchDecorator } from '../BetterMatchDecorator';
|
||||||
|
|
||||||
class PlaceholderWidget extends WidgetType {
|
class PlaceholderWidget extends WidgetType {
|
||||||
constructor(readonly name: string, readonly isExistingVariable: boolean) {
|
constructor(
|
||||||
|
readonly name: string,
|
||||||
|
readonly exists: boolean,
|
||||||
|
readonly type: 'function' | 'variable' = 'variable',
|
||||||
|
) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
eq(other: PlaceholderWidget) {
|
eq(other: PlaceholderWidget) {
|
||||||
return this.name == other.name && this.isExistingVariable == other.isExistingVariable;
|
return this.name == other.name && this.exists == other.exists;
|
||||||
}
|
}
|
||||||
|
|
||||||
toDOM() {
|
toDOM() {
|
||||||
const elt = document.createElement('span');
|
const elt = document.createElement('span');
|
||||||
elt.className = `x-theme-placeholder placeholder ${
|
elt.className = `x-theme-placeholder placeholder ${
|
||||||
this.isExistingVariable ? 'x-theme-placeholder--primary' : 'x-theme-placeholder--danger'
|
!this.exists
|
||||||
|
? 'x-theme-placeholder--danger'
|
||||||
|
: this.type === 'variable'
|
||||||
|
? 'x-theme-placeholder--primary'
|
||||||
|
: 'x-theme-placeholder--info'
|
||||||
}`;
|
}`;
|
||||||
elt.title = !this.isExistingVariable ? 'Variable not found in active environment' : '';
|
elt.title = !this.exists ? 'Variable not found in active environment' : '';
|
||||||
elt.textContent = this.name;
|
elt.textContent = this.name;
|
||||||
return elt;
|
return elt;
|
||||||
}
|
}
|
||||||
|
|
||||||
ignoreEvent() {
|
ignoreEvent() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -43,11 +54,13 @@ export const placeholders = function (variables: { name: string }[]) {
|
|||||||
return Decoration.replace({});
|
return Decoration.replace({});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isFunction = groupMatch.includes('(');
|
||||||
return Decoration.replace({
|
return Decoration.replace({
|
||||||
inclusive: true,
|
inclusive: true,
|
||||||
widget: new PlaceholderWidget(
|
widget: new PlaceholderWidget(
|
||||||
groupMatch,
|
groupMatch,
|
||||||
variables.some((v) => v.name === groupMatch),
|
isFunction ? true : variables.some((v) => v.name === groupMatch),
|
||||||
|
isFunction ? 'function' : 'variable',
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -56,9 +69,11 @@ export const placeholders = function (variables: { name: string }[]) {
|
|||||||
return ViewPlugin.fromClass(
|
return ViewPlugin.fromClass(
|
||||||
class {
|
class {
|
||||||
placeholders: DecorationSet;
|
placeholders: DecorationSet;
|
||||||
|
|
||||||
constructor(view: EditorView) {
|
constructor(view: EditorView) {
|
||||||
this.placeholders = placeholderMatcher.createDeco(view);
|
this.placeholders = placeholderMatcher.createDeco(view);
|
||||||
}
|
}
|
||||||
|
|
||||||
update(update: ViewUpdate) {
|
update(update: ViewUpdate) {
|
||||||
this.placeholders = placeholderMatcher.updateDeco(update, this.placeholders);
|
this.placeholders = placeholderMatcher.updateDeco(update, this.placeholders);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user