debian-mirror-gitlab/app/assets/javascripts/blob/sketch/index.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

68 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
import JSZip from 'jszip';
2022-10-11 01:57:18 +05:30
import axios from '~/lib/utils/axios_utils';
2019-07-31 22:56:46 +05:30
import { __ } from '~/locale';
2017-08-17 22:00:37 +05:30
export default class SketchLoader {
constructor(container) {
this.container = container;
this.loadingIcon = this.container.querySelector('.js-loading-icon');
2022-10-11 01:57:18 +05:30
this.load().catch(() => {
this.error();
});
2017-08-17 22:00:37 +05:30
}
2022-10-11 01:57:18 +05:30
async load() {
const zipContents = await this.getZipContents();
const previewContents = await zipContents.files['previews/preview.png'].async('uint8array');
const blob = new Blob([previewContents], {
type: 'image/png',
});
2017-08-17 22:00:37 +05:30
2022-10-11 01:57:18 +05:30
this.render(window.URL.createObjectURL(blob));
2017-08-17 22:00:37 +05:30
}
2022-10-11 01:57:18 +05:30
async getZipContents() {
const { data } = await axios.get(this.container.dataset.endpoint, {
responseType: 'arraybuffer',
2017-08-17 22:00:37 +05:30
});
2022-10-11 01:57:18 +05:30
return JSZip.loadAsync(data);
2017-08-17 22:00:37 +05:30
}
render(previewUrl) {
const previewLink = document.createElement('a');
const previewImage = document.createElement('img');
previewLink.href = previewUrl;
previewLink.target = '_blank';
previewImage.src = previewUrl;
2018-11-08 19:23:39 +05:30
previewImage.className = 'img-fluid';
2017-08-17 22:00:37 +05:30
previewLink.appendChild(previewImage);
this.container.appendChild(previewLink);
this.removeLoadingIcon();
}
error() {
const errorMsg = document.createElement('p');
2020-07-28 23:09:34 +05:30
errorMsg.className = 'gl-mt-3 gl-mb-3 text-center';
2019-07-31 22:56:46 +05:30
errorMsg.textContent = __(`
2017-08-17 22:00:37 +05:30
Cannot show preview. For previews on sketch files, they must have the file format
introduced by Sketch version 43 and above.
2019-07-31 22:56:46 +05:30
`);
2017-08-17 22:00:37 +05:30
this.container.appendChild(errorMsg);
this.removeLoadingIcon();
}
removeLoadingIcon() {
if (this.loadingIcon) {
this.loadingIcon.remove();
}
}
}