module.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import _ from 'lodash';
  2. import { PanelCtrl } from 'app/plugins/sdk';
  3. import Remarkable from 'remarkable';
  4. import { sanitize } from 'app/core/utils/text';
  5. import config from 'app/core/config';
  6. const defaultContent = `
  7. # Title
  8. For markdown syntax help: [commonmark.org/help](https://commonmark.org/help/)
  9. `;
  10. export class TextPanelCtrl extends PanelCtrl {
  11. static templateUrl = `public/app/plugins/panel/text/module.html`;
  12. static scrollable = true;
  13. remarkable: any;
  14. content: string;
  15. // Set and populate defaults
  16. panelDefaults = {
  17. mode: 'markdown', // 'html', 'markdown', 'text'
  18. content: defaultContent,
  19. };
  20. /** @ngInject */
  21. constructor($scope, $injector, private templateSrv, private $sce) {
  22. super($scope, $injector);
  23. _.defaults(this.panel, this.panelDefaults);
  24. this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
  25. this.events.on('refresh', this.onRefresh.bind(this));
  26. this.events.on('render', this.onRender.bind(this));
  27. const renderWhenChanged = (scope: any) => {
  28. const { panel } = scope.ctrl;
  29. return [
  30. panel.content,
  31. panel.mode
  32. ].join();
  33. };
  34. $scope.$watch(
  35. renderWhenChanged,
  36. _.throttle(() => {
  37. console.log('this.render', new Date());
  38. this.render();
  39. }, 2000, {trailing: true, leading: true})
  40. );
  41. }
  42. onInitEditMode() {
  43. this.addEditorTab('Options', 'public/app/plugins/panel/text/editor.html');
  44. if (this.panel.mode === 'text') {
  45. this.panel.mode = 'markdown';
  46. }
  47. }
  48. onRefresh() {
  49. this.render();
  50. }
  51. onRender() {
  52. if (this.panel.mode === 'markdown') {
  53. this.renderMarkdown(this.panel.content);
  54. } else if (this.panel.mode === 'html') {
  55. this.updateContent(this.panel.content);
  56. }
  57. this.renderingCompleted();
  58. }
  59. renderText(content: string) {
  60. content = content
  61. .replace(/&/g, '&')
  62. .replace(/>/g, '>')
  63. .replace(/</g, '&lt;')
  64. .replace(/\n/g, '<br/>');
  65. this.updateContent(content);
  66. }
  67. renderMarkdown(content: string) {
  68. if (!this.remarkable) {
  69. this.remarkable = new Remarkable();
  70. }
  71. this.$scope.$applyAsync(() => {
  72. this.updateContent(this.remarkable.render(content));
  73. });
  74. }
  75. updateContent(html: string) {
  76. const { sanitizeInput } = config;
  77. html = sanitizeInput ? sanitize(html) : html;
  78. console.log('html', html);
  79. try {
  80. this.content = this.$sce.trustAsHtml(this.templateSrv.replace(html, this.panel.scopedVars));
  81. } catch (e) {
  82. console.log('Text panel error: ', e);
  83. this.content = this.$sce.trustAsHtml(html);
  84. }
  85. }
  86. }
  87. export { TextPanelCtrl as PanelCtrl };