cookcli 0.32.0

A command-line interface for managing and working with Cooklang recipes
Documentation
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
{% extends "base.html" %}

{% block title %}Edit: {{ recipe_name }} - Cook{% endblock %}

{% block content %}
<div class="flex flex-col h-[calc(100vh-12rem)]">
    <!-- Header bar -->
    <div class="flex items-center justify-between mb-4">
        <div class="flex items-center gap-4">
            <a href="{{ prefix }}/recipe/{{ recipe_path }}" class="px-4 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 transition-colors flex items-center gap-2">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path>
                </svg>
                {{ tr.t("action-back") }}
            </a>
            <h1 class="text-2xl font-bold text-gray-800">{{ recipe_name }}</h1>
        </div>
        <div class="flex items-center gap-3">
            <span id="save-status" class="text-sm text-gray-500"></span>
            <button onclick="showDeleteModal()" class="px-4 py-2 bg-red-500 text-white rounded-lg hover:bg-red-600 transition-colors flex items-center gap-2">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
                </svg>
                {{ tr.t("action-delete") }}
            </button>
        </div>
    </div>

    <!-- Delete confirmation modal -->
    <div id="delete-modal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
        <div class="bg-white dark:bg-gray-800 rounded-2xl shadow-xl p-6 max-w-md mx-4">
            <div class="flex items-center gap-3 mb-4">
                <div class="w-12 h-12 rounded-full bg-red-100 dark:bg-red-900 flex items-center justify-center">
                    <svg class="w-6 h-6 text-red-600 dark:text-red-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path>
                    </svg>
                </div>
                <h2 class="text-xl font-bold text-gray-800 dark:text-gray-100">{{ tr.t("delete-recipe") }}</h2>
            </div>
            <p class="text-gray-600 dark:text-gray-300 mb-2">{{ tr.t("delete-recipe-confirm") }}</p>
            <p class="text-sm text-red-600 dark:text-red-400 mb-6">{{ tr.t("delete-recipe-warning") }}</p>
            <div class="flex justify-end gap-3">
                <button onclick="hideDeleteModal()" class="px-4 py-2 bg-gray-200 text-gray-700 dark:bg-gray-700 dark:text-gray-200 rounded-lg hover:bg-gray-300 dark:hover:bg-gray-600 transition-colors">
                    {{ tr.t("action-cancel") }}
                </button>
                <button onclick="deleteRecipe()" class="px-4 py-2 bg-red-500 text-white rounded-lg hover:bg-red-600 transition-colors">
                    {{ tr.t("action-delete") }}
                </button>
            </div>
        </div>
    </div>

    <!-- Editor area -->
    <div id="editor-container" class="flex-1 bg-white rounded-2xl shadow-lg overflow-hidden"></div>

    <!-- Status bar -->
    <div id="status-bar" class="mt-2 px-4 py-2 bg-gray-100 rounded-lg flex items-center justify-between text-sm">
        <div class="flex items-center gap-2">
            <span id="lsp-status" class="flex items-center gap-1">
                <span id="lsp-indicator" class="w-2 h-2 rounded-full bg-gray-400"></span>
                <span id="lsp-text">Disconnected</span>
            </span>
        </div>
        <div id="cursor-position" class="text-gray-500">
            Line 1, Col 1
        </div>
    </div>
</div>

<script src="{{ prefix }}/static/js/editor.bundle.js"></script>
<script>
const recipePath = {{ recipe_path|json|safe }};
const basePath = {{ base_path|json|safe }};
let originalContent = {{ content|json|safe }};
let editorView = null;
let autosaveTimer = null;
let isSaving = false;

// Timing constants
const AUTOSAVE_DELAY_MS = 1000;
const LSP_INITIAL_CONNECT_DELAY_MS = 500;
const LSP_RECONNECT_DELAY_MS = 3000;
const COMPLETION_TIMEOUT_MS = 2000;

// Initialize CodeMirror editor
document.addEventListener('DOMContentLoaded', function() {
    const container = document.getElementById('editor-container');
    editorView = window.CooklangEditor.initEditor(container, originalContent, function(newContent) {
        // Update save status
        if (newContent !== originalContent) {
            updateSaveStatus('modified');
            // Debounced autosave (1 second after last change)
            if (autosaveTimer) clearTimeout(autosaveTimer);
            autosaveTimer = setTimeout(() => saveRecipe(), AUTOSAVE_DELAY_MS);
        }

        // Send didChange to LSP with debouncing to avoid overwhelming the server
        if (lspInitialized && documentUri) {
            if (lspDebounceTimer) clearTimeout(lspDebounceTimer);
            lspDebounceTimer = setTimeout(() => {
                documentVersion++;
                sendLspNotification('textDocument/didChange', {
                    textDocument: {
                        uri: documentUri,
                        version: documentVersion
                    },
                    contentChanges: [{ text: newContent }]
                });
            }, LSP_DEBOUNCE_MS);
        }
    });

    // Set up cursor position tracking (must be after editor init)
    window.CooklangEditor.setCursorPositionCallback((line, col) => {
        const posEl = document.getElementById('cursor-position');
        if (posEl) {
            posEl.textContent = `Line ${line}, Col ${col}`;
        }
    });
});

function updateSaveStatus(state) {
    const status = document.getElementById('save-status');
    switch (state) {
        case 'modified':
            status.textContent = 'Modified';
            status.className = 'text-sm text-orange-500';
            break;
        case 'saving':
            status.textContent = 'Saving...';
            status.className = 'text-sm text-gray-500';
            break;
        case 'saved':
            status.textContent = 'Saved';
            status.className = 'text-sm text-green-500';
            break;
        case 'error':
            status.textContent = 'Save failed';
            status.className = 'text-sm text-red-500';
            break;
    }
}

function showToast(message, type = 'error') {
    // Create toast element
    const toast = document.createElement('div');
    toast.className = `fixed bottom-4 right-4 px-4 py-2 rounded-lg shadow-lg z-50 ${
        type === 'error' ? 'bg-red-500 text-white' : 'bg-green-500 text-white'
    }`;
    toast.textContent = message;
    document.body.appendChild(toast);

    // Remove after 5 seconds
    setTimeout(() => {
        toast.remove();
    }, 5000);
}

async function saveRecipe() {
    if (isSaving) return;

    const content = window.CooklangEditor.getContent(editorView);
    if (content === originalContent) return;

    isSaving = true;
    updateSaveStatus('saving');

    try {
        const response = await fetch(`{{ prefix }}/api/recipes/${encodeURIComponent(recipePath)}`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'text/plain',
            },
            body: content
        });

        if (response.ok) {
            originalContent = content;
            updateSaveStatus('saved');
        } else {
            updateSaveStatus('error');
            const errorText = await response.text();
            console.error('Save failed:', errorText);
            showToast(`Save failed: ${errorText || 'Unknown error'}`);
        }
    } catch (error) {
        updateSaveStatus('error');
        console.error('Save failed:', error.message);
        showToast(`Save failed: ${error.message}`);
    } finally {
        isSaving = false;
    }
}

// Keyboard shortcut: Ctrl+S / Cmd+S - save immediately
document.addEventListener('keydown', function(e) {
    if ((e.ctrlKey || e.metaKey) && e.key === 's') {
        e.preventDefault();
        if (autosaveTimer) clearTimeout(autosaveTimer);
        saveRecipe();
    }
});

// Initial status
updateSaveStatus('saved');

// Delete modal functions
function showDeleteModal() {
    const modal = document.getElementById('delete-modal');
    modal.classList.remove('hidden');
    modal.classList.add('flex');
}

function hideDeleteModal() {
    const modal = document.getElementById('delete-modal');
    modal.classList.add('hidden');
    modal.classList.remove('flex');
}

async function deleteRecipe() {
    try {
        const response = await fetch(`{{ prefix }}/api/recipes/${encodeURIComponent(recipePath)}`, {
            method: 'DELETE'
        });

        if (response.ok) {
            // Redirect to recipes list after successful deletion
            window.location.href = '{{ prefix }}/';
        } else {
            const errorText = await response.text();
            console.error('Delete failed:', errorText);
            hideDeleteModal();
            showToast(`Failed to delete recipe: ${errorText || 'Unknown error'}`);
        }
    } catch (error) {
        console.error('Delete failed:', error.message);
        hideDeleteModal();
        showToast(`Failed to delete recipe: ${error.message}`);
    }
}

// Close modal when clicking outside
document.getElementById('delete-modal').addEventListener('click', function(e) {
    if (e.target === this) {
        hideDeleteModal();
    }
});

// Close modal with Escape key
document.addEventListener('keydown', function(e) {
    if (e.key === 'Escape') {
        hideDeleteModal();
    }
});

// LSP WebSocket connection
let lspSocket = null;
let lspMessageId = 1;
let documentUri = null;
let documentVersion = 0;
let lspInitialized = false;
let pendingCompletions = new Map();
let lspDebounceTimer = null;
const LSP_DEBOUNCE_MS = 150;

function connectLsp() {
    const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
    const wsUrl = `${protocol}//${window.location.host}{{ prefix }}/api/ws/lsp`;

    console.log('Connecting to LSP at:', wsUrl);
    lspSocket = new WebSocket(wsUrl);

    lspSocket.onopen = function() {
        console.log('LSP WebSocket connected');
        updateLspStatus('connected');
        // Send initialize request
        sendLspRequest('initialize', {
            processId: null,
            rootUri: 'file://' + basePath,
            capabilities: {
                textDocument: {
                    completion: {
                        completionItem: {
                            snippetSupport: false
                        }
                    },
                    publishDiagnostics: {
                        relatedInformation: true
                    }
                }
            }
        });
    };

    lspSocket.onclose = function() {
        console.log('LSP WebSocket closed');
        updateLspStatus('disconnected');
        // Reconnect after delay
        setTimeout(connectLsp, LSP_RECONNECT_DELAY_MS);
    };

    lspSocket.onerror = function(error) {
        console.error('LSP WebSocket error:', error);
        updateLspStatus('error');
    };

    lspSocket.onmessage = function(event) {
        try {
            const message = JSON.parse(event.data);
            handleLspMessage(message);
        } catch (e) {
            console.error('Failed to parse LSP message:', e);
        }
    };
}

function sendLspRequest(method, params) {
    if (!lspSocket || lspSocket.readyState !== WebSocket.OPEN) {
        return null;
    }
    const id = lspMessageId++;
    const message = {
        jsonrpc: '2.0',
        id: id,
        method: method,
        params: params
    };
    console.log('Sending LSP request:', method);
    lspSocket.send(JSON.stringify(message));
    return id;
}

function sendLspNotification(method, params) {
    if (!lspSocket || lspSocket.readyState !== WebSocket.OPEN) {
        return;
    }
    const message = {
        jsonrpc: '2.0',
        method: method,
        params: params
    };
    lspSocket.send(JSON.stringify(message));
}

function openDocument() {
    documentUri = 'file://' + recipePath;
    documentVersion = 1;
    sendLspNotification('textDocument/didOpen', {
        textDocument: {
            uri: documentUri,
            languageId: 'cooklang',
            version: documentVersion,
            text: window.CooklangEditor.getContent(editorView)
        }
    });
    console.log('Document opened:', documentUri);
}

function handleDiagnostics(params) {
    if (!editorView) return;

    const doc = editorView.state.doc;
    const diagnostics = (params.diagnostics || []).map(d => {
        // Convert LSP position (0-based line/char) to CodeMirror offset
        const startLine = Math.min(d.range.start.line + 1, doc.lines);
        const endLine = Math.min(d.range.end.line + 1, doc.lines);

        const line1 = doc.line(startLine);
        const line2 = doc.line(endLine);

        const from = line1.from + Math.min(d.range.start.character, line1.length);
        const to = line2.from + Math.min(d.range.end.character, line2.length);

        return {
            from: Math.max(0, from),
            to: Math.max(from, to),
            severity: d.severity === 1 ? 'error' : d.severity === 2 ? 'warning' : 'info',
            message: d.message
        };
    });

    window.CooklangEditor.setDiagnostics(editorView, diagnostics);

    // Update status bar
    const errorCount = diagnostics.filter(d => d.severity === 'error').length;
    const warningCount = diagnostics.filter(d => d.severity === 'warning').length;
    console.log(`Diagnostics: ${errorCount} errors, ${warningCount} warnings`);
}

function handleLspMessage(message) {
    if (message.id !== undefined) {
        // Check for pending completion request
        if (pendingCompletions.has(message.id)) {
            const resolve = pendingCompletions.get(message.id);
            pendingCompletions.delete(message.id);
            const items = message.result?.items || message.result || [];
            resolve(items);
            return;
        }

        // Handle other responses
        if (message.result && message.result.capabilities) {
            sendLspNotification('initialized', {});
            lspInitialized = true;
            openDocument();
            console.log('LSP initialized successfully');
        }
    } else if (message.method) {
        console.log('LSP notification:', message.method);
        if (message.method === 'textDocument/publishDiagnostics') {
            handleDiagnostics(message.params);
        }
    }
}

function updateLspStatus(status) {
    const indicator = document.getElementById('lsp-indicator');
    const text = document.getElementById('lsp-text');

    if (!indicator || !text) return;

    switch (status) {
        case 'connected':
            indicator.className = 'w-2 h-2 rounded-full bg-green-500';
            text.textContent = 'LSP Connected';
            text.className = 'text-green-600';
            break;
        case 'disconnected':
            indicator.className = 'w-2 h-2 rounded-full bg-gray-400';
            text.textContent = 'Disconnected';
            text.className = 'text-gray-500';
            break;
        case 'error':
            indicator.className = 'w-2 h-2 rounded-full bg-red-500';
            text.textContent = 'LSP Error';
            text.className = 'text-red-600';
            break;
    }
}

// Connect to LSP after editor is initialized
setTimeout(connectLsp, LSP_INITIAL_CONNECT_DELAY_MS);

// Set up completion resolver
window.CooklangEditor.setCompletionResolver(async (line, character) => {
    return new Promise((resolve) => {
        if (!lspInitialized || !documentUri) {
            resolve([]);
            return;
        }

        const id = sendLspRequest('textDocument/completion', {
            textDocument: { uri: documentUri },
            position: { line: line, character: character }
        });

        if (id) {
            pendingCompletions.set(id, resolve);
            // Timeout after 2 seconds
            setTimeout(() => {
                if (pendingCompletions.has(id)) {
                    pendingCompletions.delete(id);
                    resolve([]);
                }
            }, COMPLETION_TIMEOUT_MS);
        } else {
            resolve([]);
        }
    });
});

</script>
{% endblock %}