mycopy.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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" ref="navTabs">
  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. <el-tooltip :content="item.value">
  134. <span>{{ item.value }}</span>
  135. </el-tooltip>
  136. </div>
  137. </el-card>
  138. </div>
  139. </draggable>
  140. <div v-if="!readOnly">
  141. <!-- 添加内容项 -->
  142. <div class="add-item">
  143. <el-input
  144. v-model="newItemKey[activeTab]"
  145. placeholder="键名"
  146. style="width: 200px; margin-right: 10px;"></el-input>
  147. <el-input
  148. v-model="newItemValue[activeTab]"
  149. placeholder="键值"
  150. style="width: 200px; margin-right: 10px;"></el-input>
  151. <el-button
  152. type="primary"
  153. @click="addContentItem(activeTab)"
  154. :disabled="!newItemKey[activeTab] || !newItemValue[activeTab]">
  155. 添加内容项
  156. </el-button>
  157. </div>
  158. <br>
  159. <!-- 删除当前标签页 -->
  160. <el-button
  161. type="danger"
  162. size="small"
  163. @click="deleteTab(activeTab)">
  164. 删除当前标签页
  165. </el-button>
  166. <!-- 修改当前标签名-->
  167. <el-button
  168. type="primary"
  169. size="small"
  170. @click="editTabName(activeTab)">
  171. 修改当前标签名
  172. </el-button>
  173. </div>
  174. </div>
  175. </el-tab-pane>
  176. <br>
  177. <!-- 添加新标签页 -->
  178. <div class="add-tab" v-if="!readOnly">
  179. <el-input
  180. v-model="newTabName"
  181. placeholder="新标签页名称"
  182. style="width: 200px; margin-right: 10px;"></el-input>
  183. <el-button
  184. type="primary"
  185. @click="addTab"
  186. :disabled="!newTabName || myObj[newTabName]">
  187. 添加标签页
  188. </el-button>
  189. </div>
  190. </el-tabs>
  191. </div>
  192. <script>
  193. // let all_data = {};
  194. let pageManager = new PageManager();
  195. pageManager.type = 'copy-page';
  196. pageManager.setNameCallBack(() => {
  197. let title = localStorage.getItem("title");
  198. document.title = title + " - 复制";
  199. _this.all_data.title = title;
  200. })
  201. let _this = new Vue({
  202. el: '#app',
  203. data: {
  204. all_data: {},
  205. activeTab: '',
  206. myObj: {
  207. "tab1": [
  208. {key: "ab1", value: "value1"},
  209. {key: "ab2", value: "value2"},
  210. {key: "ab3", value: "value3"}
  211. ],
  212. "tab2": [
  213. {key: "ab1", value: "value4"},
  214. {key: "ab2", value: "value5"}
  215. ]
  216. },
  217. newTabName: '',
  218. newItemKey: {},
  219. newItemValue: {},
  220. editingItem: null,
  221. editingKey: '',
  222. readOnly: false,
  223. },
  224. mounted() {
  225. pageManager.init().then(res => {
  226. this.all_data = res;
  227. console.log(res)
  228. if (res && res.content) {
  229. // 初始化数据
  230. let showContent = res.content;
  231. if (pageManager.isBackup) {
  232. // 如果是备份
  233. showContent = res.content_back;
  234. }
  235. this.myObj = JSON.parse(showContent);
  236. }
  237. // 设置activeTab
  238. this.activeTab = this.get_init_activeTab();
  239. if (pageManager.isReadOnly()) {
  240. // 备份页面或分享页面,设置为只读
  241. this.readOnly = true;
  242. }
  243. this.mouseOver();
  244. });
  245. },
  246. methods: {
  247. // 鼠标滑过,切换tabs
  248. mouseOver() {
  249. this.$nextTick(() => {
  250. this.$refs.navTabs.$refs.nav.$nextTick(() => {
  251. // console.log('$refs.nav', this.$refs.navTabs.$refs.nav.$el)
  252. // 此时tab的nav才渲染dom 否则拿不到el-tabs__item
  253. const target = document.getElementsByClassName('el-tabs__item')
  254. for (let i = 0; i < target.length; i++) {
  255. target[i].addEventListener('mouseover', () => {
  256. target[i].click();
  257. })
  258. }
  259. })
  260. });
  261. },
  262. // 获取初始化activeTab
  263. get_init_activeTab() {
  264. return sessionStorage.getItem("tab-" + pageManager.id) || Object.keys(this.myObj)[0] || '';
  265. },
  266. updateData() {
  267. this.all_data.content = JSON.stringify(this.myObj);
  268. pageManager.updateData(this.all_data);
  269. },
  270. editTabName(activeTab) {
  271. // 先获取当前标签名,弹框提醒用户更改
  272. this.$prompt('请输入新的标签名', '修改标签名', {
  273. confirmButtonText: '确定',
  274. cancelButtonText: '取消',
  275. closeOnClickModal: false,
  276. inputValue: activeTab
  277. })
  278. .then(({value}) => {
  279. // 确认更改后,更新标签名, 让vue监听到,使用 this.$set 方法
  280. this.$set(this.myObj, value, this.myObj[activeTab]);
  281. delete this.myObj[activeTab];
  282. this.activeTab = value;
  283. _this.updateData();
  284. })
  285. },
  286. deleteTab(activeTab) {
  287. if (this.myObj[activeTab]) {
  288. this.$confirm('确定删除该标签页?', '警告', {
  289. confirmButtonText: '确定',
  290. cancelButtonText: '取消',
  291. type: 'warning'
  292. }).then(() => {
  293. delete this.myObj[activeTab];
  294. this.activeTab = Object.keys(this.myObj)[0] || '';
  295. _this.updateData();
  296. }).catch(() => {
  297. });
  298. }
  299. },
  300. handleTabClick(tab) {
  301. this.activeTab = tab.name;
  302. // 保存到本地 localStorage
  303. sessionStorage.setItem("tab-" + pageManager.id, this.activeTab);
  304. },
  305. // 添加标签页
  306. addTab() {
  307. if (!this.newTabName || this.myObj[this.newTabName]) return;
  308. this.myObj[this.newTabName] = [];
  309. this.activeTab = this.newTabName;
  310. this.newTabName = '';
  311. _this.updateData();
  312. },
  313. // 删除标签页(需确认)
  314. deleteTab(tabName) {
  315. this.$confirm('确定删除该标签页?', '警告', {
  316. confirmButtonText: '确定',
  317. cancelButtonText: '取消',
  318. type: 'warning'
  319. }).then(() => {
  320. delete this.myObj[tabName];
  321. this.activeTab = Object.keys(this.myObj)[0] || '';
  322. _this.updateData();
  323. }).catch(() => {
  324. });
  325. },
  326. // 添加内容项
  327. addContentItem(tabName) {
  328. const key = this.newItemKey[tabName];
  329. const value = this.newItemValue[tabName];
  330. if (!key || !value) return;
  331. this.myObj[tabName].push({key, value});
  332. this.newItemKey[tabName] = '';
  333. this.newItemValue[tabName] = '';
  334. _this.updateData();
  335. },
  336. // 删除内容项
  337. deleteContentItem(tabName, key) {
  338. this.$confirm('确定删除该内容项?', '警告', {
  339. confirmButtonText: '确定',
  340. cancelButtonText: '取消',
  341. type: 'warning'
  342. }).then(() => {
  343. this.myObj[tabName] = this.myObj[tabName].filter(item => item.key !== key);
  344. this.$message({message: '删除成功', type: 'success'});
  345. _this.updateData();
  346. }).catch(() => {
  347. });
  348. },
  349. editContentItem(tabName, key, value) {
  350. this.editingItem = {tabName, key, value};
  351. this.editingKey = key;
  352. // 保存原始值用于回滚
  353. const originalKey = key;
  354. const originalValue = value;
  355. // 第一步:修改Key
  356. this.$prompt('修改Key', '编辑内容', {
  357. confirmButtonText: '保存',
  358. cancelButtonText: '取消',
  359. inputPlaceholder: '输入新Key',
  360. closeOnClickModal: false,
  361. inputValue: key
  362. }).then(({value: newKey}) => {
  363. // 第二步:修改Value
  364. return this.$prompt('修改值', '编辑内容', {
  365. confirmButtonText: '保存',
  366. cancelButtonText: '取消',
  367. inputPlaceholder: '输入新值',
  368. closeOnClickModal: false,
  369. inputValue: value
  370. }).then(({value: newValue}) => {
  371. // 更新数据
  372. const items = this.myObj[tabName];
  373. const itemIndex = items.findIndex(item => item.key === originalKey);
  374. if (itemIndex !== -1) {
  375. items[itemIndex].key = newKey;
  376. items[itemIndex].value = newValue;
  377. }
  378. this.updateData();
  379. this.editingItem = null;
  380. });
  381. }).catch(() => {
  382. // 如果任一步骤取消,重置状态
  383. this.editingItem = null;
  384. });
  385. },
  386. // 复制到剪贴板
  387. copyToClipboard(text) {
  388. const clipboard = new ClipboardJS('.dummy', {
  389. text: () => text
  390. });
  391. clipboard.on('success', () => {
  392. this.$message({message: '复制成功', type: 'success'});
  393. clipboard.destroy();
  394. });
  395. clipboard.on('error', () => {
  396. this.$message({message: '复制失败', type: 'error'});
  397. clipboard.destroy();
  398. });
  399. const dummy = document.createElement('button');
  400. dummy.className = 'dummy';
  401. document.body.appendChild(dummy);
  402. dummy.click();
  403. document.body.removeChild(dummy);
  404. },
  405. // 拖拽排序事件
  406. onDragChange(tabName) {
  407. console.log(`拖拽后 ${tabName} 的顺序:`, this.myObj[tabName]);
  408. this.$message({message: '排序已更新', type: 'success'});
  409. _this.updateData();
  410. }
  411. }
  412. });
  413. </script>
  414. </body>
  415. </html>