(refs #13)Implementing file editing on the repository viewer

This commit is contained in:
takezoe
2014-04-24 02:19:29 +09:00
parent b9cc46e5ef
commit dd688f48b7
4 changed files with 96 additions and 53 deletions

View File

@@ -93,33 +93,25 @@ trait RepositoryViewerControllerBase extends ControllerBase {
treeWalk.setRecursive(true) treeWalk.setRecursive(true)
getPathObjectId(path, treeWalk) getPathObjectId(path, treeWalk)
} map { objectId => } map { objectId =>
// if(raw){ // Viewer
// // Download val large = FileUtil.isLarge(git.getRepository.getObjectDatabase.open(objectId).getSize)
// defining(JGitUtil.getContentFromId(git, objectId, false).get){ bytes => val viewer = if(FileUtil.isImage(path)) "image" else if(large) "large" else "other"
// contentType = FileUtil.getContentType(path, bytes) val bytes = if(viewer == "other") JGitUtil.getContentFromId(git, objectId, false) else None
// bytes
// }
// } else {
// Viewer
val large = FileUtil.isLarge(git.getRepository.getObjectDatabase.open(objectId).getSize)
val viewer = if(FileUtil.isImage(path)) "image" else if(large) "large" else "other"
val bytes = if(viewer == "other") JGitUtil.getContentFromId(git, objectId, false) else None
val content = if(viewer == "other"){ val content = if(viewer == "other"){
if(bytes.isDefined && FileUtil.isText(bytes.get)){ if(bytes.isDefined && FileUtil.isText(bytes.get)){
// text // text
JGitUtil.ContentInfo("text", bytes.map(StringUtil.convertFromByteArray)) JGitUtil.ContentInfo("text", bytes.map(StringUtil.convertFromByteArray))
} else {
// binary
JGitUtil.ContentInfo("binary", None)
}
} else { } else {
// image or large // binary
JGitUtil.ContentInfo(viewer, None) JGitUtil.ContentInfo("binary", None)
} }
} else {
// image or large
JGitUtil.ContentInfo(viewer, None)
}
repo.html.editor(id, repository, path.split("/").toList, content, new JGitUtil.CommitInfo(revCommit)) repo.html.editor(id, repository, path.split("/").toList, content, new JGitUtil.CommitInfo(revCommit))
// }
} getOrElse NotFound } getOrElse NotFound
} }
}) })

View File

@@ -163,6 +163,45 @@ object helpers extends AvatarImageProvider with LinkConverter with RequestCache
*/ */
def isPast(date: Date): Boolean = System.currentTimeMillis > date.getTime def isPast(date: Date): Boolean = System.currentTimeMillis > date.getTime
/**
* Returns file type for AceEditor.
*/
def editorType(fileName: String): String = {
fileName.toLowerCase match {
case x if(x.endsWith(".bat")) => "batchfile"
case x if(x.endsWith(".java")) => "java"
case x if(x.endsWith(".scala")) => "scala"
case x if(x.endsWith(".js")) => "javascript"
case x if(x.endsWith(".css")) => "css"
case x if(x.endsWith(".md")) => "markdown"
case x if(x.endsWith(".html")) => "html"
case x if(x.endsWith(".xml")) => "xml"
case x if(x.endsWith(".c")) => "c_cpp"
case x if(x.endsWith(".cpp")) => "c_cpp"
case x if(x.endsWith(".coffee")) => "coffee"
case x if(x.endsWith(".ejs")) => "ejs"
case x if(x.endsWith(".hs")) => "haskell"
case x if(x.endsWith(".json")) => "json"
case x if(x.endsWith(".jsp")) => "jsp"
case x if(x.endsWith(".jsx")) => "jsx"
case x if(x.endsWith(".cl")) => "lisp"
case x if(x.endsWith(".clojure")) => "lisp"
case x if(x.endsWith(".lua")) => "lua"
case x if(x.endsWith(".php")) => "php"
case x if(x.endsWith(".py")) => "python"
case x if(x.endsWith(".rdoc")) => "rdoc"
case x if(x.endsWith(".rhtml")) => "rhtml"
case x if(x.endsWith(".ruby")) => "ruby"
case x if(x.endsWith(".sh")) => "sh"
case x if(x.endsWith(".sql")) => "sql"
case x if(x.endsWith(".tcl")) => "tcl"
case x if(x.endsWith(".vbs")) => "vbscript"
case x if(x.endsWith(".tcl")) => "tcl"
case x if(x.endsWith(".yml")) => "yaml"
case _ => "plain_text"
}
}
/** /**
* Implicit conversion to add mkHtml() to Seq[Html]. * Implicit conversion to add mkHtml() to Seq[Html].
*/ */

View File

@@ -29,6 +29,7 @@
<a href="@url(repository)/commit/@latestCommit.id" class="commit-message">@link(latestCommit.summary, repository)</a> <a href="@url(repository)/commit/@latestCommit.id" class="commit-message">@link(latestCommit.summary, repository)</a>
</div> </div>
<div class="btn-group pull-right"> <div class="btn-group pull-right">
<a class="btn btn-mini" href="@url(repository)/edit/@encodeRefName(branch)/@pathList.mkString("/")">Edit</a>
<a class="btn btn-mini" href="?raw=true">Raw</a> <a class="btn btn-mini" href="?raw=true">Raw</a>
<a class="btn btn-mini" href="@url(repository)/commits/@encodeRefName(branch)/@pathList.mkString("/")">History</a> <a class="btn btn-mini" href="@url(repository)/commits/@encodeRefName(branch)/@pathList.mkString("/")">History</a>
</div> </div>

View File

@@ -20,43 +20,54 @@
</div> </div>
<style type="text/css" media="screen"> <style type="text/css" media="screen">
#editor { #editor {
@*
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
*@
width: 100%; width: 100%;
height: 800px; height: 600px;
} }
</style> </style>
<table class="table table-bordered"> <form method="POST" action="@url(repository)/edit/@encodeRefName(branch)/@pathList.mkString("/")">
<tr> <table class="table table-bordered">
<th style="font-weight: normal;"> @*
<div class="pull-left"> <tr>
@avatar(latestCommit, 20) <th style="font-weight: normal;">
@user(latestCommit.committer, latestCommit.mailAddress, "username strong") <div class="pull-left">
<span class="muted">@datetime(latestCommit.time)</span> @avatar(latestCommit, 20)
<a href="@url(repository)/commit/@latestCommit.id" class="commit-message">@link(latestCommit.summary, repository)</a> @user(latestCommit.committer, latestCommit.mailAddress, "username strong")
<span class="muted">@datetime(latestCommit.time)</span>
<a href="@url(repository)/commit/@latestCommit.id" class="commit-message">@link(latestCommit.summary, repository)</a>
</div>
<div class="btn-group pull-right">
<a class="btn btn-mini" href="?raw=true">Raw</a>
<a class="btn btn-mini" href="@url(repository)/commits/@encodeRefName(branch)/@pathList.mkString("/")">History</a>
</div>
</th>
</tr>
*@
<tr>
<td>
<div id="editor">@content.content.get</div>
</td>
</tr>
</table>
<div class="issue-avatar-image">@avatar(loginAccount.get.userName, 48)</div>
<div class="box issue-comment-box">
<div class="box-content">
<div>
<strong>Commit changes</strong>
</div> </div>
<div class="btn-group pull-right"> <div>
<a class="btn btn-mini" href="?raw=true">Raw</a> <input type="text" name="message" style="width: 98%;" placeholder="Update @pathList.last"/>
<a class="btn btn-mini" href="@url(repository)/commits/@encodeRefName(branch)/@pathList.mkString("/")">History</a> </div>
</div> <div style="text-align: right;">
</th> <a href="@url(repository)/blob/@encodeRefName(branch)/@pathList.mkString("/")" class="btn btn-danger">Cancel</a>
</tr> <input type="submit" class="btn btn-success" value="Commit changes"/>
<tr> </div>
<td> </div>
<div id="editor">@content.content.get</div> </div>
</td> </form>
</tr>
</table>
} }
<script src="@assets/ace/ace.js" type="text/javascript" charset="utf-8"></script> <script src="@assets/ace/ace.js" type="text/javascript" charset="utf-8"></script>
<script> <script>
var editor = ace.edit("editor"); var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai"); editor.setTheme("ace/theme/monokai");
//editor.getSession().setMode("ace/mode/javascript"); editor.getSession().setMode("ace/mode/@editorType(pathList.last)");
editor.getSession().setMode("ace/mode/scala");
</script> </script>