Pre-fill suggested New File 'name' and 'content' with Query Params (#16556)

* feature: add (GitHub-style) querystrings for pre-filling new file content

* docs: add query parameters for new files
This commit is contained in:
AJ ONeal 2021-07-28 21:39:46 -06:00 committed by GitHub
parent 4e68d6f41d
commit b9a0e33238
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 3 deletions

View File

@ -24,3 +24,22 @@ If you are looking for [CI/CD](https://gitea.com/gitea/awesome-gitea#user-conten
an [SDK](https://gitea.com/gitea/awesome-gitea#user-content-sdk), an [SDK](https://gitea.com/gitea/awesome-gitea#user-content-sdk),
or even some extra [themes](https://gitea.com/gitea/awesome-gitea#user-content-themes), or even some extra [themes](https://gitea.com/gitea/awesome-gitea#user-content-themes),
you can find them listed in the [awesome-gitea](https://gitea.com/gitea/awesome-gitea) repository! you can find them listed in the [awesome-gitea](https://gitea.com/gitea/awesome-gitea) repository!
## Pre-Fill New File name and contents
If you'd like to open a new file with a given name and contents,
you can do so with query parameters:
```txt
GET /{{org}}/{{repo}}/_new/{{filepath}}
?filename={{filename}}
&value={{content}}
```
For example:
```txt
GET https://git.example.com/johndoe/bliss/_new/articles/
?filename=hello-world.md
&value=Hello%2C%20World!
```

View File

@ -81,7 +81,11 @@ func editFile(ctx *context.Context, isNewFile bool) {
return 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 { if !isNewFile {
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath) entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
@ -136,7 +140,8 @@ func editFile(ctx *context.Context, isNewFile bool) {
ctx.Data["FileContent"] = content ctx.Data["FileContent"] = content
} }
} else { } 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 ctx.Data["TreeNames"] = treeNames

View File

@ -1825,7 +1825,7 @@ async function initEditor() {
const $editArea = $('.repository.editor textarea#edit_area'); const $editArea = $('.repository.editor textarea#edit_area');
if (!$editArea.length) return; 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 // Using events from https://github.com/codedance/jquery.AreYouSure#advanced-usage
// to enable or disable the commit button // to enable or disable the commit button
@ -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) => { $commitButton.on('click', (event) => {
// A modal which asks if an empty file should be committed // A modal which asks if an empty file should be committed
if ($editArea.val().length === 0) { if ($editArea.val().length === 0) {