All files app.js

97.5% Statements 195/200
95.83% Branches 46/48
100% Functions 11/11
97.5% Lines 195/200

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 2011x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 166x 166x 166x 166x 166x 166x 194x 194x 166x 166x 152x 113x 113x 152x 166x 166x 110x 8x 8x 8x 110x 166x 166x 49x 2x 2x 47x 49x 166x 166x 8x 8x 8x 166x 166x 1x 1x 1x 166x 166x 9x 9x 9x 9x 166x 166x 3x 3x 3x 166x 166x 125x 125x 1x 1x 125x 125x 1x 1x 125x 125x 89x 87x 87x 87x 87x 87x 87x 125x 125x 5x 5x 5x 5x 125x 125x 10x 10x 10x 9x 9x 9x 9x 9x 9x 9x 125x 125x 6x 6x 6x 6x 6x 125x 125x 3x 3x 3x 3x 3x 3x 125x 125x 3x 1x 1x 1x 1x 2x 2x 2x 2x 2x 125x 125x 2x 2x 2x 125x 125x 1x 1x 1x 1x 125x 125x 3x 3x 3x 3x 3x 125x 125x 1x 1x 125x 125x 166x 166x 2x 2x 2x 2x 2x 10x 10x 10x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x       2x 2x 2x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x  
import { make_grids } from './gridgen.js';
import { serializeStateToUrl } from './utils.js';
 
const DEFAULT_STATE = {
    items: [],
    words: [],
    status: 'initialized',
    workflow_stage: 'building_wordlist',
    grid_width: 10,
    grid_height: 10,
    all_cross: true,
    allow_reverse: false,
    allow_diagonals: false,
    candidates: [],
    focused_candidate_index: 0,
    message: '',
    grid_mode: 'crossword'
};
 
export function createApp(options = {}) {
    const state = JSON.parse(JSON.stringify({ ...DEFAULT_STATE, ...(options.initialState || {}) }));
    const interfaces = [];
    const history = options.history;
    const enableUrlSync = options.enableUrlSync;
 
    function getState() {
        return JSON.parse(JSON.stringify(state));
    }
 
    function broadcast() {
        for (const iface of interfaces) {
            iface.onStateChange(getState());
        }
    }
 
    function syncUrl() {
        if (enableUrlSync && history) {
            const url = '?' + serializeStateToUrl(state);
            history.pushState(null, '', url);
        }
    }
 
    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) {
        const id = Date.now() + '-' + Math.random().toString(36).substr(2, 9);
        state.items.push({ id, 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);
                break;
 
            case 'REMOVE_ITEM':
                removeItem(payload);
                break;
 
            case 'ADD_WORD': {
                if (!payload || !payload.trim()) break;
                const id = Date.now() + '-' + Math.random().toString(36).substr(2, 9);
                state.words.unshift({ id, value: payload.trim().toUpperCase() });
                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) break;
                const [word] = state.words.splice(idx, 1);
                const clamped = Math.max(0, Math.min(newIndex, state.words.length));
                state.words.splice(clamped, 0, word);
                broadcast();
                syncUrl();
                break;
            }
 
            case 'SET_GRID_SIZE':
                state.grid_width = payload.width;
                state.grid_height = payload.height;
                broadcast();
                syncUrl();
                break;
 
            case 'SET_GRID_OPTIONS':
                if (payload.all_cross !== undefined) state.all_cross = payload.all_cross;
                if (payload.allow_reverse !== undefined) state.allow_reverse = payload.allow_reverse;
                if (payload.allow_diagonals !== undefined) state.allow_diagonals = payload.allow_diagonals;
                broadcast();
                syncUrl();
                break;
 
            case 'START_GENERATION':
                if (!state.words || 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();
                setTimeout(() => runGeneration(), 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.workflow_stage = 'candidate_selected';
                state.grid_mode = 'crossword';
                state.message = 'Crossword created successfully!';
                broadcast();
                break;
 
            default:
                // Unknown event, ignore silently
                break;
        }
    }
 
    function runGeneration() {
        const result = make_grids(
            state.words.map(w => w.value),
            state.grid_width, state.grid_height,
            5000, state.all_cross, state.allow_diagonals, state.allow_reverse,
            (found, max) => {
                state.message = `Generating candidate grids... (${found}/${max} found)`;
                broadcast();
            }
        );
 
        state.candidates = result.payload;
        state.focused_candidate_index = 0;
 
        if (result.payload.length > 0) {
            state.workflow_stage = 'exploring_candidates';
            let msg = result.info.message;
            if (result.info.lost_words.length > 0) {
                msg += '\nCould not fit: ' + result.info.lost_words.join(', ');
            }
            if (result.info.used_words.length > 0) {
                msg += '\nUsed words: ' + result.info.used_words.join(', ');
            }
            state.message = msg;
        } else {
            state.workflow_stage = 'building_wordlist';
            state.message = 'Failed to generate grids. ' + result.info.message;
        }
 
        broadcast();
    }
 
    return {
        getState,
        registerInterface,
        start,
        stop,
        addItem,
        removeItem,
        handleEvent
    };
}