Last active
August 29, 2015 14:13
-
-
Save filipgorczynski/aadadbe1bc54e84eff13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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