mycopy.html 15 KB

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