Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | 1x 48x 48x 48x 48x 48x 48x 48x 68x 68x 48x 48x 67x 67x 67x 67x 67x 9x 67x 58x 58x 67x 67x 62x 67x 5x 5x 67x 67x 67x 48x 48x 5x 5x 5x 5x 5x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 48x 48x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 5x 5x 48x 48x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 5x 62x 57x 57x 62x 62x 62x 62x 62x 62x 5x 5x 62x 62x 62x 9x 9x 9x 62x 62x 62x 62x 62x 62x 16x 16x 16x 16x 16x 62x 62x 62x 62x 62x 62x 62x 62x 73x 73x 73x 73x 73x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 5x 5x 5x 62x 62x 62x 62x 62x 16x 16x 22x 22x 22x 22x 16x 16x 16x 16x 16x 16x 16x 62x 62x 62x 5x 5x 62x 62x 62x 62x 62x 62x 48x 48x 16x 16x 16x 16x 47x 275x 275x 275x 47x 16x 16x 16x 48x 48x 22x 22x 22x 22x 93x 693x 693x 93x 22x 22x 22x 48x 48x 62x 62x 62x 62x 55x 6x 6x 6x 6x 6x 6x 6x 6x 6x 55x 55x 62x 62x 62x 73x 1x 73x 62x 62x 62x 62x 62x 62x 62x 1x 1x 1x 1x 1x 1x 1x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 1x 62x 62x 62x 62x 62x 22x 1x 1x 22x 62x 62x 62x 62x 62x 16x 1x 16x 16x 62x 62x 62x 73x 2x 2x 73x 73x 73x 1x 1x 73x 73x 73x 1x 73x 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 73x 62x 62x 48x 48x 34x 34x 48x 48x 48x 48x 48x 48x 48x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 9x 9x 9x 9x 9x 9x 9x 3x 1x 1x 1x | export function createUIInterface(app, options = {}) {
const doc = options.document || (typeof document !== 'undefined' ? document : null);
if (!doc) {
throw new Error('Document not available');
}
let lastState = null;
let draggedWordId = null;
function getOutputElement() {
return doc.getElementById('output');
}
function render(state) {
const output = getOutputElement();
if (!output) return;
// Manage print class on body
if (state.workflow_stage === 'candidate_selected') {
doc.body.classList.add('final_grid_only');
} else {
doc.body.classList.remove('final_grid_only');
}
if (state.workflow_stage !== undefined) {
renderWorkflow(state, output);
} else {
renderLegacy(state, output);
}
lastState = state;
}
function renderLegacy(state, output) {
output.innerHTML = `
<div class="app-status">Status: ${state.status || ''}</div>
<h3>Items (${(state.items || []).length})</h3>
<div class="item-list">
${(state.items || []).map(item => `
<div class="item" data-id="${item.id}">
<span>${item.value}</span>
<button class="remove-btn" data-id="${item.id}">Remove</button>
</div>
`).join('')}
</div>
<div class="add-item">
<input type="text" id="newItem" placeholder="Add item...">
<button id="addBtn">Add</button>
</div>
`;
attachLegacyEvents(output);
}
function attachLegacyEvents(output) {
const addBtn = doc.getElementById('addBtn');
const input = doc.getElementById('newItem');
if (addBtn && input) {
addBtn.addEventListener('click', () => {
const value = input.value.trim();
if (value) {
app.handleEvent({ type: 'ADD_ITEM', payload: value });
input.value = '';
}
});
}
output.querySelectorAll('.remove-btn').forEach(btn => {
btn.addEventListener('click', () => {
app.handleEvent({ type: 'REMOVE_ITEM', payload: btn.getAttribute('data-id') });
});
});
}
function renderWorkflow(state, output) {
const isDisabled = state.workflow_stage === 'finding_candidates';
const disabledAttr = isDisabled ? 'disabled' : '';
const hasCandidates = state.candidates && state.candidates.length > 0;
const isExploring = state.workflow_stage === 'exploring_candidates';
const isSelected = state.workflow_stage === 'candidate_selected';
// Detect viewport for layout
const clientWidth = doc.documentElement.clientWidth;
const clientHeight = doc.documentElement.clientHeight;
const isLandscape = clientWidth > clientHeight && clientWidth >= 768;
if (isLandscape) {
output.classList.add('landscape-layout');
} else {
output.classList.remove('landscape-layout');
}
const gridPreviewLabel = isSelected ? 'Final version' : 'Selected Grid Preview';
let html = `<div class="workflow-container${isLandscape ? ' landscape-layout' : ''}">`;
if (isLandscape) {
html += `<div class="controls-section">`;
}
// Message area
if (state.message) {
const messageHtml = state.message.split('\n').map(line => `<div>${line}</div>`).join('');
html += `<div class="message-area">${messageHtml}</div>`;
}
// Grid preview section
html += `<div class="grid-preview-section">`;
html += `<h3>${gridPreviewLabel}</h3>`;
html += `<div id="focused-grid-container">`;
if (hasCandidates && state.focused_candidate_index !== undefined) {
const gridData = state.candidates[state.focused_candidate_index];
if (gridData) {
html += renderGridHTML(gridData);
}
}
html += `</div>`;
html += `</div>`;
// Word list
html += `<div class="word-list-section">`;
html += `<h3>Word List (${(state.words || []).length})</h3>`;
html += `<div class="word-list">`;
(state.words || []).forEach((word, index) => {
html += `<div class="word-item" draggable="true" data-word-id="${word.id}" data-word-index="${index}">`;
html += `<span class="word-drag-handle">⋮⋮</span>`;
html += `<span class="word-value">${word.value}</span>`;
html += `<button class="remove-word-btn" data-word-id="${word.id}" ${disabledAttr}>×</button>`;
html += `</div>`;
});
html += `</div>`;
// Add word input
html += `<div class="add-word">`;
html += `<input type="text" id="new-word-input" placeholder="Add word..." ${disabledAttr}>`;
html += `<button id="add-word-btn" ${disabledAttr}>Add</button>`;
html += `</div>`;
html += `</div>`;
// Grid controls
html += `<div class="grid-controls-section">`;
html += `<h3>Grid Controls</h3>`;
html += `<div class="grid-size-controls">`;
html += `<label>Width: <input type="number" id="grid-width-input" value="${state.grid_width || 10}" min="3" max="20" ${disabledAttr}></label>`;
html += `<label>Height: <input type="number" id="grid-height-input" value="${state.grid_height || 10}" min="3" max="20" ${disabledAttr}></label>`;
html += `</div>`;
html += `</div>`;
// Generate button
html += `<button id="start-generation-btn" ${disabledAttr}>Generate Grids</button>`;
if (isLandscape) {
html += `</div>`; // close controls-section
html += `<div class="grids-section">`;
}
// Candidate grids section
html += `<div class="candidate-grids-section">`;
html += `<h3>Candidate Grids</h3>`;
if (hasCandidates) {
html += `<div class="candidate-thumbnails">`;
state.candidates.forEach((grid, idx) => {
const activeClass = idx === state.focused_candidate_index ? 'active' : '';
html += `<div class="candidate-thumbnail ${activeClass}" data-index="${idx}">`;
html += renderThumbnailHTML(grid);
html += `</div>`;
});
html += `</div>`;
if (isExploring || isSelected) {
html += `<button id="select-crossword-btn">Use This Grid</button>`;
}
}
html += `</div>`;
if (isLandscape) {
html += `</div>`; // close grids-section
}
html += `</div>`; // close workflow-container
output.innerHTML = html;
attachWorkflowEvents(output, isDisabled);
}
function renderGridHTML(gridData) {
const height = gridData.length;
const width = gridData[0] ? gridData[0].length : 0;
let html = `<div class="word-grid" style="display:grid;grid-template-columns:repeat(${width},1fr);">`;
for (let r = 0; r < height; r++) {
for (let c = 0; c < width; c++) {
const cell = gridData[r][c];
html += `<div class="grid-cell" data-row="${r}" data-col="${c}">${cell}</div>`;
}
}
html += `</div>`;
return html;
}
function renderThumbnailHTML(gridData) {
const height = gridData.length;
const width = gridData[0] ? gridData[0].length : 0;
let html = `<div class="thumbnail-grid" style="display:grid;grid-template-columns:repeat(${width},1fr);">`;
for (let r = 0; r < height; r++) {
for (let c = 0; c < width; c++) {
html += `<div class="thumbnail-cell">${gridData[r][c]}</div>`;
}
}
html += `</div>`;
return html;
}
function attachWorkflowEvents(output, isDisabled) {
// Add word button
const addBtn = doc.getElementById('add-word-btn');
const wordInput = doc.getElementById('new-word-input');
if (addBtn && wordInput && !isDisabled) {
addBtn.addEventListener('click', () => {
const value = wordInput.value.trim();
if (value) {
app.handleEvent({ type: 'ADD_WORD', payload: value });
// Re-focus input after re-render
setTimeout(() => {
const newInput = doc.getElementById('new-word-input');
if (newInput) newInput.focus();
}, 0);
}
});
}
// Remove word buttons
output.querySelectorAll('.remove-word-btn').forEach(btn => {
btn.addEventListener('click', () => {
app.handleEvent({ type: 'REMOVE_WORD', payload: btn.getAttribute('data-word-id') });
});
});
// Grid size inputs
const widthInput = doc.getElementById('grid-width-input');
const heightInput = doc.getElementById('grid-height-input');
if (widthInput) {
widthInput.addEventListener('change', () => {
app.handleEvent({
type: 'SET_GRID_SIZE',
payload: {
width: parseInt(widthInput.value, 10),
height: parseInt(heightInput ? heightInput.value : '10', 10)
}
});
});
}
if (heightInput) {
heightInput.addEventListener('change', () => {
app.handleEvent({
type: 'SET_GRID_SIZE',
payload: {
width: parseInt(widthInput ? widthInput.value : '10', 10),
height: parseInt(heightInput.value, 10)
}
});
});
}
// Generate button
const startBtn = doc.getElementById('start-generation-btn');
if (startBtn) {
startBtn.addEventListener('click', () => {
app.handleEvent({ type: 'START_GENERATION' });
});
}
// Candidate thumbnail clicks
output.querySelectorAll('.candidate-thumbnail').forEach(thumb => {
thumb.addEventListener('click', () => {
const index = parseInt(thumb.getAttribute('data-index'), 10);
app.handleEvent({ type: 'FOCUS_CANDIDATE', payload: index });
});
});
// Select crossword button
const selectBtn = doc.getElementById('select-crossword-btn');
if (selectBtn) {
selectBtn.addEventListener('click', () => {
app.handleEvent({ type: 'SELECT_CROSSWORD' });
});
}
// Drag and drop on word items
output.querySelectorAll('.word-item').forEach(item => {
item.addEventListener('dragstart', (e) => {
draggedWordId = item.getAttribute('data-word-id');
item.classList.add('dragging');
});
item.addEventListener('dragend', (e) => {
item.classList.remove('dragging');
draggedWordId = null;
});
item.addEventListener('dragover', (e) => {
e.preventDefault();
});
item.addEventListener('drop', (e) => {
e.preventDefault();
if (draggedWordId) {
const targetIndex = parseInt(item.getAttribute('data-word-index'), 10);
app.handleEvent({
type: 'REORDER_WORD',
payload: { wordId: draggedWordId, newIndex: targetIndex }
});
draggedWordId = null;
}
});
});
}
function onStateChange(state) {
render(state);
}
return {
onStateChange,
render,
getOutputElement
};
}
export function renderGridToElement(gridData, container, options = {}) {
const doc = options.document || document;
const height = gridData.length;
const width = gridData[0] ? gridData[0].length : 0;
const gridWrapper = doc.createElement('div');
gridWrapper.className = 'word-grid';
gridWrapper.style.display = 'grid';
gridWrapper.style.gridTemplateColumns = `repeat(${width}, 1fr)`;
for (let r = 0; r < height; r++) {
for (let c = 0; c < width; c++) {
const cell = doc.createElement('div');
cell.className = 'grid-cell';
cell.setAttribute('data-row', String(r));
cell.setAttribute('data-col', String(c));
cell.textContent = gridData[r][c];
gridWrapper.appendChild(cell);
}
}
container.appendChild(gridWrapper);
}
|