Skip to content

Commit

Permalink
🚸增加代码备注一键复制
Browse files Browse the repository at this point in the history
  • Loading branch information
mewhz committed Nov 8, 2021
1 parent 4552ab3 commit 92704bf
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 项目名:我要贴代码
## 项目名:贴代码

## 更新日志
>2021/10/28
Expand Down
137 changes: 83 additions & 54 deletions sql/code_mewhz_com.sql → sql/coe.mewhz.com.sql

Large diffs are not rendered by default.

24 changes: 20 additions & 4 deletions src/main/java/com/mewhz/paste/controller/CodeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

import java.util.List;

/**
* @author mewhz
*/
@Controller
public class CodeController {

Expand All @@ -20,17 +23,25 @@ public String code(@RequestParam String id) {
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>Title</title>\n" +
" <title>";
String htmlTitle ="</title>\n" +
" <link href=\"css/prism.css\" rel=\"stylesheet\">\n" +
" <link rel=\"stylesheet\" href=\"css/bootstrap.min.css\"/>\n" +
" <link href=\"https://cdn.bootcdn.net/ajax/libs/toastr.js/latest/css/toastr.min.css\" rel=\"stylesheet\">"+
"</head>\n" +
"<body>\n" +
" <pre>\n" +
" <code class=\"language-";
" <code id=\"code\" class=\"language-";
String type = null;
String text = null;
String remark = null;
String htmlLast = " </code>\n" +
" </pre>\n" +
" <script src=\"js/prism.js\"></script>\n";
"<button id=\"button\" class=\"btn btn-success\">一键复制</button>"+
" <script src=\"js/prism.js\"></script>\n"+
"<script src=\"js/jquery-3.6.0.min.js\"></script>\n" +
"<script src=\"js/code.js\"></script>\n" +
"<script src=\"https://cdn.bootcdn.net/ajax/libs/toastr.js/latest/js/toastr.min.js\"></script>";

String htmlEnd = "</body>\n" +
"</html>";
Expand All @@ -47,13 +58,17 @@ public String code(@RequestParam String id) {

type = (String) list.get(0).get("type");
text = (String) list.get(0).get("text");
remark = (String) list.get(0).get("remark");
if ("".equals(remark)){
remark = "贴代码";
}

text = text.replaceAll("<", "&lt;");
text = text.replaceAll(">", "&gt;");
text = text.replaceAll("\n", "<br>");
text = text.replaceAll(" ", "&nbsp;");

html.append(type).append("\">").append(text).append(htmlLast);
html.append(remark).append(htmlTitle).append(type).append("\">").append(text).append(htmlLast);
if ("cpp".equals(type)){
html.append(cHtml);
}
Expand All @@ -63,6 +78,7 @@ else if ("java".equals(type)){
else if ("python".equals(type)){
html.append(pythonHtml);
}
html.append(htmlEnd);

return html.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public String list(@RequestParam(value = "id", required = false) String id ){
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>Title</title>\n" +
" <title>贴代码</title>\n" +
" <link href=\"css/prism.css\" rel=\"stylesheet\">\n" +
"</head>\n" +
"<body>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
public class SubmitController {

@RequestMapping("/submit")
// @ResponseBody
@CrossOrigin
public String code(@RequestParam String text, @RequestParam String type, @RequestParam String identifying ,HttpServletRequest request){
public String code(@RequestParam String text, @RequestParam String type, @RequestParam String identifying ,@RequestParam String remark,HttpServletRequest request){
Date date = new Date();
String ip = IPUtils.getIpAddr(request);
String userAgent = request.getHeader("User-Agent");
Code code = new Code(text, type, date, ip, userAgent);
System.out.println(remark);
Code code = new Code(text, type, date, ip, userAgent, remark);
IdentifyingCode identifyingCode = new IdentifyingCode(identifying, date.getTime()+"");
System.out.println(code);
CodeSQL codeSql = new CodeSQL();
codeSql.insertCode(code);
if (!"".equals(identifying)){
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/com/mewhz/paste/model/Code.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,28 @@ public class Code {
private Date date;
private String ip;
private String userAgent;
private String remark;
public Code() {

}

public Code(int id, String text, String type, Date date, String ip, String userAgent) {
public Code(int id, String text, String type, Date date, String ip, String userAgent, String remark) {
this.id = id;
this.text = text;
this.type = type;
this.date = date;
this.ip = ip;
this.userAgent = userAgent;
this.remark = remark;
}

public Code(String text, String type, Date date, String ip, String userAgent) {
public Code(String text, String type, Date date, String ip, String userAgent, String remark) {
this.text = text;
this.type = type;
this.date = date;
this.ip = ip;
this.userAgent = userAgent;
this.remark = remark;
}

public int getId() {
Expand Down Expand Up @@ -77,4 +80,25 @@ public String getUserAgent() {
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}

public String getRemark() {
return remark;
}

public void setRemark(String remark) {
this.remark = remark;
}

@Override
public String toString() {
return "Code{" +
"id=" + id +
", text='" + text + '\'' +
", type='" + type + '\'' +
", date=" + date +
", ip='" + ip + '\'' +
", userAgent='" + userAgent + '\'' +
", remark='" + remark + '\'' +
'}';
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/mewhz/paste/utils/CodeSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void insertCode(Code code){
.set("date", code.getDate())
.set("ip", code.getIp())
.set("user_agent", code.getUserAgent())
.set("remark", code.getRemark())
);
} catch (SQLException e) {
e.printStackTrace();
Expand All @@ -48,6 +49,9 @@ public void insertIdentifyingCode(IdentifyingCode identifyingCode){
}
}

/**
* 根据 id 取出 code 数据
*/
public List<Entity> findCode(String timeId){
List<Entity> codes = null;
try {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/db.setting
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
url = jdbc:mysql://127.0.0.1:3306/code_mewhz_com?useSSL=false
url = jdbc:mysql://127.0.0.1:3306/code_mewhz_com?useSSL=false&characterEncoding=UTF-8

user = root
pass = root
24 changes: 18 additions & 6 deletions src/main/resources/static/code.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@
<meta charset="UTF-8">
<title>Title</title>
<link href="css/prism.css" rel="stylesheet">
<link rel="stylesheet" href="css/bootstrap.min.css"/>
<link href="https://cdn.bootcdn.net/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet">
</head>
<body>
<pre>
<code class="language-python">
print("Hello World")
<code class="language-cpp" id="code">#include &lt;iostream&gt;
using namespace std;
int main() {
int n;
cin >> n;
cout << n << endl;
return 0;
}
</code>
</pre>
<button id="button" class="btn btn-success">一键复制</button>
<script src="js/prism.js"></script>
<!-- <script src="js/prism-c.min.js"></script>-->
<!-- <script src="js/prism-cpp.min.js"></script>-->
<script src="js/prism-java.min.js"></script>
<script src="js/prism-python-min.js"></script>
<script src="js/prism-c.min.js"></script>
<script src="js/prism-cpp.min.js"></script>
<script src="js/jquery-3.6.0.min.js"></script>
<script src="js/code.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<!-- <script src="js/prism-java.min.js"></script>-->
<!-- <script src="js/prism-python.min.js"></script>-->
</body>
</html>
4 changes: 4 additions & 0 deletions src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ <h1>
<option value="python">python</option>
</select>
</label>
<label>
<input class="form-control" name="remark" type="text" placeholder="代码备注(非必填)"/>
</label>
</div>
<div class="controls">
<button class="btn btn-success" type="submit">提交</button>
Expand All @@ -43,5 +46,6 @@ <h1>
</div>
</div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions src/main/resources/static/js/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$(document).ready(()=>{
$("#button").click(()=>{
let text = $("#code").text();
navigator.clipboard.writeText(text);
toastr.success("复制成功,快去粘贴吧!");
});
});
4 changes: 4 additions & 0 deletions src/main/resources/static/js/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
$(document).ready(()=>{
$("#list").click(()=>{
let id = $("#identifying").val();
if (id === "" || id.length === 0){
swal("未输入标识码", "未输入标识码,请输入标识码重试!", "error");
throw SyntaxError();
}
$("#list").attr("href", "/list?id=" + id);
})
});
11 changes: 1 addition & 10 deletions src/test/java/com/mewhz/paste/Main.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
package com.mewhz.paste;

import cn.hutool.db.Entity;
import com.mewhz.paste.utils.CodeSQL;

import java.util.List;

public class Main {
public static void main(String[] args) {
CodeSQL codeSQL = new CodeSQL();
List<Entity> codes = codeSQL.findIdentifyingCode("6666");
for (Entity e:codes){
System.out.println(e.get("time_id"));
}
System.out.println("Hello World");
}
}

0 comments on commit 92704bf

Please sign in to comment.