Skip to content
Draft
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions apps/sim/hooks/use-collaborative-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback, useEffect, useRef } from 'react'
import type { Edge } from 'reactflow'
import { useSession } from '@/lib/auth/auth-client'
import { createLogger } from '@/lib/logs/console/logger'
import { NEW_BLOCK_OFFSET } from '@/lib/workflows/autolayout/constants'
import { getBlockOutputs } from '@/lib/workflows/blocks/block-outputs'
import { TriggerUtils } from '@/lib/workflows/triggers/triggers'
import { useSocket } from '@/app/workspace/providers/socket-provider'
Expand Down Expand Up @@ -1326,8 +1327,8 @@ export function useCollaborativeWorkflow() {
// Generate new ID and calculate position
const newId = crypto.randomUUID()
const offsetPosition = {
x: sourceBlock.position.x + 250,
y: sourceBlock.position.y + 20,
x: sourceBlock.position.x + NEW_BLOCK_OFFSET.X,

Choose a reason for hiding this comment

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

Trước: vị trí mới được tính bằng các "magic numbers" cố định x: sourceBlock.position.x + 250 và y: sourceBlock.position.y + 20
Sau: các giá trị 250 và 20 được thay bằng NEW_BLOCK_OFFSET.X và NEW_BLOCK_OFFSET.Y (và bạn thấy dòng import mới: import { NEW_BLOCK_OFFSET } from '@/lib/workflows/autolayout/constants')

y: sourceBlock.position.y + NEW_BLOCK_OFFSET.Y,
}

const newName = getUniqueBlockName(sourceBlock.name, workflowStore.blocks)
Expand Down
12 changes: 11 additions & 1 deletion apps/sim/lib/workflows/autolayout/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ export { BLOCK_DIMENSIONS, CONTAINER_DIMENSIONS } from '@/lib/workflows/blocks/b
/**
* Horizontal spacing between layers (columns)
*/
export const DEFAULT_HORIZONTAL_SPACING = 250
export const DEFAULT_HORIZONTAL_SPACING = 200

/**
* Offset for newly added/duplicated blocks
*/
export const NEW_BLOCK_OFFSET = {
/** Horizontal offset from source block */
X: 200,

Choose a reason for hiding this comment

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

Hằng xuất khẩu NEW_BLOCK_OFFSET:
X: 200 (offset ngang từ block nguồn)
Y: 20 (offset dọc từ block nguồn)
Khai báo với as const để giữ giá trị dạng literal/readonly.

/** Vertical offset from source block */
Y: 20,
} as const

/**
* Vertical spacing between blocks in the same layer
Expand Down
5 changes: 3 additions & 2 deletions apps/sim/stores/workflows/workflow/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Edge } from 'reactflow'
import { create } from 'zustand'
import { devtools } from 'zustand/middleware'
import { createLogger } from '@/lib/logs/console/logger'
import { NEW_BLOCK_OFFSET } from '@/lib/workflows/autolayout/constants'

Choose a reason for hiding this comment

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

Thay vì dùng 250 và 20 cố định, code giờ dùng giá trị từ một hằng số tập trung (NEW_BLOCK_OFFSET). Điều này làm cho offset khi tạo block mới có thể điều chỉnh/đồng bộ từ một nơi duy nhất (dễ bảo trì, dễ thay đổi giao diện/autolayout)

import { getBlockOutputs } from '@/lib/workflows/blocks/block-outputs'
import { TriggerUtils } from '@/lib/workflows/triggers/triggers'
import { getBlock } from '@/blocks'
Expand Down Expand Up @@ -591,8 +592,8 @@ export const useWorkflowStore = create<WorkflowStore>()(

const newId = crypto.randomUUID()
const offsetPosition = {
x: block.position.x + 250,
y: block.position.y + 20,
x: block.position.x + NEW_BLOCK_OFFSET.X,
y: block.position.y + NEW_BLOCK_OFFSET.Y,
}

const newName = getUniqueBlockName(block.name, get().blocks)
Expand Down
Loading