refactor(editor): Refactor BinaryDataDisplay components to Vue 3 syntax (no-changelog) (#9753)

Signed-off-by: Oleg Ivaniv <me@olegivaniv.com>
This commit is contained in:
oleg 2024-06-18 10:50:28 +02:00 committed by GitHub
parent d1163533a6
commit fb73ec3994
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 102 additions and 120 deletions

View file

@ -18,77 +18,75 @@
</div> </div>
</template> </template>
<script lang="ts"> <script setup lang="ts">
import { defineComponent } from 'vue'; import { computed } from 'vue';
import { mapStores } from 'pinia';
import type { IBinaryData, IRunData } from 'n8n-workflow'; import type { IBinaryData, IRunData } from 'n8n-workflow';
import BinaryDataDisplayEmbed from '@/components/BinaryDataDisplayEmbed.vue'; import BinaryDataDisplayEmbed from '@/components/BinaryDataDisplayEmbed.vue';
import { useWorkflowsStore } from '@/stores/workflows.store'; import { useWorkflowsStore } from '@/stores/workflows.store';
import { useNodeHelpers } from '@/composables/useNodeHelpers'; import { useNodeHelpers } from '@/composables/useNodeHelpers';
export default defineComponent({ const props = defineProps<{
name: 'BinaryDataDisplay', displayData: IBinaryData;
windowVisible: boolean;
}>();
components: { const emit = defineEmits<{
BinaryDataDisplayEmbed, (event: 'close'): void;
}, }>();
props: [
'displayData', // IBinaryData
'windowVisible', // boolean
],
setup() {
const nodeHelpers = useNodeHelpers();
return { const nodeHelpers = useNodeHelpers();
nodeHelpers, const workflowsStore = useWorkflowsStore();
};
},
computed: {
...mapStores(useWorkflowsStore),
binaryData(): IBinaryData | null {
const binaryData = this.nodeHelpers.getBinaryData(
this.workflowRunData,
this.displayData.node,
this.displayData.runIndex,
this.displayData.outputIndex,
);
if (binaryData.length === 0) { const workflowRunData = computed<IRunData | null>(() => {
return null; const workflowExecution = workflowsStore.getWorkflowExecution;
} if (workflowExecution === null) {
return null;
if ( }
this.displayData.index >= binaryData.length || const executionData = workflowExecution.data;
binaryData[this.displayData.index][this.displayData.key] === undefined return executionData ? executionData.resultData.runData : null;
) {
return null;
}
const binaryDataItem: IBinaryData = binaryData[this.displayData.index][this.displayData.key];
return binaryDataItem;
},
workflowRunData(): IRunData | null {
const workflowExecution = this.workflowsStore.getWorkflowExecution;
if (workflowExecution === null) {
return null;
}
const executionData = workflowExecution.data;
return executionData ? executionData.resultData.runData : null;
},
},
methods: {
closeWindow() {
// Handle the close externally as the visible parameter is an external prop
// and is so not allowed to be changed here.
this.$emit('close');
return false;
},
},
}); });
const binaryData = computed<IBinaryData | null>(() => {
if (
typeof props.displayData.node !== 'string' ||
typeof props.displayData.key !== 'string' ||
typeof props.displayData.runIndex !== 'number' ||
typeof props.displayData.index !== 'number' ||
typeof props.displayData.outputIndex !== 'number'
) {
return null;
}
const binaryDataLocal = nodeHelpers.getBinaryData(
workflowRunData.value,
props.displayData.node,
props.displayData.runIndex,
props.displayData.outputIndex,
);
if (binaryDataLocal.length === 0) {
return null;
}
if (
props.displayData.index >= binaryDataLocal.length ||
binaryDataLocal[props.displayData.index][props.displayData.key] === undefined
) {
return null;
}
const binaryDataItem: IBinaryData =
binaryDataLocal[props.displayData.index][props.displayData.key];
return binaryDataItem;
});
function closeWindow() {
// Handle the close externally as the visible parameter is an external prop
// and is so not allowed to be changed here.
emit('close');
return false;
}
</script> </script>
<style lang="scss"> <style lang="scss">

View file

@ -18,77 +18,60 @@
:show-length="true" :show-length="true"
/> />
<RunDataHtml v-else-if="binaryData.fileType === 'html'" :input-html="data" /> <RunDataHtml v-else-if="binaryData.fileType === 'html'" :input-html="data" />
<embed v-else :src="embedSource" class="binary-data" :class="embedClass()" /> <embed v-else :src="embedSource" class="binary-data" :class="embedClass" />
</span> </span>
</span> </span>
</template> </template>
<script lang="ts"> <script setup lang="ts">
import { defineComponent } from 'vue'; import { ref, onMounted, computed } from 'vue';
import { mapStores } from 'pinia'; import { useWorkflowsStore } from '@/stores/workflows.store';
import type { IBinaryData } from 'n8n-workflow'; import type { IBinaryData } from 'n8n-workflow';
import { jsonParse } from 'n8n-workflow'; import { jsonParse } from 'n8n-workflow';
import type { PropType } from 'vue';
import VueJsonPretty from 'vue-json-pretty'; import VueJsonPretty from 'vue-json-pretty';
import { useWorkflowsStore } from '@/stores/workflows.store';
import RunDataHtml from '@/components/RunDataHtml.vue'; import RunDataHtml from '@/components/RunDataHtml.vue';
export default defineComponent({ const props = defineProps<{
name: 'BinaryDataDisplayEmbed', binaryData: IBinaryData;
components: { }>();
VueJsonPretty,
RunDataHtml,
},
props: {
binaryData: {
type: Object as PropType<IBinaryData>,
required: true,
},
},
data() {
return {
isLoading: true,
embedSource: '',
error: false,
data: '',
};
},
computed: {
...mapStores(useWorkflowsStore),
},
async mounted() {
const { id, data, fileName, fileType, mimeType } = this.binaryData;
const isJSONData = fileType === 'json';
const isHTMLData = fileType === 'html';
if (!id) { const isLoading = ref(true);
if (isJSONData || isHTMLData) { const embedSource = ref('');
this.data = jsonParse(atob(data)); const error = ref(false);
} else { const data = ref('');
this.embedSource = 'data:' + mimeType + ';base64,' + data;
} const workflowsStore = useWorkflowsStore();
const embedClass = computed(() => {
return [props.binaryData.fileType ?? 'other'];
});
onMounted(async () => {
const { id, data: binaryData, fileName, fileType, mimeType } = props.binaryData;
const isJSONData = fileType === 'json';
const isHTMLData = fileType === 'html';
if (!id) {
if (isJSONData || isHTMLData) {
data.value = jsonParse(atob(binaryData));
} else { } else {
try { embedSource.value = 'data:' + mimeType + ';base64,' + binaryData;
const binaryUrl = this.workflowsStore.getBinaryUrl(id, 'view', fileName ?? '', mimeType);
if (isJSONData || isHTMLData) {
const fetchedData = await fetch(binaryUrl, { credentials: 'include' });
this.data = await (isJSONData ? fetchedData.json() : fetchedData.text());
} else {
this.embedSource = binaryUrl;
}
} catch (e) {
this.error = true;
}
} }
} else {
try {
const binaryUrl = workflowsStore.getBinaryUrl(id, 'view', fileName ?? '', mimeType);
if (isJSONData || isHTMLData) {
const fetchedData = await fetch(binaryUrl, { credentials: 'include' });
data.value = await (isJSONData ? fetchedData.json() : fetchedData.text());
} else {
embedSource.value = binaryUrl;
}
} catch (e) {
error.value = true;
}
}
this.isLoading = false; isLoading.value = false;
},
methods: {
embedClass(): string[] {
const { fileType } = this.binaryData;
return [fileType ?? 'other'];
},
},
}); });
</script> </script>

View file

@ -35,6 +35,7 @@
</n8n-callout> </n8n-callout>
<BinaryDataDisplay <BinaryDataDisplay
v-if="binaryDataDisplayData"
:window-visible="binaryDataDisplayVisible" :window-visible="binaryDataDisplayVisible"
:display-data="binaryDataDisplayData" :display-data="binaryDataDisplayData"
@close="closeBinaryDataDisplay" @close="closeBinaryDataDisplay"