mirror of
https://github.com/langgenius/dify.git
synced 2026-05-04 09:28:04 +08:00
refactor(skill-editor): extract hooks and utils into separate directories
- Extract useFileOperations hook to hooks/use-file-operations.ts - Move tree utilities to utils/tree-utils.ts - Move file utilities to utils/file-utils.ts (renamed from utils.ts) - Remove unnecessary JSDoc comments throughout components - Simplify type.ts to only contain local type definitions - Clean up store/index.ts by removing verbose comments
This commit is contained in:
75
web/app/components/workflow/skill/utils/file-utils.ts
Normal file
75
web/app/components/workflow/skill/utils/file-utils.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import { FileAppearanceTypeEnum } from '@/app/components/base/file-uploader/types'
|
||||
|
||||
const MARKDOWN_EXTENSIONS = ['md', 'markdown', 'mdx']
|
||||
const CODE_EXTENSIONS = ['json', 'yaml', 'yml', 'toml', 'js', 'jsx', 'ts', 'tsx', 'py', 'schema']
|
||||
const TEXT_EXTENSIONS = ['txt', 'log', 'ini', 'env']
|
||||
const IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'bmp', 'ico']
|
||||
const VIDEO_EXTENSIONS = ['mp4', 'mov', 'webm', 'mpeg', 'mpg', 'm4v', 'avi']
|
||||
const OFFICE_EXTENSIONS = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx']
|
||||
|
||||
export function getFileExtension(name?: string, extension?: string): string {
|
||||
if (extension)
|
||||
return extension.toLowerCase()
|
||||
if (!name)
|
||||
return ''
|
||||
return name.split('.').pop()?.toLowerCase() ?? ''
|
||||
}
|
||||
|
||||
export function getFileIconType(name: string): FileAppearanceTypeEnum {
|
||||
const extension = name.split('.').pop()?.toLowerCase() ?? ''
|
||||
|
||||
if (MARKDOWN_EXTENSIONS.includes(extension))
|
||||
return FileAppearanceTypeEnum.markdown
|
||||
|
||||
if (CODE_EXTENSIONS.includes(extension))
|
||||
return FileAppearanceTypeEnum.code
|
||||
|
||||
return FileAppearanceTypeEnum.document
|
||||
}
|
||||
|
||||
export function isMarkdownFile(extension: string): boolean {
|
||||
return MARKDOWN_EXTENSIONS.includes(extension)
|
||||
}
|
||||
|
||||
export function isCodeOrTextFile(extension: string): boolean {
|
||||
return CODE_EXTENSIONS.includes(extension) || TEXT_EXTENSIONS.includes(extension)
|
||||
}
|
||||
|
||||
export function isImageFile(extension: string): boolean {
|
||||
return IMAGE_EXTENSIONS.includes(extension)
|
||||
}
|
||||
|
||||
export function isVideoFile(extension: string): boolean {
|
||||
return VIDEO_EXTENSIONS.includes(extension)
|
||||
}
|
||||
|
||||
export function isOfficeFile(extension: string): boolean {
|
||||
return OFFICE_EXTENSIONS.includes(extension)
|
||||
}
|
||||
|
||||
export function getFileLanguage(name: string): string {
|
||||
const extension = name.split('.').pop()?.toLowerCase() ?? ''
|
||||
|
||||
const languageMap: Record<string, string> = {
|
||||
md: 'markdown',
|
||||
markdown: 'markdown',
|
||||
mdx: 'markdown',
|
||||
json: 'json',
|
||||
jsonl: 'json',
|
||||
yaml: 'yaml',
|
||||
yml: 'yaml',
|
||||
js: 'javascript',
|
||||
jsx: 'javascript',
|
||||
ts: 'typescript',
|
||||
tsx: 'typescript',
|
||||
py: 'python',
|
||||
html: 'html',
|
||||
css: 'css',
|
||||
xml: 'xml',
|
||||
sql: 'sql',
|
||||
sh: 'shell',
|
||||
bash: 'shell',
|
||||
}
|
||||
|
||||
return languageMap[extension] ?? 'plaintext'
|
||||
}
|
||||
86
web/app/components/workflow/skill/utils/tree-utils.ts
Normal file
86
web/app/components/workflow/skill/utils/tree-utils.ts
Normal file
@ -0,0 +1,86 @@
|
||||
import type { AppAssetTreeView } from '@/types/app-asset'
|
||||
|
||||
export function buildNodeMap(nodes: AppAssetTreeView[]): Map<string, AppAssetTreeView> {
|
||||
const map = new Map<string, AppAssetTreeView>()
|
||||
|
||||
function traverse(nodeList: AppAssetTreeView[]): void {
|
||||
for (const node of nodeList) {
|
||||
map.set(node.id, node)
|
||||
if (node.children && node.children.length > 0)
|
||||
traverse(node.children)
|
||||
}
|
||||
}
|
||||
|
||||
traverse(nodes)
|
||||
return map
|
||||
}
|
||||
|
||||
export function getAncestorIds(nodeId: string, nodes: AppAssetTreeView[]): string[] {
|
||||
const ancestors: string[] = []
|
||||
|
||||
function findPath(nodeList: AppAssetTreeView[], targetId: string, currentPath: string[]): boolean {
|
||||
for (const node of nodeList) {
|
||||
if (node.id === targetId) {
|
||||
ancestors.push(...currentPath)
|
||||
return true
|
||||
}
|
||||
if (node.children && node.children.length > 0) {
|
||||
const newPath = node.node_type === 'folder' ? [...currentPath, node.id] : currentPath
|
||||
if (findPath(node.children, targetId, newPath))
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
findPath(nodes, nodeId, [])
|
||||
return ancestors
|
||||
}
|
||||
|
||||
export function toOpensObject(expandedIds: Set<string>): Record<string, boolean> {
|
||||
return Object.fromEntries([...expandedIds].map(id => [id, true]))
|
||||
}
|
||||
|
||||
export function findNodeById(
|
||||
nodes: AppAssetTreeView[],
|
||||
nodeId: string,
|
||||
): AppAssetTreeView | null {
|
||||
for (const node of nodes) {
|
||||
if (node.id === nodeId)
|
||||
return node
|
||||
if (node.children && node.children.length > 0) {
|
||||
const found = findNodeById(node.children, nodeId)
|
||||
if (found)
|
||||
return found
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export function getAllDescendantFileIds(
|
||||
nodeId: string,
|
||||
nodes: AppAssetTreeView[],
|
||||
): string[] {
|
||||
const targetNode = findNodeById(nodes, nodeId)
|
||||
if (!targetNode)
|
||||
return []
|
||||
|
||||
if (targetNode.node_type === 'file')
|
||||
return [targetNode.id]
|
||||
|
||||
const fileIds: string[] = []
|
||||
|
||||
function collectFileIds(nodeList: AppAssetTreeView[]): void {
|
||||
for (const node of nodeList) {
|
||||
if (node.node_type === 'file')
|
||||
fileIds.push(node.id)
|
||||
if (node.children && node.children.length > 0)
|
||||
collectFileIds(node.children)
|
||||
}
|
||||
}
|
||||
|
||||
if (targetNode.children)
|
||||
collectFileIds(targetNode.children)
|
||||
|
||||
return fileIds
|
||||
}
|
||||
Reference in New Issue
Block a user