locize
Version:
This package adds the incontext editor to your i18next setup.
132 lines (125 loc) • 5.98 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var parser = require('./parser.js');
var observer = require('./observer.js');
var mouseDistance = require('./ui/mouseDistance.js');
var popup$1 = require('./ui/popup.js');
var popup = require('./ui/elements/popup.js');
var vars = require('./vars.js');
require('./api/handleEditKey.js');
require('./api/handleCommitKey.js');
require('./api/handleCommitKeys.js');
require('./api/handleConfirmInitialized.js');
require('./api/handleRequestPopupChanges.js');
require('./api/handleRequestResourceBundle.js');
require('./api/handleSelectedKeys.js');
require('./api/handleIsLocizeEnabled.js');
require('./api/handleSendMatchedUninstrumented.js');
var postMessage = require('./api/postMessage.js');
var utils = require('./utils.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/typeof');
var dummyImplementation$1 = require('./implementations/dummyImplementation.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty__default["default"](e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var dummyImplementation = dummyImplementation$1.getImplementation();
function start() {
var implementation = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : dummyImplementation;
var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
show: false,
qsProp: 'incontext'
};
if (typeof document === 'undefined') return;
var showInContext = opt.show || utils.getQsParameterByName(opt.qsProp || 'incontext') === 'true';
var scriptEle = document.getElementById('locize');
var config = {};
['projectId', 'version'].forEach(function (attr) {
if (!scriptEle) return;
var value = scriptEle.getAttribute(attr.toLowerCase()) || scriptEle.getAttribute('data-' + attr.toLowerCase());
if (value === 'true') value = true;
if (value === 'false') value = false;
if (value !== undefined && value !== null) config[attr] = value;
});
config = _objectSpread(_objectSpread(_objectSpread({}, implementation.getLocizeDetails()), config), opt);
postMessage.api.config = config;
postMessage.api.init(implementation);
parser.setImplementation(implementation);
implementation === null || implementation === void 0 || implementation.bindLanguageChange(function (lng) {
postMessage.api.sendCurrentTargetLanguage(implementation.getLng());
});
function continueToStart() {
if (!utils.isInIframe && !showInContext) return;
var observer$1 = observer.createObserver(document.body, function (eles) {
eles.forEach(function (ele) {
parser.parseTree(ele);
});
postMessage.api.sendCurrentParsedContent();
});
observer$1.start();
mouseDistance.startMouseTracking(observer$1);
if (!utils.isInIframe && !document.getElementById(popup.popupId)) {
var popupEl = popup.Popup(vars.getIframeUrl(), function () {
var _document$getElementB;
postMessage.api.source = (_document$getElementB = document.getElementById('i18next-editor-iframe')) === null || _document$getElementB === void 0 ? void 0 : _document$getElementB.contentWindow;
postMessage.api.initialized = false;
if (postMessage.api.initInterval) {
clearInterval(postMessage.api.initInterval);
delete postMessage.api.initInterval;
}
postMessage.api.requestInitialize(config);
});
document.documentElement.append(popupEl);
popup$1.initDragElement();
popup$1.initResizeElement();
if (typeof MutationObserver === 'function') {
var MAX_REATTACHMENTS = 5;
var WATCH_DURATION_MS = 10000;
var reattachments = 0;
var watcher = new MutationObserver(function () {
if (document.getElementById(popup.popupId)) return;
if (reattachments >= MAX_REATTACHMENTS) {
watcher.disconnect();
return;
}
reattachments++;
document.documentElement.append(popupEl);
});
watcher.observe(document.documentElement, {
childList: true,
subtree: true
});
setTimeout(function () {
return watcher.disconnect();
}, WATCH_DURATION_MS);
}
}
if (typeof window !== 'undefined') {
var oldHref = window.document.location.href;
postMessage.api.sendHrefchanged(oldHref);
var bodyList = window.document.querySelector('body');
var _observer = new window.MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (oldHref !== window.document.location.href) {
oldHref = window.document.location.href;
postMessage.api.sendHrefchanged(oldHref);
}
});
});
var _config = {
childList: true,
subtree: true
};
_observer.observe(bodyList, _config);
}
}
if (document.body) return continueToStart();
if (typeof window !== 'undefined') {
window.addEventListener('load', function () {
continueToStart();
});
}
}
exports.start = start;