You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
5.0 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (C) 2017 MuK IT GmbH
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. **********************************************************************************/
  19. odoo.define('muk_preview_markdown.PreviewContentText', function (require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var ajax = require('web.ajax');
  23. var utils = require('web.utils');
  24. var session = require('web.session');
  25. var registry = require('muk_preview.registry');
  26. var AbstractPreviewContent = require('muk_preview.AbstractPreviewContent');
  27. var QWeb = core.qweb;
  28. var _t = core._t;
  29. var PreviewContentText = AbstractPreviewContent.extend({
  30. events: _.extend({}, AbstractPreviewContent.prototype.events, {
  31. 'change .code-lang': '_onLanguageChange',
  32. }),
  33. template: "muk_preview.PreviewContentText",
  34. cssLibs: [
  35. '/muk_web_preview_text/static/lib/highlight/styles/default.css',
  36. ],
  37. jsLibs: [
  38. '/muk_web_preview_text/static/lib/highlight/highlight.pack.js',
  39. '/muk_web_preview_text/static/lib/highlight_line_numbers/highlight_line_numbers.js',
  40. ],
  41. willStart: function() {
  42. var def = $.ajax({
  43. url: this.url,
  44. dataType: "text",
  45. }).fail(function(jqXHR, textStatus) {
  46. console.error(textStatus);
  47. }).done(function(text) {
  48. this.text = text;
  49. }.bind(this));
  50. return $.when(this._super.apply(this, arguments), def);
  51. },
  52. renderPreviewContent: function() {
  53. this.$codeBlock = this.$('.code-view');
  54. this.$codeLang = this.$('.code-lang');
  55. this._setTextLanguage();
  56. this.$codeLang.select2();
  57. $.each(this.$codeBlock.attr('class').split(" "), function (i, cls) {
  58. if(this.$(".code-lang option[value='" + cls + "']").val()) {
  59. this.$codeLang.val(cls).trigger("change");
  60. }
  61. }.bind(this));
  62. return this._super.apply(this, arguments);
  63. },
  64. _onLanguageChange: function(event) {
  65. if(event.val === 'default') {
  66. this._setTextLanguage();
  67. } else {
  68. this._setTextLanguage(event.val);
  69. }
  70. },
  71. _setTextLanguage: function (language) {
  72. this.$codeBlock.removeClass();
  73. this.$codeBlock.addClass('code-view');
  74. if(language) {
  75. this.$codeBlock.addClass(language);
  76. }
  77. this.$codeBlock.text(this.text);
  78. hljs.highlightBlock(this.$codeBlock[0]);
  79. hljs.lineNumbersBlock(this.$codeBlock[0]);
  80. },
  81. downloadable: true,
  82. printable: true,
  83. });
  84. _.each([
  85. 'abc', 'acgi', 'aip', 'asm', 'asp', 'c', 'c', 'c++', 'cc', 'cc', 'com', 'conf',
  86. 'cpp', 'csh', 'css', 'cxx', 'def', 'el', 'etx', 'f', 'f', 'f77', 'f90', 'f90',
  87. 'flx', 'for', 'for', 'g', 'h', 'h', 'hh', 'hh', 'hlb', 'htc', 'htm', 'html',
  88. 'htmls', 'htt', 'htx', 'idc', 'jav', 'jav', 'java', 'java', 'js', 'js', 'ksh',
  89. 'list', 'log', 'lsp', 'lst', 'lsx', 'm', 'm', 'mar', 'mcf', 'p', 'pas', 'pl',
  90. 'pl', 'pm', 'py', 'rexx', 'rt', 'rt', 'rtf', 'rtx', 's', 'scm', 'scm', 'sdml',
  91. 'sgm', 'sgm', 'sgml', 'sgml', 'sh', 'shtml', 'shtml', 'spc', 'ssi', 'talk', 'tcl',
  92. 'tcsh', 'text', 'tsv', 'txt', 'uil', 'uni', 'unis', 'uri', 'uris', 'uu', 'uue',
  93. 'vcs', 'wml', 'wmls', 'wsc', 'xml', 'zsh', 'less', 'aj', 'cbl', 'cs', 'coffee',
  94. 'go', 'groovy', 'sc', 'js', 'json',
  95. ], function(extension) {
  96. registry.add(extension, PreviewContentText);
  97. registry.add("." + extension, PreviewContentText);
  98. });
  99. _.each([
  100. 'text/vnd.abc', 'text/html', 'text/x-audiosoft-intra', 'text/x-asm', 'text/asp', 'text/plain',
  101. 'text/x-c', 'text/x-script.csh', 'text/css', 'text/x-script.elisp', 'text/x-setext', 'text/x-fortran',
  102. 'text/vnd.fmi.flexstor', 'text/x-h', 'text/x-script', 'text/x-component', 'text/webviewhtml',
  103. 'text/x-java-source', 'text/javascript', 'text/ecmascript', 'text/x-script.ksh', 'text/x-script.lisp',
  104. 'text/x-la-asf', 'text/x-m', 'text/mcf', 'text/x-pascal', 'text/pascal', 'text/x-script.perl',
  105. 'text/x-script.perl-module', 'text/x-script.phyton', 'text/x-script.rexx', 'text/richtext',
  106. 'text/vnd.rn-realtext', 'text/x-script.guile', 'text/x-script.scheme', 'text/sgml', 'text/x-sgml',
  107. 'text/x-script.sh', 'text/x-server-parsed-html', 'text/x-speech', 'text/x-script.tcl',
  108. 'text/x-script.tcsh', 'text/tab-separated-values', 'text/x-uil', 'text/uri-list', 'text/x-uuencode',
  109. 'text/x-vcalendar', 'text/vnd.wap.wml', 'text/vnd.wap.wmlscript', 'text/scriplet', 'text/xml',
  110. 'text/x-script.zsh', 'text/javascript', 'application/javascript', 'application/json',
  111. ], function(mimetype) {
  112. registry.add(mimetype, PreviewContentText);
  113. });
  114. return PreviewContentText;
  115. });