BxEditorEditor.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Editor integration
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Editor representation.
  13. * @see BxDolEditor
  14. */
  15. class BxEditorEditor extends BxDolEditor
  16. {
  17. protected $_sModule;
  18. protected $_oModule;
  19. protected $_aButtons;
  20. /**
  21. * Common initialization params
  22. */
  23. protected static $CONF_COMMON = "
  24. var oParams = {
  25. root_url: '{bx_url_root}',
  26. name: '{bx_var_editor_name}',
  27. selector: '{bx_var_selector}',
  28. toolbar: {toolbar},
  29. toolbar_inline: {toolbar_inline},
  30. }
  31. {bx_var_editor_name} = bx_ex_editor_init({bx_var_editor_name}, oParams);";
  32. protected $_oTemplate;
  33. protected $_bJsCssAdded = false;
  34. public function __construct ($aObject, $oTemplate)
  35. {
  36. parent::__construct ($aObject);
  37. if ($oTemplate)
  38. $this->_oTemplate = $oTemplate;
  39. else
  40. $this->_oTemplate = BxDolTemplate::getInstance();
  41. $this->_sModule = 'bx_editor';
  42. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  43. $this->_aButtons = $this->_oModule->_aButtons;
  44. }
  45. /**
  46. * Attach editor to HTML element, in most cases - textarea.
  47. * @param $sSelector - jQuery selector to attach editor to.
  48. * @param $iViewMode - editor view mode: BX_EDITOR_STANDARD, BX_EDITOR_MINI, BX_EDITOR_FULL
  49. * @param $bDynamicMode - is AJAX mode or not, the HTML with editor area is loaded dynamically.
  50. */
  51. public function attachEditor ($sSelector, $iViewMode = BX_EDITOR_STANDARD, $bDynamicMode = false, $aAttrs = [])
  52. {
  53. // set visual mode
  54. switch ($iViewMode) {
  55. case BX_EDITOR_MINI:
  56. $sToolbarItems = $this->_oModule->_oDb->getSettings('mini', 1);
  57. $sToolbarItemsInline = $this->_oModule->_oDb->getSettings('mini', 0);
  58. break;
  59. case BX_EDITOR_FULL:
  60. $sToolbarItems = $this->_oModule->_oDb->getSettings('full', 1);
  61. $sToolbarItemsInline = $this->_oModule->_oDb->getSettings('full', 0);
  62. break;
  63. case BX_EDITOR_STANDARD:
  64. default:
  65. $sToolbarItems = $this->_oModule->_oDb->getSettings('standard', 1);
  66. $sToolbarItemsInline = $this->_oModule->_oDb->getSettings('standard', 0);
  67. }
  68. if ($this->_sButtonsCustom !== false) {
  69. $sToolbarItems = $this->_sButtonsCustom;
  70. }
  71. if ($sToolbarItems != '')
  72. $sToolbarItems = "'" . str_replace(',', "','", $sToolbarItems) . "'";
  73. if ($sToolbarItemsInline != '')
  74. $sToolbarItemsInline = "'" . str_replace(',', "','", $sToolbarItemsInline) . "'";
  75. $sEditorName = 'editor_' . str_replace(['-', ' '], '_', $aAttrs['form_id'] . '_' . $aAttrs['element_name']);
  76. $this->_oTemplate->addJsTranslation([
  77. '_bx_editor_embed_popup_header',
  78. ]);
  79. // initialize editor
  80. $sInitEditor = $this->_replaceMarkers(self::$CONF_COMMON, array(
  81. 'bx_var_selector' => bx_js_string($sSelector, BX_ESCAPE_STR_APOS),
  82. 'bx_var_query_params' => isset($aAttrs['query_params']) ? json_encode($aAttrs['query_params']) : "''",
  83. 'bx_var_form_id' => $aAttrs['form_id'],
  84. 'toolbar' => $sToolbarItems ? '[' . $sToolbarItems . ']' : 'false',
  85. 'toolbar_inline' => $sToolbarItemsInline ? '[' . $sToolbarItemsInline . ']' : 'false',
  86. 'bx_var_css_additional_class' => $sToolbarItems ? '' : 'bx-form-input-html-editor-empty',
  87. 'bx_var_element_name' => str_replace(['-', ' '], '_', $aAttrs['element_name']),
  88. 'bx_var_editor_name' => $sEditorName,
  89. 'bx_url_root' => bx_js_string(BX_DOL_URL_ROOT, BX_ESCAPE_STR_APOS),
  90. ));
  91. $sInitCallBack = "
  92. bEditorInited = true;
  93. " . $sInitEditor;
  94. if ($bDynamicMode) {
  95. list($aJs, $aCss) = $this->_getJsCss(true);
  96. $sScript = "var " . $sEditorName . "; " . $this->_oTemplate->addJsPreloaded($aJs, $sInitCallBack, "typeof bEditorInited === 'undefined'", $sInitEditor);
  97. $sScript = $this->_oTemplate->_wrapInTagJsCode($sScript);
  98. $sScript = $this->_oTemplate->addCss($aCss, true) . $sScript;
  99. } else {
  100. $sScript = "var " . $sEditorName . "; " . $this->_oTemplate->addJsCodeOnLoad($sInitCallBack);
  101. $sScript = $this->_oTemplate->_wrapInTagJsCode($sScript);
  102. }
  103. return $this->_addJsCss($bDynamicMode) . $sScript;
  104. }
  105. /**
  106. * Add css/js files which are needed for editor display and functionality.
  107. */
  108. protected function _addJsCss($bDynamicMode = false, $sInitEditor = '')
  109. {
  110. if ($bDynamicMode)
  111. return '';
  112. if ($this->_bJsCssAdded)
  113. return '';
  114. list($aJs, $aCss) = $this->_getJsCss();
  115. $this->_oTemplate->addJs($aJs);
  116. $this->_oTemplate->addCss($aCss);
  117. $this->_bJsCssAdded = true;
  118. return '';
  119. }
  120. protected function _getJsCss($bUseUrlsForJs = false)
  121. {
  122. $sJsPrefix = $bUseUrlsForJs ? BX_DOL_URL_MODULES : BX_DIRECTORY_PATH_MODULES;
  123. $sJsPrefixPlugins = $bUseUrlsForJs ? BX_DOL_URL_PLUGINS : BX_DIRECTORY_PATH_PLUGINS_PUBLIC;
  124. $sJsSuffix = $bUseUrlsForJs ? '' : '|';
  125. $aCss = [
  126. BX_DIRECTORY_PATH_MODULES . 'boonex/editor/template/css/|main.css',
  127. BX_DIRECTORY_PATH_MODULES . 'boonex/editor/plugins/tribute/|tribute.css',
  128. ];
  129. $aJs = [
  130. $sJsPrefix . 'boonex/editor/plugins/editorjs/' . $sJsSuffix . 'editor.js',
  131. $sJsPrefix . 'boonex/editor/plugins/editorjs/' . $sJsSuffix . 'header.js',
  132. $sJsPrefix . 'boonex/editor/plugins/editorjs/' . $sJsSuffix . 'paragraph.js',
  133. $sJsPrefix . 'boonex/editor/plugins/editorjs/' . $sJsSuffix . 'edjsHTML.js',
  134. $sJsPrefix . 'boonex/editor/plugins/editorjs/' . $sJsSuffix . 'list.js',
  135. $sJsPrefix . 'boonex/editor/plugins/editorjs/' . $sJsSuffix . 'delimiter.js',
  136. $sJsPrefix . 'boonex/editor/plugins/editorjs/' . $sJsSuffix . 'code.js',
  137. $sJsPrefix . 'boonex/editor/plugins/editorjs/' . $sJsSuffix . 'inline-code.js',
  138. $sJsPrefix . 'boonex/editor/plugins/editorjs/' . $sJsSuffix . 'image.js',
  139. $sJsPrefix . 'boonex/editor/plugins/editorjs/' . $sJsSuffix . 'marker.js',
  140. $sJsPrefix . 'boonex/editor/plugins/tribute/' . $sJsSuffix . 'tribute.min.js',
  141. /*$sJsPrefix . 'boonex/editor/js/' . $sJsSuffix . 'embed-inline.js',
  142. $sJsPrefix . 'boonex/editor/js/' . $sJsSuffix . 'embed-block.js',
  143. $sJsPrefix . 'boonex/editor/js/' . $sJsSuffix . 'mention.js',*/
  144. $sJsPrefix . 'boonex/editor/js/' . $sJsSuffix . 'editor.js',
  145. ];
  146. return array($aJs, $aCss);
  147. }
  148. }
  149. /** @} */