2024-03-13 04:57:08 -07:00
|
|
|
<script setup lang="ts">
|
|
|
|
import { useI18n } from '@/composables/useI18n';
|
|
|
|
import { useNDVStore } from '@/stores/ndv.store';
|
2024-03-26 07:23:30 -07:00
|
|
|
import { computed, ref, watch } from 'vue';
|
|
|
|
import { EditorSelection, EditorState, type SelectionRange } from '@codemirror/state';
|
|
|
|
import { type Completion, CompletionContext } from '@codemirror/autocomplete';
|
|
|
|
import { datatypeCompletions } from '@/plugins/codemirror/completions/datatype.completions';
|
|
|
|
import { watchDebounced } from '@vueuse/core';
|
|
|
|
import { FIELDS_SECTION } from '@/plugins/codemirror/completions/constants';
|
|
|
|
import { isCompletionSection } from '@/plugins/codemirror/completions/utils';
|
|
|
|
|
|
|
|
type TipId = 'executePrevious' | 'drag' | 'default' | 'dotObject' | 'dotPrimitive';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
editorState?: EditorState;
|
|
|
|
unresolvedExpression?: string;
|
|
|
|
selection?: SelectionRange;
|
|
|
|
};
|
|
|
|
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
|
editorState: undefined,
|
|
|
|
unresolvedExpression: '',
|
|
|
|
selection: () => EditorSelection.cursor(0),
|
|
|
|
});
|
2024-03-13 04:57:08 -07:00
|
|
|
|
|
|
|
const i18n = useI18n();
|
|
|
|
const ndvStore = useNDVStore();
|
|
|
|
|
2024-03-26 07:23:30 -07:00
|
|
|
const canAddDotToExpression = ref(false);
|
|
|
|
const resolvedExpressionHasFields = ref(false);
|
|
|
|
|
|
|
|
const canDragToFocusedInput = computed(
|
|
|
|
() => !ndvStore.isNDVDataEmpty('input') && ndvStore.focusedMappableInput,
|
|
|
|
);
|
|
|
|
|
|
|
|
const emptyExpression = computed(() => props.unresolvedExpression.trim().length === 0);
|
2024-03-13 04:57:08 -07:00
|
|
|
|
2024-03-26 07:23:30 -07:00
|
|
|
const tip = computed<TipId>(() => {
|
|
|
|
if (!ndvStore.hasInputData && ndvStore.isInputParentOfActiveNode) {
|
2024-03-13 04:57:08 -07:00
|
|
|
return 'executePrevious';
|
|
|
|
}
|
|
|
|
|
2024-03-26 07:23:30 -07:00
|
|
|
if (canAddDotToExpression.value) {
|
|
|
|
return resolvedExpressionHasFields.value ? 'dotObject' : 'dotPrimitive';
|
|
|
|
}
|
2024-03-13 04:57:08 -07:00
|
|
|
|
2024-03-26 07:23:30 -07:00
|
|
|
if (canDragToFocusedInput.value && emptyExpression.value) return 'drag';
|
2024-03-13 04:57:08 -07:00
|
|
|
|
|
|
|
return 'default';
|
|
|
|
});
|
2024-03-26 07:23:30 -07:00
|
|
|
|
|
|
|
function getCompletionsWithDot(): readonly Completion[] {
|
|
|
|
if (!props.editorState || !props.selection || !props.unresolvedExpression) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const cursorAfterDot = props.selection.from + 1;
|
|
|
|
const docWithDot =
|
|
|
|
props.editorState.sliceDoc(0, props.selection.from) +
|
|
|
|
'.' +
|
|
|
|
props.editorState.sliceDoc(props.selection.to);
|
|
|
|
const selectionWithDot = EditorSelection.create([EditorSelection.cursor(cursorAfterDot)]);
|
|
|
|
|
|
|
|
if (cursorAfterDot >= docWithDot.length) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const stateWithDot = EditorState.create({
|
|
|
|
doc: docWithDot,
|
|
|
|
selection: selectionWithDot,
|
|
|
|
});
|
|
|
|
|
|
|
|
const context = new CompletionContext(stateWithDot, cursorAfterDot, true);
|
|
|
|
const completionResult = datatypeCompletions(context);
|
|
|
|
return completionResult?.options ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
watch(tip, (newTip) => {
|
|
|
|
ndvStore.setHighlightDraggables(!ndvStore.isMappingOnboarded && newTip === 'drag');
|
|
|
|
});
|
|
|
|
|
|
|
|
watchDebounced(
|
|
|
|
[() => props.selection, () => props.unresolvedExpression],
|
|
|
|
() => {
|
|
|
|
const completions = getCompletionsWithDot();
|
|
|
|
canAddDotToExpression.value = completions.length > 0;
|
|
|
|
resolvedExpressionHasFields.value = completions.some(
|
|
|
|
({ section }) => isCompletionSection(section) && section.name === FIELDS_SECTION.name,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
{ debounce: 200 },
|
|
|
|
);
|
2024-03-13 04:57:08 -07:00
|
|
|
</script>
|
|
|
|
|
2024-03-26 07:23:30 -07:00
|
|
|
<template>
|
|
|
|
<div :class="[$style.tip, { [$style.drag]: tip === 'drag' }]">
|
|
|
|
<n8n-text size="small" :class="$style.tipText"
|
|
|
|
>{{ i18n.baseText('parameterInput.tip') }}:
|
|
|
|
</n8n-text>
|
|
|
|
|
|
|
|
<div v-if="tip === 'drag'" :class="$style.content">
|
|
|
|
<n8n-text size="small" :class="$style.text">
|
|
|
|
{{ i18n.baseText('parameterInput.dragTipBeforePill') }}
|
|
|
|
</n8n-text>
|
|
|
|
<div :class="[$style.pill, { [$style.highlight]: !ndvStore.isMappingOnboarded }]">
|
|
|
|
{{ i18n.baseText('parameterInput.inputField') }}
|
|
|
|
</div>
|
|
|
|
<n8n-text size="small" :class="$style.text">
|
|
|
|
{{ i18n.baseText('parameterInput.dragTipAfterPill') }}
|
|
|
|
</n8n-text>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div v-else-if="tip === 'executePrevious'" :class="$style.content">
|
|
|
|
<span> {{ i18n.baseText('expressionTip.noExecutionData') }} </span>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div v-else-if="tip === 'dotPrimitive'" :class="$style.content">
|
|
|
|
<span v-html="i18n.baseText('expressionTip.typeDotPrimitive')" />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div v-else-if="tip === 'dotObject'" :class="$style.content">
|
|
|
|
<span v-html="i18n.baseText('expressionTip.typeDotObject')" />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div v-else :class="$style.content">
|
|
|
|
<span v-html="i18n.baseText('expressionTip.javascript')" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2024-03-13 04:57:08 -07:00
|
|
|
<style lang="scss" module>
|
|
|
|
.tip {
|
2024-03-26 07:23:30 -07:00
|
|
|
display: flex;
|
|
|
|
align-items: flex-start;
|
|
|
|
gap: var(--spacing-4xs);
|
2024-03-13 04:57:08 -07:00
|
|
|
line-height: var(--font-line-height-regular);
|
|
|
|
color: var(--color-text-base);
|
|
|
|
font-size: var(--font-size-2xs);
|
|
|
|
padding: var(--spacing-2xs);
|
2024-03-26 07:23:30 -07:00
|
|
|
}
|
2024-03-13 04:57:08 -07:00
|
|
|
|
2024-03-26 07:23:30 -07:00
|
|
|
.content {
|
|
|
|
display: inline-block;
|
|
|
|
}
|
2024-03-13 04:57:08 -07:00
|
|
|
|
2024-03-26 07:23:30 -07:00
|
|
|
.tipText {
|
|
|
|
display: inline;
|
|
|
|
color: var(--color-text-dark);
|
|
|
|
font-weight: var(--font-weight-bold);
|
|
|
|
}
|
2024-03-13 04:57:08 -07:00
|
|
|
|
2024-03-26 07:23:30 -07:00
|
|
|
.drag .tipText {
|
|
|
|
line-height: 21px;
|
|
|
|
}
|
2024-03-13 04:57:08 -07:00
|
|
|
|
2024-03-26 07:23:30 -07:00
|
|
|
.text {
|
|
|
|
display: inline;
|
|
|
|
}
|
2024-03-13 04:57:08 -07:00
|
|
|
|
2024-03-26 07:23:30 -07:00
|
|
|
code {
|
|
|
|
font-size: var(--font-size-3xs);
|
|
|
|
background: var(--color-background-base);
|
|
|
|
padding: var(--spacing-5xs);
|
|
|
|
border-radius: var(--border-radius-base);
|
|
|
|
}
|
2024-03-13 04:57:08 -07:00
|
|
|
|
2024-03-26 07:23:30 -07:00
|
|
|
.pill {
|
|
|
|
display: inline-flex;
|
|
|
|
align-items: center;
|
|
|
|
color: var(--color-text-dark);
|
|
|
|
|
|
|
|
border: var(--border-base);
|
|
|
|
border-color: var(--color-foreground-light);
|
|
|
|
background-color: var(--color-background-xlight);
|
|
|
|
padding: var(--spacing-5xs) var(--spacing-3xs);
|
|
|
|
margin: 0 var(--spacing-4xs);
|
|
|
|
border-radius: var(--border-radius-base);
|
|
|
|
}
|
|
|
|
|
|
|
|
.highlight {
|
|
|
|
color: var(--color-primary);
|
|
|
|
background-color: var(--color-primary-tint-3);
|
|
|
|
border-color: var(--color-primary-tint-1);
|
2024-03-13 04:57:08 -07:00
|
|
|
}
|
|
|
|
</style>
|