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 | 1x 1x 1x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 1x 1x 158x 158x 158x 158x 158x 158x 158x 14x 14x 158x 158x 16x 16x 158x 158x 99x 99x 58x 58x 99x 158x 158x 44x 8x 8x 44x 158x 158x 55x 55x 158x 158x 31x 2x 2x 29x 31x 158x 158x 11x 11x 11x 158x 158x 1x 1x 1x 158x 158x 9x 9x 9x 9x 9x 9x 9x 158x 158x 3x 3x 3x 158x 158x 66x 66x 1x 1x 1x 66x 66x 1x 1x 1x 66x 66x 31x 31x 31x 31x 31x 31x 66x 66x 2x 2x 2x 2x 66x 66x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 66x 66x 4x 4x 4x 4x 4x 66x 66x 3x 3x 3x 3x 66x 66x 3x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 10x 10x 10x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 66x 66x 2x 2x 2x 66x 66x 1x 1x 1x 1x 66x 66x 8x 8x 8x 8x 8x 8x 8x 8x 66x 66x 7x 7x 7x 7x 7x 7x 7x 7x 21x 63x 38x 38x 63x 21x 7x 7x 7x 7x 66x 66x 1x 66x 66x 158x 158x 158x | import { make_grids } from './gridgen.js';
import { createId, deepClone, randomLetter, serializeStateToUrl } from './utils.js';
function defaultState() {
return {
items: [],
status: 'initialized',
words: [],
workflow_stage: 'building_wordlist',
grid_width: 10,
grid_height: 10,
all_cross: true,
allow_reverse: false,
allow_diagonals: false,
candidates: [],
focused_candidate_index: 0,
grid_mode: null,
message: ''
};
}
export function createApp(options = {}) {
const { initialState, history, enableUrlSync } = options;
let state = { ...defaultState(), ...(initialState || {}) };
const interfaces = [];
let originalGrids = null;
if (state.candidates.length > 0) {
saveOriginalGrids();
}
function saveOriginalGrids() {
originalGrids = state.candidates.map(c => deepClone(c.grid));
}
function broadcast() {
const snapshot = deepClone(state);
for (const iface of interfaces) {
iface.onStateChange(snapshot);
}
}
function syncUrl() {
if (enableUrlSync && history) {
history.pushState(null, '', '?' + serializeStateToUrl(state));
}
}
function getState() {
return deepClone(state);
}
function registerInterface(iface) {
if (!iface || typeof iface.onStateChange !== 'function') {
throw new Error('Interface must implement onStateChange method');
}
interfaces.push(iface);
}
function start() {
state.status = 'running';
broadcast();
}
function stop() {
state.status = 'stopped';
broadcast();
}
function addItem(value) {
state.items.push({
id: createId(),
value,
timestamp: Date.now()
});
broadcast();
}
function removeItem(id) {
state.items = state.items.filter(item => item.id !== id);
broadcast();
}
function handleEvent({ type, payload }) {
switch (type) {
case 'ADD_ITEM':
addItem(payload);
syncUrl();
break;
case 'REMOVE_ITEM':
removeItem(payload);
syncUrl();
break;
case 'ADD_WORD': {
const word = { id: createId(), value: payload };
state.words.unshift(word);
broadcast();
syncUrl();
break;
}
case 'REMOVE_WORD':
state.words = state.words.filter(w => w.id !== payload);
broadcast();
syncUrl();
break;
case 'REORDER_WORD': {
const { wordId, newIndex } = payload;
const idx = state.words.findIndex(w => w.id === wordId);
if (idx !== -1) {
const [word] = state.words.splice(idx, 1);
state.words.splice(newIndex, 0, word);
}
broadcast();
syncUrl();
break;
}
case 'SET_GRID_SIZE':
state.grid_width = parseInt(payload.width, 10);
state.grid_height = parseInt(payload.height, 10);
broadcast();
syncUrl();
break;
case 'SET_GRID_OPTIONS':
Object.assign(state, payload);
broadcast();
syncUrl();
break;
case 'START_GENERATION': {
if (state.words.length === 0) {
state.message = 'Please add at least one word';
broadcast();
break;
}
state.workflow_stage = 'finding_candidates';
state.message = 'Generating candidate grids';
broadcast();
const words = state.words.map(w => w.value);
const gw = state.grid_width;
const gh = state.grid_height;
const ac = state.all_cross;
const ad = state.allow_diagonals;
const ar = state.allow_reverse;
setTimeout(() => {
const result = make_grids(
words, gw, gh, 5000, ac, ad, ar,
(found, max) => {
state.message = `Generating candidate grids (${found}/${max})`;
broadcast();
}
);
state.candidates = result.payload;
state.focused_candidate_index = 0;
state.workflow_stage = 'exploring_candidates';
state.message = result.info.message;
saveOriginalGrids();
broadcast();
}, 0);
break;
}
case 'FOCUS_CANDIDATE':
state.focused_candidate_index = payload;
broadcast();
break;
case 'SELECT_CANDIDATE':
state.workflow_stage = 'candidate_selected';
state.message = 'Grid created successfully';
broadcast();
break;
case 'SELECT_CROSSWORD':
state.grid_mode = 'crossword';
if (originalGrids) {
state.candidates.forEach((candidate, i) => {
candidate.grid = deepClone(originalGrids[i]);
});
}
broadcast();
break;
case 'SELECT_WORDSEARCH': {
state.grid_mode = 'wordsearch';
if (originalGrids) {
state.candidates.forEach((candidate, i) => {
candidate.grid = deepClone(originalGrids[i]);
});
}
for (const candidate of state.candidates) {
for (const row of candidate.grid) {
for (let c = 0; c < row.length; c++) {
if (row[c] === '') {
row[c] = randomLetter();
}
}
}
}
broadcast();
break;
}
default:
break;
}
}
return { getState, registerInterface, start, stop, addItem, removeItem, handleEvent };
}
|