BlockGroup.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <template>
  2. <block-base
  3. :id="componentId"
  4. :data="data"
  5. :block-id="id"
  6. :block-data="block"
  7. class="w-64"
  8. content-class="p-0"
  9. @edit="$emit('edit')"
  10. @delete="$emit('delete', id)"
  11. @update="$emit('update', $event)"
  12. @settings="$emit('settings', $event)"
  13. >
  14. <Handle :id="`${id}-input-1`" type="target" :position="Position.Left" />
  15. <div class="p-4">
  16. <div class="flex items-center mb-2">
  17. <div
  18. :class="
  19. data.disableBlock ? 'bg-box-transparent' : block.category.color
  20. "
  21. class="inline-flex items-center text-sm mr-4 p-2 rounded-lg dark:text-black"
  22. >
  23. <v-remixicon
  24. :name="block.details.icon || 'riFolderZipLine'"
  25. size="20"
  26. class="inline-block mr-2"
  27. />
  28. <span>{{ t('workflow.blocks.blocks-group.name') }}</span>
  29. </div>
  30. </div>
  31. <input
  32. :value="data.name"
  33. :placeholder="t('workflow.blocks.blocks-group.groupName')"
  34. type="text"
  35. class="bg-transparent w-full focus:ring-0"
  36. @input="$emit('update', { name: $event.target.value })"
  37. />
  38. </div>
  39. <draggable
  40. :model-value="blocks"
  41. item-key="itemId"
  42. class="px-4 pb-4 overflow-auto nowheel scroll text-sm space-y-1 max-h-60"
  43. @mousedown.stop
  44. @dragover.prevent
  45. @drop="handleDrop"
  46. @update:modelValue="$emit('update', { blocks: $event })"
  47. >
  48. <template #item="{ element, index }">
  49. <div
  50. class="p-2 rounded-lg bg-input space-x-2 flex items-center group"
  51. style="cursor: grab"
  52. @dragstart="onDragStart(element, $event)"
  53. @dragend="onDragEnd(element.itemId)"
  54. >
  55. <v-remixicon
  56. :name="tasks[element.id].icon"
  57. size="20"
  58. class="flex-shrink-0"
  59. />
  60. <div class="leading-tight flex-1 overflow-hidden">
  61. <p class="text-overflow">
  62. {{
  63. getTranslation(
  64. `workflow.blocks.${element.id}.name`,
  65. tasks[element.id].name
  66. )
  67. }}
  68. </p>
  69. <p
  70. :title="element.data.description"
  71. class="text-gray-600 dark:text-gray-200 text-overflow"
  72. >
  73. {{ element.data.description }}
  74. </p>
  75. </div>
  76. <div class="invisible group-hover:visible">
  77. <v-remixicon
  78. name="riPencilLine"
  79. size="18"
  80. class="cursor-pointer inline-block mr-2"
  81. @click="editBlock(element)"
  82. />
  83. <v-remixicon
  84. name="riSettings3Line"
  85. size="18"
  86. class="cursor-pointer inline-block mr-2"
  87. @click="editItemSettings(element)"
  88. />
  89. <v-remixicon
  90. name="riDeleteBin7Line"
  91. size="18"
  92. class="cursor-pointer inline-block"
  93. @click="deleteItem(index, element.itemId)"
  94. />
  95. </div>
  96. </div>
  97. </template>
  98. <template #footer>
  99. <div
  100. class="p-2 rounded-lg text-gray-600 dark:text-gray-200 border text-center border-dashed"
  101. >
  102. {{ t('workflow.blocks.blocks-group.dropText') }}
  103. </div>
  104. </template>
  105. </draggable>
  106. <Handle :id="`${id}-output-1`" type="source" :position="Position.Right" />
  107. </block-base>
  108. </template>
  109. <script setup>
  110. import { inject, computed, shallowReactive } from 'vue';
  111. import { useI18n } from 'vue-i18n';
  112. import { nanoid } from 'nanoid';
  113. import { useToast } from 'vue-toastification';
  114. import { Handle, Position } from '@vue-flow/core';
  115. import draggable from 'vuedraggable';
  116. import { tasks, excludeGroupBlocks } from '@/utils/shared';
  117. import { useComponentId } from '@/composable/componentId';
  118. import { useEditorBlock } from '@/composable/editorBlock';
  119. import BlockBase from './BlockBase.vue';
  120. const props = defineProps({
  121. id: {
  122. type: String,
  123. default: '',
  124. },
  125. label: {
  126. type: String,
  127. default: '',
  128. },
  129. data: {
  130. type: Object,
  131. default: () => ({}),
  132. },
  133. editor: {
  134. type: Object,
  135. default: () => ({}),
  136. },
  137. events: {
  138. type: Object,
  139. default: () => ({}),
  140. },
  141. });
  142. const emit = defineEmits(['update', 'delete', 'edit', 'settings']);
  143. const { t, te } = useI18n();
  144. const toast = useToast();
  145. const componentId = useComponentId('blocks-group');
  146. const block = useEditorBlock(props.label);
  147. const workflow = inject('workflow', {});
  148. const blocks = computed(() =>
  149. Array.isArray(props.data.blocks)
  150. ? props.data.blocks
  151. : Object.values(props.data.blocks)
  152. );
  153. function editItemSettings(element) {
  154. emit('settings', {
  155. blockId: props.id,
  156. data: element.data,
  157. itemId: element.itemId,
  158. details: { id: element.id },
  159. });
  160. }
  161. function onDragStart(item, event) {
  162. event.dataTransfer.setData(
  163. 'block',
  164. JSON.stringify({ ...tasks[item.id], ...item, fromGroup: true })
  165. );
  166. }
  167. function onDragEnd(itemId) {
  168. setTimeout(() => {
  169. const blockEl = document.querySelector(`[group-item-id="${itemId}"]`);
  170. if (blockEl) {
  171. const blockIndex = blocks.value.findIndex(
  172. (item) => item.itemId === itemId
  173. );
  174. if (blockIndex !== -1) {
  175. const copyBlocks = [...props.data.blocks];
  176. copyBlocks.splice(blockIndex, 1);
  177. emit('update', { blocks: copyBlocks });
  178. }
  179. }
  180. }, 200);
  181. }
  182. function editBlock(payload) {
  183. emit('edit', payload);
  184. }
  185. function deleteItem(index, itemId) {
  186. const copyBlocks = [...props.data.blocks];
  187. if (workflow.editState.blockData.itemId === itemId) {
  188. workflow.editState.editing = false;
  189. workflow.editState.blockData = false;
  190. }
  191. copyBlocks.splice(index, 1);
  192. emit('update', { blocks: copyBlocks });
  193. }
  194. function getTranslation(key, defText = '') {
  195. return te(key) ? t(key) : defText;
  196. }
  197. function handleDrop(event) {
  198. event.preventDefault();
  199. event.stopPropagation();
  200. const droppedBlock = JSON.parse(event.dataTransfer.getData('block') || null);
  201. if (!droppedBlock || droppedBlock.fromGroup) return;
  202. const { id, data, blockId } = droppedBlock;
  203. if (excludeGroupBlocks.includes(id)) {
  204. toast.error(
  205. t('workflow.blocks.blocks-group.cantAdd', {
  206. blockName: t(`workflow.blocks.${id}.name`),
  207. })
  208. );
  209. return;
  210. }
  211. if (blockId) {
  212. emit('delete', blockId);
  213. }
  214. const copyBlocks = [
  215. ...props.data.blocks,
  216. shallowReactive({ id, data, itemId: nanoid(5) }),
  217. ];
  218. emit('update', { blocks: copyBlocks });
  219. }
  220. </script>