noteEditor.directive.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. angular.module('kityminderEditor')
  2. .directive('noteEditor', ['valueTransfer', function(valueTransfer) {
  3. return {
  4. restrict: 'A',
  5. templateUrl: 'ui/directive/noteEditor/noteEditor.html',
  6. scope: {
  7. minder: '='
  8. },
  9. replace: true,
  10. controller: function($scope) {
  11. var minder = $scope.minder;
  12. var isInteracting = false;
  13. var cmEditor;
  14. $scope.codemirrorLoaded = function(_editor) {
  15. cmEditor = $scope.cmEditor = _editor;
  16. _editor.setSize('100%', '100%');
  17. };
  18. function updateNote() {
  19. var enabled = $scope.noteEnabled = minder.queryCommandState('note') != -1;
  20. var noteValue = minder.queryCommandValue('note') || '';
  21. if (enabled) {
  22. $scope.noteContent = noteValue;
  23. }
  24. isInteracting = true;
  25. $scope.$apply();
  26. isInteracting = false;
  27. }
  28. $scope.$watch('noteContent', function(content) {
  29. var enabled = minder.queryCommandState('note') != -1;
  30. if (content && enabled && !isInteracting) {
  31. minder.execCommand('note', content);
  32. }
  33. setTimeout(function() {
  34. cmEditor.refresh();
  35. });
  36. });
  37. var noteEditorOpen = function() {
  38. return valueTransfer.noteEditorOpen;
  39. };
  40. // 监听面板状态变量的改变
  41. $scope.$watch(noteEditorOpen, function(newVal, oldVal) {
  42. if (newVal) {
  43. setTimeout(function() {
  44. cmEditor.refresh();
  45. cmEditor.focus();
  46. });
  47. }
  48. $scope.noteEditorOpen = valueTransfer.noteEditorOpen;
  49. }, true);
  50. $scope.closeNoteEditor = function() {
  51. valueTransfer.noteEditorOpen = false;
  52. editor.receiver.selectAll();
  53. };
  54. minder.on('interactchange', updateNote);
  55. }
  56. }
  57. }]);