Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-29 05:06:11 +01:00

DRY: Add existing ctx value to data-branch-path

This commit is contained in:
Nirmal Kumar R 2024-11-04 23:44:17 +00:00
parent 4d73c51a26
commit a0d1f75f74
3 changed files with 4 additions and 12 deletions

View file

@ -211,6 +211,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
ctx.Data["TreeNames"] = treeNames
ctx.Data["TreePaths"] = treePaths
ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
ctx.Data["BranchPath"] = ctx.Repo.BranchNameSubURL()
ctx.Data["commit_summary"] = ""
ctx.Data["commit_message"] = ""
if canCommit {

View file

@ -28,7 +28,7 @@
<div class="field">
<div class="ui top attached tabular menu" data-write="write" data-preview="preview" data-diff="diff">
<a class="active item" data-tab="write">{{svg "octicon-code"}} {{if .IsNewFile}}{{ctx.Locale.Tr "repo.editor.new_file"}}{{else}}{{ctx.Locale.Tr "repo.editor.edit_file"}}{{end}}</a>
<a class="item" data-tab="preview" data-url="{{.Repository.Link}}/markup" data-context="{{.RepoLink}}/src/{{.BranchNameSubURL}}" data-markup-mode="file">{{svg "octicon-eye"}} {{ctx.Locale.Tr "preview"}}</a>
<a class="item" data-tab="preview" data-url="{{.Repository.Link}}/markup" data-context="{{.RepoLink}}/src/{{.BranchNameSubURL}}" data-branch-path="{{.BranchNameSubURL}}" data-relative-path="{{.Repository.Link}}" data-markup-mode="file">{{svg "octicon-eye"}} {{ctx.Locale.Tr "preview"}}</a>
{{if not .IsNewFile}}
<a class="item" data-tab="diff" hx-params="context,content" hx-vals='{"context":"{{.BranchLink}}"}' hx-include="#edit_area" hx-swap="innerHTML" hx-target=".tab[data-tab='diff']" hx-indicator=".tab[data-tab='diff']" hx-post="{{.RepoLink}}/_preview/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">{{svg "octicon-diff"}} {{ctx.Locale.Tr "repo.editor.preview_changes"}}</a>
{{end}}

View file

@ -23,20 +23,11 @@ function initEditPreviewTab($form) {
}
context = context.substring(0, context.lastIndexOf('/'));
// fetch branch path, Eg: `/branch/main`
const pathSegments = context.split("/").filter(segment => segment !== "");
const last2Seg = pathSegments.slice(-2);
const branchPath = "/" + last2Seg.join("/")
// fetch relative path to the current repository as `/<username>/<repository>`
// Eg: `/jon-doe/repo1`
const relativePath = context.split("/src"+branchPath)[0]
const formData = new FormData();
formData.append('mode', mode);
formData.append('context', context);
formData.append('branch_path', branchPath)
formData.append('relative_path', relativePath)
formData.append('branch_path', $this.data('branch-path'))
formData.append('relative_path', $this.data('relative-path'))
formData.append(
'text',
$form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val(),