locize
Version:
This package adds the incontext editor to your i18next setup.
55 lines (52 loc) • 2.47 kB
JavaScript
;
var postMessage = require('./postMessage.js');
var popup = require('../ui/elements/popup.js');
var CSS_LENGTH_RE = /^\d+(?:\.\d+)?(?:px|%|em|rem|vh|vw|ch|ex)$/i;
function isSafeCssLength(v) {
return typeof v === 'string' && CSS_LENGTH_RE.test(v);
}
function handler(payload) {
var containerStyle = payload.containerStyle;
if (containerStyle) {
var popup$1 = document.getElementById(popup.popupId);
if (!popup$1) return;
var storedPos = window.localStorage.getItem('locize_popup_pos');
if (storedPos) storedPos = JSON.parse(storedPos);
var storedSize = window.localStorage.getItem('locize_popup_size');
if (storedSize) storedSize = JSON.parse(storedSize);
if (storedSize && storedSize.height && storedSize.width) {
containerStyle.height = storedSize.height + 'px';
containerStyle.width = storedSize.width + 'px';
}
if (containerStyle.height && !isSafeCssLength(containerStyle.height)) {
delete containerStyle.height;
}
if (containerStyle.width && !isSafeCssLength(containerStyle.width)) {
delete containerStyle.width;
}
if (containerStyle.height) {
var diff = "calc(".concat(containerStyle.height, " - ").concat(popup$1.style.height, ")");
popup$1.style.setProperty('top', "calc(".concat(popup$1.style.top, " - ").concat(diff, ")"));
popup$1.style.setProperty('height', containerStyle.height);
}
if (containerStyle.width) {
var _diff = "calc(".concat(containerStyle.width, " - ").concat(popup$1.style.width, ")");
popup$1.style.setProperty('left', "calc(".concat(popup$1.style.left, " - ").concat(_diff, ")"));
popup$1.style.setProperty('width', containerStyle.width);
}
var MIN_VISIBLE = 40;
if (storedPos && typeof storedPos.top === 'number' && containerStyle.height) {
var maxTop = Math.max(0, window.innerHeight - MIN_VISIBLE);
var top = Math.max(0, Math.min(storedPos.top, maxTop));
popup$1.style.setProperty('top', top + 'px');
}
if (storedPos && typeof storedPos.left === 'number' && containerStyle.width) {
var width = parseInt(containerStyle.width, 10) || 0;
var minLeft = Math.min(0, MIN_VISIBLE - width);
var maxLeft = Math.max(0, window.innerWidth - MIN_VISIBLE);
var left = Math.max(minLeft, Math.min(storedPos.left, maxLeft));
popup$1.style.setProperty('left', left + 'px');
}
}
}
postMessage.api.addHandler('requestPopupChanges', handler);