addCommentEntry.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import App from '../../../base'
  2. let content = ''
  3. App.directive('addCommentEntry', () => ({
  4. restrict: 'E',
  5. templateUrl: 'addCommentEntryTemplate',
  6. scope: {
  7. onStartNew: '&',
  8. onSubmit: '&',
  9. onCancel: '&',
  10. },
  11. link(scope, element, attrs) {
  12. scope.state = {
  13. isAdding: false,
  14. content,
  15. }
  16. scope.$on('comment:start_adding', () => scope.startNewComment())
  17. scope.$on('$destroy', function () {
  18. content = scope.state.content
  19. })
  20. scope.startNewComment = function () {
  21. scope.state.isAdding = true
  22. scope.onStartNew()
  23. setTimeout(() => scope.$broadcast('comment:new:open'))
  24. }
  25. scope.cancelNewComment = function () {
  26. scope.state.isAdding = false
  27. scope.state.content = ''
  28. scope.onCancel()
  29. }
  30. const ignoreKeysInTextAreas = ['PageDown', 'PageUp']
  31. scope.handleCommentKeyDown = function (ev) {
  32. if (ignoreKeysInTextAreas.includes(ev.key)) {
  33. if (ev.target.closest('textarea')) {
  34. ev.preventDefault()
  35. }
  36. }
  37. }
  38. scope.handleCommentKeyPress = function (ev) {
  39. if (ev.keyCode === 13 && !ev.shiftKey && !ev.ctrlKey && !ev.metaKey) {
  40. ev.preventDefault()
  41. if (scope.state.content.length > 0) {
  42. scope.submitNewComment()
  43. }
  44. }
  45. }
  46. scope.submitNewComment = function (event) {
  47. scope.onSubmit({ content: scope.state.content })
  48. content = scope.state.content
  49. scope.state.isAdding = false
  50. scope.state.content = ''
  51. }
  52. },
  53. }))