Skip to content

Instantly share code, notes, and snippets.

@mlegenhausen
Created August 7, 2013 15:46
Show Gist options
  • Save mlegenhausen/6175299 to your computer and use it in GitHub Desktop.
Save mlegenhausen/6175299 to your computer and use it in GitHub Desktop.
WYMeditor directive for AngularJS
angular.module('directives.wymeditor', [])
.constant('WYM_EDITOR_OPTIONS', {
lang: 'de',
skin: 'compact',
iframeBasePath: 'vendor/wymeditor/iframe/pretty/',
logoHtml: '',
containersHtml: '',
classesHtml: '',
toolsItems: [
{'name': 'Bold', 'title': 'Strong', 'css': 'wym_tools_strong'},
{'name': 'Italic', 'title': 'Emphasis', 'css': 'wym_tools_emphasis'},
{'name': 'InsertOrderedList', 'title': 'Ordered_List', 'css': 'wym_tools_ordered_list'},
{'name': 'InsertUnorderedList', 'title': 'Unordered_List', 'css': 'wym_tools_unordered_list'},
{'name': 'Undo', 'title': 'Undo', 'css': 'wym_tools_undo'},
{'name': 'Redo', 'title': 'Redo', 'css': 'wym_tools_redo'},
{'name': 'CreateLink', 'title': 'Link', 'css': 'wym_tools_link'},
{'name': 'Unlink', 'title': 'Unlink', 'css': 'wym_tools_unlink'},
{'name': 'InsertTable', 'title': 'Table', 'css': 'wym_tools_table'},
{'name': 'Paste', 'title': 'Paste_From_Word', 'css': 'wym_tools_paste'}
]
})
.directive('znWymEditor', function(WYM_EDITOR_OPTIONS) {
return {
require: '^ngModel',
link: function(scope, element, attrs, controller) {
var options = _.defaults(scope[attrs.znWymEditor], WYM_EDITOR_OPTIONS);
_.extend(options, {
postInit: function(editor) {
editor._html(controller.$modelValue);
// Throttled update method for the content
var update = _.throttle(function update() {
scope.$apply(function() {
controller.$setViewValue(editor._html());
});
}, 250);
// Bind all text area events
var $doc = $(editor._doc);
$doc.bind('keyup', update);
$doc.bind('keydown', update);
// Bind all editor events
var $box = $(editor._box);
$box.find(editor._options.toolSelector).bind('click', update);
$box.find(editor._options.containerSelector).bind('click', update);
scope.$watch(attrs.ngModel, function(content) {
if (content !== editor._html()) {
editor._html(content);
}
});
}
});
$(element).wymeditor(options);
}
};
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment