mycopy.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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">
  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. v-model="myObj[tabName]"
  100. tag="div"
  101. @change="onDragChange(tabName)"
  102. handle=".drag-handle"
  103. class="content-list">
  104. <div
  105. v-for="(item, index) in myObj[tabName]"
  106. :key="item.key"
  107. class="content-item"
  108. @click.stop="copyToClipboard(item.value)"
  109. >
  110. <el-card shadow="hover" class="item-card">
  111. <div slot="header" class="item-content">
  112. <span>{{ item.key }}</span>
  113. <div class="action-buttons">
  114. <el-button
  115. type="text"
  116. class="drag-handle">
  117. <i class="el-icon-rank"></i>
  118. </el-button>
  119. <el-button
  120. type="text"
  121. @click.stop="editContentItem(tabName, item.key, item.value)">
  122. <i class="el-icon-edit"></i>
  123. </el-button>
  124. <el-button
  125. type="text"
  126. @click.stop="deleteContentItem(tabName, item.key)">
  127. <i class="el-icon-delete"></i>
  128. </el-button>
  129. </div>
  130. </div>
  131. <div class="item-2">
  132. <!-- 内容第一行,不能换行,用css控制,多余的隐藏-->
  133. <span>{{ item.value }}</span>
  134. </div>
  135. </el-card>
  136. </div>
  137. </draggable>
  138. <div v-if="!readOnly">
  139. <!-- 添加内容项 -->
  140. <div class="add-item">
  141. <el-input
  142. v-model="newItemKey[activeTab]"
  143. placeholder="键名"
  144. style="width: 200px; margin-right: 10px;"></el-input>
  145. <el-input
  146. v-model="newItemValue[activeTab]"
  147. placeholder="键值"
  148. style="width: 200px; margin-right: 10px;"></el-input>
  149. <el-button
  150. type="primary"
  151. @click="addContentItem(activeTab)"
  152. :disabled="!newItemKey[activeTab] || !newItemValue[activeTab]">
  153. 添加内容项
  154. </el-button>
  155. </div>
  156. <br>
  157. <!-- 删除当前标签页 -->
  158. <el-button
  159. type="danger"
  160. size="small"
  161. @click="deleteTab(activeTab)">
  162. 删除当前标签页
  163. </el-button>
  164. <!-- 修改当前标签名-->
  165. <el-button
  166. type="primary"
  167. size="small"
  168. @click="editTabName(activeTab)">
  169. 修改当前标签名
  170. </el-button>
  171. </div>
  172. </div>
  173. </el-tab-pane>
  174. <br>
  175. <!-- 添加新标签页 -->
  176. <div class="add-tab" v-if="!readOnly">
  177. <el-input
  178. v-model="newTabName"
  179. placeholder="新标签页名称"
  180. style="width: 200px; margin-right: 10px;"></el-input>
  181. <el-button
  182. type="primary"
  183. @click="addTab"
  184. :disabled="!newTabName || myObj[newTabName]">
  185. 添加标签页
  186. </el-button>
  187. </div>
  188. </el-tabs>
  189. </div>
  190. <script>
  191. // let all_data = {};
  192. let pageManager = new PageManager();
  193. pageManager.type = 'copy-page';
  194. pageManager.setNameCallBack(() => {
  195. let title = localStorage.getItem("title");
  196. document.title = title + " - 复制";
  197. _this.all_data.title = title;
  198. })
  199. let _this = new Vue({
  200. el: '#app',
  201. data: {
  202. all_data: {},
  203. activeTab: '',
  204. myObj: {
  205. "tab1": [
  206. {key: "ab1", value: "value1"},
  207. {key: "ab2", value: "value2"},
  208. {key: "ab3", value: "value3"}
  209. ],
  210. "tab2": [
  211. {key: "ab1", value: "value4"},
  212. {key: "ab2", value: "value5"}
  213. ]
  214. },
  215. newTabName: '',
  216. newItemKey: {},
  217. newItemValue: {},
  218. editingItem: null,
  219. editingKey: '',
  220. readOnly: false,
  221. },
  222. mounted() {
  223. pageManager.init().then(res => {
  224. this.all_data = res;
  225. console.log(res)
  226. if (res && res.content) {
  227. // 初始化数据
  228. let showContent = res.content;
  229. if (pageManager.isBackup) {
  230. // 如果是备份
  231. showContent = res.content_back;
  232. }
  233. this.myObj = JSON.parse(showContent);
  234. // 设置activeTab
  235. this.activeTab = localStorage.getItem("tab") || Object.keys(this.myObj)[0] || '';
  236. }
  237. if (pageManager.isReadOnly()) {
  238. // 备份页面或分享页面,设置为只读
  239. this.readOnly = true;
  240. }
  241. });
  242. },
  243. methods: {
  244. updateData() {
  245. this.all_data.content = JSON.stringify(this.myObj);
  246. pageManager.updateData(this.all_data);
  247. },
  248. editTabName(activeTab) {
  249. // 先获取当前标签名,弹框提醒用户更改
  250. this.$prompt('请输入新的标签名', '修改标签名', {
  251. confirmButtonText: '确定',
  252. cancelButtonText: '取消',
  253. closeOnClickModal: false,
  254. inputValue: activeTab
  255. })
  256. .then(({value}) => {
  257. // 确认更改后,更新标签名, 让vue监听到,使用 this.$set 方法
  258. this.$set(this.myObj, value, this.myObj[activeTab]);
  259. delete this.myObj[activeTab];
  260. this.activeTab = value;
  261. _this.updateData();
  262. })
  263. },
  264. deleteTab(activeTab) {
  265. if (this.myObj[activeTab]) {
  266. this.$confirm('确定删除该标签页?', '警告', {
  267. confirmButtonText: '确定',
  268. cancelButtonText: '取消',
  269. type: 'warning'
  270. }).then(() => {
  271. delete this.myObj[activeTab];
  272. this.activeTab = Object.keys(this.myObj)[0] || '';
  273. _this.updateData();
  274. }).catch(() => {
  275. });
  276. }
  277. },
  278. handleTabClick(tab) {
  279. this.activeTab = tab.name;
  280. // 保存到本地 localStorage
  281. localStorage.setItem("tab", this.activeTab);
  282. },
  283. // 添加标签页
  284. addTab() {
  285. if (!this.newTabName || this.myObj[this.newTabName]) return;
  286. this.myObj[this.newTabName] = [];
  287. this.activeTab = this.newTabName;
  288. this.newTabName = '';
  289. _this.updateData();
  290. },
  291. // 删除标签页(需确认)
  292. deleteTab(tabName) {
  293. this.$confirm('确定删除该标签页?', '警告', {
  294. confirmButtonText: '确定',
  295. cancelButtonText: '取消',
  296. type: 'warning'
  297. }).then(() => {
  298. delete this.myObj[tabName];
  299. this.activeTab = Object.keys(this.myObj)[0] || '';
  300. _this.updateData();
  301. }).catch(() => {
  302. });
  303. },
  304. // 添加内容项
  305. addContentItem(tabName) {
  306. const key = this.newItemKey[tabName];
  307. const value = this.newItemValue[tabName];
  308. if (!key || !value) return;
  309. this.myObj[tabName].push({key, value});
  310. this.newItemKey[tabName] = '';
  311. this.newItemValue[tabName] = '';
  312. _this.updateData();
  313. },
  314. // 删除内容项
  315. deleteContentItem(tabName, key) {
  316. this.$confirm('确定删除该内容项?', '警告', {
  317. confirmButtonText: '确定',
  318. cancelButtonText: '取消',
  319. type: 'warning'
  320. }).then(() => {
  321. this.myObj[tabName] = this.myObj[tabName].filter(item => item.key !== key);
  322. this.$message({message: '删除成功', type: 'success'});
  323. _this.updateData();
  324. }).catch(() => {
  325. });
  326. },
  327. editContentItem(tabName, key, value) {
  328. this.editingItem = {tabName, key, value};
  329. this.editingKey = key;
  330. // 保存原始值用于回滚
  331. const originalKey = key;
  332. const originalValue = value;
  333. // 第一步:修改Key
  334. this.$prompt('修改Key', '编辑内容', {
  335. confirmButtonText: '保存',
  336. cancelButtonText: '取消',
  337. inputPlaceholder: '输入新Key',
  338. closeOnClickModal: false,
  339. inputValue: key
  340. }).then(({value: newKey}) => {
  341. // 第二步:修改Value
  342. return this.$prompt('修改值', '编辑内容', {
  343. confirmButtonText: '保存',
  344. cancelButtonText: '取消',
  345. inputPlaceholder: '输入新值',
  346. closeOnClickModal: false,
  347. inputValue: value
  348. }).then(({value: newValue}) => {
  349. // 更新数据
  350. const items = this.myObj[tabName];
  351. const itemIndex = items.findIndex(item => item.key === originalKey);
  352. if (itemIndex !== -1) {
  353. items[itemIndex].key = newKey;
  354. items[itemIndex].value = newValue;
  355. }
  356. this.updateData();
  357. this.editingItem = null;
  358. });
  359. }).catch(() => {
  360. // 如果任一步骤取消,重置状态
  361. this.editingItem = null;
  362. });
  363. },
  364. // 复制到剪贴板
  365. copyToClipboard(text) {
  366. const clipboard = new ClipboardJS('.dummy', {
  367. text: () => text
  368. });
  369. clipboard.on('success', () => {
  370. this.$message({message: '复制成功', type: 'success'});
  371. clipboard.destroy();
  372. });
  373. clipboard.on('error', () => {
  374. this.$message({message: '复制失败', type: 'error'});
  375. clipboard.destroy();
  376. });
  377. const dummy = document.createElement('button');
  378. dummy.className = 'dummy';
  379. document.body.appendChild(dummy);
  380. dummy.click();
  381. document.body.removeChild(dummy);
  382. },
  383. // 拖拽排序事件
  384. onDragChange(tabName) {
  385. console.log(`拖拽后 ${tabName} 的顺序:`, this.myObj[tabName]);
  386. this.$message({message: '排序已更新', type: 'success'});
  387. _this.updateData();
  388. }
  389. }
  390. });
  391. </script>
  392. </body>
  393. </html>