plugin.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.10.0 (2021-10-11)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global$1 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var global = tinymce.util.Tools.resolve('tinymce.util.Tools');
  13. var html2bbcode = function (s) {
  14. s = global.trim(s);
  15. var rep = function (re, str) {
  16. s = s.replace(re, str);
  17. };
  18. rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi, '[url=$1]$2[/url]');
  19. rep(/<font.*?color=\"(.*?)\".*?class=\"codeStyle\".*?>(.*?)<\/font>/gi, '[code][color=$1]$2[/color][/code]');
  20. rep(/<font.*?color=\"(.*?)\".*?class=\"quoteStyle\".*?>(.*?)<\/font>/gi, '[quote][color=$1]$2[/color][/quote]');
  21. rep(/<font.*?class=\"codeStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi, '[code][color=$1]$2[/color][/code]');
  22. rep(/<font.*?class=\"quoteStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi, '[quote][color=$1]$2[/color][/quote]');
  23. rep(/<span style=\"color: ?(.*?);\">(.*?)<\/span>/gi, '[color=$1]$2[/color]');
  24. rep(/<font.*?color=\"(.*?)\".*?>(.*?)<\/font>/gi, '[color=$1]$2[/color]');
  25. rep(/<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi, '[size=$1]$2[/size]');
  26. rep(/<font>(.*?)<\/font>/gi, '$1');
  27. rep(/<img.*?src=\"(.*?)\".*?\/>/gi, '[img]$1[/img]');
  28. rep(/<span class=\"codeStyle\">(.*?)<\/span>/gi, '[code]$1[/code]');
  29. rep(/<span class=\"quoteStyle\">(.*?)<\/span>/gi, '[quote]$1[/quote]');
  30. rep(/<strong class=\"codeStyle\">(.*?)<\/strong>/gi, '[code][b]$1[/b][/code]');
  31. rep(/<strong class=\"quoteStyle\">(.*?)<\/strong>/gi, '[quote][b]$1[/b][/quote]');
  32. rep(/<em class=\"codeStyle\">(.*?)<\/em>/gi, '[code][i]$1[/i][/code]');
  33. rep(/<em class=\"quoteStyle\">(.*?)<\/em>/gi, '[quote][i]$1[/i][/quote]');
  34. rep(/<u class=\"codeStyle\">(.*?)<\/u>/gi, '[code][u]$1[/u][/code]');
  35. rep(/<u class=\"quoteStyle\">(.*?)<\/u>/gi, '[quote][u]$1[/u][/quote]');
  36. rep(/<\/(strong|b)>/gi, '[/b]');
  37. rep(/<(strong|b)>/gi, '[b]');
  38. rep(/<\/(em|i)>/gi, '[/i]');
  39. rep(/<(em|i)>/gi, '[i]');
  40. rep(/<\/u>/gi, '[/u]');
  41. rep(/<span style=\"text-decoration: ?underline;\">(.*?)<\/span>/gi, '[u]$1[/u]');
  42. rep(/<u>/gi, '[u]');
  43. rep(/<blockquote[^>]*>/gi, '[quote]');
  44. rep(/<\/blockquote>/gi, '[/quote]');
  45. rep(/<br \/>/gi, '\n');
  46. rep(/<br\/>/gi, '\n');
  47. rep(/<br>/gi, '\n');
  48. rep(/<p>/gi, '');
  49. rep(/<\/p>/gi, '\n');
  50. rep(/&nbsp;|\u00a0/gi, ' ');
  51. rep(/&quot;/gi, '"');
  52. rep(/&lt;/gi, '<');
  53. rep(/&gt;/gi, '>');
  54. rep(/&amp;/gi, '&');
  55. return s;
  56. };
  57. var bbcode2html = function (s) {
  58. s = global.trim(s);
  59. var rep = function (re, str) {
  60. s = s.replace(re, str);
  61. };
  62. rep(/\n/gi, '<br />');
  63. rep(/\[b\]/gi, '<strong>');
  64. rep(/\[\/b\]/gi, '</strong>');
  65. rep(/\[i\]/gi, '<em>');
  66. rep(/\[\/i\]/gi, '</em>');
  67. rep(/\[u\]/gi, '<u>');
  68. rep(/\[\/u\]/gi, '</u>');
  69. rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi, '<a href="$1">$2</a>');
  70. rep(/\[url\](.*?)\[\/url\]/gi, '<a href="$1">$1</a>');
  71. rep(/\[img\](.*?)\[\/img\]/gi, '<img src="$1" />');
  72. rep(/\[color=(.*?)\](.*?)\[\/color\]/gi, '<font color="$1">$2</font>');
  73. rep(/\[code\](.*?)\[\/code\]/gi, '<span class="codeStyle">$1</span>&nbsp;');
  74. rep(/\[quote.*?\](.*?)\[\/quote\]/gi, '<span class="quoteStyle">$1</span>&nbsp;');
  75. return s;
  76. };
  77. function Plugin () {
  78. global$1.add('bbcode', function (editor) {
  79. editor.on('BeforeSetContent', function (e) {
  80. e.content = bbcode2html(e.content);
  81. });
  82. editor.on('PostProcess', function (e) {
  83. if (e.set) {
  84. e.content = bbcode2html(e.content);
  85. }
  86. if (e.get) {
  87. e.content = html2bbcode(e.content);
  88. }
  89. });
  90. });
  91. }
  92. Plugin();
  93. }());