UNPKG

locize

Version:

This package adds the incontext editor to your i18next setup.

124 lines (121 loc) 5.5 kB
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 '@babel/runtime/helpers/esm/typeof'; 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)) { var popupEl = Popup(getIframeUrl(), function () { var _document$getElementB; api.source = (_document$getElementB = document.getElementById('i18next-editor-iframe')) === null || _document$getElementB === void 0 ? void 0 : _document$getElementB.contentWindow; api.initialized = false; if (api.initInterval) { clearInterval(api.initInterval); delete api.initInterval; } api.requestInitialize(config); }); document.documentElement.append(popupEl); initDragElement(); 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(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; 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 () { continueToStart(); }); } } export { start };