Skip to content

Commit

Permalink
App: Allow interpolation string as the only value on input-code int…
Browse files Browse the repository at this point in the history
…erface (#22318)

Co-authored-by: Pascal Jufer <[email protected]>
  • Loading branch information
joselcvarela and paescuj committed May 3, 2024
1 parent 6ad1c22 commit 4de6079
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-donuts-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@directus/app": patch
---

Added support to define single interpolation value in code interface
16 changes: 16 additions & 0 deletions app/src/interfaces/input-code/input-code.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import 'codemirror/addon/search/searchcursor.js';
import 'codemirror/keymap/sublime.js';
/** Regex to check for interpolation, e.g. `{{ $trigger }}` */
const INTERPOLATION_REGEX = /^\{\{\s*[^}\s]+\s*\}\}$/;
const props = withDefaults(
defineProps<{
value?: string | Record<string, unknown> | unknown[] | boolean | number | null;
Expand Down Expand Up @@ -76,6 +79,10 @@ onMounted(async () => {
return emit('input', null);
}
if (isInterpolation(content)) {
return emit('input', content);
}
try {
const parsedJson = JSON.parse(content);
if (typeof parsedJson !== 'string') return emit('input', parsedJson);
Expand All @@ -93,6 +100,8 @@ onMounted(async () => {
const stringValue = computed(() => {
if (props.value === null || props.value === undefined) return '';
if (props.type === 'json' && isInterpolation(props.value)) return props.value;
return getStringifiedValue(props.value, props.type === 'json');
});
Expand Down Expand Up @@ -126,6 +135,9 @@ async function setLanguage() {
CodeMirror.registerHelper('lint', 'json', (text: string) => {
const found: Record<string, any>[] = [];
if (isInterpolation(text)) return found;
const parser = jsonlint.parser;
parser.parseError = (str: string, hash: any) => {
Expand Down Expand Up @@ -278,6 +290,10 @@ function fillTemplate() {
emit('input', props.template);
}
}
function isInterpolation(value: any) {
return typeof value === 'string' && value.match(INTERPOLATION_REGEX);
}
</script>

<template>
Expand Down

0 comments on commit 4de6079

Please sign in to comment.