mycopy.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Dynamic Clipboard Tabs</title>
  6. <script src="https://map.tianyunperfect.cn/common-page.js"></script>
  7. <script src="js/clipboard.min.js"></script>
  8. <script src="js/vue.min.js"></script>
  9. <script src="elementUI/index.js"></script>
  10. <link rel="stylesheet" href="elementUI/index.css"/>
  11. <style>
  12. .tab-container {
  13. max-width: 1600px;
  14. margin: 0 auto;
  15. padding: 20px;
  16. }
  17. .tab-content {
  18. margin-top: 20px;
  19. }
  20. .content-item {
  21. margin: 10px;
  22. /*padding: 15px;*/
  23. border: 1px solid #ebeef5;
  24. border-radius: 4px;
  25. transition: all .3s;
  26. cursor: pointer;
  27. display: inline-block;
  28. width: 300px;
  29. box-sizing: border-box;
  30. }
  31. .content-item:hover {
  32. background-color: #f5f7fa;
  33. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
  34. }
  35. .el-tabs__item.is-active {
  36. color: #409EFF !important;
  37. }
  38. .el-tabs__active-bar {
  39. background-color: #409EFF;
  40. }
  41. .action-buttons {
  42. text-align: right;
  43. }
  44. .item-card {
  45. width: 100%;
  46. border: none;
  47. box-shadow: none;
  48. }
  49. .item-content {
  50. display: flex;
  51. justify-content: space-between;
  52. align-items: center;
  53. }
  54. .item-2 {
  55. /* 禁止换行 - */
  56. white-space: nowrap;
  57. /* 超出部分隐藏 */
  58. overflow: hidden;
  59. /* 超出部分显示省略号 */
  60. text-overflow: ellipsis;
  61. }
  62. .copy-btn {
  63. color: #409EFF;
  64. }
  65. .copy-btn:hover {
  66. color: #206BC4;
  67. }
  68. .content-item {
  69. margin-bottom: 15px;
  70. }
  71. @media (max-width: 768px) {
  72. .content-item {
  73. width: calc(50% - 20px);
  74. }
  75. }
  76. @media (max-width: 480px) {
  77. .content-item {
  78. width: 100%;
  79. }
  80. }
  81. </style>
  82. <script src="js/Sortable.min.js"></script>
  83. <script src="js/vuedraggable.umd.min.js"></script>
  84. <script src="js/axios.min.js"></script>
  85. </head>
  86. <body>
  87. <div id="app" class="tab-container">
  88. <!-- 标签导航 -->
  89. <el-tabs v-model="activeTab" @tab-click="handleTabClick" type="card">
  90. <el-tab-pane
  91. v-for="(content, tabName) in myObj"
  92. :key="tabName"
  93. :label="tabName"
  94. :name="tabName">
  95. <!-- 标签页内容 -->
  96. <div class="tab-content">
  97. <!-- 内容项列表 -->
  98. <draggable
  99. :disabled="readOnly"
  100. v-model="myObj[tabName]"
  101. tag="div"
  102. @change="onDragChange(tabName)"
  103. handle=".drag-handle"
  104. class="content-list">
  105. <div
  106. v-for="(item, index) in myObj[tabName]"
  107. :key="item.key"
  108. class="content-item"
  109. @click.stop="copyToClipboard(item.value)"
  110. >
  111. <el-card shadow="hover" class="item-card">
  112. <div slot="header" class="item-content">
  113. <span>{{ item.key }}</span>
  114. <div class="action-buttons">
  115. <el-button
  116. type="text"
  117. class="drag-handle">
  118. <i class="el-icon-rank"></i>
  119. </el-button>
  120. <el-button
  121. type="text"
  122. @click.stop="editContentItem(tabName, item.key, item.value)">
  123. <i class="el-icon-edit"></i>
  124. </el-button>
  125. <el-button
  126. type="text"
  127. @click.stop="deleteContentItem(tabName, item.key)">
  128. <i class="el-icon-delete"></i>
  129. </el-button>
  130. </div>
  131. </div>
  132. <div class="item-2">
  133. <!-- 内容第一行,不能换行,用css控制,多余的隐藏-->
  134. <span>{{ item.value }}</span>
  135. </div>
  136. </el-card>
  137. </div>
  138. </draggable>
  139. <div v-if="!readOnly">
  140. <!-- 添加内容项 -->
  141. <div class="add-item">
  142. <el-input
  143. v-model="newItemKey[activeTab]"
  144. placeholder="键名"
  145. style="width: 200px; margin-right: 10px;"></el-input>
  146. <el-input
  147. v-model="newItemValue[activeTab]"
  148. placeholder="键值"
  149. style="width: 200px; margin-right: 10px;"></el-input>
  150. <el-button
  151. type="primary"
  152. @click="addContentItem(activeTab)"
  153. :disabled="!newItemKey[activeTab] || !newItemValue[activeTab]">
  154. 添加内容项
  155. </el-button>
  156. </div>
  157. <br>
  158. <!-- 删除当前标签页 -->
  159. <el-button
  160. type="danger"
  161. size="small"
  162. @click="deleteTab(activeTab)">
  163. 删除当前标签页
  164. </el-button>
  165. <!-- 修改当前标签名-->
  166. <el-button
  167. type="primary"
  168. size="small"
  169. @click="editTabName(activeTab)">
  170. 修改当前标签名
  171. </el-button>
  172. </div>
  173. </div>
  174. </el-tab-pane>
  175. <br>
  176. <!-- 添加新标签页 -->
  177. <div class="add-tab" v-if="!readOnly">
  178. <el-input
  179. v-model="newTabName"
  180. placeholder="新标签页名称"
  181. style="width: 200px; margin-right: 10px;"></el-input>
  182. <el-button
  183. type="primary"
  184. @click="addTab"
  185. :disabled="!newTabName || myObj[newTabName]">
  186. 添加标签页
  187. </el-button>
  188. </div>
  189. </el-tabs>
  190. </div>
  191. <script>
  192. // let all_data = {};
  193. let pageManager = new PageManager();
  194. pageManager.type = 'copy-page';
  195. pageManager.setNameCallBack(() => {
  196. let title = localStorage.getItem("title");
  197. document.title = title + " - 复制";
  198. _this.all_data.title = title;
  199. })
  200. let _this = new Vue({
  201. el: '#app',
  202. data: {
  203. all_data: {},
  204. activeTab: '',
  205. myObj: {
  206. "tab1": [
  207. {key: "ab1", value: "value1"},
  208. {key: "ab2", value: "value2"},
  209. {key: "ab3", value: "value3"}
  210. ],
  211. "tab2": [
  212. {key: "ab1", value: "value4"},
  213. {key: "ab2", value: "value5"}
  214. ]
  215. },
  216. newTabName: '',
  217. newItemKey: {},
  218. newItemValue: {},
  219. editingItem: null,
  220. editingKey: '',
  221. readOnly: false,
  222. },
  223. mounted() {
  224. pageManager.init().then(res => {
  225. this.all_data = res;
  226. console.log(res)
  227. if (res && res.content) {
  228. // 初始化数据
  229. let showContent = res.content;
  230. if (pageManager.isBackup) {
  231. // 如果是备份
  232. showContent = res.content_back;
  233. }
  234. this.myObj = JSON.parse(showContent);
  235. // 设置activeTab
  236. this.activeTab = localStorage.getItem("tab") || Object.keys(this.myObj)[0] || '';
  237. }
  238. if (pageManager.isReadOnly()) {
  239. // 备份页面或分享页面,设置为只读
  240. this.readOnly = true;
  241. }
  242. });
  243. },
  244. methods: {
  245. updateData() {
  246. this.all_data.content = JSON.stringify(this.myObj);
  247. pageManager.updateData(this.all_data);
  248. },
  249. editTabName(activeTab) {
  250. // 先获取当前标签名,弹框提醒用户更改
  251. this.$prompt('请输入新的标签名', '修改标签名', {
  252. confirmButtonText: '确定',
  253. cancelButtonText: '取消',
  254. closeOnClickModal: false,
  255. inputValue: activeTab
  256. })
  257. .then(({value}) => {
  258. // 确认更改后,更新标签名, 让vue监听到,使用 this.$set 方法
  259. this.$set(this.myObj, value, this.myObj[activeTab]);
  260. delete this.myObj[activeTab];
  261. this.activeTab = value;
  262. _this.updateData();
  263. })
  264. },
  265. deleteTab(activeTab) {
  266. if (this.myObj[activeTab]) {
  267. this.$confirm('确定删除该标签页?', '警告', {
  268. confirmButtonText: '确定',
  269. cancelButtonText: '取消',
  270. type: 'warning'
  271. }).then(() => {
  272. delete this.myObj[activeTab];
  273. this.activeTab = Object.keys(this.myObj)[0] || '';
  274. _this.updateData();
  275. }).catch(() => {
  276. });
  277. }
  278. },
  279. handleTabClick(tab) {
  280. this.activeTab = tab.name;
  281. // 保存到本地 localStorage
  282. localStorage.setItem("tab", this.activeTab);
  283. },
  284. // 添加标签页
  285. addTab() {
  286. if (!this.newTabName || this.myObj[this.newTabName]) return;
  287. this.myObj[this.newTabName] = [];
  288. this.activeTab = this.newTabName;
  289. this.newTabName = '';
  290. _this.updateData();
  291. },
  292. // 删除标签页(需确认)
  293. deleteTab(tabName) {
  294. this.$confirm('确定删除该标签页?', '警告', {
  295. confirmButtonText: '确定',
  296. cancelButtonText: '取消',
  297. type: 'warning'
  298. }).then(() => {
  299. delete this.myObj[tabName];
  300. this.activeTab = Object.keys(this.myObj)[0] || '';
  301. _this.updateData();
  302. }).catch(() => {
  303. });
  304. },
  305. // 添加内容项
  306. addContentItem(tabName) {
  307. const key = this.newItemKey[tabName];
  308. const value = this.newItemValue[tabName];
  309. if (!key || !value) return;
  310. this.myObj[tabName].push({key, value});
  311. this.newItemKey[tabName] = '';
  312. this.newItemValue[tabName] = '';
  313. _this.updateData();
  314. },
  315. // 删除内容项
  316. deleteContentItem(tabName, key) {
  317. this.$confirm('确定删除该内容项?', '警告', {
  318. confirmButtonText: '确定',
  319. cancelButtonText: '取消',
  320. type: 'warning'
  321. }).then(() => {
  322. this.myObj[tabName] = this.myObj[tabName].filter(item => item.key !== key);
  323. this.$message({message: '删除成功', type: 'success'});
  324. _this.updateData();
  325. }).catch(() => {
  326. });
  327. },
  328. editContentItem(tabName, key, value) {
  329. this.editingItem = {tabName, key, value};
  330. this.editingKey = key;
  331. // 保存原始值用于回滚
  332. const originalKey = key;
  333. const originalValue = value;
  334. // 第一步:修改Key
  335. this.$prompt('修改Key', '编辑内容', {
  336. confirmButtonText: '保存',
  337. cancelButtonText: '取消',
  338. inputPlaceholder: '输入新Key',
  339. closeOnClickModal: false,
  340. inputValue: key
  341. }).then(({value: newKey}) => {
  342. // 第二步:修改Value
  343. return this.$prompt('修改值', '编辑内容', {
  344. confirmButtonText: '保存',
  345. cancelButtonText: '取消',
  346. inputPlaceholder: '输入新值',
  347. closeOnClickModal: false,
  348. inputValue: value
  349. }).then(({value: newValue}) => {
  350. // 更新数据
  351. const items = this.myObj[tabName];
  352. const itemIndex = items.findIndex(item => item.key === originalKey);
  353. if (itemIndex !== -1) {
  354. items[itemIndex].key = newKey;
  355. items[itemIndex].value = newValue;
  356. }
  357. this.updateData();
  358. this.editingItem = null;
  359. });
  360. }).catch(() => {
  361. // 如果任一步骤取消,重置状态
  362. this.editingItem = null;
  363. });
  364. },
  365. // 复制到剪贴板
  366. copyToClipboard(text) {
  367. const clipboard = new ClipboardJS('.dummy', {
  368. text: () => text
  369. });
  370. clipboard.on('success', () => {
  371. this.$message({message: '复制成功', type: 'success'});
  372. clipboard.destroy();
  373. });
  374. clipboard.on('error', () => {
  375. this.$message({message: '复制失败', type: 'error'});
  376. clipboard.destroy();
  377. });
  378. const dummy = document.createElement('button');
  379. dummy.className = 'dummy';
  380. document.body.appendChild(dummy);
  381. dummy.click();
  382. document.body.removeChild(dummy);
  383. },
  384. // 拖拽排序事件
  385. onDragChange(tabName) {
  386. console.log(`拖拽后 ${tabName} 的顺序:`, this.myObj[tabName]);
  387. this.$message({message: '排序已更新', type: 'success'});
  388. _this.updateData();
  389. }
  390. }
  391. });
  392. </script>
  393. </body>
  394. </html>