2022-10-06 06:03:55 -07:00
|
|
|
<template>
|
|
|
|
<div :class="$style.actionsGroup">
|
|
|
|
<el-dropdown trigger="click" @command="handleCopyClick">
|
|
|
|
<span class="el-dropdown-link">
|
|
|
|
<n8n-icon-button
|
|
|
|
:title="$locale.baseText('runData.copyToClipboard')"
|
|
|
|
icon="copy"
|
|
|
|
type="tertiary"
|
|
|
|
:circle="false"
|
|
|
|
/>
|
|
|
|
</span>
|
2022-11-18 05:59:31 -08:00
|
|
|
<template #dropdown>
|
|
|
|
<el-dropdown-menu>
|
2022-12-14 01:04:10 -08:00
|
|
|
<el-dropdown-item :command="{ command: 'value' }">
|
2022-11-18 05:59:31 -08:00
|
|
|
{{ $locale.baseText('runData.copyValue') }}
|
|
|
|
</el-dropdown-item>
|
2022-12-14 01:04:10 -08:00
|
|
|
<el-dropdown-item :command="{ command: 'itemPath' }" divided>
|
2022-11-18 05:59:31 -08:00
|
|
|
{{ $locale.baseText('runData.copyItemPath') }}
|
|
|
|
</el-dropdown-item>
|
2022-12-14 01:04:10 -08:00
|
|
|
<el-dropdown-item :command="{ command: 'parameterPath' }">
|
2022-11-18 05:59:31 -08:00
|
|
|
{{ $locale.baseText('runData.copyParameterPath') }}
|
|
|
|
</el-dropdown-item>
|
|
|
|
</el-dropdown-menu>
|
|
|
|
</template>
|
2022-10-06 06:03:55 -07:00
|
|
|
</el-dropdown>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2022-12-14 01:04:10 -08:00
|
|
|
import { PropType } from 'vue';
|
|
|
|
import mixins from 'vue-typed-mixins';
|
|
|
|
import jp from 'jsonpath';
|
|
|
|
import { INodeUi } from '@/Interface';
|
|
|
|
import { IDataObject } from 'n8n-workflow';
|
|
|
|
import { copyPaste } from '@/mixins/copyPaste';
|
|
|
|
import { pinData } from '@/mixins/pinData';
|
|
|
|
import { nodeHelpers } from '@/mixins/nodeHelpers';
|
|
|
|
import { genericHelpers } from '@/mixins/genericHelpers';
|
2022-11-23 04:41:53 -08:00
|
|
|
import { clearJsonKey, convertPath, executionDataToJson } from '@/utils';
|
2022-12-14 01:04:10 -08:00
|
|
|
import { mapStores } from 'pinia';
|
|
|
|
import { useWorkflowsStore } from '@/stores/workflows';
|
|
|
|
import { useNDVStore } from '@/stores/ndv';
|
2022-10-06 06:03:55 -07:00
|
|
|
|
|
|
|
type JsonPathData = {
|
|
|
|
path: string;
|
|
|
|
startPath: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A path that does not exist so that nothing is selected by default
|
|
|
|
const nonExistingJsonPath = '_!^&*';
|
|
|
|
|
2022-12-14 01:04:10 -08:00
|
|
|
export default mixins(genericHelpers, nodeHelpers, pinData, copyPaste).extend({
|
2022-10-06 06:03:55 -07:00
|
|
|
name: 'run-data-json-actions',
|
|
|
|
props: {
|
|
|
|
node: {
|
|
|
|
type: Object as PropType<INodeUi>,
|
|
|
|
},
|
|
|
|
paneType: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
sessionId: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
currentOutputIndex: {
|
|
|
|
type: Number,
|
|
|
|
},
|
|
|
|
runIndex: {
|
|
|
|
type: Number,
|
|
|
|
},
|
|
|
|
displayMode: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
distanceFromActive: {
|
|
|
|
type: Number,
|
|
|
|
},
|
|
|
|
selectedJsonPath: {
|
|
|
|
type: String,
|
|
|
|
default: nonExistingJsonPath,
|
|
|
|
},
|
|
|
|
jsonData: {
|
|
|
|
type: Array as PropType<IDataObject[]>,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
computed: {
|
2022-12-14 01:04:10 -08:00
|
|
|
...mapStores(useNDVStore, useWorkflowsStore),
|
2022-11-04 06:04:31 -07:00
|
|
|
activeNode(): INodeUi | null {
|
|
|
|
return this.ndvStore.activeNode;
|
2022-10-06 06:03:55 -07:00
|
|
|
},
|
|
|
|
normalisedJsonPath(): string {
|
|
|
|
const isNotSelected = this.selectedJsonPath === nonExistingJsonPath;
|
|
|
|
return isNotSelected ? '[""]' : this.selectedJsonPath;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
getJsonValue(): string {
|
2022-12-14 01:04:10 -08:00
|
|
|
let selectedValue = jp.query(this.jsonData, `$${this.normalisedJsonPath}`)[0];
|
2022-10-06 06:03:55 -07:00
|
|
|
if (this.selectedJsonPath === nonExistingJsonPath) {
|
|
|
|
if (this.hasPinData) {
|
|
|
|
selectedValue = clearJsonKey(this.pinData as object);
|
|
|
|
} else {
|
2022-12-14 01:04:10 -08:00
|
|
|
selectedValue = executionDataToJson(
|
|
|
|
this.getNodeInputData(this.node, this.runIndex, this.currentOutputIndex),
|
|
|
|
);
|
2022-10-06 06:03:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let value = '';
|
|
|
|
if (typeof selectedValue === 'object') {
|
|
|
|
value = JSON.stringify(selectedValue, null, 2);
|
|
|
|
} else {
|
|
|
|
value = selectedValue.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
getJsonItemPath(): JsonPathData {
|
|
|
|
const newPath = convertPath(this.normalisedJsonPath);
|
|
|
|
let startPath = '';
|
|
|
|
let path = '';
|
|
|
|
|
|
|
|
const pathParts = newPath.split(']');
|
|
|
|
const index = pathParts[0].slice(1);
|
|
|
|
path = pathParts.slice(1).join(']');
|
2022-12-14 01:04:10 -08:00
|
|
|
startPath = `$item(${index}).$node["${this.node!.name}"].json`;
|
2022-10-06 06:03:55 -07:00
|
|
|
|
|
|
|
return { path, startPath };
|
|
|
|
},
|
|
|
|
getJsonParameterPath(): JsonPathData {
|
|
|
|
const newPath = convertPath(this.normalisedJsonPath);
|
|
|
|
const path = newPath.split(']').slice(1).join(']');
|
2022-12-14 01:04:10 -08:00
|
|
|
let startPath = `$node["${this.node!.name}"].json`;
|
2022-10-06 06:03:55 -07:00
|
|
|
|
|
|
|
if (this.distanceFromActive === 1) {
|
2022-12-29 03:20:43 -08:00
|
|
|
startPath = '$json';
|
2022-10-06 06:03:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return { path, startPath };
|
|
|
|
},
|
|
|
|
handleCopyClick(commandData: { command: string }) {
|
|
|
|
let value: string;
|
|
|
|
if (commandData.command === 'value') {
|
|
|
|
value = this.getJsonValue();
|
|
|
|
|
|
|
|
this.$showToast({
|
|
|
|
title: this.$locale.baseText('runData.copyValue.toast'),
|
|
|
|
message: '',
|
|
|
|
type: 'success',
|
|
|
|
duration: 2000,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
let startPath = '';
|
|
|
|
let path = '';
|
|
|
|
if (commandData.command === 'itemPath') {
|
|
|
|
const jsonItemPath = this.getJsonItemPath();
|
|
|
|
startPath = jsonItemPath.startPath;
|
|
|
|
path = jsonItemPath.path;
|
|
|
|
|
|
|
|
this.$showToast({
|
|
|
|
title: this.$locale.baseText('runData.copyItemPath.toast'),
|
|
|
|
message: '',
|
|
|
|
type: 'success',
|
|
|
|
duration: 2000,
|
|
|
|
});
|
|
|
|
} else if (commandData.command === 'parameterPath') {
|
|
|
|
const jsonParameterPath = this.getJsonParameterPath();
|
|
|
|
startPath = jsonParameterPath.startPath;
|
|
|
|
path = jsonParameterPath.path;
|
|
|
|
|
|
|
|
this.$showToast({
|
|
|
|
title: this.$locale.baseText('runData.copyParameterPath.toast'),
|
|
|
|
message: '',
|
|
|
|
type: 'success',
|
|
|
|
duration: 2000,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!path.startsWith('[') && !path.startsWith('.') && path) {
|
|
|
|
path += '.';
|
|
|
|
}
|
2022-12-14 01:04:10 -08:00
|
|
|
value = `{{ ${startPath + path} }}`;
|
2022-10-06 06:03:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const copyType = {
|
|
|
|
value: 'selection',
|
|
|
|
itemPath: 'item_path',
|
|
|
|
parameterPath: 'parameter_path',
|
|
|
|
}[commandData.command];
|
|
|
|
|
|
|
|
this.$telemetry.track('User copied ndv data', {
|
|
|
|
node_type: this.activeNode.type,
|
|
|
|
session_id: this.sessionId,
|
|
|
|
run_index: this.runIndex,
|
|
|
|
view: 'json',
|
|
|
|
copy_type: copyType,
|
2022-11-04 06:04:31 -07:00
|
|
|
workflow_id: this.workflowsStore.workflowId,
|
2022-10-06 06:03:55 -07:00
|
|
|
pane: this.paneType,
|
|
|
|
in_execution_log: this.isReadOnly,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.copyToClipboard(value);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" module>
|
|
|
|
.actionsGroup {
|
|
|
|
position: sticky;
|
|
|
|
height: 0;
|
|
|
|
overflow: visible;
|
|
|
|
z-index: 10;
|
|
|
|
top: 0;
|
|
|
|
padding-right: var(--spacing-s);
|
|
|
|
opacity: 0;
|
|
|
|
transition: opacity 0.3s ease;
|
|
|
|
text-align: right;
|
|
|
|
}
|
|
|
|
</style>
|