plugin.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.0.1 (2019-02-21)
  8. */
  9. (function () {
  10. var bbcode = (function () {
  11. 'use strict';
  12. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  13. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  14. var html2bbcode = function (s) {
  15. s = global$1.trim(s);
  16. var rep = function (re, str) {
  17. s = s.replace(re, str);
  18. };
  19. rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi, '[url=$1]$2[/url]');
  20. rep(/<font.*?color=\"(.*?)\".*?class=\"codeStyle\".*?>(.*?)<\/font>/gi, '[code][color=$1]$2[/color][/code]');
  21. rep(/<font.*?color=\"(.*?)\".*?class=\"quoteStyle\".*?>(.*?)<\/font>/gi, '[quote][color=$1]$2[/color][/quote]');
  22. rep(/<font.*?class=\"codeStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi, '[code][color=$1]$2[/color][/code]');
  23. rep(/<font.*?class=\"quoteStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi, '[quote][color=$1]$2[/color][/quote]');
  24. rep(/<span style=\"color: ?(.*?);\">(.*?)<\/span>/gi, '[color=$1]$2[/color]');
  25. rep(/<font.*?color=\"(.*?)\".*?>(.*?)<\/font>/gi, '[color=$1]$2[/color]');
  26. rep(/<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi, '[size=$1]$2[/size]');
  27. rep(/<font>(.*?)<\/font>/gi, '$1');
  28. rep(/<img.*?src=\"(.*?)\".*?\/>/gi, '[img]$1[/img]');
  29. rep(/<span class=\"codeStyle\">(.*?)<\/span>/gi, '[code]$1[/code]');
  30. rep(/<span class=\"quoteStyle\">(.*?)<\/span>/gi, '[quote]$1[/quote]');
  31. rep(/<strong class=\"codeStyle\">(.*?)<\/strong>/gi, '[code][b]$1[/b][/code]');
  32. rep(/<strong class=\"quoteStyle\">(.*?)<\/strong>/gi, '[quote][b]$1[/b][/quote]');
  33. rep(/<em class=\"codeStyle\">(.*?)<\/em>/gi, '[code][i]$1[/i][/code]');
  34. rep(/<em class=\"quoteStyle\">(.*?)<\/em>/gi, '[quote][i]$1[/i][/quote]');
  35. rep(/<u class=\"codeStyle\">(.*?)<\/u>/gi, '[code][u]$1[/u][/code]');
  36. rep(/<u class=\"quoteStyle\">(.*?)<\/u>/gi, '[quote][u]$1[/u][/quote]');
  37. rep(/<\/(strong|b)>/gi, '[/b]');
  38. rep(/<(strong|b)>/gi, '[b]');
  39. rep(/<\/(em|i)>/gi, '[/i]');
  40. rep(/<(em|i)>/gi, '[i]');
  41. rep(/<\/u>/gi, '[/u]');
  42. rep(/<span style=\"text-decoration: ?underline;\">(.*?)<\/span>/gi, '[u]$1[/u]');
  43. rep(/<u>/gi, '[u]');
  44. rep(/<blockquote[^>]*>/gi, '[quote]');
  45. rep(/<\/blockquote>/gi, '[/quote]');
  46. rep(/<br \/>/gi, '\n');
  47. rep(/<br\/>/gi, '\n');
  48. rep(/<br>/gi, '\n');
  49. rep(/<p>/gi, '');
  50. rep(/<\/p>/gi, '\n');
  51. rep(/&nbsp;|\u00a0/gi, ' ');
  52. rep(/&quot;/gi, '"');
  53. rep(/&lt;/gi, '<');
  54. rep(/&gt;/gi, '>');
  55. rep(/&amp;/gi, '&');
  56. return s;
  57. };
  58. var bbcode2html = function (s) {
  59. s = global$1.trim(s);
  60. var rep = function (re, str) {
  61. s = s.replace(re, str);
  62. };
  63. rep(/\n/gi, '<br />');
  64. rep(/\[b\]/gi, '<strong>');
  65. rep(/\[\/b\]/gi, '</strong>');
  66. rep(/\[i\]/gi, '<em>');
  67. rep(/\[\/i\]/gi, '</em>');
  68. rep(/\[u\]/gi, '<u>');
  69. rep(/\[\/u\]/gi, '</u>');
  70. rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi, '<a href="$1">$2</a>');
  71. rep(/\[url\](.*?)\[\/url\]/gi, '<a href="$1">$1</a>');
  72. rep(/\[img\](.*?)\[\/img\]/gi, '<img src="$1" />');
  73. rep(/\[color=(.*?)\](.*?)\[\/color\]/gi, '<font color="$1">$2</font>');
  74. rep(/\[code\](.*?)\[\/code\]/gi, '<span class="codeStyle">$1</span>&nbsp;');
  75. rep(/\[quote.*?\](.*?)\[\/quote\]/gi, '<span class="quoteStyle">$1</span>&nbsp;');
  76. return s;
  77. };
  78. var Convert = {
  79. html2bbcode: html2bbcode,
  80. bbcode2html: bbcode2html
  81. };
  82. global.add('bbcode', function () {
  83. return {
  84. init: function (editor) {
  85. editor.on('beforeSetContent', function (e) {
  86. e.content = Convert.bbcode2html(e.content);
  87. });
  88. editor.on('postProcess', function (e) {
  89. if (e.set) {
  90. e.content = Convert.bbcode2html(e.content);
  91. }
  92. if (e.get) {
  93. e.content = Convert.html2bbcode(e.content);
  94. }
  95. });
  96. }
  97. };
  98. });
  99. function Plugin () {
  100. }
  101. return Plugin;
  102. }());
  103. })();