From d48fd667a2fc46b90e78a867d5334cc4c132ad43 Mon Sep 17 00:00:00 2001 From: Juergen Kunz Date: Thu, 26 Jun 2025 13:38:09 +0000 Subject: [PATCH] update --- .../wysiwyg/blocks/text/code.block.ts | 52 +++++++++++++------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/ts_web/elements/wysiwyg/blocks/text/code.block.ts b/ts_web/elements/wysiwyg/blocks/text/code.block.ts index 6fb7995..2fc1682 100644 --- a/ts_web/elements/wysiwyg/blocks/text/code.block.ts +++ b/ts_web/elements/wysiwyg/blocks/text/code.block.ts @@ -5,13 +5,14 @@ import { WysiwygSelection } from '../../wysiwyg.selection.js'; import hlight from 'highlight.js'; /** - * New CodeBlockHandler with improved architecture + * CodeBlockHandler with improved architecture * - * Key improvements: - * 1. Simpler DOM structure - * 2. Better line number handling - * 3. Non-intrusive syntax highlighting - * 4. Cleaner event handling + * Key features: + * 1. Simple DOM structure + * 2. Line number handling + * 3. Syntax highlighting only when not focused (grey text while editing) + * 4. Clean event handling + * 5. Copy button functionality */ export class CodeBlockHandler extends BaseBlockHandler { type = 'code'; @@ -118,6 +119,23 @@ export class CodeBlockHandler extends BaseBlockHandler { editor.addEventListener('focus', () => { isEditing = true; container.classList.add('editing'); + + // Remove all syntax highlighting when focused + const content = editor.textContent || ''; + editor.textContent = content; // This removes all HTML formatting + + // Restore cursor position after removing highlighting + requestAnimationFrame(() => { + const range = document.createRange(); + const selection = window.getSelection(); + if (editor.firstChild) { + range.setStart(editor.firstChild, 0); + range.collapse(true); + selection?.removeAllRanges(); + selection?.addRange(range); + } + }); + handlers.onFocus(); }); @@ -137,15 +155,8 @@ export class CodeBlockHandler extends BaseBlockHandler { // Update line numbers this.updateLineNumbers(element); - // Clear any pending highlight + // Clear any pending highlight timer (no highlighting while editing) clearTimeout(this.highlightTimer); - - // Schedule highlighting (only if not actively editing) - if (!isEditing) { - this.highlightTimer = setTimeout(() => { - this.applyHighlighting(element, block); - }, 500); - } }); // Keydown handler @@ -196,8 +207,8 @@ export class CodeBlockHandler extends BaseBlockHandler { editor.addEventListener('compositionstart', () => handlers.onCompositionStart()); editor.addEventListener('compositionend', () => handlers.onCompositionEnd()); - // Initial syntax highlighting if content exists - if (block.content && !isEditing) { + // Initial syntax highlighting if content exists and not focused + if (block.content && document.activeElement !== editor) { requestAnimationFrame(() => { this.applyHighlighting(element, block); }); @@ -504,6 +515,15 @@ export class CodeBlockHandler extends BaseBlockHandler { pointer-events: none; } + /* When editing (focused), show grey text without highlighting */ + .code-block-container.editing .code-editor { + color: ${cssManager.bdTheme('#6b7280', '#9ca3af')} !important; + } + + .code-block-container.editing .code-editor * { + color: inherit !important; + } + /* Syntax Highlighting - Muted colors */ .code-editor .hljs-keyword { color: ${cssManager.bdTheme('#dc2626', '#f87171')};