Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App: Allow interpolation string as the only value on input-code interface #22318

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
---

Check warning on line 1 in .changeset/wise-donuts-sit.md

View workflow job for this annotation

GitHub Actions / Lint

File ignored by default.
"@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