Skip to content

Instantly share code, notes, and snippets.

@filipgorczynski
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipgorczynski/aadadbe1bc54e84eff13 to your computer and use it in GitHub Desktop.
Save filipgorczynski/aadadbe1bc54e84eff13 to your computer and use it in GitHub Desktop.
(function () {
var koView = ko.views.manager.currentView.scimoz;
var currentPos = koView.currentPos;
try {
// Group operations into a single undo
koView.beginUndoAction();
// http://www.bennadel.com/blog/142-ask-ben-javascript-string-replace-method.htm
String.prototype.replaceAll = function(
strTarget, // The substring you want to replace
strSubString // The string you want to replace in.
) {
var strText = this;
var intIndexOfMatch = strText.indexOf(strTarget);
// Keep looping while an instance of the target string
// still exists in the string.
while (intIndexOfMatch != -1) {
// Relace out the current instance.
strText = strText.replace(strTarget, strSubString)
// Get the index of any next matching substring.
intIndexOfMatch = strText.indexOf(strTarget);
}
// Return the updated string with ALL the target strings
// replaced out with the new substring.
return strText;
};
var descriptionBuilder = function(text) {
var productName = text.replace(/^([^,]+),[^,]*/gim, '$1');
var pattern =
'@@@ Product Details:\n' +
'\n' +
'xxx\n' +
'\n' +
'@@@ Supplement Facts:\n' +
'Serving Size: xxx\n' +
'Serving Per Container: xxx\n' +
'\n' +
'Amount Per Serving:\n' +
'xxx\n' +
'\n' +
'@@@ Other Ingredients:\n' +
'xxx\n' +
'\n' +
'@@@ Recommended Use:\n' +
'xxx\n' +
'\n' +
'Warning:\n' +
'xxx\n';
return pattern.replaceAll('@@@', productName);
}
if (!!koView.selText) {
//Super Whey, Smakowy - 4589g
var newText = descriptionBuilder(koView.selText);
koView.replaceSel(newText);
};
} catch (e) {
alert(e);
}
finally {
// Must end undo action or may corrupt edit buffer
koView.endUndoAction();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment