page = ({
id = randomId(),
title,
cells = [],
onDelete = (id) => {},
onUp = (id) => {},
onDown = (id) => {},
} = {}) => {
const cellBuilder = (args) => cell({
...args,
onDelete: (id) => {
const index = ui.value.cells.findIndex(cell => cell.id === id);
ui.value.cells.splice(index, 1);
ui.cells.dispatchEvent(new Event('input', {bubbles: true}));
},
onDown: (id) => {
const index = ui.value.cells.findIndex(cell => cell.id === id);
const element = ui.value.cells[index];
ui.value.cells.splice(index, 1);
ui.value.cells.splice(index + 1, 0, element);
ui.cells.dispatchEvent(new Event('input', {bubbles: true}));
},
onUp: (id) => {
const index = ui.value.cells.findIndex(cell => cell.id === id);
const element = ui.value.cells[index];
ui.value.cells.splice(index, 1);
ui.value.cells.splice(Math.max(index - 1, 0), 0, element);
ui.cells.dispatchEvent(new Event('input', {bubbles: true}));
}
});
const ui = view`<div class="[ page card ][ solid-shadow-1 space-y-3 ]">
${['_id', Inputs.input(id)]}
<div class="flex justify-between">
<div class="w-100 pr4">${['title', Inputs.text({label: "Page title", value: title})]}</div>
<div class="flex space-x-2">
${Inputs.button(buttonLabel({ariaLabel: "Delete", iconLeft: "trash-2", iconLeftClass: "icon--danger"}), {reduce: () => onDelete(id)})}
<div class="button-group">
${Inputs.button(buttonLabel({ariaLabel: "Move up", iconLeft: "arrow-up"}), {reduce: () => onUp(id)})}
${Inputs.button(buttonLabel({ariaLabel: "Move down", iconLeft: "arrow-down"}), {reduce: () => onDown(id)})}
</div>
</div>
</div>
<div class="box space-y-3">
${['cells', cells.map(cellData => cellBuilder(cellData)), cellBuilder]}
</div>
<div class="flex space-x-3">
<div class="button-group">
${Inputs.button('Add Question', {
reduce: () => {
ui.value.cells.push({inner: {type: "textarea"}})
ui.cells.dispatchEvent(new Event('input', {bubbles: true}));
}
})}
${Inputs.button('Add Text', {
reduce: () => {
ui.value.cells.push({inner: {type: "md"}});
ui.cells.dispatchEvent(new Event('input', {bubbles: true}));
}
})}
</div>
${Inputs.button('Add Section', {
reduce: () => {
ui.value.cells.push({inner: {type: "section"}});
ui.cells.dispatchEvent(new Event('input', {bubbles: true}));
}
})}
</div>
</div>`
return ui;
}