|
103 | 103 | } |
104 | 104 |
|
105 | 105 |
|
106 | | - // CodeMirror syntax highlighting on reference/example pages |
107 | | - if (document.querySelector('.syntax-block, .impl-block')) { |
108 | | - const CDNJS = 'https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16'; |
109 | | - function loadScript(src, cb) { |
110 | | - const s = document.createElement('script'); s.src = src; |
111 | | - s.onload = cb; document.head.appendChild(s); |
112 | | - } |
113 | | - function loadLink(href) { |
114 | | - const l = document.createElement('link'); |
115 | | - l.rel = 'stylesheet'; l.href = href; |
116 | | - document.head.appendChild(l); |
117 | | - } |
118 | | - loadLink(CDNJS + '/codemirror.min.css'); |
119 | | - loadLink(SITE + '/assets/cppmode-theme.css'); |
120 | | - loadScript(CDNJS + '/codemirror.min.js', function() { |
121 | | - loadScript(CDNJS + '/mode/clike/clike.min.js', function() { |
122 | | - loadScript(SITE + '/assets/cppmode-keywords.js', function() { |
123 | | - loadScript(SITE + '/assets/cppmode.js', function() { |
124 | | - const dark = document.body.classList.contains('dark-mode'); |
125 | | - const allBlocks = document.querySelectorAll('.syntax-block, .impl-block'); |
126 | | - console.log('CM: found', allBlocks.length, 'blocks'); |
127 | | - allBlocks.forEach((el, i) => { |
128 | | - console.log('block', i, 'tag:', el.tagName, 'class:', el.className, 'display:', getComputedStyle(el).display, 'content len:', el.textContent.trim().length); |
129 | | - }); |
130 | | - console.log('CM wrappers already:', document.querySelectorAll('.cm-editor-wrap').length); |
131 | | - console.log('CM instances already:', document.querySelectorAll('.CodeMirror').length); |
132 | | - document.querySelectorAll('.CodeMirror').forEach((el,i) => { |
133 | | - if(i < 3) console.log('CM', i, 'parent:', el.parentElement?.className, 'grandparent:', el.parentElement?.parentElement?.className); |
134 | | - }); |
135 | | - if (document.querySelector('.cm-editor-wrap')) { console.log('GUARD: already rendered'); return; } |
136 | | - console.log('content children:', Array.from(document.querySelector('.content')?.children||[]).map(el=>el.tagName+'.'+el.className).join(', ')); |
137 | | - document.querySelectorAll('.syntax-block, .impl-block').forEach(el => { |
138 | | - el.style.cssText = 'display:none!important;visibility:hidden!important;height:0!important;overflow:hidden!important;'; |
139 | | - if (!el.parentNode) return; |
140 | | - const code = el.textContent.trim(); |
141 | | - if (!code) return; |
142 | | - const wrap = document.createElement('div'); |
143 | | - wrap.className = 'cm-editor-wrap'; |
144 | | - wrap.style.cssText = 'margin:0;padding:0;border:none;background:none;'; |
145 | | - el.parentNode.insertBefore(wrap, el); |
146 | | - const cm = CodeMirror(wrap, { |
147 | | - value: code, |
148 | | - mode: 'cppmode', |
149 | | - theme: dark ? 'cppmode-dark' : 'cppmode', |
150 | | - readOnly: true, |
151 | | - lineNumbers: false, |
152 | | - lineWrapping: false, |
153 | | - }); |
154 | | - // Force full height - CM defaults to 300px |
155 | | - const lineCount = cm.lineCount(); |
156 | | - const lineHeight = 18; |
157 | | - const height = lineCount * lineHeight + 10; |
158 | | - cm.setSize('100%', height + 'px'); |
159 | | - }); |
160 | | - // Switch theme on dark toggle |
161 | | - document.querySelectorAll('.CodeMirror').forEach(el => { |
162 | | - if (el.CodeMirror) el.CodeMirror.setOption('theme', dark ? 'cppmode-dark' : 'cppmode'); |
163 | | - }); |
164 | | - }); |
165 | | - }); |
166 | | - }); |
167 | | - }); |
168 | | - } |
169 | 106 |
|
170 | 107 | // Dark mode via CSS class |
171 | 108 | (function() { |
|
0 commit comments