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.

127 lines
5.1 KiB

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