Skip to content

Commit b9a0e33

Browse files
author
AJ ONeal
authored
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
1 parent 4e68d6f commit b9a0e33

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

docs/content/doc/developers/integrations.en-us.md

+19
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,22 @@ If you are looking for [CI/CD](https://gitea.com/gitea/awesome-gitea#user-conten
2424
an [SDK](https://gitea.com/gitea/awesome-gitea#user-content-sdk),
2525
or even some extra [themes](https://gitea.com/gitea/awesome-gitea#user-content-themes),
2626
you can find them listed in the [awesome-gitea](https://gitea.com/gitea/awesome-gitea) repository!
27+
28+
## Pre-Fill New File name and contents
29+
30+
If you'd like to open a new file with a given name and contents,
31+
you can do so with query parameters:
32+
33+
```txt
34+
GET /{{org}}/{{repo}}/_new/{{filepath}}
35+
?filename={{filename}}
36+
&value={{content}}
37+
```
38+
39+
For example:
40+
41+
```txt
42+
GET https://git.example.com/johndoe/bliss/_new/articles/
43+
?filename=hello-world.md
44+
&value=Hello%2C%20World!
45+
```

routers/web/repo/editor.go

+7-2
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

+9-1
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)