Skip to content

Instantly share code, notes, and snippets.

@sarink
Created August 10, 2014 05:39
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 sarink/50bdf2434bb26a24753f to your computer and use it in GitHub Desktop.
Save sarink/50bdf2434bb26a24753f to your computer and use it in GitHub Desktop.
// Adds `ng-context-menu` for right click support
app.directive("ngContextMenu", function($parse) {
return function(scope, element, attrs) {
var fn = $parse(attrs.ngContextMenu);
element.bind("contextmenu", function(event) {
scope.$apply(function() {
event.preventDefault();
fn(scope, {$event:event});
});
});
};
});
// Adds `contenteditable` with two-way binding
app.directive("contenteditable", ["$sce", function($sce) {
return {
restrict: "A", // only activate on element attribute
require: "?ngModel", // get a hold of NgModelController
link: function(scope, element, attrs, ngModel) {
if (!ngModel) return; // do nothing if no ng-model
// Specify how UI should be updated
ngModel.$render = function() {
element.html($sce.getTrustedHtml(ngModel.$viewValue || ""));
};
// Listen for change events to enable binding
element.on("change blur keydown", function(event) {
if (event.keyCode === 13) document.execCommand("insertHTML", false, "<br><br>");
ngModel.$setViewValue(element.decodeHtmlEntities());
if (event.keyCode === 13) return false;
});
element.text(ngModel.$modelValue);
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment