BxFroalaEditor.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Froala Froala editor integration
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Froala editor representation.
  13. * @see BxDolEditor
  14. */
  15. class BxFroalaEditor extends BxDolEditor
  16. {
  17. /**
  18. * Common initialization params
  19. */
  20. protected static $CONF_COMMON = <<<EOS
  21. jQuery('{bx_var_selector}')
  22. .on('froalaEditor.initialized', function (e, editor) {
  23. editor.\$el.atwho({
  24. searchKey: 'label',
  25. at: "@",
  26. limit: 20,
  27. displayTpl: '<li class="bx-mention-row" data-value="\${value}"><span>\${label}</span> <img class="bx-def-round-corners" src="\${thumb}" /></li>',
  28. insertTpl: '<a class="bx-mention" data-profile-id="\${value}" href="\${url}">\${label}</a>',
  29. callbacks: {
  30. remoteFilter: function(query, callback) {
  31. $.getJSON("{bx_url_root}searchExtended.php?action=get_authors", {term: query}, function(data) {
  32. callback(data);
  33. });
  34. }
  35. },
  36. });
  37. editor.events.on('keydown', function (e) {
  38. if (e.which == $.FroalaEditor.KEYCODE.ENTER || e.which == $.FroalaEditor.KEYCODE.SPACE)
  39. bx_editor_on_space_enter('{bx_var_selector}');
  40. if (e.which == $.FroalaEditor.KEYCODE.ENTER && editor.\$el.atwho('isSelecting'))
  41. return false;
  42. }, true);
  43. bx_editor_on_init('{bx_var_selector}');
  44. })
  45. .froalaEditor({
  46. {bx_var_custom_init}
  47. {bx_var_custom_conf}
  48. key:'TB14A7D6A6kF4A3F3J4C6B7D6A3D3F3C-8LAHYKAJOEh1HQDUH==',
  49. embedlyKey: '{bx_var_embedly_key}',
  50. emoticonsUseImage: false,
  51. charCounterCount: false,
  52. toolbarSticky: false,
  53. quickInsertTags: [],
  54. pastePlain: true,
  55. imageUpload: true,
  56. imageUploadURL: '{bx_var_image_upload_url}',
  57. videoUpload: false,
  58. language: '{bx_var_lang}',
  59. theme: '{bx_var_skin}',
  60. iconsTemplate: 'font_awesome_5'
  61. });
  62. EOS;
  63. /**
  64. * Standard view initialization params
  65. */
  66. protected static $CONF_STANDARD = "";
  67. /**
  68. * Minimal view initialization params
  69. */
  70. protected static $CONF_MINI = "";
  71. /**
  72. * Full view initialization params
  73. */
  74. protected static $CONF_FULL = "";
  75. /**
  76. * Available editor languages
  77. */
  78. protected static $CONF_LANGS = array('ar' => 1, 'bs' => 1, 'cs' => 1, 'da' => 1, 'de' => 1, 'el' => 1, 'en_ca' => 1, 'en_gb' => 1, 'es' => 1, 'et' => 1, 'fa' => 1, 'fi' => 1, 'fr' => 1, 'he' => 1, 'hr' => 1, 'hu' => 1, 'id' => 1, 'it' => 1, 'ja' => 1, 'ko' => 1, 'ku' => 1, 'me' => 1, 'nb' => 1, 'nl' => 1, 'pl' => 1, 'pt_br' => 1, 'pt_pt' => 1, 'ro' => 1, 'ru' => 1, 'sk' => 1, 'sr' => 1, 'sv' => 1, 'th' => 1, 'tr' => 1, 'uk' => 1, 'vi' => 1, 'zh_cn' => 1, 'zh_tw' => 1);
  79. protected $_oTemplate;
  80. protected $_bJsCssAdded = false;
  81. public function __construct ($aObject, $oTemplate)
  82. {
  83. parent::__construct ($aObject);
  84. if ($oTemplate)
  85. $this->_oTemplate = $oTemplate;
  86. else
  87. $this->_oTemplate = BxDolTemplate::getInstance();
  88. }
  89. /**
  90. * Attach editor to HTML element, in most cases - textarea.
  91. * @param $sSelector - jQuery selector to attach editor to.
  92. * @param $iViewMode - editor view mode: BX_EDITOR_STANDARD, BX_EDITOR_MINI, BX_EDITOR_FULL
  93. * @param $bDynamicMode - is AJAX mode or not, the HTML with editor area is loaded dynamically.
  94. */
  95. public function attachEditor ($sSelector, $iViewMode = BX_EDITOR_STANDARD, $bDynamicMode = false)
  96. {
  97. // set visual mode
  98. switch ($iViewMode) {
  99. case BX_EDITOR_MINI:
  100. $aToolbarItems = explode(',', $this->_sButtonsCustom !== false ? $this->_sButtonsCustom : getParam('bx_froala_option_toolbar_mini'));
  101. $sCustomInit = self::$CONF_MINI;
  102. break;
  103. case BX_EDITOR_FULL:
  104. $aToolbarItems = explode(',', $this->_sButtonsCustom !== false ? $this->_sButtonsCustom : getParam('bx_froala_option_toolbar_full'));
  105. $sCustomInit = self::$CONF_FULL;
  106. break;
  107. case BX_EDITOR_STANDARD:
  108. default:
  109. $aToolbarItems = explode(',', $this->_sButtonsCustom !== false ? $this->_sButtonsCustom : getParam('bx_froala_option_toolbar_standard'));
  110. $sCustomInit = self::$CONF_STANDARD;
  111. }
  112. $sCustomInit .= "\ntoolbarButtons: " . json_encode($aToolbarItems) . ",";
  113. $sCustomInit .= "\ntoolbarButtonsSM: " . json_encode($aToolbarItems) . ",";
  114. $sCustomInit .= "\ntoolbarButtonsXS: " . json_encode($aToolbarItems) . ",";
  115. // detect language
  116. $sLang = BxDolLanguages::getInstance()->detectLanguageFromArray (self::$CONF_LANGS, 'en', true);
  117. $sCss = 'editor.less';
  118. $aCss = BxDolTemplate::getInstance()->_lessCss(array(
  119. 'path' => $this->_oTemplate->getCssPath($sCss),
  120. 'url' => $this->_oTemplate->getCssUrl($sCss)
  121. ));
  122. $oModule = BxDolModule::getInstance('bx_froala');
  123. // initialize editor
  124. $sInitEditor = $this->_replaceMarkers(self::$CONF_COMMON, array(
  125. 'bx_var_custom_init' => $sCustomInit,
  126. 'bx_var_custom_conf' => $this->_sConfCustom,
  127. 'bx_var_plugins_path' => bx_js_string(BX_DOL_URL_PLUGINS_PUBLIC, BX_ESCAPE_STR_APOS),
  128. 'bx_var_css_path' => bx_js_string($aCss['url'], BX_ESCAPE_STR_APOS),
  129. 'bx_var_skin' => bx_js_string($this->_aObject['skin'], BX_ESCAPE_STR_APOS),
  130. 'bx_var_lang' => bx_js_string($sLang, BX_ESCAPE_STR_APOS),
  131. 'bx_var_selector' => bx_js_string($sSelector, BX_ESCAPE_STR_APOS),
  132. 'bx_url_root' => bx_js_string(BX_DOL_URL_ROOT, BX_ESCAPE_STR_APOS),
  133. 'bx_var_embedly_key' => bx_js_string(getParam('sys_embedly_api_key'), BX_ESCAPE_STR_APOS),
  134. 'bx_var_image_upload_url' => $oModule ? BX_DOL_URL_ROOT . $oModule->_oConfig->getBaseUri() . 'upload' : '',
  135. ));
  136. if ($bDynamicMode) {
  137. list($aJs, $aCss) = $this->_getJsCss(true);
  138. $sCss = $this->_oTemplate->addCss($aCss, true);
  139. $sScript = $sCss . "<script>
  140. if ('undefined' == typeof(jQuery(document).froalaEditor)) {
  141. bx_get_scripts(" . json_encode($aJs) . ", function () {
  142. $sInitEditor
  143. });
  144. } else {
  145. setTimeout(function () {
  146. $sInitEditor
  147. }, 10); // wait while html is rendered in case of dynamic adding html
  148. }
  149. </script>";
  150. } else {
  151. $sScript = "
  152. <script>
  153. $(document).ready(function () {
  154. $sInitEditor
  155. });
  156. </script>";
  157. }
  158. return $this->_addJsCss($bDynamicMode) . $sScript;
  159. }
  160. /**
  161. * Add css/js files which are needed for editor display and functionality.
  162. */
  163. protected function _addJsCss($bDynamicMode = false, $sInitEditor = '')
  164. {
  165. if ($bDynamicMode)
  166. return '';
  167. if ($this->_bJsCssAdded)
  168. return '';
  169. list($aJs, $aCss) = $this->_getJsCss();
  170. $this->_oTemplate->addJs($aJs);
  171. $this->_oTemplate->addCss($aCss);
  172. $this->_bJsCssAdded = true;
  173. return '';
  174. }
  175. protected function _getPlugins($b3rdParty = false)
  176. {
  177. $a3rdParty = array('embedly', 'image_aviary', 'spell_checker');
  178. $a = explode(',', $this->_sPluginsCustom !== false ? $this->_sPluginsCustom : getParam('bx_froala_option_plugins'));
  179. return array_filter($a, function ($s) use ($a3rdParty, $b3rdParty) {
  180. return !($b3rdParty ^ in_array($s, $a3rdParty));
  181. });
  182. }
  183. protected function _getJsCss($bUseUrlsForJs = false)
  184. {
  185. $sLang = BxDolLanguages::getInstance()->detectLanguageFromArray (self::$CONF_LANGS, 'en', true);
  186. $sJsPrefix = $bUseUrlsForJs ? BX_DOL_URL_MODULES : BX_DIRECTORY_PATH_MODULES;
  187. $sJsSuffix = $bUseUrlsForJs ? '' : '|';
  188. $aJs = array(
  189. $sJsPrefix . 'boonex/froala/js/' . $sJsSuffix . 'editor.js',
  190. $sJsPrefix . 'boonex/froala/plugins/froala/js/' . $sJsSuffix . 'froala_editor.pkgd.min.js',
  191. );
  192. if ($sLang && 'en' != $sLang)
  193. $aJs[] = $sJsPrefix . 'boonex/froala/plugins/froala/js/languages/' . $sJsSuffix . $sLang . '.js';
  194. $aCss = array(
  195. BX_DIRECTORY_PATH_MODULES . 'boonex/froala/template/css/|editor.css',
  196. BX_DIRECTORY_PATH_MODULES . 'boonex/froala/plugins/froala/css/|froala_editor.pkgd.min.css',
  197. BX_DIRECTORY_PATH_MODULES . 'boonex/froala/plugins/froala/css/|froala_style.min.css',
  198. BX_DIRECTORY_PATH_MODULES . 'boonex/froala/plugins/froala/css/themes/|' . $this->_aObject['skin'] . '.min.css',
  199. );
  200. $aPlugins = $this->_getPlugins(true);
  201. foreach ($aPlugins as $sPlugin) {
  202. $aJs[] = $sJsPrefix . 'boonex/froala/plugins/froala/js/third_party/' . $sJsSuffix . $sPlugin . '.min.js';
  203. $aCss[] = BX_DIRECTORY_PATH_MODULES . 'boonex/froala/plugins/froala/css/third_party/|' . $sPlugin . '.min.css';
  204. }
  205. $aPlugins = $this->_getPlugins(false);
  206. foreach ($aPlugins as $sPlugin) {
  207. $aJs[] = $sJsPrefix . 'boonex/froala/plugins/froala/js/plugins/' . $sJsSuffix . $sPlugin . '.min.js';
  208. $aCss[] = BX_DIRECTORY_PATH_MODULES . 'boonex/froala/plugins/froala/css/plugins/|' . $sPlugin . '.min.css';
  209. }
  210. return array($aJs, $aCss);
  211. }
  212. }
  213. /** @} */