mycopy.html 16 KB

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