Skip to content

Instantly share code, notes, and snippets.

@mandrasch
Created June 16, 2012 14:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mandrasch/2941550 to your computer and use it in GitHub Desktop.
Save mandrasch/2941550 to your computer and use it in GitHub Desktop.
PagedownBootstrap for AngularJS (directive)
/*
*usage: <textarea ui:pagedown-bootstrap ng:model="box.content"></textarea>
*/
myApp.directive('uiPagedownBootstrap', function() {
var nextId = 0;
return {
require: 'ngModel',
replace:true,
template:'<div class="pagedown-bootstrap-editor"></div>',
link:function (scope, iElement, iAttrs, ngModel) {
var editorUniqueId = nextId++;
iElement.html($('<div>'+
'<div class="wmd-panel">'+
'<div id="wmd-button-bar-'+editorUniqueId+'"></div>'+
'<textarea class="wmd-input" id="wmd-input-'+editorUniqueId+'">{{modelValue()}}'+
'</textarea>'+
'</div>'+
'<div id="wmd-preview-'+editorUniqueId+'" class="wmd-panel wmd-preview"></div>'+
'</div>'));
var converter = new Markdown.Converter();
var help = function () {
// 2DO: add nice modal dialog
alert("Do you need help?");
}
var editor = new Markdown.Editor(converter, "-"+editorUniqueId, {
handler: help
});
editor.run();
// local -> parent scope change (model)
$("#wmd-input-"+editorUniqueId).on('change',function(){
var rawContent = $(this).val();
ngModel.$setViewValue(rawContent);
scope.$apply();
});
// parent scope -> local change
scope.modelValue = function() {
console.log('modelvalue - ',ngModel.$viewValue);
return ngModel.$viewValue;
};
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment