mycopy.html 18 KB

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