|
304 | 304 | return true; |
305 | 305 | } |
306 | 306 |
|
307 | | - function selectTemplate(tpl) { |
| 307 | + /** |
| 308 | + * Actually apply the template content into the editor (no confirmation). |
| 309 | + */ |
| 310 | + function loadTemplate(tpl) { |
| 311 | + // Clear any stale cloud/shared-doc session so the template loads cleanly |
| 312 | + // and cloud auto-save doesn't re-inject the old session hash into the URL. |
| 313 | + if (M.clearCloudSession) M.clearCloudSession(); |
| 314 | + |
308 | 315 | let content = tpl.content; |
309 | 316 |
|
310 | 317 | // If the template defines variables, generate the block at the top |
|
315 | 322 | // Resolve global built-in variables (date, time, etc.) |
316 | 323 | content = resolveGlobalVariables(content); |
317 | 324 |
|
| 325 | + // Push undo state so user can Ctrl+Z to recover previous content |
| 326 | + M.markdownEditor.dispatchEvent(new Event('input')); // ensure undo captures current state |
| 327 | + |
318 | 328 | M.markdownEditor.value = content; |
319 | 329 | M.renderMarkdown(); |
320 | 330 | closeTemplateModal(); |
|
323 | 333 | M.markdownEditor.scrollTop = 0; |
324 | 334 | } |
325 | 335 |
|
| 336 | + /** |
| 337 | + * Show a confirmation overlay before replacing editor content with a template. |
| 338 | + * Styled consistently with the clear-confirm overlay in editor-features.js. |
| 339 | + */ |
| 340 | + function showTemplateConfirm(tpl) { |
| 341 | + // Remove any existing modal |
| 342 | + var old = document.getElementById('template-confirm-modal'); |
| 343 | + if (old) old.remove(); |
| 344 | + |
| 345 | + var overlay = document.createElement('div'); |
| 346 | + overlay.id = 'template-confirm-modal'; |
| 347 | + overlay.className = 'clear-confirm-overlay'; |
| 348 | + overlay.innerHTML = |
| 349 | + '<div class="clear-confirm-card">' + |
| 350 | + '<div class="clear-confirm-header">' + |
| 351 | + '<i class="bi bi-arrow-repeat"></i> Replace editor content?' + |
| 352 | + '</div>' + |
| 353 | + '<div class="clear-confirm-body">' + |
| 354 | + '<p>Loading <strong>' + tpl.name + '</strong> will replace your current work.</p>' + |
| 355 | + '<p class="clear-confirm-hint"><i class="bi bi-arrow-counterclockwise"></i> You can Undo with <kbd>Ctrl+Z</kbd></p>' + |
| 356 | + '</div>' + |
| 357 | + '<div class="clear-confirm-actions">' + |
| 358 | + '<button class="clear-confirm-cancel">Cancel</button>' + |
| 359 | + '<button class="clear-confirm-ok"><i class="bi bi-file-earmark-text"></i> Use Template</button>' + |
| 360 | + '</div>' + |
| 361 | + '</div>'; |
| 362 | + |
| 363 | + document.body.appendChild(overlay); |
| 364 | + requestAnimationFrame(function () { overlay.classList.add('active'); }); |
| 365 | + |
| 366 | + function close() { |
| 367 | + overlay.classList.remove('active'); |
| 368 | + setTimeout(function () { overlay.remove(); }, 200); |
| 369 | + document.removeEventListener('keydown', escHandler); |
| 370 | + } |
| 371 | + |
| 372 | + function escHandler(e) { |
| 373 | + if (e.key === 'Escape') { e.stopPropagation(); close(); } |
| 374 | + } |
| 375 | + document.addEventListener('keydown', escHandler, true); |
| 376 | + |
| 377 | + overlay.addEventListener('click', function (e) { if (e.target === overlay) close(); }); |
| 378 | + overlay.querySelector('.clear-confirm-cancel').addEventListener('click', close); |
| 379 | + overlay.querySelector('.clear-confirm-ok').addEventListener('click', function () { |
| 380 | + close(); |
| 381 | + loadTemplate(tpl); |
| 382 | + }); |
| 383 | + overlay.querySelector('.clear-confirm-cancel').focus(); |
| 384 | + } |
| 385 | + |
| 386 | + /** |
| 387 | + * Select a template: if editor has meaningful content, confirm first; |
| 388 | + * otherwise load immediately. |
| 389 | + */ |
| 390 | + function selectTemplate(tpl) { |
| 391 | + var editorContent = M.markdownEditor.value.trim(); |
| 392 | + var defaultContent = (M.getDefaultContent ? M.getDefaultContent().trim() : ''); |
| 393 | + var hasContent = editorContent.length > 0 && editorContent !== defaultContent; |
| 394 | + |
| 395 | + if (hasContent) { |
| 396 | + showTemplateConfirm(tpl); |
| 397 | + } else { |
| 398 | + loadTemplate(tpl); |
| 399 | + } |
| 400 | + } |
| 401 | + |
326 | 402 | function openTemplateModal() { |
327 | 403 | templateSearchInput.value = ''; |
328 | 404 | activeTemplateCategory = 'all'; |
|
0 commit comments