module.ts 2.6 KB

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