Skip to content

Commit f7923b1

Browse files
committed
feat: 加入复制代码按钮
1 parent b7dd68e commit f7923b1

6 files changed

Lines changed: 78 additions & 91 deletions

File tree

README-zh-cn.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
# Python Standard Libraries Cheatsheet
1+
# Python 标准库速查表
22

3-
Depend on Python v3.13.7
3+
依赖 Python v3.13.7
44

5-
All code snippets have been tested to ensure they work properly.
5+
所有代码片段都经过测试确保可以正常运行
66

7-
Fork me on [GitHub](https://github.com/pynickle/python-cheatsheet-redefined).
7+
[GitHub](https://github.com/pynickle/python-cheatsheet-redefined)上fork这个仓库吧
88

99
- [中文](README-zh-cn.md)
1010
- [English](README.md)
1111

12-
**Notes**:
12+
**注意**:
13+
- **这里的每个代码片段都可以独立运行 (一些需要这个仓库提供的文件)**
14+
- **在我的 [网站](https://enc.is-a.dev/python-cheatsheet-redefined) 上享受最佳阅读体验**
15+
- **你可以使用 readme_snatcher.py 来从仓库中下载 README.md (中英文,命令行前缀的有无皆可选择)**
1316

14-
- **Every code snippet here can run independently (some need the files provided by this repo)**
15-
- **You can use readme_snatcher.py to download README.md from the repository (Chinese or English, with or not with command line prefixes is up to you!)**
16-
17-
## Contents
17+
## 目录
1818

1919
**文本处理**: [``string``](#string), [``re``](#re), [``difflib``](#difflib),
2020
[``textwrap``](#textwrap), [``unicodedata``](#unicodedata), [``readline``](#readline)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Fork me on [GitHub](https://github.com/pynickle/python-cheatsheet-redefined).
1212
**Notes**:
1313

1414
- **Every code snippet here can run independently (some need the files provided by this repo)**
15+
- **Get the best reading experience on my [website](https://enc.is-a.dev/python-cheatsheet-redefined)**
1516
- **You can use readme_snatcher.py to download README.md from the repository (Chinese or English, with or not with command line prefixes is up to you!)**
1617

1718
## Contents

readme_sync/README_S.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

readme_sync/README_zh_cn_S.md

Lines changed: 0 additions & 16 deletions
This file was deleted.

readme_sync/sync_readme.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

web/src/main.ts

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,91 @@ function setupCodeBlocks(): void {
9191
// 设置pre元素的相对定位,以便按钮可以绝对定位
9292
preElement.style.position = 'relative';
9393

94-
// 创建复制按钮
94+
// 创建复制代码按钮
9595
const copyButton = document.createElement('button');
9696
copyButton.className = 'button is-small mt-2 mr-2 is-white';
9797

98-
const buttonIcon = document.createElement('span');
99-
copyButton.appendChild(buttonIcon)
100-
buttonIcon.innerHTML = '<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="128" height="128"><path d="M256 938.666667c-12.8 0-21.333333-4.266667-29.866667-12.8l-128-128c-17.066667-17.066667-17.066667-42.666667 0-59.733334l640-640c17.066667-17.066667 42.666667-17.066667 59.733334 0l128 128c17.066667 17.066667 17.066667 42.666667 0 59.733334l-640 640c-8.533333 8.533333-17.066667 12.8-29.866667 12.8z m-68.266667-170.666667L256 836.266667 836.266667 256 768 187.733333 187.733333 768z" fill="#000000"></path><path d="M768 426.666667c-12.8 0-21.333333-4.266667-29.866667-12.8l-128-128c-17.066667-17.066667-17.066667-42.666667 0-59.733334s42.666667-17.066667 59.733334 0l128 128c17.066667 17.066667 17.066667 42.666667 0 59.733334-8.533333 8.533333-17.066667 12.8-29.866667 12.8zM384 341.333333c-25.6 0-42.666667-17.066667-42.666667-42.666666s-17.066667-42.666667-42.666666-42.666667-42.666667-17.066667-42.666667-42.666667 17.066667-42.666667 42.666667-42.666666 42.666667-17.066667 42.666666-42.666667 17.066667-42.666667 42.666667-42.666667 42.666667 17.066667 42.666667 42.666667 17.066667 42.666667 42.666666 42.666667 42.666667 17.066667 42.666667 42.666666-17.066667 42.666667-42.666667 42.666667-42.666667 17.066667-42.666666 42.666667-17.066667 42.666667-42.666667 42.666666zM810.666667 768c-25.6 0-42.666667-17.066667-42.666667-42.666667s-17.066667-42.666667-42.666667-42.666666-42.666667-17.066667-42.666666-42.666667 17.066667-42.666667 42.666666-42.666667 42.666667-17.066667 42.666667-42.666666 17.066667-42.666667 42.666667-42.666667 42.666667 17.066667 42.666666 42.666667 17.066667 42.666667 42.666667 42.666666 42.666667 17.066667 42.666667 42.666667-17.066667 42.666667-42.666667 42.666667-42.666667 17.066667-42.666667 42.666666-17.066667 42.666667-42.666666 42.666667z" fill="#000000"></path></svg>';
101-
buttonIcon.className = 'icon is-small'
98+
const copyIcon = document.createElement('span');
99+
copyButton.appendChild(copyIcon);
100+
copyIcon.innerHTML = '<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" p-id="2301" width="128" height="128"><path d="M672 832 224 832c-52.928 0-96-43.072-96-96L128 160c0-52.928 43.072-96 96-96l448 0c52.928 0 96 43.072 96 96l0 576C768 788.928 724.928 832 672 832zM224 128C206.368 128 192 142.368 192 160l0 576c0 17.664 14.368 32 32 32l448 0c17.664 0 32-14.336 32-32L704 160c0-17.632-14.336-32-32-32L224 128zM800 960 320 960c-17.664 0-32-14.304-32-32s14.336-32 32-32l480 0c17.664 0 32-14.336 32-32L832 256c0-17.664 14.304-32 32-32s32 14.336 32 32l0 608C896 916.928 852.928 960 800 960zM544 320 288 320c-17.664 0-32-14.336-32-32s14.336-32 32-32l256 0c17.696 0 32 14.336 32 32S561.696 320 544 320zM608 480 288.032 480c-17.664 0-32-14.336-32-32s14.336-32 32-32L608 416c17.696 0 32 14.336 32 32S625.696 480 608 480zM608 640 288 640c-17.664 0-32-14.304-32-32s14.336-32 32-32l320 0c17.696 0 32 14.304 32 32S625.696 640 608 640z" fill="#272636"></path></svg>';
101+
copyIcon.className = 'icon is-small';
102102

103-
copyButton.title = 'Remove prefix';
103+
copyButton.title = 'Copy code';
104104
copyButton.style.position = 'absolute';
105105
copyButton.style.top = '17px';
106106
copyButton.style.right = '20px';
107107
copyButton.style.cursor = 'pointer';
108108

109109
// 添加点击事件
110110
copyButton.addEventListener('click', function() {
111+
// 获取当前代码块的内容并复制
112+
const codeToCopy = block.textContent || '';
113+
navigator.clipboard.writeText(codeToCopy).then(() => {
114+
// 创建浮动提示框
115+
const tooltip = document.createElement('div');
116+
tooltip.textContent = 'copied';
117+
tooltip.style.position = 'absolute';
118+
tooltip.style.top = '60px';
119+
tooltip.style.right = '30px'; // 复制按钮左侧
120+
tooltip.style.padding = '5px 10px';
121+
tooltip.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
122+
tooltip.style.color = 'white';
123+
tooltip.style.borderRadius = '4px';
124+
tooltip.style.fontSize = '12px';
125+
tooltip.style.zIndex = '1000';
126+
tooltip.style.opacity = '0';
127+
tooltip.style.transition = 'opacity 0.3s ease';
128+
tooltip.style.pointerEvents = 'none';
129+
tooltip.style.minWidth = '60px';
130+
tooltip.style.textAlign = 'center';
131+
tooltip.style.fontWeight = 'bold';
132+
133+
// 添加到pre元素
134+
preElement.appendChild(tooltip);
135+
136+
// 触发重排后显示提示框
137+
setTimeout(() => {
138+
tooltip.style.opacity = '1';
139+
}, 10);
140+
141+
// 2秒后移除提示框
142+
setTimeout(() => {
143+
tooltip.style.opacity = '0';
144+
setTimeout(() => {
145+
if (tooltip.parentNode) {
146+
tooltip.parentNode.removeChild(tooltip);
147+
}
148+
}, 300);
149+
}, 2000);
150+
}).catch(err => {
151+
console.error('Failed to copy text:', err);
152+
});
153+
});
154+
155+
// 创建处理前缀按钮
156+
const prefixButton = document.createElement('button');
157+
prefixButton.className = 'button is-small mt-2 mr-2 is-white';
158+
159+
const prefixIcon = document.createElement('span');
160+
prefixButton.appendChild(prefixIcon);
161+
prefixIcon.innerHTML = '<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path d="M256 938.666667c-12.8 0-21.333333-4.266667-29.866667-12.8l-128-128c-17.066667-17.066667-17.066667-42.666667 0-59.733334l640-640c17.066667-17.066667 42.666667-17.066667 59.733334 0l128 128c17.066667 17.066667 17.066667 42.666667 0 59.733334l-640 640c-8.533333 8.533333-17.066667 12.8-29.866667 12.8z m-68.266667-170.666667L256 836.266667 836.266667 256 768 187.733333 187.733333 768z" fill="#000000"></path><path d="M768 426.666667c-12.8 0-21.333333-4.266667-29.866667-12.8l-128-128c-17.066667-17.066667-17.066667-42.666667 0-59.733334s42.666667-17.066667 59.733334 0l128 128c17.066667 17.066667 17.066667 42.666667 0 59.733334-8.533333 8.533333-17.066667 12.8-29.866667 12.8zM384 341.333333c-25.6 0-42.666667-17.066667-42.666667-42.666666s-17.066667-42.666667-42.666666-42.666667-42.666667-17.066667-42.666667-42.666667 17.066667-42.666667 42.666667-42.666666 42.666667-17.066667 42.666666-42.666667 17.066667-42.666667 42.666667-42.666667 42.666667 17.066667 42.666667 42.666667 17.066667 42.666667 42.666666 42.666667 42.666667 17.066667 42.666667 42.666666-17.066667 42.666667-42.666667 42.666667-42.666667 17.066667-42.666666 42.666667-17.066667 42.666667-42.666667 42.666666zM810.666667 768c-25.6 0-42.666667-17.066667-42.666667-42.666667s-17.066667-42.666667-42.666667-42.666666-42.666667-17.066667-42.666666-42.666667 17.066667-42.666667 42.666666-42.666667 42.666667-17.066667 42.666667-42.666666 17.066667-42.666667 42.666667-42.666667 42.666667 17.066667 42.666666 42.666667 17.066667 42.666667 42.666667 42.666666 42.666667 17.066667 42.666667 42.666667-17.066667 42.666667-42.666667 42.666667-42.666667 17.066667-42.666667 42.666666-17.066667 42.666667-42.666666 42.666667z" fill="#000000"></path></svg>';
162+
prefixIcon.className = 'icon is-small';
163+
164+
prefixButton.title = 'Remove prefix';
165+
prefixButton.style.position = 'absolute';
166+
prefixButton.style.top = '17px';
167+
prefixButton.style.right = '60px'; // 移到复制按钮左侧
168+
prefixButton.style.cursor = 'pointer';
169+
170+
// 添加点击事件
171+
prefixButton.addEventListener('click', function() {
111172
// 切换当前代码块的前缀显示
112173
toggleCodePrefix(blockId);
113174
});
114175

115176
// 添加按钮到pre元素
116177
preElement.appendChild(copyButton);
178+
preElement.appendChild(prefixButton);
117179
}
118180
}
119181
});

0 commit comments

Comments
 (0)