-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from mewhz/dev
Dev
- Loading branch information
Showing
9 changed files
with
177 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,85 +2,49 @@ | |
|
||
|
||
import cn.hutool.db.Entity; | ||
import cn.hutool.json.JSONObject; | ||
import cn.hutool.json.JSONUtil; | ||
import com.mewhz.paste.utils.CodeSQL; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author mewhz | ||
*/ | ||
@Controller | ||
@RestController | ||
public class CodeController { | ||
|
||
@RequestMapping("/code") | ||
@ResponseBody | ||
public String code(@RequestParam String id) { | ||
String htmlFront = "<!DOCTYPE html>\n" + | ||
"<html lang=\"en\">\n" + | ||
"<head>\n" + | ||
" <meta charset=\"UTF-8\">\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 id=\"code\" class=\"language-"; | ||
public ModelAndView code(@RequestParam String id){ | ||
ModelAndView mav = new ModelAndView("/code.html"); | ||
return mav; | ||
} | ||
|
||
@RequestMapping("/selectCode") | ||
public JSONObject selectCode(@RequestParam String id){ | ||
|
||
JSONObject json = JSONUtil.createObj(); | ||
String type = null; | ||
String text = null; | ||
String remark = null; | ||
String htmlLast = " </code>\n" + | ||
" </pre>\n" + | ||
"<button id=\"button\" class=\"btn btn-success\" class=\"btn btn-success\" onclick=\"copy()\" data-clipboard-target=\"#code\" data-clipboard-action=\"copy\">一键复制</button>"+ | ||
" <script src=\"js/prism.js\"></script>\n"+ | ||
"<script src=\"js/jquery-3.6.0.min.js\"></script>\n" + | ||
"<script src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/clipboard.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>"; | ||
StringBuilder html = new StringBuilder(htmlFront); | ||
|
||
String cHtml = "<script src=\"js/prism-c.min.js\"></script>\n" + | ||
"<script src=\"js/prism-cpp.min.js\"></script>\n"; | ||
String javaHtml = "<script src=\"js/prism-java.min.js\"></script>\n"; | ||
String pythonHtml = "<script src=\"js/prism-python.min.js\"></script>\n"; | ||
|
||
|
||
CodeSQL codesql = new CodeSQL(); | ||
List<Entity> list = codesql.findCode(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("<", "<"); | ||
text = text.replaceAll(">", ">"); | ||
text = text.replaceAll("\n", "<br>"); | ||
text = text.replaceAll(" ", " "); | ||
|
||
html.append(remark).append(htmlTitle).append(type).append("\">").append(text).append(htmlLast); | ||
if ("cpp".equals(type)){ | ||
html.append(cHtml); | ||
} | ||
else if ("java".equals(type)){ | ||
html.append(javaHtml); | ||
} | ||
else if ("python".equals(type)){ | ||
html.append(pythonHtml); | ||
} | ||
html.append(htmlEnd); | ||
json.append("text", text); | ||
json.append("type", type); | ||
json.append("remark", remark); | ||
|
||
return html.toString(); | ||
return json; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,25 +9,14 @@ | |
</head> | ||
<body> | ||
<pre> | ||
<code class="language-cpp" id="code">#include <iostream> | ||
using namespace std; | ||
int main() { | ||
int n; | ||
cin >> n; | ||
cout << n << endl; | ||
return 0; | ||
} | ||
<code id="code" class="code"> | ||
</code> | ||
</pre> | ||
<button id="button" class="btn btn-success" onclick="copy()" data-clipboard-target="#code" data-clipboard-action="copy">一键复制</button> | ||
<button class="btn btn-success button" data-clipboard-target="#code" data-clipboard-action="copy">一键复制</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/jquery-3.6.0.min.js"></script> | ||
<script src="js/jquery-3.6.0.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/clipboard.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>--> | ||
<script src="https://cdn.bootcdn.net/ajax/libs/toastr.js/latest/js/toastr.min.js"></script> | ||
<script src="js/code.js" type="module"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,41 @@ | ||
// $(document).ready(()=>{ | ||
// $("#button").click(()=>{ | ||
// let text = $("#code").text(); | ||
// navigator.clipboard.writeText(text); | ||
// toastr.success("复制成功,快去粘贴吧!"); | ||
// }); | ||
// }); | ||
function copy(){ | ||
let clipboard = new ClipboardJS('#button'); | ||
clipboard.on('success', function(e) { | ||
e.clearSelection(); | ||
clipboard.destroy(); | ||
// 释放内存,如果不释放,下次总会多复制 | ||
toastr.success("复制成功,快去粘贴吧!"); | ||
}); | ||
import {copy, getQueryVariable} from './utils.js' | ||
|
||
$(document).ready(()=>{ | ||
|
||
clipboard.on('error', function(e) { | ||
toastr.error("一键复制失败,请手动复制!") | ||
$(".button").click((e)=>{ | ||
|
||
let id = $(e.target).attr("id"); | ||
// 单击事件复制 | ||
copy(id); | ||
}); | ||
} | ||
|
||
let id = getQueryVariable("id"); | ||
const codeId = $("#code"); | ||
const title = $("title"); | ||
const body = $("body"); | ||
$.ajax({ | ||
url: "/selectCode?id=" + id, | ||
success: (result)=>{ | ||
codeId.attr("class","language-" + result.type); | ||
codeId.text(result.text); | ||
title.text(result.remark); | ||
if (result.remark === "") { | ||
title.text("贴代码"); | ||
} | ||
console.log(result.type[0]); | ||
switch (result.type[0]){ | ||
case "java": | ||
body.append('<script src="js/prism-java.min.js"></script>'); | ||
break; | ||
case "cpp": | ||
body.append('<script src="js/prism-c.min.js"></script>'); | ||
body.append('<script src="js/prism-cpp.min.js"></script>'); | ||
break; | ||
case "python": | ||
body.append('<script src="js/prism-python.min.js"></script>'); | ||
} | ||
// 对代码重新渲染 | ||
Prism.highlightAll(); | ||
} | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {copy, getQueryVariable} from './utils.js' | ||
|
||
$(document).ready(()=>{ | ||
let all = $("#all"); | ||
let body = $("body"); | ||
let set = new Set(); | ||
let id = getQueryVariable("id"); | ||
|
||
$.ajax({ | ||
url: "/selectList?id=" + id, | ||
success: (result) => { | ||
console.log(result.type); | ||
let len = result.type.length; | ||
let type = result.type; | ||
let date = result.date; | ||
let time_id = result.time_id; | ||
let text = result.text; | ||
let remark = result.remark; | ||
for (let i = 0; i < len; i ++){ | ||
all.append("<details><summary>"+ date[i] +" " + time_id[i] + " " + remark[i] + "</summary>" + | ||
"<pre><code class='language-"+ type[i] +"'>"+ text[i] +"</code></pre></details>"); | ||
if (!set.has(type[i])){ | ||
switch (type[i]){ | ||
case "java": | ||
body.append('<script src="js/prism-java.min.js"></script>'); | ||
break; | ||
case "cpp": | ||
body.append('<script src="js/prism-c.min.js"></script>'); | ||
body.append('<script src="js/prism-cpp.min.js"></script>'); | ||
break; | ||
case "python": | ||
body.append('<script src="js/prism-python.min.js"></script>'); | ||
} | ||
set.add(type[i]); | ||
} | ||
} | ||
Prism.highlightAll(); | ||
} | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export function copy(id){ | ||
let clipboard = new ClipboardJS('.button'); | ||
clipboard.on('success', (e)=>{ | ||
e.clearSelection(); | ||
// 释放内存,否则会多复制 | ||
clipboard.destroy(); | ||
toastr.success("复制成功,快去站贴吧!"); | ||
}); | ||
|
||
clipboard.on('error', (e)=>{ | ||
toastr.error("一键复制失败,请手动复制!"); | ||
}) | ||
} | ||
export function getQueryVariable(variable){ | ||
let query = window.location.search.substring(1); | ||
let vars = query.split("&"); | ||
for (let i = 0; i < vars.length; i ++){ | ||
let pair = vars[i].split("="); | ||
if (pair[0] === variable) return pair[1]; | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.mewhz.paste; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
System.out.println("Hello World"); | ||
} | ||
} |