|
1 | 1 | import {basename, extname, isObject, isDarkTheme} from '../utils.js';
|
| 2 | +import {debounce} from 'throttle-debounce'; |
2 | 3 |
|
3 | 4 | const languagesByFilename = {};
|
4 | 5 | const languagesByExt = {};
|
@@ -130,34 +131,46 @@ function getFileBasedOptions(filename, lineWrapExts) {
|
130 | 131 | };
|
131 | 132 | }
|
132 | 133 |
|
| 134 | +function togglePreviewDisplay(previewable) { |
| 135 | + const previewTab = document.querySelector('a[data-tab="preview"]'); |
| 136 | + if (!previewTab) return; |
| 137 | + |
| 138 | + if (previewable) { |
| 139 | + const newUrl = (previewTab.getAttribute('data-url') || '').replace(/(.*)\/.*/i, `$1/markup`); |
| 140 | + previewTab.setAttribute('data-url', newUrl); |
| 141 | + previewTab.style.display = ''; |
| 142 | + } else { |
| 143 | + previewTab.style.display = 'none'; |
| 144 | + // If the "preview" tab was active, user changes the filename to a non-previewable one, |
| 145 | + // then the "preview" tab becomes inactive (hidden), so the "write" tab should become active |
| 146 | + if (previewTab.classList.contains('active')) { |
| 147 | + const writeTab = document.querySelector('a[data-tab="write"]'); |
| 148 | + writeTab.click(); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
133 | 153 | export async function createCodeEditor(textarea, filenameInput) {
|
134 | 154 | const filename = basename(filenameInput.value);
|
135 |
| - const previewLink = document.querySelector('a[data-tab=preview]'); |
136 |
| - const previewableExts = (textarea.getAttribute('data-previewable-extensions') || '').split(','); |
| 155 | + const previewableExts = new Set((textarea.getAttribute('data-previewable-extensions') || '').split(',')); |
137 | 156 | const lineWrapExts = (textarea.getAttribute('data-line-wrap-extensions') || '').split(',');
|
138 |
| - const previewable = previewableExts.includes(extname(filename)); |
| 157 | + const previewable = previewableExts.has(extname(filename)); |
139 | 158 | const editorConfig = getEditorconfig(filenameInput);
|
140 | 159 |
|
141 |
| - if (previewLink) { |
142 |
| - if (previewable) { |
143 |
| - const newUrl = (previewLink.getAttribute('data-url') || '').replace(/(.*)\/.*/i, `$1/markup`); |
144 |
| - previewLink.setAttribute('data-url', newUrl); |
145 |
| - previewLink.style.display = ''; |
146 |
| - } else { |
147 |
| - previewLink.style.display = 'none'; |
148 |
| - } |
149 |
| - } |
| 160 | + togglePreviewDisplay(previewable); |
150 | 161 |
|
151 | 162 | const {monaco, editor} = await createMonaco(textarea, filename, {
|
152 | 163 | ...baseOptions,
|
153 | 164 | ...getFileBasedOptions(filenameInput.value, lineWrapExts),
|
154 | 165 | ...getEditorConfigOptions(editorConfig),
|
155 | 166 | });
|
156 | 167 |
|
157 |
| - filenameInput.addEventListener('keyup', () => { |
| 168 | + filenameInput.addEventListener('input', debounce(500, () => { |
158 | 169 | const filename = filenameInput.value;
|
| 170 | + const previewable = previewableExts.has(extname(filename)); |
| 171 | + togglePreviewDisplay(previewable); |
159 | 172 | updateEditor(monaco, editor, filename, lineWrapExts);
|
160 |
| - }); |
| 173 | + })); |
161 | 174 |
|
162 | 175 | return editor;
|
163 | 176 | }
|
|
0 commit comments