editor.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. Template.editor.onRendered(() => {
  2. const textareaSelector = 'textarea';
  3. const disableRicherEditor = Meteor.settings.public.NO_RICHER_EDITOR;
  4. const mentions = [
  5. // User mentions
  6. {
  7. match: /\B@([\w.]*)$/,
  8. search(term, callback) {
  9. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  10. callback(
  11. currentBoard
  12. .activeMembers()
  13. .map(member => {
  14. const username = Users.findOne(member.userId).username;
  15. return username.includes(term) ? username : null;
  16. })
  17. .filter(Boolean),
  18. );
  19. },
  20. template(value) {
  21. return value;
  22. },
  23. replace(username) {
  24. return `@${username} `;
  25. },
  26. index: 1,
  27. },
  28. ];
  29. if (!disableRicherEditor) {
  30. const isSmall = Utils.isMiniScreen();
  31. const toolbar = isSmall
  32. ? [
  33. ['font', ['bold', 'underline']],
  34. ['fontsize', ['fontsize']],
  35. ['color', ['color']],
  36. ['table', ['table']],
  37. ['view', ['fullscreen']],
  38. ]
  39. : [
  40. ['style', ['style']],
  41. ['font', ['bold', 'underline', 'clear']],
  42. ['fontsize', ['fontsize']],
  43. ['fontname', ['fontname']],
  44. ['color', ['color']],
  45. ['para', ['ul', 'ol', 'paragraph']],
  46. ['table', ['table']],
  47. //['insert', ['link', 'picture', 'video']], // iframe tag will be sanitized TODO if iframe[class=note-video-clip] can be added into safe list, insert video can be enabled
  48. ['insert', ['link', 'picture']],
  49. ['view', ['fullscreen', 'help']],
  50. ];
  51. const cleanPastedHTML = function(input) {
  52. const badTags = [
  53. 'style',
  54. 'script',
  55. 'applet',
  56. 'embed',
  57. 'noframes',
  58. 'noscript',
  59. 'meta',
  60. 'link',
  61. 'button',
  62. 'form',
  63. ].join('|');
  64. const badPatterns = new RegExp(
  65. `(?:${[
  66. `<(${badTags})s*[^>][\\s\\S]*?<\\/\\1>`,
  67. `<(${badTags})[^>]*?\\/>`,
  68. ].join('|')})`,
  69. 'gi',
  70. );
  71. let output = input;
  72. // remove bad Tags
  73. output = output.replace(badPatterns, '');
  74. // remove attributes ' style="..."'
  75. const badAttributes = new RegExp(
  76. `(?:${[
  77. 'on\\S+=([\'"]?).*?\\1',
  78. 'href=([\'"]?)javascript:.*?\\2',
  79. 'style=([\'"]?).*?\\3',
  80. 'target=\\S+',
  81. ].join('|')})`,
  82. 'gi',
  83. );
  84. output = output.replace(badAttributes, '');
  85. output = output.replace(/(<a )/gi, '$1target=_ '); // always to new target
  86. return output;
  87. };
  88. const editor = '.editor';
  89. const selectors = [
  90. `.js-new-comment-form ${editor}`,
  91. `.js-edit-comment ${editor}`,
  92. ].join(','); // only new comment and edit comment
  93. $(selectors).summernote({
  94. callbacks: {
  95. onInit(object) {
  96. const jEditor = object && object.editor;
  97. const toolbar = object && object.toolbar;
  98. if (jEditor !== undefined) {
  99. jEditor.find('.note-editable').escapeableTextComplete(mentions);
  100. }
  101. if (toolbar !== undefined) {
  102. const fBtn = toolbar.find('.btn-fullscreen');
  103. fBtn.on('click', function() {
  104. const $this = $(this),
  105. isActive = $this.hasClass('active');
  106. $('.minicards').toggle(!isActive); // mini card is still showing when editor is in fullscreen mode, we hide here manually
  107. });
  108. }
  109. },
  110. onPaste() {
  111. // clear up unwanted tag info when user pasted in text
  112. const thisNote = $(this);
  113. const updatePastedText = function(someNote) {
  114. const original = someNote.summernote('code');
  115. const cleaned = cleanPastedHTML(original); //this is where to call whatever clean function you want. I have mine in a different file, called CleanPastedHTML.
  116. someNote.summernote('code', ''); //clear original
  117. someNote.summernote('pasteHTML', cleaned); //this sets the displayed content editor to the cleaned pasted code.
  118. };
  119. setTimeout(function() {
  120. //this kinda sucks, but if you don't do a setTimeout,
  121. //the function is called before the text is really pasted.
  122. updatePastedText(thisNote);
  123. }, 10);
  124. },
  125. },
  126. dialogsInBody: true,
  127. disableDragAndDrop: true,
  128. toolbar,
  129. popover: {
  130. image: [
  131. [
  132. 'image',
  133. ['resizeFull', 'resizeHalf', 'resizeQuarter', 'resizeNone'],
  134. ],
  135. ['float', ['floatLeft', 'floatRight', 'floatNone']],
  136. ['remove', ['removeMedia']],
  137. ],
  138. table: [
  139. ['add', ['addRowDown', 'addRowUp', 'addColLeft', 'addColRight']],
  140. ['delete', ['deleteRow', 'deleteCol', 'deleteTable']],
  141. ],
  142. air: [['color', ['color']], ['font', ['bold', 'underline', 'clear']]],
  143. },
  144. height: 200,
  145. });
  146. } else {
  147. const $textarea = this.$(textareaSelector);
  148. autosize($textarea);
  149. $textarea.escapeableTextComplete(mentions);
  150. }
  151. });
  152. import sanitizeXss from 'xss';
  153. // XXX I believe we should compute a HTML rendered field on the server that
  154. // would handle markdown and user mentions. We can simply have two
  155. // fields, one source, and one compiled version (in HTML) and send only the
  156. // compiled version to most users -- who don't need to edit.
  157. // In the meantime, all the transformation are done on the client using the
  158. // Blaze API.
  159. const at = HTML.CharRef({ html: '&commat;', str: '@' });
  160. Blaze.Template.registerHelper(
  161. 'mentions',
  162. new Template('mentions', function() {
  163. const view = this;
  164. let content = Blaze.toHTML(view.templateContentBlock);
  165. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  166. if (!currentBoard) return HTML.Raw(sanitizeXss(content));
  167. const knowedUsers = currentBoard.members.map(member => {
  168. const u = Users.findOne(member.userId);
  169. if (u) {
  170. member.username = u.username;
  171. }
  172. return member;
  173. });
  174. const mentionRegex = /\B@([\w.]*)/gi;
  175. let currentMention;
  176. while ((currentMention = mentionRegex.exec(content)) !== null) {
  177. const [fullMention, username] = currentMention;
  178. const knowedUser = _.findWhere(knowedUsers, { username });
  179. if (!knowedUser) {
  180. continue;
  181. }
  182. const linkValue = [' ', at, knowedUser.username];
  183. let linkClass = 'atMention js-open-member';
  184. if (knowedUser.userId === Meteor.userId()) {
  185. linkClass += ' me';
  186. }
  187. const link = HTML.A(
  188. {
  189. class: linkClass,
  190. // XXX Hack. Since we stringify this render function result below with
  191. // `Blaze.toHTML` we can't rely on blaze data contexts to pass the
  192. // `userId` to the popup as usual, and we need to store it in the DOM
  193. // using a data attribute.
  194. 'data-userId': knowedUser.userId,
  195. },
  196. linkValue,
  197. );
  198. content = content.replace(fullMention, Blaze.toHTML(link));
  199. }
  200. return HTML.Raw(sanitizeXss(content));
  201. }),
  202. );
  203. Template.viewer.events({
  204. // Viewer sometimes have click-able wrapper around them (for instance to edit
  205. // the corresponding text). Clicking a link shouldn't fire these actions, stop
  206. // we stop these event at the viewer component level.
  207. 'click a'(event, templateInstance) {
  208. event.stopPropagation();
  209. // XXX We hijack the build-in browser action because we currently don't have
  210. // `_blank` attributes in viewer links, and the transformer function is
  211. // handled by a third party package that we can't configure easily. Fix that
  212. // by using directly `_blank` attribute in the rendered HTML.
  213. event.preventDefault();
  214. const userId = event.currentTarget.dataset.userid;
  215. if (userId) {
  216. Popup.open('member').call({ userId }, event, templateInstance);
  217. } else {
  218. const href = event.currentTarget.href;
  219. if (href) {
  220. window.open(href, '_blank');
  221. }
  222. }
  223. },
  224. });