Skip to content

Commit 8846ca3

Browse files
committed
apply changes
1 parent 6d301b9 commit 8846ca3

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

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: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,35 @@ function getFileBasedOptions(filename, lineWrapExts) {
131131
};
132132
}
133133

134+
function togglePreviewDisplay(previewable) {
135+
const previewTab = document.querySelector('a[data-tab=preview]');
136+
if (!previewTab) {
137+
return;
138+
}
139+
if (previewable) {
140+
const newUrl = (previewTab.getAttribute('data-url') || '').replace(/(.*)\/.*/i, `$1/markup`);
141+
previewTab.setAttribute('data-url', newUrl);
142+
previewTab.style.display = '';
143+
} else {
144+
previewTab.style.display = 'none';
145+
// If the "preview" tab was active, user changes the filename to a non-previewable one,
146+
// then the "preview" tab becomes inactive (hidden), so the "write" tab should become active
147+
if (previewTab.classList.contains('active')) {
148+
const writeTab = document.querySelector('a[data-tab=write]');
149+
writeTab.click();
150+
}
151+
}
152+
}
153+
134154
export async function createCodeEditor(textarea, filenameInput) {
135155
const filename = basename(filenameInput.value);
136-
const previewLink = document.querySelector('a[data-tab=preview]');
137156
const previewableExts = (textarea.getAttribute('data-previewable-extensions') || '').split(',');
157+
const previewableExtsSet = new Set(previewableExts);
138158
const lineWrapExts = (textarea.getAttribute('data-line-wrap-extensions') || '').split(',');
139-
const previewable = previewableExts.includes(extname(filename));
159+
const previewable = previewableExtsSet.has(extname(filename));
140160
const editorConfig = getEditorconfig(filenameInput);
141161

142-
if (previewLink) {
143-
if (previewable) {
144-
const newUrl = (previewLink.getAttribute('data-url') || '').replace(/(.*)\/.*/i, `$1/markup`);
145-
previewLink.setAttribute('data-url', newUrl);
146-
previewLink.style.display = '';
147-
} else {
148-
previewLink.style.display = 'none';
149-
}
150-
}
162+
togglePreviewDisplay(previewable);
151163

152164
const {monaco, editor} = await createMonaco(textarea, filename, {
153165
...baseOptions,
@@ -157,8 +169,8 @@ export async function createCodeEditor(textarea, filenameInput) {
157169

158170
const debounceInputHandler = debounce(500, () => {
159171
const filename = filenameInput.value;
160-
const isMarkdown = markdownExtsSet.has(extname(filename));
161-
togglePreviewDisplay(isMarkdown, hasMarkdown);
172+
const previewable = previewableExtsSet.has(extname(filename));
173+
togglePreviewDisplay(previewable);
162174
updateEditor(monaco, editor, filename, lineWrapExts);
163175
});
164176

0 commit comments

Comments
 (0)