mycopy.html 18 KB

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