Open plain text files in code app && plain text files thumbnail
This commit is contained in:
@@ -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);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user