Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ui/src/workflow/nodes/form-node/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ const sync_form_field_list = () => {
]
set(props.nodeModel.properties.config, 'fields', fields)
props.nodeModel.clear_next_node_field(false)
onDragHandle()
}
const addFormCollectRef = ref<InstanceType<typeof AddFormCollect>>()
const editFormCollectRef = ref<InstanceType<typeof EditFormCollect>>()
Expand Down Expand Up @@ -264,7 +263,7 @@ function onDragHandle() {
onEnd: (evt) => {
if (evt.oldIndex === undefined || evt.newIndex === undefined) return
// 更新数据顺序
const items = [...form_data.value.form_field_list]
const items = cloneDeep([...form_data.value.form_field_list])
const [movedItem] = items.splice(evt.oldIndex, 1)
items.splice(evt.newIndex, 0, movedItem)
form_data.value.form_field_list = items
Expand All @@ -276,6 +275,7 @@ onMounted(() => {
set(props.nodeModel, 'validate', validate)
sync_form_field_list()
props.nodeModel.graphModel.eventCenter.emit('refresh_incoming_node_field')
onDragHandle()
})
</script>
<style lang="scss" scoped></style>
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code looks largely correct, but there are a few suggestions and improvements to consider:

  1. Cloning form_data.value.form_field_list:
    Since you're modifying an array directly (which can lead to unintended side effects), cloning it before performing operations is generally a good practice. However, this might be more relevant within the context of larger applications where multiple components might access the same data.

  2. Using map() for Array Operations:
    The current implementation uses filter, which creates a new filtered list instead of modifying the original. If you intend to perform modifications that change the order or remove elements, using map() would better fit the intent.

  3. Consistent Variable Naming:
    Ensure variable names like items.oldIndex, movedItem`, etc., are consistent with standard naming conventions.

  4. Handling Edge Cases:
    Consider adding checks to ensure that evt.oldIndex and evt.newIndex fall within valid bounds.

Here's a refined version of the code incorporating these suggestions:

const sync_form_field_list = () => {
  const fields = [
    { /* ... */ },
    { /* ... */ }
  ]
  set(props.nodeModel.properties.config, 'fields', fields)
  props.nodeModel.clear_next_node_field(false)
  onDragHandle()
}

const addFormCollectRef = ref<InstanceType<typeof AddFormCollect>>()
const editFormCollectRef = ref<InstanceType<typeof EditFormCollect>>()

function onDragHandle() {
  onEnd: (evt) => {
    if (evt.oldIndex === undefined || evt.newIndex === undefined) return
    const items = cloneDeep(form_data.value.form_field_list) // Clone the array first

    if (
      evt.oldIndex <= items.length - 1 &&
      evt.newIndex >= 0 && 
      evt.newIndex <= items.length - 1
    ) {
      const movedItem = items.splice(evt.oldIndex, 1)[0];
      items.splice(evt.newIndex, 0, movedItem);
      form_data.value.form_field_list = items;

      // Optionally emit event(s) here based on your requirements
    } else {
      console.warn(`Invalid drag index. Old Index: ${evt.oldIndex}, New Index: ${evt.newIndex}`);
    }
  },
})

onMounted(() => {
  set(props.nodeModel, 'validate', validate)
  sync_form_field_list();
  props.nodeModel.graphModel.eventCenter.emit('refresh_incoming_node_field');
})

These changes should help improve readability and robustness, especially in larger-scale projects.