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.

utils.js 4.0 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import uuidv1 from 'uuidv1';
  2. // retrieve a HTML string for given SVG icon name and size in pixels
  3. export function svg(name, size) {
  4. return `<svg class="svg ${name}" width="${size}" height="${size}" aria-hidden="true"><use xlink:href="#${name}"/></svg>`;
  5. }
  6. // transform /path/to/file.ext to file.ext
  7. export function basename(path = '') {
  8. return path ? path.replace(/^.*\//, '') : '';
  9. }
  10. // transform /path/to/file.ext to .ext
  11. export function extname(path = '') {
  12. const [_, ext] = /.+(\.[^.]+)$/.exec(path) || [];
  13. return ext || '';
  14. }
  15. // test whether a variable is an object
  16. export function isObject(obj) {
  17. return Object.prototype.toString.call(obj) === '[object Object]';
  18. }
  19. // returns whether a dark theme is enabled
  20. export function isDarkTheme() {
  21. return document.documentElement.classList.contains('theme-arc-green');
  22. }
  23. export function matterTree(data) {
  24. for (let i = 0; i < data.length; i++) {
  25. data[i].label = data[i].name;
  26. data[i].filePath = data[i].path;
  27. data[i].isEdit = true;
  28. if (!data[i].children.length) data[i].isLeaf = true;
  29. if (data[i].children && data[i].children.length > 0) {
  30. // children若不为空数组,则继续 递归调用 本方法
  31. matterTree(data[i].children);
  32. }
  33. }
  34. return data;
  35. }
  36. export function disposeTreeName(data, target) {
  37. for (let i = 0; i < data.length; i++) {
  38. data[i].filePath = `${target}/${data[i].name}`;
  39. data[i].id = uuidv1().replaceAll('-', '');
  40. if (data[i].children && data[i].children.length > 0) {
  41. // children若不为空数组,则继续 递归调用 本方法
  42. matterTree(data[i].children);
  43. }
  44. }
  45. return data;
  46. }
  47. export function RecurveAddNode(treeRootData, treeD, FilePath) {
  48. treeRootData.forEach((item) => {
  49. if (item.filePath === FilePath) {
  50. return item.children.push(treeD);
  51. }
  52. if (item.children?.length) {
  53. RecurveAddNode(item.children, treeD, FilePath);
  54. }
  55. });
  56. return treeRootData;
  57. }
  58. // 递归单个循环操作 判断是否新增
  59. export function RecurveQuery(currentData, filePath) {
  60. currentData.forEach((item, index) => {
  61. if (item.filePath === filePath && item.isLeaf) {
  62. currentData[index].operation = 'delete';
  63. } else if (item.children?.length > 0) {
  64. RecurveQuery(item.children, filePath);
  65. }
  66. });
  67. return currentData;
  68. }
  69. // 递归 对新增的数据进行删除
  70. export function RecurveQueryDelete(data, filePath) {
  71. data.forEach((item, index) => {
  72. if (item.filePath === filePath && item.isLeaf) {
  73. data.splice(index, 1);
  74. } else if (item.children?.length) {
  75. RecurveQueryDelete(item.children, filePath);
  76. }
  77. });
  78. return data;
  79. }
  80. // 循环插入改变的数据
  81. export function RecurveUpdateValue(currentData, fileInfoParams, base64Content) {
  82. currentData.forEach((item, index) => {
  83. if (item.filePath === fileInfoParams.filePath) {
  84. currentData[index].oldContent = base64Content;
  85. currentData[index].content = base64Content;
  86. } else if (item.children?.length > 0) {
  87. RecurveQuery(item.children, fileInfoParams.filePath);
  88. }
  89. });
  90. return currentData;
  91. }
  92. const flattenTree = (data) => {
  93. return data.reduce((arr, currentValue) => {
  94. if (currentValue.children && currentValue.children.length > 0) {
  95. return arr.concat(flattenTree(currentValue.children))
  96. } else {
  97. return arr.concat(currentValue)
  98. }
  99. }, [])
  100. }
  101. export const uniqueArray = (array1, array2) => {
  102. return array1
  103. .concat(array2)
  104. .reduce((prev, cur) => {
  105. const duplicate = prev.filter(x => x.name === cur.name)
  106. if (duplicate.length) {
  107. return prev
  108. } else {
  109. prev.push(cur)
  110. return prev
  111. }
  112. }, [])
  113. }
  114. export const isBase64 = (str) => {
  115. if(!str) return false;
  116. if (/^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/.test(str)) {
  117. try {
  118. return window.btoa(window.atob(str)) == str;
  119. } catch (err) {
  120. return false;
  121. }
  122. }else{
  123. return false
  124. }
  125. }