mycopy.html 17 KB

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