mycopy.html 17 KB

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