agora-edu-core
Version:
Core APIs for building an online classroom
171 lines (167 loc) • 5.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.UUAparser = exports.RteRole2EduRole = exports.EduRole2RteRole = exports.DevicePlatform = exports.CustomBtoa = void 0;
exports.getImageSize = getImageSize;
exports.number2Percent = exports.getPlatform = void 0;
var _isNaN = _interopRequireDefault(require("lodash/isNaN"));
var _uaParserJs = require("ua-parser-js");
var _ = require("..");
var _UUAparser;
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const EduRole2RteRole = (roomType, roleType) => {
if (roleType === _.EduRoleTypeEnum.teacher) {
return 'host';
}
if (roleType === _.EduRoleTypeEnum.assistant) {
return 'assistant';
}
if ([_.EduRoomTypeEnum.Room1v1Class, _.EduRoomTypeEnum.RoomStudy].includes(roomType) && roleType === _.EduRoleTypeEnum.student) {
return 'broadcaster';
}
if (roleType === _.EduRoleTypeEnum.invisible) {
return 'invisible';
}
if (roleType === _.EduRoleTypeEnum.observer) {
return 'observer';
}
return 'audience';
};
exports.EduRole2RteRole = EduRole2RteRole;
const RteRole2EduRole = (roomType, role) => {
if (role === 'host') {
return _.EduRoleTypeEnum.teacher;
}
if (role === 'assistant') {
return _.EduRoleTypeEnum.assistant;
}
//下台学生 与班型无关
if (role === 'audience') {
return _.EduRoleTypeEnum.student;
}
//上台学生 与班型无关
if (role === 'broadcaster') {
return _.EduRoleTypeEnum.student;
}
if (role === 'invisible') {
return _.EduRoleTypeEnum.invisible;
}
if (role === 'observer') {
return _.EduRoleTypeEnum.observer;
}
return _.EduRoleTypeEnum.none;
};
exports.RteRole2EduRole = RteRole2EduRole;
const number2Percent = function (v) {
let fixed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return !(0, _isNaN.default)(Number(v * 100)) ? Number(v * 100).toFixed(fixed) + '%' : '0%';
};
exports.number2Percent = number2Percent;
const CustomBtoa = input => {
const keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
let output = '';
let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
let i = 0;
while (i < input.length) {
chr1 = input[i++];
chr2 = i < input.length ? input[i++] : Number.NaN; // Not sure if the index
chr3 = i < input.length ? input[i++] : Number.NaN; // checks are needed here
enc1 = chr1 >> 2;
enc2 = (chr1 & 3) << 4 | chr2 >> 4;
enc3 = (chr2 & 15) << 2 | chr3 >> 6;
enc4 = chr3 & 63;
if ((0, _isNaN.default)(chr2)) {
enc3 = enc4 = 64;
} else if ((0, _isNaN.default)(chr3)) {
enc4 = 64;
}
output += keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
}
return output;
};
exports.CustomBtoa = CustomBtoa;
var UAType = /*#__PURE__*/function (UAType) {
UAType["console"] = "console";
UAType["mobile"] = "mobile";
UAType["tablet"] = "tablet";
UAType["smarttv"] = "starttv";
UAType["wearable"] = "wearable";
UAType["embedded"] = "embedded";
return UAType;
}(UAType || {});
let DevicePlatform = exports.DevicePlatform = /*#__PURE__*/function (DevicePlatform) {
DevicePlatform[DevicePlatform["WEB"] = 1] = "WEB";
DevicePlatform[DevicePlatform["MACOS"] = 2] = "MACOS";
DevicePlatform[DevicePlatform["WINDOWS"] = 3] = "WINDOWS";
DevicePlatform[DevicePlatform["IOS"] = 4] = "IOS";
DevicePlatform[DevicePlatform["ANDROID"] = 5] = "ANDROID";
DevicePlatform[DevicePlatform["H5"] = 6] = "H5";
return DevicePlatform;
}({});
class UUAparser {
static get getType() {
return UUAparser._UAParserInstance.getDevice().type;
}
static get mobileBrowser() {
const isTablet = navigator.maxTouchPoints && navigator.maxTouchPoints > 2 && /MacIntel/.test(navigator.platform) || 'ontouchend' in document;
return UUAparser.getType === UAType.mobile || UUAparser.getType === UAType.tablet || isTablet;
}
}
exports.UUAparser = UUAparser;
_UUAparser = UUAparser;
UUAparser._userAgent = window.navigator.userAgent;
UUAparser._UAParserInstance = new _uaParserJs.UAParser(_UUAparser._userAgent);
const getPlatform = () => {
let platform = DevicePlatform.WEB;
const isElectron = window && window.process && window.process.versions && window.process.versions['electron'];
if (isElectron) {
if (process.platform == 'darwin') {
platform = DevicePlatform.MACOS;
}
if (process.platform == 'win32') {
platform = DevicePlatform.WINDOWS;
}
} else if (UUAparser.mobileBrowser) {
platform = DevicePlatform.H5;
} else {
platform = DevicePlatform.WEB;
}
return platform;
};
exports.getPlatform = getPlatform;
function getImageSize(file) {
const image = new Image();
const url = URL.createObjectURL(file);
const {
innerWidth,
innerHeight
} = window;
// shrink the image a little to fit the screen
const maxWidth = innerWidth * 0.8;
const maxHeight = innerHeight * 0.8;
image.src = url;
return new Promise(resolve => {
image.onload = () => {
URL.revokeObjectURL(url);
const {
width,
height
} = image;
let scale = 1;
if (width > maxWidth || height > maxHeight) {
scale = Math.min(maxWidth / width, maxHeight / height);
}
resolve({
width: Math.floor(width * scale),
height: Math.floor(height * scale)
});
};
image.onerror = () => {
resolve({
width: innerWidth,
height: innerHeight
});
};
});
}