Blame view

resources/js/common/directives/frMarkdown.js 7.95 KB
e77200db5   nologostudio.ru   Initial commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
  import showdown from '../../../js/vendor/showdown';
  import htmlescape from '../../../js/vendor/showdown-htmlescape';
  import newline from '../../../js/showdown-extensions/newline';
  import autoembed from '../../../js/showdown-extensions/autoembed';
  import poll from '../../../js/showdown-extensions/poll';
  import pagedown from '../../../../resources/js/markdown/Markdown.Editor';
  
  angular.module('footyroom')
  
  .service('markdown', function () {
      var converter = new showdown.Converter({
          extensions: [
              htmlescape,
              newline,
              poll,
              autoembed,
          ],
          simplifiedAutoLink: true,
          tables: true,
      });
  
      return {
          render: function (markdown) {
              return converter.makeHtml(markdown);
          },
  
          _converter: converter,
      };
  })
  
  .directive('markdownEditor', ['$timeout', '$compile', 'markdown', function ($timeout, $compile, markdown) {
      var nextId = 0;
  
      return {
          restrict: 'E',
          template:
  
  		'<div id="wmd-button-bar-{{ editorId }}"></div>' +
  		'<div ng-if="showHelp" class="mdhelp">' +
  			'<div class="spinner mdhelp-spinner"></div>' +
  			'<div ng-include="\'/views/ng/markdown/help.html?3\'"></div>' +
  		'</div>' +
  		'<textarea id="wmd-input-{{ editorId }}" class="wmd-input" rows="{{ rows }}" tabindex="100" placeholder="{{ placeholder }}"></textarea>' +
  		'<div class="wmd-preview comment-text markdown-view" ng-style="content? {}: {display: \'none\'}" ng-if="showPreview"></div>',
  
          scope: {
              content: '=',
              showPreview: '=',
              previewEl: '=?',
              rows: '=?',
              placeholder: '=?',
          },
  
          link: function (scope, element, attrs) {
              scope.rows = scope.rows || 12;
              scope.editorId = nextId++;
  
              function help() {
                  scope.showHelp = !scope.showHelp;
                  scope.$apply();
              }
  
              $timeout(function () {
                  /** @type {HTMLInputElement} */
                  const inputElement = document.querySelector('#wmd-input-' + scope.editorId);
                  const previewElement = scope.previewEl
                      ? document.querySelector(scope.previewEl)
                      : document.querySelector('.wmd-preview');
  
                  previewElement.setAttribute('id', 'wmd-preview-' + scope.editorId);
  
                  const editor = new pagedown.Editor(markdown._converter, '-' + scope.editorId, {
                      handler: help,
                  });
  
                  inputElement.value = scope.content || window.localStorage.getItem('markdownContent');
  
                  // We need to watch changes on content happening programmatically.
                  // When they happen we need to update the textarea and call for a
                  // refresh.
                  scope.$watch('content', function (newValue, oldValue) {
                      window.localStorage.setItem('markdownContent', inputElement.value);
  
                      if (newValue !== inputElement.value) {
                          inputElement.value = newValue;
                          editor.refreshPreview();
                      }
                  });
  
                  editor.hooks.chain('onPreviewRefresh', function () {
                      // Wire up changes caused by user interaction with the editor.
                      // We need this because we don't use ng-model on textarea.
                      // Doing so leads to many redundant call for refreshes.
                      // This seems to be the best way.
                      if (scope.content !== inputElement.value) {
                          scope.$apply(function () {
                              scope.content = inputElement.value;
                          });
                      }
  
                      // Compile output of preview with angular. We need this because we have
                      // angular directives in the output which need to be compiled, otherwise
                      // they just don't work.
                      if (previewElement) {
                          $compile(previewElement.innerHTML)(scope);
                      }
                  });
  
                  editor.run();
              });
          },
      };
  }])
  
  .directive('forumEditor', function () {
      return {
          restrict: 'E',
          template:
  
  		'<div id="commenter" class="forum-editor clearfix">' +
  			'<div class="box">' +
  				'<markdown-editor content="content" show-preview="true"></markdown-editor>' +
  				'<div class="error commenter-errors" ng-show="errors" ng-bind="errors"></div>' +
  				'<div class="control clearfix">' +
  					'<ul class="right-menu">' +
  						'<li ng-click="submit()" tabindex="120">Say it</li>' +
  						'<li class="cancel" ng-if="cancel" ng-click="cancel()" tabindex="121">Cancel</li>' +
  					'</ul>' +
  				'</div>' +
  			'</div>' +
  		'</div>',
  
          scope: {
              showPreview: '=',
              content: '=',
              errors: '=',
              submit: '=onSubmit',
              cancel: '=onCancel',
          },
      };
  })
  
  .directive('forumCommenter', ['$http', '$window', function ($http, $window) {
      var isSaving;
  
      return {
          restrict: 'E',
          template: `
              <forum-editor content="content" show-preview="showPreview" errors="errors" on-submit="submit" on-cancel="cancel"></forum-editor>
              <input name="name-email-url" type="text" style="display:none !important" tabindex="-1" autocomplete="off" ng-model="nameEmailUrl">
          `,
          scope: {
              commentId: '=',
              showPreview: '=',
              postId: '=',
              cancel: '=onCancel',
          },
  
          link: function ($scope, $element) {
              if ($scope.commentId) {
                  loadContent();
              }
  
              $scope.submit = function () {
                  save()
  
                  .then(function () {
                      window.localStorage.removeItem('markdownContent');
                      $window.location.reload();
                  })
  
                  .catch(function (response) {
                      $scope.errors = response.data.errors || "Something went wrong, but we don't know what. Try again later or report this to us.";
  
                      isSaving = false;
  
                      $element.find('markdown-editor').litelay({ off: true });
                  });
              };
  
              function loadContent() {
                  $element.find('markdown-editor').litelay({ spinner: true });
  
                  $http.get('/comments/' + $scope.commentId)
  
                  .then(function (response) {
                      $scope.content = response.data.comment.content;
                      $element.find('markdown-editor').litelay({ off: true });
                  });
              }
  
              function save(data) {
                  if (!isSaving) {
                      isSaving = true;
  
                      $element.find('markdown-editor').litelay({ spinner: true });
  
                      if ($scope.commentId) {
                          return update();
                      }
                      return create();
                  }
              }
  
              function create() {
                  return $http.post('/forum/comments', {
                      content: $scope.content,
                      honeypot: $scope.nameEmailUrl,
                      postId: $scope.postId,
                  });
              }
  
              function update() {
                  return $http.put('/forum/comments/' + $scope.commentId, {
                      content: $scope.content,
                  });
              }
          },
      };
  }])
  
  /**
   * A directive that binds click event on the element, upon which the element is
   * replaced with iframe whose source is defined by the directive's attribute.
   */
  .directive('frMarkdownIframe', function () {
      return {
          restrict: 'A',
          link: function (scope, element, attr) {
              element.bind('click', function (e) {
                  element.replaceWith('<div class="iframe-wrap"><iframe src="' + attr.frMarkdownIframe + '" frameborder="0" allowfullscreen="allowfullscreen"></iframe></div>');
              });
          },
      };
  });