message_text.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. // tslint:disable:max-line-length
  18. import {WebClientService} from '../services/webclient';
  19. export default [
  20. function() {
  21. return {
  22. restrict: 'EA',
  23. scope: {},
  24. bindToController: {
  25. message: '=eeeMessage',
  26. multiLine: '=?eeeMultiLine',
  27. },
  28. controllerAs: 'ctrl',
  29. controller: ['WebClientService', '$filter', function(webClientService: WebClientService, $filter: ng.IFilterService) {
  30. // Get text depending on type
  31. function getText(message: threema.Message): string {
  32. switch (message.type) {
  33. case 'text':
  34. return message.body;
  35. case 'location':
  36. return message.location.description;
  37. case 'file':
  38. // Prefer caption for file messages, if available
  39. if (message.caption && message.caption.length > 0) {
  40. return message.caption;
  41. }
  42. return message.file.name;
  43. }
  44. return message.caption;
  45. }
  46. // TODO: Extract filters into helper functions
  47. const escapeHtml = $filter('escapeHtml') as any;
  48. const markify = $filter('markify') as any;
  49. const emojify = $filter('emojify') as any;
  50. const enlargeSingleEmoji = $filter('enlargeSingleEmoji') as any;
  51. const mentionify = $filter('mentionify') as any;
  52. const linkify = $filter('linkify') as any;
  53. const nlToBr = $filter('nlToBr') as any;
  54. /**
  55. * Apply filters to text.
  56. */
  57. function processText(text: string, largeSingleEmoji: boolean, multiLine: boolean): string {
  58. return nlToBr(linkify(mentionify(enlargeSingleEmoji(emojify(markify(escapeHtml(text))), enlargeSingleEmoji))), multiLine);
  59. }
  60. this.enlargeSingleEmoji = webClientService.appConfig.largeSingleEmoji;
  61. this.$onInit = function() {
  62. if (this.multiLine === undefined) {
  63. this.multiLine = true;
  64. }
  65. this.text = processText(getText(this.message), this.largeSingleEmoji, this.multiLine);
  66. };
  67. }],
  68. template: `
  69. <span click-action ng-bind-html="ctrl.text"></span>
  70. `,
  71. };
  72. },
  73. ];