Commit 5e2d50aa authored by Denys Mishunov's avatar Denys Mishunov

Passes file type down to ContentViewer

Instead of ContentViewer attempts to compute file type and render
appropriate viewer type, compute it on the parent RepoEditor having all
file-related information at its disposal and pass it down to
ContentViewer as a prop
parent a6a1afe0
<script> <script>
import { mapState, mapGetters, mapActions } from 'vuex'; import { mapState, mapGetters, mapActions } from 'vuex';
import { viewerInformationForPath } from '~/vue_shared/components/content_viewer/lib/viewer_utils';
import flash from '~/flash'; import flash from '~/flash';
import ContentViewer from '~/vue_shared/components/content_viewer/content_viewer.vue'; import ContentViewer from '~/vue_shared/components/content_viewer/content_viewer.vue';
import DiffViewer from '~/vue_shared/components/diff_viewer/diff_viewer.vue'; import DiffViewer from '~/vue_shared/components/diff_viewer/diff_viewer.vue';
...@@ -56,6 +57,10 @@ export default { ...@@ -56,6 +57,10 @@ export default {
active: this.file.viewMode === 'preview', active: this.file.viewMode === 'preview',
}; };
}, },
fileType() {
const info = viewerInformationForPath(this.file.path);
return (info && info.id) || '';
},
}, },
watch: { watch: {
file(newVal, oldVal) { file(newVal, oldVal) {
...@@ -258,6 +263,7 @@ export default { ...@@ -258,6 +263,7 @@ export default {
:path="file.rawPath || file.path" :path="file.rawPath || file.path"
:file-size="file.size" :file-size="file.size"
:project-path="file.projectId" :project-path="file.projectId"
:type="fileType"
/> />
<diff-viewer <diff-viewer
v-if="showDiffViewer" v-if="showDiffViewer"
......
<script> <script>
import { viewerInformationForPath } from './lib/viewer_utils';
import MarkdownViewer from './viewers/markdown_viewer.vue'; import MarkdownViewer from './viewers/markdown_viewer.vue';
import ImageViewer from './viewers/image_viewer.vue'; import ImageViewer from './viewers/image_viewer.vue';
import DownloadViewer from './viewers/download_viewer.vue'; import DownloadViewer from './viewers/download_viewer.vue';
...@@ -24,15 +23,18 @@ export default { ...@@ -24,15 +23,18 @@ export default {
required: false, required: false,
default: '', default: '',
}, },
type: {
type: String,
required: false,
default: '',
},
}, },
computed: { computed: {
viewer() { viewer() {
if (!this.path) return null; if (!this.path) return null;
if (!this.type) return DownloadViewer;
const previewInfo = viewerInformationForPath(this.path); switch (this.type) {
if (!previewInfo) return DownloadViewer;
switch (previewInfo.id) {
case 'markdown': case 'markdown':
return MarkdownViewer; return MarkdownViewer;
case 'image': case 'image':
......
...@@ -4,6 +4,7 @@ import axios from '~/lib/utils/axios_utils'; ...@@ -4,6 +4,7 @@ import axios from '~/lib/utils/axios_utils';
import contentViewer from '~/vue_shared/components/content_viewer/content_viewer.vue'; import contentViewer from '~/vue_shared/components/content_viewer/content_viewer.vue';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import mountComponent from 'spec/helpers/vue_mount_component_helper';
import { GREEN_BOX_IMAGE_URL } from 'spec/test_constants'; import { GREEN_BOX_IMAGE_URL } from 'spec/test_constants';
import '~/behaviors/markdown/render_gfm';
describe('ContentViewer', () => { describe('ContentViewer', () => {
let vm; let vm;
...@@ -29,6 +30,7 @@ describe('ContentViewer', () => { ...@@ -29,6 +30,7 @@ describe('ContentViewer', () => {
path: 'test.md', path: 'test.md',
content: '* Test', content: '* Test',
projectPath: 'testproject', projectPath: 'testproject',
type: 'markdown',
}); });
const previewContainer = vm.$el.querySelector('.md-previewer'); const previewContainer = vm.$el.querySelector('.md-previewer');
...@@ -44,6 +46,7 @@ describe('ContentViewer', () => { ...@@ -44,6 +46,7 @@ describe('ContentViewer', () => {
createComponent({ createComponent({
path: GREEN_BOX_IMAGE_URL, path: GREEN_BOX_IMAGE_URL,
fileSize: 1024, fileSize: 1024,
type: 'image',
}); });
setTimeout(() => { setTimeout(() => {
......
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