@zohodesk/dot
Version:
In this Library, we Provide Some Basic Components to Build Your Application
287 lines (239 loc) • 7.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ZoomEvent = exports.Zoom = exports.SUPPORTED_FILE_EXTENSIONS = exports.FILE_EXTENSIONS = void 0;
exports.checkAudioUrlValidity = checkAudioUrlValidity;
exports.checkFileSourcesValidation = checkFileSourcesValidation;
exports.checkImageValidity = checkImageValidity;
exports.checkVideoUrlValidity = checkVideoUrlValidity;
exports.isValidDocument = isValidDocument;
var _Attachment = require("./Attachment");
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
var Zoom = /*#__PURE__*/function () {
function Zoom() {
_classCallCheck(this, Zoom);
var matrix = [1, 0, 0, 1, 0, 0]; // current view transform
this.m = matrix; // alias
this.scale = 1; // current scale
this.pos = {
x: 0,
y: 0
}; // current position of origin
this.dirty = true;
}
_createClass(Zoom, [{
key: "applyTo",
value: function applyTo(el) {
var dirty = this.dirty,
m = this.m;
if (dirty) {
this.update();
}
el.style.transform = "matrix(".concat(m[0], ",").concat(m[1], ",").concat(m[2], ",").concat(m[3], ",").concat(m[4], ",").concat(m[5], ")");
el.style.transformOrigin = '0 0';
}
}, {
key: "update",
value: function update() {
// m[4], m[5] - need to set transforms based on positions. Currently set to 0 to zoom based on center
var m = this.m,
scale = this.scale,
pos = this.pos;
this.dirty = false;
m[3] = m[0] = scale;
m[2] = m[1] = 0;
m[4] = pos.x;
m[5] = pos.y;
}
}, {
key: "pan",
value: function pan(amount) {
if (this.dirty) {
this.update();
}
this.pos.x += amount.x;
this.pos.y += amount.y;
this.dirty = true;
}
}, {
key: "scaleAt",
value: function scaleAt(at, amount) {
// at in screen coords
var dirty = this.dirty,
pos = this.pos;
if (dirty) {
this.update();
}
this.scale *= amount;
this.pos.x = at.x - (at.x - pos.x) * amount;
this.pos.y = at.y - (at.y - pos.y) * amount;
this.dirty = true;
}
}]);
return Zoom;
}();
exports.Zoom = Zoom;
;
var ZoomEvent = /*#__PURE__*/function () {
function ZoomEvent() {
_classCallCheck(this, ZoomEvent);
this.pos = [];
}
_createClass(ZoomEvent, [{
key: "on",
value: function on(dir, ele, zoom, event) {
if (dir === 'in') {
var x = event.pageX - ele.width / 2;
var y = event.pageY - ele.height / 2;
this.pos.push({
x: x,
y: y
});
zoom.scaleAt({
x: x,
y: y
}, 1.6);
zoom.applyTo(ele);
} else if (this.pos.length) {
var _this$pos$pop = this.pos.pop(),
_x = _this$pos$pop.x,
_y = _this$pos$pop.y;
zoom.scaleAt({
x: _x,
y: _y
}, 1 / 1.6);
zoom.applyTo(ele);
}
event.preventDefault();
}
}]);
return ZoomEvent;
}();
exports.ZoomEvent = ZoomEvent;
function checkImageValidity(src) {
return new Promise(function (resolve) {
var img = new Image();
img.src = src;
img.onload = function () {
resolve(true);
};
img.onerror = function () {
resolve(false);
};
});
}
function checkVideoUrlValidity(url) {
return new Promise(function (resolve) {
var video = document.createElement('video');
video.src = url;
video.oncanplaythrough = function () {
return resolve(true);
};
video.onerror = function () {
return resolve(false);
};
});
}
function checkAudioUrlValidity(url) {
return new Promise(function (resolve) {
var audio = new Audio(url);
audio.oncanplaythrough = function () {
resolve(true);
};
audio.onerror = function () {
resolve(false);
};
});
}
;
function isValidDocument(url) {
if (!url) {
return false;
}
return true;
}
var FILE_EXTENSIONS = {
audio: ['mp3', 'wav', 'wma', 'aac', 'm4r', 'm4a', 'flac', 'aiff', 'alac', 'ogg', 'opus', 'amr', 'mid', 'midi'],
video: ['mp4', 'mkv', 'mov', 'mpeg', 'mpg', 'flv', 'wmv', 'avi', 'webm', 'ogv', 'm4v', '3gp', '3g2'],
document: ['doc', 'docx', 'docm', 'dot', 'dotx', 'dotm', 'odt', 'rtf', 'txt', 'md', 'pages', 'xls', 'xlsx', 'xlsm', 'xlsb', 'csv', 'tsv', 'ods', 'sxc', 'numbers', 'ppt', 'pptx', 'pps', 'ppsx', 'pot', 'potx', 'odp', 'sxi', 'key', 'pdf', 'xml', 'json', 'yaml', 'yml', 'log', 'eml', 'msg'],
image: ['jpeg', 'jpg', 'png', 'apng', 'gif', 'bmp', 'dib', 'tiff', 'tif', 'ico', 'svg', 'webp', 'heic', 'heif', 'jfif', 'pjpeg', 'pjp', 'avif']
};
exports.FILE_EXTENSIONS = FILE_EXTENSIONS;
var SUPPORTED_FILE_EXTENSIONS = {
image: ['jpeg', 'jpg', 'png', 'apng', 'gif', 'bmp', 'tiff', 'tif', 'ico', 'svg', 'heic', 'webp'],
doc: ['txt'],
pdf: ['pdf'],
html: ['html', 'htm', 'xhtml'],
ppt: ['ppt', 'pps', 'odp', 'sxi', 'pptx', 'ppsx', 'pot', 'potx', 'key'],
zip: ['rar', 'jar', 'zip'],
word: ['doc', 'docx', 'sxw', 'odt', 'docm', 'dot', 'dotm', 'dotx', 'rtf', 'pages'],
xml: ['xml'],
sheet: ['xls', 'xlsx', 'xlsm', 'xlsb', 'sxc', 'ods', 'csv', 'tsv', 'numbers'],
audio: ['mp3', 'wav', 'wma', 'aac', 'm4r', 'ogg', 'opus'],
video: ['mp4', 'mkv', 'mov', 'mpeg', 'flv', 'wmv', 'avi', 'webm', 'ogv'],
mail: ['eml', 'msg'],
linux: ['sh', 'bin'],
css: ['css'],
exe: ['exe'],
event: ['ics']
};
exports.SUPPORTED_FILE_EXTENSIONS = SUPPORTED_FILE_EXTENSIONS;
function checkFileSourcesValidation(_ref) {
var fileName = _ref.fileName,
viewURL = _ref.viewURL,
previewUrl = _ref.previewUrl,
allowedPreviewExtensionsData = _ref.allowedPreviewExtensionsData;
var extension = ((0, _Attachment.getExtensionFromFileName)(fileName) || '').toLowerCase();
if (!extension) {
return Promise.resolve({
isViewURLValid: false,
canZoom: false
});
}
var extensionSource = allowedPreviewExtensionsData || FILE_EXTENSIONS;
var fileExtensionValidation = function fileExtensionValidation(type) {
var list = extensionSource[type];
if (!Array.isArray(list)) return false;
return list.map(function (e) {
return e.toLowerCase();
}).includes(extension);
};
if (fileExtensionValidation('audio')) {
return checkAudioUrlValidity(viewURL).then(function (isURLValid) {
return {
isViewURLValid: isURLValid,
canZoom: false
};
});
}
if (fileExtensionValidation('video')) {
return checkVideoUrlValidity(viewURL).then(function (isURLValid) {
return {
isViewURLValid: isURLValid,
canZoom: false
};
});
}
if (fileExtensionValidation('document')) {
var isValid = isValidDocument(previewUrl, fileName);
return Promise.resolve({
isViewURLValid: isValid,
canZoom: false
});
}
if (fileExtensionValidation('image')) {
return checkImageValidity(viewURL).then(function (isURLValid) {
return {
isViewURLValid: isURLValid,
canZoom: isURLValid
};
});
}
return Promise.resolve({
isViewURLValid: false,
canZoom: false
});
}