fixed-field-layout.js 858 Bytes
/**
 * 将 fixedFields 按 divider 拆成白卡片区块与独立 divider 块。
 * @param {Array} fixedFields
 * @returns {Array<{ type: 'section'|'divider', fields?: Array, field?: object, blockKey: string }>}
 */
function buildFixedFieldBlocks(fixedFields = []) {
	const blocks = []
	let section = []

	for (const field of fixedFields) {
		if (field.type === 'divider') {
			if (section.length) {
				blocks.push({
					type: 'section',
					fields: section,
					blockKey: `section-${section[0].key}`
				})
			}
			blocks.push({
				type: 'divider',
				field,
				blockKey: `divider-${field.key}`
			})
			section = []
		} else {
			section.push(field)
		}
	}

	if (section.length) {
		blocks.push({
			type: 'section',
			fields: section,
			blockKey: `section-${section[0].key}`
		})
	}

	return blocks
}

module.exports = {
	buildFixedFieldBlocks
}