js/minieditor.js

  1. /**=====LICENSE STATEMENT START=====
  2. Translator++
  3. CAT (Computer-Assisted Translation) tools and framework to create quality
  4. translations and localizations efficiently.
  5. Copyright (C) 2018 Dreamsavior<dreamsavior@gmail.com>
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  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 General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. =====LICENSE STATEMENT END=====*/
  17. const MiniEditor = function(elm) {
  18. this.elm = $(elm);
  19. this.init();
  20. }
  21. MiniEditor.patterns = [
  22. // /\\(\S+)\[.*\]/gi, // standard tag (accept unicode)
  23. // /\\(\S+)\<.*\>/gi, // Yanfly's Message core tag (accept unicode)
  24. /(if|en)\([\w \=\[\]\&\<\>\|\.\$\_\+\-\*\/\@]+\)/g, //MPP_ChoiceEX
  25. /(\\[a-zA-Z0-9]+\[.*\])+/gi, // standard tag (alphabet only)
  26. /(\\[a-zA-Z0-9]+\<.*\>)+/gi, // Yanfly's Message core tag (alphabet only)
  27. // /\\([a-zA-Z\{\}\\\$\.\|\!\>\<\^])/g
  28. /(\\[a-zA-Z\{\}\\\$\.\|\!\>\<\^])+/g, // standard rpg maker tags
  29. /(\@[0-9]+)+/g //@ command for wolfRpg
  30. ]
  31. MiniEditor.prototype.highlight = function(text) {
  32. if (!text) return text;
  33. this.formulas = MiniEditor.patterns;
  34. console.log("pattern", this.formulas);
  35. var escaper1 = "-----PLACEHOLDER2225821-----";
  36. var escaper2 = "-----PLACEHOLDER2225823-----";
  37. for (var i=0; i<this.formulas.length; i++) {
  38. if (!Boolean(this.formulas[i])) continue;
  39. if (typeof this.formulas[i] == 'function') {
  40. var arrayStrings = this.formulas[i].call(this, text);
  41. console.log(`result`, arrayStrings);
  42. if (typeof arrayStrings == 'string') arrayStrings = [arrayStrings];
  43. if (Array.isArray(arrayStrings) == false) continue;
  44. for (var x in arrayStrings) {
  45. text = text.replaceAll(arrayStrings[x], (match) => {
  46. return escaper1+match+escaper2;
  47. });
  48. }
  49. continue;
  50. }
  51. text = text.replaceAll(this.formulas[i], (match) => {
  52. if (!Boolean(match)) return match;
  53. return escaper1+match+escaper2;
  54. });
  55. }
  56. //text.replace(new RegExp("&", "g"), "&amp;").replace(new RegExp("<", "g"), "&lt;");
  57. text = common.htmlEntities(text);
  58. text = text.replaceAll(escaper1, "<span class='highlight'>");
  59. text = text.replaceAll(escaper2, "</span>");
  60. return text;
  61. }
  62. MiniEditor.prototype.trigger = function(text) {
  63. text = text || this.elm.val();
  64. text = this.highlight(text);
  65. //text.replace(new RegExp("&", "g"), "&amp;").replace(new RegExp("<", "g"), "&lt;").
  66. this.bgContent.html(text.replace(/\n/g, "<br />"))
  67. }
  68. MiniEditor.prototype.init = function() {
  69. this.elm.attr("data-role", "fgContent");
  70. this.wrapper = $("<div data-role='editorWrapper'></div>");
  71. this.elm.after( this.wrapper);
  72. this.wrapper.append(this.elm);
  73. this.bg = $("<pre data-role='bgWrapper'></pre>");
  74. this.bgContent = $("<code data-role='bgContent'></code>");
  75. this.wrapper.append(this.bg);
  76. this.bg.append(this.bgContent);
  77. this.bg.css("height", this.elm.height());
  78. this.wrapper.css("height", this.elm.height());
  79. var that = this;
  80. this.elm.on("input", function() {
  81. var text = $(this).val();
  82. text = that.highlight(text);
  83. //text.replace(new RegExp("&", "g"), "&amp;").replace(new RegExp("<", "g"), "&lt;").
  84. that.bgContent.html(text.replace(/\n/g, "<br />"))
  85. })
  86. this.elm.on("scroll", function() {
  87. that.bg.scrollTop($(this).scrollTop());
  88. that.bg.scrollLeft($(this).scrollLeft());
  89. })
  90. }
  91. $(document).ready(function() {
  92. })