Skip to content

Commit ac64c82

Browse files
HesterGsilverwind
andauthored
Allow new file and edit file preview if it has editable extension (#23624)
Close #23579 Inspired by [idea](#23579 (comment)) 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 <[email protected]>
1 parent 2d2b4bd commit ac64c82

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"pretty-ms": "8.0.0",
3838
"sortablejs": "1.15.0",
3939
"swagger-ui-dist": "4.18.1",
40+
"throttle-debounce": "5.0.0",
4041
"tippy.js": "6.3.7",
4142
"tributejs": "5.1.3",
4243
"uint8-to-base64": "0.2.0",

templates/repo/editor/edit.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
<div class="field">
3131
<div class="ui top attached tabular menu" data-write="write" data-preview="preview" data-diff="diff">
3232
<a class="active item" data-tab="write">{{svg "octicon-code"}} {{if .IsNewFile}}{{.locale.Tr "repo.editor.new_file"}}{{else}}{{.locale.Tr "repo.editor.edit_file"}}{{end}}</a>
33-
{{if not .IsNewFile}}
3433
<a class="item" data-tab="preview" data-url="{{.Repository.Link}}/markup" data-context="{{.RepoLink}}/src/{{.BranchNameSubURL}}" data-markup-mode="file">{{svg "octicon-eye"}} {{.locale.Tr "preview"}}</a>
34+
{{if not .IsNewFile}}
3535
<a class="item" data-tab="diff" data-url="{{.RepoLink}}/_preview/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}" data-context="{{.BranchLink}}">{{svg "octicon-diff"}} {{.locale.Tr "repo.editor.preview_changes"}}</a>
3636
{{end}}
3737
</div>

web_src/js/features/codeeditor.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {basename, extname, isObject, isDarkTheme} from '../utils.js';
2+
import {debounce} from 'throttle-debounce';
23

34
const languagesByFilename = {};
45
const languagesByExt = {};
@@ -130,34 +131,46 @@ function getFileBasedOptions(filename, lineWrapExts) {
130131
};
131132
}
132133

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+
133153
export async function createCodeEditor(textarea, filenameInput) {
134154
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(','));
137156
const lineWrapExts = (textarea.getAttribute('data-line-wrap-extensions') || '').split(',');
138-
const previewable = previewableExts.includes(extname(filename));
157+
const previewable = previewableExts.has(extname(filename));
139158
const editorConfig = getEditorconfig(filenameInput);
140159

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);
150161

151162
const {monaco, editor} = await createMonaco(textarea, filename, {
152163
...baseOptions,
153164
...getFileBasedOptions(filenameInput.value, lineWrapExts),
154165
...getEditorConfigOptions(editorConfig),
155166
});
156167

157-
filenameInput.addEventListener('keyup', () => {
168+
filenameInput.addEventListener('input', debounce(500, () => {
158169
const filename = filenameInput.value;
170+
const previewable = previewableExts.has(extname(filename));
171+
togglePreviewDisplay(previewable);
159172
updateEditor(monaco, editor, filename, lineWrapExts);
160-
});
173+
}));
161174

162175
return editor;
163176
}

0 commit comments

Comments
 (0)