Skip to content

Commit 735b7dd

Browse files
author
AJ ONeal
committed
feature: add (GitHub-style) querystrings for pre-filling new file content
1 parent 3705168 commit 735b7dd

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

routers/web/repo/editor.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ func editFile(ctx *context.Context, isNewFile bool) {
8181
return
8282
}
8383

84-
treeNames, treePaths := getParentTreeFields(ctx.Repo.TreePath)
84+
// Check if the filename (and additional path) is specified in the querystring
85+
// (filename is a misnomer, but kept for compatibility with Github)
86+
filePath, fileName := path.Split(ctx.Req.URL.Query().Get("filename"))
87+
filePath = strings.Trim(filePath, "/")
88+
treeNames, treePaths := getParentTreeFields(path.Join(ctx.Repo.TreePath, filePath))
8589

8690
if !isNewFile {
8791
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
@@ -136,7 +140,8 @@ func editFile(ctx *context.Context, isNewFile bool) {
136140
ctx.Data["FileContent"] = content
137141
}
138142
} else {
139-
treeNames = append(treeNames, "") // Append empty string to allow user name the new file.
143+
// Append filename from query, or empty string to allow user name the new file.
144+
treeNames = append(treeNames, fileName)
140145
}
141146

142147
ctx.Data["TreeNames"] = treeNames

web_src/js/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ async function initEditor() {
18251825
const $editArea = $('.repository.editor textarea#edit_area');
18261826
if (!$editArea.length) return;
18271827

1828-
await createCodeEditor($editArea[0], $editFilename[0], previewFileModes);
1828+
const editor = await createCodeEditor($editArea[0], $editFilename[0], previewFileModes);
18291829

18301830
// Using events from https://github.com/codedance/jquery.AreYouSure#advanced-usage
18311831
// to enable or disable the commit button
@@ -1849,6 +1849,14 @@ async function initEditor() {
18491849
}
18501850
});
18511851

1852+
// Update the editor from query params, if available,
1853+
// only after the dirtyFileClass initialization
1854+
const params = new URLSearchParams(window.location.search);
1855+
const value = params.get('value');
1856+
if (value) {
1857+
editor.setValue(value);
1858+
}
1859+
18521860
$commitButton.on('click', (event) => {
18531861
// A modal which asks if an empty file should be committed
18541862
if ($editArea.val().length === 0) {

0 commit comments

Comments
 (0)