Skip to content

Commit

Permalink
feature: add (GitHub-style) querystrings for pre-filling new file con…
Browse files Browse the repository at this point in the history
…tent
  • Loading branch information
AJ ONeal committed Jul 28, 2021
1 parent 3705168 commit 735b7dd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 7 additions & 2 deletions routers/web/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ func editFile(ctx *context.Context, isNewFile bool) {
return
}

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

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

ctx.Data["TreeNames"] = treeNames
Expand Down
10 changes: 9 additions & 1 deletion web_src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ async function initEditor() {
const $editArea = $('.repository.editor textarea#edit_area');
if (!$editArea.length) return;

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

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

// Update the editor from query params, if available,
// only after the dirtyFileClass initialization
const params = new URLSearchParams(window.location.search);
const value = params.get('value');
if (value) {
editor.setValue(value);
}

$commitButton.on('click', (event) => {
// A modal which asks if an empty file should be committed
if ($editArea.val().length === 0) {
Expand Down

0 comments on commit 735b7dd

Please sign in to comment.