You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

vueTree.vue 6.6 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <div class="myTrees">
  3. <el-tree
  4. ref="tree"
  5. :data="treeData"
  6. node-key="filePath"
  7. empty-text="暂无数据"
  8. highlight-current
  9. @node-click="handleLeftclick"
  10. >
  11. <div slot-scope="{ node , data }" class="custom-tree-node">
  12. <span>
  13. <i :class="getIcon(node.data)" />
  14. {{ node.label }}
  15. </span>
  16. <span>
  17. <el-dropdown trigger="click" class="el-dropdown"
  18. v-show="!isLeaf && data.type == 'tree'"
  19. >
  20. <span class="el-dropdown-link">
  21. <svg class="icon el-dropdown-svg" aria-hidden="true">
  22. <use xlink:href="#icon-a-bianzu31"></use>
  23. </svg>
  24. </span>
  25. <el-dropdown-menu slot="dropdown">
  26. <el-dropdown-item
  27. @click.native="addChildNode('leaf')"
  28. >新建文件</el-dropdown-item
  29. >
  30. <el-dropdown-item
  31. @click.native="addChildNode('')"
  32. >新建文件夹</el-dropdown-item
  33. >
  34. <!-- <el-dropdown-item @click.native="deleteNode">删除</el-dropdown-item> -->
  35. </el-dropdown-menu>
  36. </el-dropdown>
  37. </span>
  38. </div>
  39. </el-tree>
  40. </div>
  41. </template>
  42. <!-- file-icon ide-icon word-icon dark-blue -->
  43. <link rel="stylesheet" href="/web_src/js/components/treeIcon.css" />
  44. <script>
  45. import {icons} from "./icons";
  46. console.log("icons:",icons)
  47. export default {
  48. name: "List",
  49. props: {
  50. treeListData: {
  51. type: Array,
  52. default: () => [],
  53. },
  54. fileInfoParams: {
  55. type: Object,
  56. default: () => {},
  57. },
  58. },
  59. data() {
  60. return {
  61. fileParams: {},
  62. treeData: [],
  63. isShow: false,
  64. currentData: "",
  65. currentNode: "",
  66. menuVisible: false,
  67. firstLevel: false,
  68. lastLevel: false,
  69. filterText: "",
  70. isLeaf: false,
  71. };
  72. },
  73. watch: {
  74. treeListData(val) {
  75. this.treeData = val;
  76. },
  77. fileInfoParams(val) {
  78. this.fileParams = val;
  79. },
  80. },
  81. methods: {
  82. // 鼠标左击事件
  83. handleLeftclick(data, node) {
  84. this.currentData = data;
  85. this.currentNode = node;
  86. this.firstLevel = false;
  87. this.isLeaf = data.isLeaf;
  88. this.lastLevel = false;
  89. if (data.type === 'tree') return;
  90. // if (data.sha) {
  91. this.$emit("handleChangFile", data, this.treeData);
  92. // }
  93. },
  94. getIcon(data){
  95. console.log("data:",data.name.split("."))
  96. let icon = '';
  97. if(data.type === 'tree'){
  98. return 'fa fa-folder ide-icon ide-icon-folder'
  99. }
  100. try {
  101. let suffix = data.name.split(".").pop();
  102. if(data.name.indexOf(".") > -1){
  103. suffix = "." + suffix
  104. }
  105. icons.forEach(element => {
  106. if(element[2].test(suffix)){
  107. icon = element[0] + ' ' + element[1].join(' ');
  108. throw('')
  109. }
  110. })
  111. } catch (error) {
  112. }
  113. return "file-icon ide-icon " + icon;
  114. },
  115. // 增加子级节点事件
  116. addChildNode(shape) {
  117. const id = Math.ceil(Math.random() * 100);
  118. this.$prompt("请输入名称", "提示", {
  119. confirmButtonText: "确定",
  120. cancelButtonText: "取消",
  121. })
  122. .then(({ value }) => {
  123. if (value.trim().indexOf("") === -1)
  124. return this.$message.warning("名称不能包含空格!");
  125. const treeD = {
  126. id,
  127. label: value,
  128. operation: "add",
  129. isEdit: true,
  130. type: shape === "leaf" ? "blob" : "tree",
  131. filePath: `${this.currentData.filePath}/${value}`,
  132. isLeaf: shape === "leaf",
  133. name: value,
  134. fileType:"txt",
  135. children: [],
  136. };
  137. if(shape === "leaf"){
  138. treeD.content = "";
  139. treeD.oldContent = "";
  140. treeD.newContent = "";
  141. treeD.fileType = 'txt'
  142. }
  143. treeD.path = treeD.filePath;
  144. this.$refs.tree.append(treeD, this.currentData.filePath);
  145. this.$emit("handleAddNode", treeD, this.currentData.filePath); // 触发父组件更改提交界面的数据变化
  146. })
  147. .catch(() => {});
  148. },
  149. // 删除节点
  150. deleteNode() {
  151. this.$confirm(`确定删除当前文件,是否继续?`, "提示", {
  152. confirmButtonText: "确定",
  153. cancelButtonText: "取消",
  154. type: "warning",
  155. })
  156. .then(() => {
  157. if (this.currentData.isLeaf) {
  158. // isLeaf为true 代表文件类型
  159. if (this.currentData.operation === "add") {
  160. // 新增的节点删去
  161. this.$emit("handleDeleteAddNode", this.currentData);
  162. } else if (this.currentData.sha) {
  163. // 原本存在的数据 触发父组件更新已存在数据的状态
  164. this.$emit("handleDeleteOldNode", this.currentData);
  165. }
  166. }
  167. this.$refs.tree.remove(this.currentNode);
  168. })
  169. .catch(() => {});
  170. },
  171. },
  172. };
  173. </script>
  174. <style lang="less" scoped>
  175. @import "./treeIcon.css";
  176. .myTrees {
  177. /*background: transparent;*/
  178. height: 100%;
  179. overflow: auto;
  180. background: #FFFFFF;
  181. }
  182. .el-tree {
  183. /* padding: 20px; */
  184. /*background: transparent;*/
  185. background: #FFFFFF;
  186. color: black;
  187. }
  188. .el-tree .is-current{
  189. background: #f5f7fa;
  190. }
  191. .custom-tree-node {
  192. flex: 1;
  193. display: flex;
  194. align-items: center;
  195. justify-content: space-between;
  196. padding-right: 8px;
  197. font-size: 14px;
  198. height: 34px;
  199. line-height: 34px;
  200. .el-dropdown-svg{
  201. height: 16px;
  202. width: 16px;
  203. color: #979797;
  204. }
  205. .el-dropdown-svg:hover{
  206. color: #007aff;
  207. }
  208. }
  209. /deep/ .el-tree-node__content{
  210. height: 34px;
  211. line-height: 34px;
  212. font-size: 14px;
  213. font-family: PingFangSC-Regular, PingFang SC;
  214. font-weight: 400;
  215. }
  216. /deep/ .el-tree-node.is-current > .el-tree-node__content{
  217. background: #F5F7FA;
  218. color: #2285D0 !important;
  219. font-family: PingFangSC-Regular, PingFang SC;
  220. font-weight: 400;
  221. font-size: 14px;
  222. color: #2285D0;
  223. }
  224. .el-dropdown-menu{
  225. padding: 0px !important;
  226. }
  227. .el-popper[x-placement^=bottom] .popper__arrow {
  228. display: none !important;
  229. }
  230. .el-dropdown-menu__item{
  231. color: #333333 !important;
  232. height: 34px !important;
  233. }
  234. li.el-dropdown-menu__item:hover {
  235. background: #f2f2f2 !important;
  236. }
  237. .icon {
  238. width: 1em;
  239. height: 1em;
  240. vertical-align: -0.15em;
  241. fill: currentColor;
  242. overflow: hidden;
  243. }
  244. </style>
  245. <style>
  246. .myTrees .el-tree-node__expand-icon{
  247. visibility: hidden;
  248. }
  249. .monaco-editor .line-numbers{
  250. color: #999;
  251. }
  252. </style>