locize
Version:
This package adds the incontext editor to your i18next setup.
92 lines (89 loc) • 4.32 kB
JavaScript
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
import { setImplementation, parseTree } from './parser.js';
import { createObserver } from './observer.js';
import { startMouseTracking } from './ui/mouseDistance.js';
import { initDragElement, initResizeElement } from './ui/popup.js';
import { popupId, Popup } from './ui/elements/popup.js';
import { getIframeUrl } from './vars.js';
import './api/handleEditKey.js';
import './api/handleCommitKey.js';
import './api/handleCommitKeys.js';
import './api/handleConfirmInitialized.js';
import './api/handleRequestPopupChanges.js';
import './api/handleRequestResourceBundle.js';
import './api/handleSelectedKeys.js';
import './api/handleIsLocizeEnabled.js';
import './api/handleSendMatchedUninstrumented.js';
import { api } from './api/postMessage.js';
import { getQsParameterByName, isInIframe } from './utils.js';
import '@babel/runtime/helpers/esm/toConsumableArray';
import { getImplementation } from './implementations/dummyImplementation.js';
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(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 = 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 || 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);
api.config = config;
api.init(implementation);
setImplementation(implementation);
implementation === null || implementation === void 0 || implementation.bindLanguageChange(function (lng) {
api.sendCurrentTargetLanguage(implementation.getLng());
});
function continueToStart() {
if (!isInIframe && !showInContext) return;
var observer = createObserver(document.body, function (eles) {
eles.forEach(function (ele) {
parseTree(ele);
});
api.sendCurrentParsedContent();
});
observer.start();
startMouseTracking(observer);
if (!isInIframe && !document.getElementById(popupId)) {
document.body.append(Popup(getIframeUrl(), function () {
api.requestInitialize(config);
}));
initDragElement();
initResizeElement();
}
if (typeof window !== 'undefined') {
var oldHref = window.document.location.href;
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;
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 () {
return continueToStart();
});
}
export { start };