2023-05-16 02:43:46 -07:00
|
|
|
import { defineComponent } from 'vue';
|
|
|
|
import type { PropType } from 'vue';
|
2022-12-01 04:26:22 -08:00
|
|
|
import { mapStores } from 'pinia';
|
2023-05-16 02:43:46 -07:00
|
|
|
|
2023-08-25 01:33:46 -07:00
|
|
|
import type { IDataObject } from 'n8n-workflow';
|
2023-05-16 02:43:46 -07:00
|
|
|
import { Expression, ExpressionExtensions } from 'n8n-workflow';
|
2023-01-26 01:03:13 -08:00
|
|
|
import { ensureSyntaxTree } from '@codemirror/language';
|
2022-12-01 04:26:22 -08:00
|
|
|
|
|
|
|
import { workflowHelpers } from '@/mixins/workflowHelpers';
|
2023-05-05 01:41:54 -07:00
|
|
|
import { useNDVStore } from '@/stores/ndv.store';
|
2022-12-23 07:13:16 -08:00
|
|
|
import { EXPRESSION_EDITOR_PARSER_TIMEOUT } from '@/constants';
|
2022-12-01 04:26:22 -08:00
|
|
|
|
2022-12-14 05:43:02 -08:00
|
|
|
import type { EditorView } from '@codemirror/view';
|
|
|
|
import type { TargetItem } from '@/Interface';
|
2023-01-26 01:03:13 -08:00
|
|
|
import type { Html, Plaintext, RawSegment, Resolvable, Segment } from '@/types/expressions';
|
2022-12-01 04:26:22 -08:00
|
|
|
|
2023-05-16 02:43:46 -07:00
|
|
|
export const expressionManager = defineComponent({
|
|
|
|
mixins: [workflowHelpers],
|
2022-12-01 04:26:22 -08:00
|
|
|
props: {
|
2022-12-14 05:43:02 -08:00
|
|
|
targetItem: {
|
|
|
|
type: Object as PropType<TargetItem | null>,
|
2022-12-01 04:26:22 -08:00
|
|
|
},
|
2023-08-25 01:33:46 -07:00
|
|
|
additionalData: {
|
|
|
|
type: Object as PropType<IDataObject>,
|
|
|
|
default: () => ({}),
|
|
|
|
},
|
2022-12-01 04:26:22 -08:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2023-02-02 03:35:38 -08:00
|
|
|
editor: {} as EditorView,
|
2023-06-22 07:47:28 -07:00
|
|
|
skipSegments: [] as string[],
|
2023-07-28 00:51:07 -07:00
|
|
|
editorState: undefined,
|
2022-12-01 04:26:22 -08:00
|
|
|
};
|
|
|
|
},
|
2022-12-14 05:43:02 -08:00
|
|
|
watch: {
|
|
|
|
targetItem() {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.$emit('change', {
|
|
|
|
value: this.unresolvedExpression,
|
|
|
|
segments: this.displayableSegments,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2022-12-01 04:26:22 -08:00
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
...mapStores(useNDVStore),
|
2022-12-14 05:43:02 -08:00
|
|
|
|
2022-12-01 04:26:22 -08:00
|
|
|
unresolvedExpression(): string {
|
|
|
|
return this.segments.reduce((acc, segment) => {
|
|
|
|
acc += segment.kind === 'resolvable' ? segment.resolvable : segment.plaintext;
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, '=');
|
|
|
|
},
|
2022-12-14 05:43:02 -08:00
|
|
|
|
|
|
|
hoveringItem(): TargetItem | undefined {
|
|
|
|
return this.ndvStore.hoveringItem ?? undefined;
|
|
|
|
},
|
|
|
|
|
2022-12-01 04:26:22 -08:00
|
|
|
resolvableSegments(): Resolvable[] {
|
|
|
|
return this.segments.filter((s): s is Resolvable => s.kind === 'resolvable');
|
|
|
|
},
|
2022-12-14 05:43:02 -08:00
|
|
|
|
2022-12-01 04:26:22 -08:00
|
|
|
plaintextSegments(): Plaintext[] {
|
|
|
|
return this.segments.filter((s): s is Plaintext => s.kind === 'plaintext');
|
|
|
|
},
|
|
|
|
|
2023-02-02 03:35:38 -08:00
|
|
|
expressionExtensionNames(): Set<string> {
|
|
|
|
return new Set(
|
|
|
|
ExpressionExtensions.reduce<string[]>((acc, cur) => {
|
|
|
|
return [...acc, ...Object.keys(cur.functions)];
|
|
|
|
}, []),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2023-01-26 01:03:13 -08:00
|
|
|
htmlSegments(): Html[] {
|
|
|
|
return this.segments.filter((s): s is Html => s.kind !== 'resolvable');
|
|
|
|
},
|
|
|
|
|
2022-12-14 05:43:02 -08:00
|
|
|
segments(): Segment[] {
|
2023-07-28 00:51:07 -07:00
|
|
|
if (!this.editorState || !this.editorState) return [];
|
2023-06-22 07:47:28 -07:00
|
|
|
|
2022-12-14 05:43:02 -08:00
|
|
|
const rawSegments: RawSegment[] = [];
|
|
|
|
|
2022-12-23 07:13:16 -08:00
|
|
|
const fullTree = ensureSyntaxTree(
|
2023-07-28 00:51:07 -07:00
|
|
|
this.editorState,
|
|
|
|
this.editorState.doc.length,
|
2022-12-23 07:13:16 -08:00
|
|
|
EXPRESSION_EDITOR_PARSER_TIMEOUT,
|
|
|
|
);
|
2022-12-14 05:43:02 -08:00
|
|
|
|
2022-12-23 07:13:16 -08:00
|
|
|
if (fullTree === null) {
|
2023-07-28 00:51:07 -07:00
|
|
|
throw new Error(`Failed to parse expression: ${this.editorValue}`);
|
2022-12-23 07:13:16 -08:00
|
|
|
}
|
|
|
|
|
2023-06-22 07:47:28 -07:00
|
|
|
const skipSegments = ['Program', 'Script', 'Document', ...this.skipSegments];
|
|
|
|
|
2022-12-23 07:13:16 -08:00
|
|
|
fullTree.cursor().iterate((node) => {
|
2023-07-28 00:51:07 -07:00
|
|
|
const text = this.editorState.sliceDoc(node.from, node.to);
|
2023-06-22 07:47:28 -07:00
|
|
|
|
|
|
|
if (skipSegments.includes(node.type.name)) return;
|
2022-12-23 07:13:16 -08:00
|
|
|
|
|
|
|
rawSegments.push({
|
|
|
|
from: node.from,
|
|
|
|
to: node.to,
|
2023-06-22 07:47:28 -07:00
|
|
|
text,
|
|
|
|
token: node.type.name === 'Resolvable' ? 'Resolvable' : 'Plaintext',
|
2022-12-14 05:43:02 -08:00
|
|
|
});
|
2022-12-23 07:13:16 -08:00
|
|
|
});
|
2022-12-14 05:43:02 -08:00
|
|
|
|
|
|
|
return rawSegments.reduce<Segment[]>((acc, segment) => {
|
2023-02-02 03:35:38 -08:00
|
|
|
const { from, to, text, token } = segment;
|
2022-12-14 05:43:02 -08:00
|
|
|
|
2023-02-06 07:29:10 -08:00
|
|
|
if (token === 'Resolvable') {
|
|
|
|
const { resolved, error, fullError } = this.resolve(text, this.hoveringItem);
|
|
|
|
|
2023-06-22 07:47:28 -07:00
|
|
|
acc.push({
|
|
|
|
kind: 'resolvable',
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
resolvable: text,
|
|
|
|
// TODO:
|
|
|
|
// For some reason, expressions that resolve to a number 0 are breaking preview in the SQL editor
|
|
|
|
// This fixes that but as as TODO we should figure out why this is happening
|
|
|
|
resolved: String(resolved),
|
|
|
|
error,
|
|
|
|
fullError,
|
|
|
|
});
|
2022-12-14 05:43:02 -08:00
|
|
|
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
2023-02-06 07:29:10 -08:00
|
|
|
acc.push({ kind: 'plaintext', from, to, plaintext: text });
|
2022-12-14 05:43:02 -08:00
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
},
|
|
|
|
|
2022-12-01 04:26:22 -08:00
|
|
|
/**
|
2023-02-02 03:35:38 -08:00
|
|
|
* Segments to display in the output of an expression editor.
|
|
|
|
*
|
|
|
|
* Some segments are not displayed when they are _part_ of the result,
|
|
|
|
* but displayed when they are the _entire_ result:
|
|
|
|
*
|
|
|
|
* - `This is a {{ [] }} test` displays as `This is a test`.
|
|
|
|
* - `{{ [] }}` displays as `[Array: []]`.
|
2022-12-01 04:26:22 -08:00
|
|
|
*
|
2023-02-02 03:35:38 -08:00
|
|
|
* Some segments display differently based on context:
|
2022-12-01 04:26:22 -08:00
|
|
|
*
|
2023-02-02 03:35:38 -08:00
|
|
|
* Date displays as
|
|
|
|
* - `Mon Nov 14 2022 17:26:13 GMT+0100 (CST)` when part of the result
|
|
|
|
* - `[Object: "2022-11-14T17:26:13.130Z"]` when the entire result
|
2022-12-01 04:26:22 -08:00
|
|
|
*
|
2023-02-02 03:35:38 -08:00
|
|
|
* Only needed in order to mimic behavior of `ParameterInputHint`.
|
2022-12-01 04:26:22 -08:00
|
|
|
*/
|
|
|
|
displayableSegments(): Segment[] {
|
|
|
|
return this.segments
|
|
|
|
.map((s) => {
|
|
|
|
if (this.segments.length <= 1 || s.kind !== 'resolvable') return s;
|
|
|
|
|
|
|
|
if (typeof s.resolved === 'string' && /\[Object: "\d{4}-\d{2}-\d{2}T/.test(s.resolved)) {
|
|
|
|
const utcDateString = s.resolved.replace(/(\[Object: "|\"\])/g, '');
|
|
|
|
s.resolved = new Date(utcDateString).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof s.resolved === 'string' && /\[Array:\s\[.+\]\]/.test(s.resolved)) {
|
|
|
|
s.resolved = s.resolved.replace(/(\[Array: \[|\])/g, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
})
|
|
|
|
.filter((s) => {
|
|
|
|
if (
|
|
|
|
this.segments.length > 1 &&
|
|
|
|
s.kind === 'resolvable' &&
|
|
|
|
typeof s.resolved === 'string' &&
|
2022-12-21 04:29:30 -08:00
|
|
|
(s.resolved === '[Array: []]' ||
|
|
|
|
s.resolved === this.$locale.baseText('expressionModalInput.empty'))
|
2022-12-01 04:26:22 -08:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
isEmptyExpression(resolvable: string) {
|
|
|
|
return /\{\{\s*\}\}/.test(resolvable);
|
|
|
|
},
|
2022-12-14 05:43:02 -08:00
|
|
|
|
|
|
|
resolve(resolvable: string, targetItem?: TargetItem) {
|
2022-12-01 04:26:22 -08:00
|
|
|
const result: { resolved: unknown; error: boolean; fullError: Error | null } = {
|
|
|
|
resolved: undefined,
|
|
|
|
error: false,
|
|
|
|
fullError: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2023-04-21 05:08:51 -07:00
|
|
|
const ndvStore = useNDVStore();
|
|
|
|
if (!ndvStore.activeNode) {
|
2023-02-02 03:35:38 -08:00
|
|
|
// e.g. credential modal
|
2023-08-25 01:33:46 -07:00
|
|
|
result.resolved = Expression.resolveWithoutWorkflow(resolvable, this.additionalData);
|
2023-02-02 03:35:38 -08:00
|
|
|
} else {
|
2023-04-21 05:08:51 -07:00
|
|
|
let opts;
|
|
|
|
if (ndvStore.isInputParentOfActiveNode) {
|
|
|
|
opts = {
|
|
|
|
targetItem: targetItem ?? undefined,
|
|
|
|
inputNodeName: this.ndvStore.ndvInputNodeName,
|
|
|
|
inputRunIndex: this.ndvStore.ndvInputRunIndex,
|
|
|
|
inputBranchIndex: this.ndvStore.ndvInputBranchIndex,
|
2023-08-25 01:33:46 -07:00
|
|
|
additionalKeys: this.additionalData,
|
2023-04-21 05:08:51 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
result.resolved = this.resolveExpression('=' + resolvable, undefined, opts);
|
2023-02-02 03:35:38 -08:00
|
|
|
}
|
2022-12-01 04:26:22 -08:00
|
|
|
} catch (error) {
|
|
|
|
result.resolved = `[${error.message}]`;
|
|
|
|
result.error = true;
|
|
|
|
result.fullError = error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.resolved === '') {
|
|
|
|
result.resolved = this.$locale.baseText('expressionModalInput.empty');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.resolved === undefined && this.isEmptyExpression(resolvable)) {
|
|
|
|
result.resolved = this.$locale.baseText('expressionModalInput.empty');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.resolved === undefined) {
|
2023-02-02 03:35:38 -08:00
|
|
|
result.resolved = this.isUncalledExpressionExtension(resolvable)
|
|
|
|
? this.$locale.baseText('expressionEditor.uncalledFunction')
|
|
|
|
: this.$locale.baseText('expressionModalInput.undefined');
|
|
|
|
|
2022-12-01 04:26:22 -08:00
|
|
|
result.error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof result.resolved === 'number' && isNaN(result.resolved)) {
|
|
|
|
result.resolved = this.$locale.baseText('expressionModalInput.null');
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
2023-02-02 03:35:38 -08:00
|
|
|
|
|
|
|
isUncalledExpressionExtension(resolvable: string) {
|
|
|
|
const end = resolvable
|
|
|
|
.replace(/^{{|}}$/g, '')
|
|
|
|
.trim()
|
|
|
|
.split('.')
|
|
|
|
.pop();
|
|
|
|
|
|
|
|
return end !== undefined && this.expressionExtensionNames.has(end);
|
|
|
|
},
|
2022-12-01 04:26:22 -08:00
|
|
|
},
|
|
|
|
});
|