diff options
author | Hester Gong <hestergong@gmail.com> | 2023-03-26 13:25:41 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-26 13:25:41 +0800 |
commit | ac64c8297444ade63a2a364c4afb7e6c1de5a75f (patch) | |
tree | 1d5de1a378638660ebc6de9192955d5325c318a3 /web_src/js/features | |
parent | 2d2b4bdf62104bf11b1cef1e460633378af5a1aa (diff) | |
download | gitea-ac64c8297444ade63a2a364c4afb7e6c1de5a75f.tar.gz gitea-ac64c8297444ade63a2a364c4afb7e6c1de5a75f.zip |
Allow new file and edit file preview if it has editable extension (#23624)
Close #23579
Inspired by
[idea](https://github.com/go-gitea/gitea/issues/23579#issuecomment-1475429247)
from @brechtvl
In this PR, the behavior is when extension switches from writatble to
not, preview will hide, and vice versa.
demo:
https://user-images.githubusercontent.com/17645053/226786119-d20063da-8763-41ce-9b00-ae34929120e1.mov
---------
Co-authored-by: silverwind <me@silverwind.io>
Diffstat (limited to 'web_src/js/features')
-rw-r--r-- | web_src/js/features/codeeditor.js | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/web_src/js/features/codeeditor.js b/web_src/js/features/codeeditor.js index 4f5ea317b4..40bc6d618f 100644 --- a/web_src/js/features/codeeditor.js +++ b/web_src/js/features/codeeditor.js @@ -1,4 +1,5 @@ import {basename, extname, isObject, isDarkTheme} from '../utils.js'; +import {debounce} from 'throttle-debounce'; const languagesByFilename = {}; const languagesByExt = {}; @@ -130,23 +131,33 @@ function getFileBasedOptions(filename, lineWrapExts) { }; } +function togglePreviewDisplay(previewable) { + const previewTab = document.querySelector('a[data-tab="preview"]'); + if (!previewTab) return; + + if (previewable) { + const newUrl = (previewTab.getAttribute('data-url') || '').replace(/(.*)\/.*/i, `$1/markup`); + previewTab.setAttribute('data-url', newUrl); + previewTab.style.display = ''; + } else { + previewTab.style.display = 'none'; + // If the "preview" tab was active, user changes the filename to a non-previewable one, + // then the "preview" tab becomes inactive (hidden), so the "write" tab should become active + if (previewTab.classList.contains('active')) { + const writeTab = document.querySelector('a[data-tab="write"]'); + writeTab.click(); + } + } +} + export async function createCodeEditor(textarea, filenameInput) { const filename = basename(filenameInput.value); - const previewLink = document.querySelector('a[data-tab=preview]'); - const previewableExts = (textarea.getAttribute('data-previewable-extensions') || '').split(','); + const previewableExts = new Set((textarea.getAttribute('data-previewable-extensions') || '').split(',')); const lineWrapExts = (textarea.getAttribute('data-line-wrap-extensions') || '').split(','); - const previewable = previewableExts.includes(extname(filename)); + const previewable = previewableExts.has(extname(filename)); const editorConfig = getEditorconfig(filenameInput); - if (previewLink) { - if (previewable) { - const newUrl = (previewLink.getAttribute('data-url') || '').replace(/(.*)\/.*/i, `$1/markup`); - previewLink.setAttribute('data-url', newUrl); - previewLink.style.display = ''; - } else { - previewLink.style.display = 'none'; - } - } + togglePreviewDisplay(previewable); const {monaco, editor} = await createMonaco(textarea, filename, { ...baseOptions, @@ -154,10 +165,12 @@ export async function createCodeEditor(textarea, filenameInput) { ...getEditorConfigOptions(editorConfig), }); - filenameInput.addEventListener('keyup', () => { + filenameInput.addEventListener('input', debounce(500, () => { const filename = filenameInput.value; + const previewable = previewableExts.has(extname(filename)); + togglePreviewDisplay(previewable); updateEditor(monaco, editor, filename, lineWrapExts); - }); + })); return editor; } |