@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
95 lines • 4.37 kB
JavaScript
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import * as _ from "underscore";
import { InStringPlaceholder } from "@aurigma/design-atoms-model/Product/Items/InStringPlaceholder";
import { VariableItemHelper } from "./VariableItemHelper";
import * as Utils from "@aurigma/design-atoms-model/Utils/Utils";
var InStringPlaceholderConverter = /** @class */ (function () {
function InStringPlaceholderConverter() {
}
InStringPlaceholderConverter.prototype.convert = function (textItem) {
var replacedStrings = this._replaceInterpolationStrings(textItem);
if (replacedStrings.isEmpty)
return false;
textItem.originalText = replacedStrings.inStringText;
textItem.text = replacedStrings.inStringText;
var existedPlaceholders = textItem.placeholders;
textItem.placeholders.addRange(existedPlaceholders.concat(replacedStrings.placeholders));
return true;
};
InStringPlaceholderConverter.prototype._replaceInterpolationStrings = function (textItem) {
var e_1, _a;
var variables = VariableItemHelper.getInterpolationVariables(textItem);
var replacedStrings = new ReplacedStrings();
if (_.isEmpty(variables))
return replacedStrings;
var inStringText = textItem.text;
try {
for (var variables_1 = __values(variables), variables_1_1 = variables_1.next(); !variables_1_1.done; variables_1_1 = variables_1.next()) {
var variable = variables_1_1.value;
var id = "[#" + variable.name + "]";
var interpolationPlaceholderId = "{{" + variable.name + "}}";
inStringText = Utils.isValidXml(textItem.text)
? replaceRichText(inStringText, interpolationPlaceholderId, id)
: replaceText(inStringText, interpolationPlaceholderId, id);
var placeholder = new InStringPlaceholder(id, variable.name, variable.mask, variable.name, variable.values);
replacedStrings.placeholders.push(placeholder);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (variables_1_1 && !variables_1_1.done && (_a = variables_1.return)) _a.call(variables_1);
}
finally { if (e_1) throw e_1.error; }
}
replacedStrings.inStringText = inStringText;
return replacedStrings;
};
return InStringPlaceholderConverter;
}());
export { InStringPlaceholderConverter };
var ReplacedStrings = /** @class */ (function () {
function ReplacedStrings() {
this.placeholders = [];
}
Object.defineProperty(ReplacedStrings.prototype, "isEmpty", {
get: function () {
return _.isEmpty(this.placeholders);
},
enumerable: true,
configurable: true
});
return ReplacedStrings;
}());
function replaceRichText(text, searchValue, replaceValue) {
var parser = new DOMParser();
var xmlDocument = parser.parseFromString("<body>" + text + "</body>", 'text/xml');
var body = xmlDocument.getElementsByTagName('body')[0];
if (!body.textContent.includes(searchValue))
return text;
var currentNode = body;
var foundNode = null;
while (currentNode !== foundNode) {
currentNode = _.find(currentNode.childNodes, function (childNode) { return childNode.textContent.includes(searchValue); });
if (!currentNode)
return text;
if (_.isEmpty(currentNode.childNodes))
foundNode = currentNode;
}
foundNode.textContent = foundNode.textContent.replace(searchValue, replaceValue);
return body.innerHTML;
}
function replaceText(text, searchValue, replaceValue) {
return text.replace(searchValue, replaceValue);
}
//# sourceMappingURL=InStringPlaceholderConverter.js.map