locize
Version:
This package adds the incontext editor to your i18next setup.
209 lines (202 loc) • 9.89 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var i18nextSubliminal = require('i18next-subliminal');
var store = require('./store.js');
var uninstrumentedStore = require('./uninstrumentedStore.js');
var vars = require('./vars.js');
var utils = require('./utils.js');
require('./shims/uniqueID.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 currentSourceLng;
var i18n;
var ignoreMergedEleUniqueIds = [];
function setImplementation(impl) {
i18n = impl;
}
function walk(node, func) {
if (node.dataset && node.dataset.i18nextEditorElement === 'true') return;
func(node);
var instr = store.store.get(node.uniqueID);
var uninstr = uninstrumentedStore.uninstrumentedStore.get(node.uniqueID);
if (instr || uninstr) {
var _node$parentElement;
var id = (_node$parentElement = node.parentElement) === null || _node$parentElement === void 0 ? void 0 : _node$parentElement.uniqueID;
uninstrumentedStore.uninstrumentedStore.remove(id, node.parentElement);
}
var children = node.childNodes;
for (var i = 0; i < children.length; i++) {
walk(children[i], func);
}
}
function extractHiddenMeta(id, type, meta, children) {
var _i18n, _i18n2, _i18n3;
var invisibleMeta = meta.invisibleMeta,
text = meta.text;
if (!invisibleMeta || !invisibleMeta.key || !invisibleMeta.ns) return;
if (!currentSourceLng) currentSourceLng = i18n.getSourceLng();
return _objectSpread(_objectSpread({
eleUniqueID: id,
textType: type,
children: children && children.map ? children.map(function (c) {
return c.childIndex;
}).join(',') : null,
qualifiedKey: "".concat(invisibleMeta.ns, ":").concat(invisibleMeta.key)
}, invisibleMeta), {}, {
extractedText: text,
i18nTargetLng: (_i18n = i18n) === null || _i18n === void 0 ? void 0 : _i18n.getLng(),
i18nSourceLng: currentSourceLng,
i18nRawText: _defineProperty__default["default"](_defineProperty__default["default"]({}, "".concat(invisibleMeta.lng), invisibleMeta.source === 'translation' && i18n ? (_i18n2 = i18n) === null || _i18n2 === void 0 ? void 0 : _i18n2.getResource(invisibleMeta.lng, invisibleMeta.ns, invisibleMeta.key) : null), "".concat(currentSourceLng), invisibleMeta.source === 'translation' && i18n ? (_i18n3 = i18n) === null || _i18n3 === void 0 ? void 0 : _i18n3.getResource(currentSourceLng, invisibleMeta.ns, invisibleMeta.key) : null)
});
}
function extractNodeMeta(id, type) {
var _i18n4, _i18n5, _i18n6, _i18n7, _i18n8;
var nodeMeta = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var text = arguments.length > 3 ? arguments[3] : undefined;
var children = arguments.length > 4 ? arguments[4] : undefined;
var meta = nodeMeta[type];
if (!meta) return;
if (!currentSourceLng) currentSourceLng = i18n.getSourceLng();
var i18nTargetLng = i18n.getLng();
return {
eleUniqueID: id,
textType: type,
children: children && children.map ? children.map(function (c) {
return c.childIndex;
}).join(',') : null,
qualifiedKey: meta.key && (meta.ns || (_i18n4 = i18n) !== null && _i18n4 !== void 0 && _i18n4.getDefaultNS()) ? "".concat(meta.ns || ((_i18n5 = i18n) === null || _i18n5 === void 0 ? void 0 : _i18n5.getDefaultNS()), ":").concat(meta.key) : null,
key: meta.key,
ns: meta.ns || ((_i18n6 = i18n) === null || _i18n6 === void 0 ? void 0 : _i18n6.getDefaultNS()),
extractedText: text,
i18nTargetLng: i18nTargetLng,
i18nSourceLng: currentSourceLng,
i18nRawText: _defineProperty__default["default"](_defineProperty__default["default"]({}, "".concat(i18nTargetLng), i18n && meta.ns && meta.key ? ((_i18n7 = i18n) === null || _i18n7 === void 0 ? void 0 : _i18n7.getResource(i18nTargetLng, meta.ns, meta.key)) || text : text), "".concat(currentSourceLng), i18n && meta.ns && meta.key ? (_i18n8 = i18n) === null || _i18n8 === void 0 ? void 0 : _i18n8.getResource(currentSourceLng, meta.ns, meta.key) : null)
};
}
function containsOnlySpaces(str) {
return /^\s*$/.test(str);
}
function storeIfQualifiedKey(id, subliminal, type, nodeI18nMeta, node, children, txt) {
var stored = store.store.get(id);
var storedMeta = stored && stored.keys["".concat(type)] || {};
var typeMeta = nodeI18nMeta["".concat(type)] || {};
if (!typeMeta.key && storedMeta.key) typeMeta.key = storedMeta.key;
if (!typeMeta.ns && storedMeta.ns) typeMeta.ns = storedMeta.ns;
nodeI18nMeta["".concat(type)] = typeMeta;
var meta = extractNodeMeta(id, type, nodeI18nMeta, txt, children);
if (meta.qualifiedKey) {
store.store.save(id, null, type, meta, node, children);
uninstrumentedStore.uninstrumentedStore.removeKey(id, type, node);
} else {
uninstrumentedStore.uninstrumentedStore.save(id, type, node, txt);
}
}
function handleNode(node) {
if (vars.ignoreElements.indexOf(node.nodeName) > -1) return;
var nodeI18nMeta = utils.getI18nMetaFromNode(node);
var usedSubliminalForText = false;
if (node.childNodes && !ignoreMergedEleUniqueIds.includes(node.uniqueID)) {
var merge = [];
node.childNodes.forEach(function (child, i) {
if (merge.length && child.nodeName !== '#text') {
ignoreMergedEleUniqueIds.push(child.uniqueID);
merge.push({
childIndex: i,
child: child
});
}
if (child.nodeName !== '#text') return;
var txt = child.textContent;
if (containsOnlySpaces(txt)) return;
var hasHiddenMeta = i18nextSubliminal.containsHiddenMeta(txt);
var hasHiddenStartMarker = i18nextSubliminal.containsHiddenStartMarker(txt);
if (hasHiddenMeta) usedSubliminalForText = true;
if (hasHiddenStartMarker && hasHiddenMeta) {
var meta = i18nextSubliminal.unwrap(txt);
uninstrumentedStore.uninstrumentedStore.remove(node.uniqueID, node);
store.store.save(node.uniqueID, meta.invisibleMeta, 'text', extractHiddenMeta(node.uniqueID, 'text', meta), node);
} else if (hasHiddenStartMarker) {
merge.push({
childIndex: i,
child: child,
text: txt
});
} else if (merge.length && !hasHiddenMeta) {
merge.push({
childIndex: i,
child: child,
text: txt
});
} else if (merge.length && hasHiddenMeta) {
merge.push({
childIndex: i,
child: child,
text: txt
});
var _meta = i18nextSubliminal.unwrap(merge.reduce(function (mem, item) {
return mem + item.text;
}, ''));
uninstrumentedStore.uninstrumentedStore.removeKey(node.uniqueID, 'html', node, txt);
store.store.save(node.uniqueID, _meta.invisibleMeta, 'html', extractHiddenMeta(node.uniqueID, 'html', _meta, merge), node, merge);
merge = [];
}
});
if (!usedSubliminalForText) {
node.childNodes.forEach(function (child, i) {
if (merge.length && child.nodeName !== '#text') {
ignoreMergedEleUniqueIds.push(child.uniqueID);
}
var txt = child.textContent;
if (nodeI18nMeta && nodeI18nMeta.html && i < node.childNodes.length - 1) {
merge.push({
childIndex: i,
child: child,
text: txt
});
} else if (nodeI18nMeta && nodeI18nMeta.html && i === node.childNodes.length - 1) {
merge.push({
childIndex: i,
child: child,
text: txt
});
storeIfQualifiedKey(node.uniqueID, null, 'html', nodeI18nMeta, node, merge, node.innerHTML);
merge = [];
} else if (txt) {
if (nodeI18nMeta && nodeI18nMeta.text) {
storeIfQualifiedKey(node.uniqueID, null, 'text', nodeI18nMeta, node, undefined, txt);
} else if (child.nodeName === '#text' && !containsOnlySpaces(txt)) {
uninstrumentedStore.uninstrumentedStore.save(node.uniqueID, 'text', node, txt);
}
}
});
}
}
if (!node.getAttribute) return;
vars.validAttributes.forEach(function (attr) {
var txt = node.getAttribute(attr);
if (i18nextSubliminal.containsHiddenMeta(txt)) {
var meta = i18nextSubliminal.unwrap(txt);
uninstrumentedStore.uninstrumentedStore.removeKey(node.uniqueID, attr, node);
store.store.save(node.uniqueID, meta.invisibleMeta, attr, extractHiddenMeta(node.uniqueID, "".concat(attr), meta), node);
} else if (txt) {
if (nodeI18nMeta && nodeI18nMeta[attr]) {
storeIfQualifiedKey(node.uniqueID, null, attr, nodeI18nMeta, node, undefined, txt);
} else {
uninstrumentedStore.uninstrumentedStore.save(node.uniqueID, attr, node, txt);
}
}
});
}
function parseTree(node) {
currentSourceLng = undefined;
walk(node, handleNode);
store.store.clean();
ignoreMergedEleUniqueIds = [];
return store.store.data;
}
exports.extractNodeMeta = extractNodeMeta;
exports.parseTree = parseTree;
exports.setImplementation = setImplementation;