Open plain text files in code app && plain text files thumbnail

This commit is contained in:
ClemDee
2019-08-13 16:09:06 +02:00
parent 3a8914aefa
commit f55c8c6b04
9 changed files with 190 additions and 25 deletions

View File

@@ -15,6 +15,7 @@ define([
};
var supportedTypes = [
'text/plain',
'image/png',
'image/jpeg',
'image/jpg',
@@ -23,7 +24,11 @@ define([
'application/pdf'
];
Thumb.isSupportedType = function (type) {
Thumb.isSupportedType = function (file) {
var type = file.type;
if (Util.isPlainTextFile(file.type, file.name)) {
type = "text/plain";
}
return supportedTypes.some(function (t) {
return type.indexOf(t) !== -1;
});
@@ -164,6 +169,26 @@ define([
});
});
};
Thumb.fromPlainTextBlob = function (blob, cb) {
var canvas = document.createElement("canvas");
canvas.width = canvas.height = Thumb.dimension;
var reader = new FileReader();
reader.addEventListener('loadend', function (e) {
var content = e.srcElement.result;
var lines = content.split("\n");
var canvasContext = canvas.getContext("2d");
var fontSize = 4;
canvas.height = (lines.length) * (fontSize + 1);
canvasContext.font = fontSize + 'px monospace';
lines.forEach(function (text, i) {
canvasContext.fillText(text, 5, i * (fontSize + 1));
});
var D = getResizedDimensions(canvas, "txt");
Thumb.fromCanvas(canvas, D, cb);
});
reader.readAsText(blob);
};
Thumb.fromBlob = function (blob, cb) {
if (blob.type.indexOf('video/') !== -1) {
return void Thumb.fromVideoBlob(blob, cb);
@@ -171,6 +196,9 @@ define([
if (blob.type.indexOf('application/pdf') !== -1) {
return void Thumb.fromPdfBlob(blob, cb);
}
if (Util.isPlainTextFile(blob.type, blob.name)) {
return void Thumb.fromPlainTextBlob(blob, cb);
}
Thumb.fromImageBlob(blob, cb);
};