Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 50d25fa

Browse files
Merge pull request #52 from A91y/main
Added Text Copy Button
2 parents f0c7d3c + aa67a73 commit 50d25fa

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

src/paste/main.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070

7171
BASE_DIR: Path = Path(__file__).resolve().parent
7272

73-
templates: Jinja2Templates = Jinja2Templates(directory=str(Path(BASE_DIR, "templates")))
73+
templates: Jinja2Templates = Jinja2Templates(
74+
directory=str(Path(BASE_DIR, "templates")))
7475

7576

7677
@app.post("/file")
@@ -126,9 +127,12 @@ async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) ->
126127
try:
127128
lexer = get_lexer_by_name(file_extension, stripall=True)
128129
except ClassNotFound:
129-
lexer = get_lexer_by_name("text", stripall=True) # Default lexer
130-
formatter = HtmlFormatter(style="colorful", full=True, linenos="inline", cssclass="code")
130+
lexer = get_lexer_by_name(
131+
"text", stripall=True) # Default lexer
132+
formatter = HtmlFormatter(
133+
style="colorful", full=True, linenos="inline", cssclass="code")
131134
highlighted_code: str = highlight(content, lexer, formatter)
135+
# print(highlighted_code)
132136
custom_style = """
133137
.code pre span.linenos {
134138
color: #999;
@@ -166,17 +170,58 @@ async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) ->
166170
pre {
167171
font-family: 'Consolas','Monaco','Andale Mono','Ubuntu Mono','monospace;' !important;
168172
}
173+
.copy-button {
174+
position: fixed;
175+
top: 10px;
176+
right: 10px;
177+
padding: 10px;
178+
background-color: #4CAF50;
179+
color: #fff;
180+
cursor: pointer;
181+
border: none;
182+
border-radius: 5px;
183+
outline: none;
184+
}
185+
"""
186+
custom_script = """
187+
function copyAllText() {
188+
// Create a range object to select the entire document
189+
const range = document.createRange();
190+
range.selectNode(document.body);
191+
192+
// Create a selection object and add the range to it
193+
const selection = window.getSelection();
194+
selection.removeAllRanges();
195+
selection.addRange(range);
196+
197+
// Copy the selected text to the clipboard
198+
document.execCommand('copy');
199+
200+
// Clear the selection to avoid interfering with the user's selection
201+
selection.removeAllRanges();
202+
203+
// You can customize the copied message
204+
alert('All text copied to clipboard!');
205+
}
206+
169207
"""
170208
response_content: str = f"""
171209
<html>
172210
<head>
173-
<title>{uuid}</title>
211+
<title>{uuid} | paste.py 🐍</title>
212+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
174213
<style>{custom_style}</style>
175214
<style>{formatter.get_style_defs('.highlight')}</style>
176215
</head>
177216
<body>
217+
<div id="copyButton" class="copy-button" onclick="copyAllText()">
218+
<i class="fas fa-copy"></i>
219+
</div>
178220
{highlighted_code}
179221
</body>
222+
<script>
223+
{custom_script}
224+
</script>
180225
</html>
181226
"""
182227
return HTMLResponse(content=response_content)
@@ -199,9 +244,11 @@ async def delete_paste(uuid: str) -> PlainTextResponse:
199244
os.remove(path)
200245
return PlainTextResponse(f"File successfully deleted {uuid}")
201246
except FileNotFoundError:
202-
raise HTTPException(detail="File Not Found", status_code=status.HTTP_404_NOT_FOUND)
247+
raise HTTPException(detail="File Not Found",
248+
status_code=status.HTTP_404_NOT_FOUND)
203249
except Exception as e:
204-
raise HTTPException(detail=f"The exception is {e}", status_code=status.HTTP_409_CONFLICT)
250+
raise HTTPException(
251+
detail=f"The exception is {e}", status_code=status.HTTP_409_CONFLICT)
205252

206253

207254
@app.get("/web", response_class=HTMLResponse)

0 commit comments

Comments
 (0)