Commit b8fb0e76 authored by liaozan's avatar liaozan 🏀

feat: support preview

parent c1bc259c
...@@ -5,6 +5,7 @@ import lombok.Data; ...@@ -5,6 +5,7 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
...@@ -18,8 +19,16 @@ public class PreviewFileTree { ...@@ -18,8 +19,16 @@ public class PreviewFileTree {
private String fileName; private String fileName;
private String fileContent;
private List<PreviewFileTree> children; private List<PreviewFileTree> children;
public PreviewFileTree(String fileName, String fileContent) {
this.fileName = fileName;
this.fileContent = fileContent;
this.children = Collections.emptyList();
}
public boolean isFile() { public boolean isFile() {
return CollectionUtils.isEmpty(children); return CollectionUtils.isEmpty(children);
} }
......
package com.schbrain.archetype.initializer.service; package com.schbrain.archetype.initializer.service;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ZipUtil; import cn.hutool.core.util.ZipUtil;
import com.schbrain.archetype.initializer.maven.MavenUtils; import com.schbrain.archetype.initializer.maven.MavenUtils;
...@@ -17,7 +18,10 @@ import java.io.File; ...@@ -17,7 +18,10 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -53,7 +57,7 @@ public class ArchetypeService { ...@@ -53,7 +57,7 @@ public class ArchetypeService {
private PreviewFileTree buildFileTree(File root) { private PreviewFileTree buildFileTree(File root) {
File[] fileItems = root.listFiles(); File[] fileItems = root.listFiles();
if (ArrayUtil.isEmpty(fileItems)) { if (ArrayUtil.isEmpty(fileItems)) {
return new PreviewFileTree(root.getName(), Collections.emptyList()); return new PreviewFileTree(root.getName(), null);
} }
List<PreviewFileTree> children = Arrays.stream(fileItems) List<PreviewFileTree> children = Arrays.stream(fileItems)
.map(fileItem -> { .map(fileItem -> {
...@@ -61,7 +65,8 @@ public class ArchetypeService { ...@@ -61,7 +65,8 @@ public class ArchetypeService {
if (fileItem.isDirectory()) { if (fileItem.isDirectory()) {
childrenFileTree = buildFileTree(fileItem); childrenFileTree = buildFileTree(fileItem);
} else { } else {
childrenFileTree = new PreviewFileTree(fileItem.getName(), Collections.emptyList()); String fileContent = FileUtil.readUtf8String(fileItem);
childrenFileTree = new PreviewFileTree(fileItem.getName(), fileContent);
} }
return childrenFileTree; return childrenFileTree;
}) })
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
<input :disabled="this.id === null" class="form-submit" type="button" value="下载" @click="download"> <input :disabled="this.id === null" class="form-submit" type="button" value="下载" @click="download">
<el-dialog v-model="dialogVisible" title="预览" width="30%"> <el-dialog v-model="dialogVisible" title="预览" width="30%">
<el-tree ref="fileTree" :data="treeData" :props="defaultProps" @node-click="handleNodeClick"/> <el-tree :data="treeData" :props="defaultProps" @node-click="handleNodeClick"/>
<template #footer> <template #footer>
<span class="dialog-footer"> <span class="dialog-footer">
<el-button type="primary" @click="dialogVisible = false">关闭</el-button> <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
...@@ -28,6 +28,15 @@ ...@@ -28,6 +28,15 @@
</template> </template>
</el-dialog> </el-dialog>
<el-dialog v-model="fileContentDialogVisible" title="预览" width="80%">
<div v-html="fileContent"></div>
<template #footer>
<span class="dialog-footer">
<el-button type="primary" @click="fileContentDialogVisible = false">关闭</el-button>
</span>
</template>
</el-dialog>
</div> </div>
</template> </template>
...@@ -41,6 +50,8 @@ export default { ...@@ -41,6 +50,8 @@ export default {
return { return {
id: null, id: null,
dialogVisible: false, dialogVisible: false,
fileContentDialogVisible: false,
fileContent: '',
defaultProps: { defaultProps: {
children: 'children', children: 'children',
label: 'fileName', label: 'fileName',
...@@ -87,7 +98,12 @@ export default { ...@@ -87,7 +98,12 @@ export default {
}) })
}, },
handleNodeClick: function (data) { handleNodeClick: function (data) {
console.log(data) const {file, fileContent} = data
if (!file) {
return
}
this.fileContentDialogVisible = true
this.fileContent = fileContent
}, },
downloadGeneratedProject: function (data, fileName) { downloadGeneratedProject: function (data, fileName) {
const blob = new Blob([data]) const blob = new Blob([data])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment