mycopy.html 18 KB

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