module.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from "lodash";
  3. import { PanelCtrl } from "app/plugins/sdk";
  4. export class TextPanelCtrl extends PanelCtrl {
  5. static templateUrl = `public/app/plugins/panel/text/module.html`;
  6. static scrollable = true;
  7. remarkable: any;
  8. content: string;
  9. // Set and populate defaults
  10. panelDefaults = {
  11. mode: "markdown", // 'html', 'markdown', 'text'
  12. content: "# title"
  13. };
  14. /** @ngInject **/
  15. constructor($scope, $injector, private templateSrv, private $sce) {
  16. super($scope, $injector);
  17. _.defaults(this.panel, this.panelDefaults);
  18. this.events.on("init-edit-mode", this.onInitEditMode.bind(this));
  19. this.events.on("refresh", this.onRefresh.bind(this));
  20. this.events.on("render", this.onRender.bind(this));
  21. $scope.$watch(
  22. "ctrl.panel.content",
  23. _.throttle(() => {
  24. this.render();
  25. }, 1000)
  26. );
  27. }
  28. onInitEditMode() {
  29. this.addEditorTab("Options", "public/app/plugins/panel/text/editor.html");
  30. this.editorTabIndex = 1;
  31. if (this.panel.mode === "text") {
  32. this.panel.mode = "markdown";
  33. }
  34. }
  35. onRefresh() {
  36. this.render();
  37. }
  38. onRender() {
  39. if (this.panel.mode === "markdown") {
  40. this.renderMarkdown(this.panel.content);
  41. } else if (this.panel.mode === "html") {
  42. this.updateContent(this.panel.content);
  43. }
  44. this.renderingCompleted();
  45. }
  46. renderText(content) {
  47. content = content
  48. .replace(/&/g, "&amp;")
  49. .replace(/>/g, "&gt;")
  50. .replace(/</g, "&lt;")
  51. .replace(/\n/g, "<br/>");
  52. this.updateContent(content);
  53. }
  54. renderMarkdown(content) {
  55. if (!this.remarkable) {
  56. return System.import("remarkable").then(Remarkable => {
  57. this.remarkable = new Remarkable();
  58. this.$scope.$apply(() => {
  59. this.updateContent(this.remarkable.render(content));
  60. });
  61. });
  62. }
  63. this.$scope.$applyAsync(() => {
  64. this.updateContent(this.remarkable.render(content));
  65. });
  66. }
  67. updateContent(html) {
  68. try {
  69. this.content = this.$sce.trustAsHtml(
  70. this.templateSrv.replace(html, this.panel.scopedVars)
  71. );
  72. } catch (e) {
  73. console.log("Text panel error: ", e);
  74. this.content = this.$sce.trustAsHtml(html);
  75. }
  76. }
  77. }
  78. export { TextPanelCtrl as PanelCtrl };