/**
* FormComponents — 表单字段组件库(create/edit 左栏调色板)
*
* 用法:
*
*
*
* 组件瓦片可点击添加,也可拖拽:dragstart 写入
* dataTransfer['application/x-field-type'] = type,
* 由 create/edit 页面字段列表的 drop 事件读取(拖到指定位置插入)。
*/
(function () {
'use strict';
if (window.FormComponents) return;
const ICONS = {
text: '',
textarea: '',
select: '',
radio: '',
checkbox: '',
rating: '',
number: '',
date: '',
time: '',
file: '',
slider: '',
page_break: '',
};
const CATALOG = [
{ type: 'text', label: '文本', hint: '短文本输入' },
{ type: 'textarea', label: '多行文本', hint: '长文本输入' },
{ type: 'number', label: '数字', hint: '数字输入' },
{ type: 'select', label: '下拉框', hint: '单选下拉' },
{ type: 'radio', label: '单选', hint: '多个选项选一个' },
{ type: 'checkbox', label: '多选', hint: '多个选项可多选' },
{ type: 'date', label: '日期', hint: '日期选择' },
{ type: 'time', label: '时间', hint: '时间选择' },
{ type: 'rating', label: '评分', hint: '星级评分' },
{ type: 'slider', label: '滑块', hint: '范围取值' },
{ type: 'file', label: '文件上传', hint: '附件收集' },
{ type: 'page_break', label: '分页', hint: '从这里开始新的一页' },
];
const CSS = `
.fc-palette { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 8px; }
@media (max-width: 480px) { .fc-palette { grid-template-columns: repeat(3, minmax(0,1fr)); } }
.fc-tile {
display: flex; flex-direction: column; align-items: center; gap: 6px;
padding: 12px 6px 10px; border-radius: 12px; cursor: grab;
border: 1px solid var(--border-color);
background: transparent;
width: 100%; color: var(--text-secondary); font: inherit; text-align: left;
transition: border-color .18s ease, background-color .18s ease, transform .18s ease;
user-select: none; -webkit-user-select: none;
}
.fc-tile:focus-visible { outline: 2px solid var(--border-focus); outline-offset: 2px; }
.fc-tile:hover {
border-color: var(--accent-bg, #6366f1);
background: color-mix(in srgb, var(--accent-bg, #6366f1) 8%, transparent);
transform: translateY(-1px);
}
.fc-tile:active { cursor: grabbing; }
.fc-tile svg { width: 20px; height: 20px; stroke: var(--text-secondary); fill: none;
stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.fc-tile:hover svg { stroke: var(--accent-bg, #6366f1); }
.fc-tile .fc-label { font-size: 11px; color: var(--text-secondary); white-space: nowrap; }
`;
function tileHtml(item) {
return `
`;
}
window.FormComponents = {
CATALOG,
createPageBreakCard(fieldId, title, onRemove) {
const card = document.createElement('div');
card.className = 'field-card page-break-card';
card.id = fieldId;
card.innerHTML = `
填写者完成当前页后,点击“下一页”进入这里。
`;
const input = card.querySelector('.page-title');
input.id = `${fieldId}_page_title`;
input.value = title || '';
card.querySelector('label').htmlFor = input.id;
card.querySelector('.page-break-remove').addEventListener('click', onRemove);
return card;
},
/* 渲染组件调色板。onAdd(type):点击组件回调;拖拽落位由字段列表处理。 */
renderPalette(container, onAdd) {
if (!container) return;
if (!document.getElementById('fc-palette-style')) {
const style = document.createElement('style');
style.id = 'fc-palette-style';
style.textContent = CSS;
document.head.appendChild(style);
}
container.classList.add('fc-palette');
container.innerHTML = CATALOG.map(tileHtml).join('');
container.querySelectorAll('.fc-tile').forEach(tile => {
const type = tile.dataset.fieldType;
tile.addEventListener('click', () => { if (onAdd) onAdd(type); });
tile.addEventListener('dragstart', (e) => {
e.dataTransfer.setData('application/x-field-type', type);
e.dataTransfer.setData('text/plain', 'fc:' + type);
e.dataTransfer.effectAllowed = 'copy';
tile.classList.add('fc-dragging');
});
tile.addEventListener('dragend', () => tile.classList.remove('fc-dragging'));
});
},
};
})();