Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import { yakitNotify } from '@/utils/notification'
import { invalidateCustomSnippetsCache } from '@/utils/monacoSpec/yakCompletionSchema'
import type {
CodeCustomizeModalProps,
RowOf,
TCodeCustomizeTagProps,
TCustomCodeGeneral,
TCustomEditorCodeGeneral,
TQueryCustomCodeRequest,
} from './CustomizeCodeTypes'
import { getAllRows } from './CustomizeCodeTypes'
import { YakitSpin } from '../yakitUI/YakitSpin/YakitSpin'
import { OutlineXIcon } from '@/assets/icon/outline'
import { useI18nNamespaces } from '@/i18n/useI18nNamespaces'
Expand Down Expand Up @@ -156,23 +156,6 @@ const LevelOptions = [
},
]

export const getAllRows = <T extends Record<string, any[]>>(data: T): RowOf<T>[] => {
const keys = Object.keys(data) as (keyof T)[]
const length = Math.max(...keys.map((k) => data[k].length))

return Array.from({ length }, (_, index) =>
keys.reduce((obj, key) => {
const arr = data[key]
if (Array.isArray(arr) && index < arr.length) {
// 将 key 末尾的 s 去掉
const singular = (key as string).replace(/s$/, '')
;(obj as any)[singular] = arr[index]
}
return obj
}, {} as RowOf<T>),
)
}

const CodeCustomize: FC<Partial<TCodeCustomizeTagProps>> = ({ value }) => {
const [form] = useForm()
const { theme } = useTheme()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ type RowOf<T extends Record<string, any[]>> = {
[K in keyof T as K extends `${infer S}s` ? S : K]: T[K][number]
}

/** 将「列数组」结构转为行对象数组(与 UI 解耦,供 monacoSpec 等轻量引用) */
export const getAllRows = <T extends Record<string, any[]>>(data: T): RowOf<T>[] => {
const keys = Object.keys(data) as (keyof T)[]
const length = Math.max(...keys.map((k) => data[k].length))

Comment on lines +45 to +48
return Array.from({ length }, (_, index) =>
keys.reduce((obj, key) => {
const arr = data[key]
if (Array.isArray(arr) && index < arr.length) {
// 将 key 末尾的 s 去掉
const singular = (key as string).replace(/s$/, '')
;(obj as any)[singular] = arr[index]
}
return obj
}, {} as RowOf<T>),
)
}

export type {
TCodeCustomizeTagProps,
CodeCustomizeModalProps,
Expand Down
Loading
Loading