BxEditorEditor.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. bx_url_uploader: '{bx_url_uploader}',
  27. name: '{bx_var_editor_name}',
  28. selector: '{bx_var_selector}',
  29. toolbar: {toolbar},
  30. buttons: {buttons},
  31. }
  32. {bx_var_editor_name} = bx_ex_editor_init({bx_var_editor_name}, oParams);";
  33. protected $_oTemplate;
  34. protected $_bJsCssAdded = false;
  35. public function __construct ($aObject, $oTemplate)
  36. {
  37. parent::__construct ($aObject);
  38. if ($oTemplate)
  39. $this->_oTemplate = $oTemplate;
  40. else
  41. $this->_oTemplate = BxDolTemplate::getInstance();
  42. $this->_sModule = 'bx_editor';
  43. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  44. $this->_aButtons = $this->_oModule->_aButtons;
  45. }
  46. /**
  47. * Attach editor to HTML element, in most cases - textarea.
  48. * @param $sSelector - jQuery selector to attach editor to.
  49. * @param $iViewMode - editor view mode: BX_EDITOR_STANDARD, BX_EDITOR_MINI, BX_EDITOR_FULL
  50. * @param $bDynamicMode - is AJAX mode or not, the HTML with editor area is loaded dynamically.
  51. */
  52. public function attachEditor ($sSelector, $iViewMode = BX_EDITOR_STANDARD, $bDynamicMode = false, $aAttrs = [])
  53. {
  54. // set visual mode
  55. switch ($iViewMode) {
  56. case BX_EDITOR_MINI:
  57. $sToolbarItems = $this->_oModule->_oDb->getSettings('mini');
  58. break;
  59. case BX_EDITOR_FULL:
  60. $sToolbarItems = $this->_oModule->_oDb->getSettings('full');
  61. break;
  62. case BX_EDITOR_STANDARD:
  63. default:
  64. $sToolbarItems = $this->_oModule->_oDb->getSettings('standard');
  65. }
  66. if ($this->_sButtonsCustom !== false) {
  67. $sToolbarItems = $this->_sButtonsCustom;
  68. }
  69. $sToolbarItems = str_replace(',', "','", $sToolbarItems);
  70. $sToolbarItems = str_replace(",'separator',", '],[', $sToolbarItems);
  71. $sToolbarItems = "['" . $sToolbarItems . "']";
  72. $sEditorName = 'editor_' . str_replace(['-', ' '], '_', $aAttrs['form_id'] . '_' . $aAttrs['element_name']);
  73. $this->_oTemplate->addJsTranslation([
  74. '_bx_editor_embed_popup_link',
  75. '_bx_editor_embed_popup_embed',
  76. ]);
  77. // initialize editor
  78. $sInitEditor = $this->_replaceMarkers(self::$CONF_COMMON, array(
  79. 'bx_var_selector' => bx_js_string($sSelector, BX_ESCAPE_STR_APOS),
  80. 'bx_var_query_params' => isset($aAttrs['query_params']) ? json_encode($aAttrs['query_params']) : "''",
  81. 'bx_var_form_id' => $aAttrs['form_id'],
  82. 'toolbar' => $sToolbarItems ? '[' . $sToolbarItems . ']' : 'false',
  83. 'buttons' => json_encode($this->_aButtons),
  84. 'insert_as_plain_text' => getParam('sys_quill_insert_as_plain_text') == 'on' ? 'true' : 'false',
  85. 'bx_var_css_additional_class' => $sToolbarItems ? '' : 'bx-form-input-html-quill-empty',
  86. 'bx_var_element_name' => str_replace(['-', ' '], '_', $aAttrs['element_name']),
  87. 'bx_var_editor_name' => $sEditorName,
  88. 'bx_var_skin' => bx_js_string($this->_aObject['skin'], BX_ESCAPE_STR_APOS),
  89. 'bx_url_root' => bx_js_string(BX_DOL_URL_ROOT, BX_ESCAPE_STR_APOS),
  90. 'bx_url_uploader' => bx_js_string(BX_DOL_URL_ROOT . "storage.php?o=sys_images_editor&t=sys_images_editor&a=upload", BX_ESCAPE_STR_APOS)
  91. ));
  92. $sInitCallBack = "
  93. bQuillEditorInited = true;
  94. " . $sInitEditor;
  95. if ($bDynamicMode) {
  96. list($aJs, $aCss) = $this->_getJsCss(true);
  97. $sScript = "var " . $sEditorName . "; " . $this->_oTemplate->addJsPreloaded($aJs, $sInitCallBack, "typeof bQuillEditorInited === 'undefined'", $sInitEditor);
  98. $sScript = $this->_oTemplate->_wrapInTagJsCode($sScript);
  99. $sScript = $this->_oTemplate->addCss($aCss, true) . $sScript;
  100. } else {
  101. $sScript = "var " . $sEditorName . "; " . $this->_oTemplate->addJsCodeOnLoad($sInitCallBack);
  102. $sScript = $this->_oTemplate->_wrapInTagJsCode($sScript);
  103. }
  104. return $this->_addJsCss($bDynamicMode) . $sScript;
  105. }
  106. /**
  107. * Add css/js files which are needed for editor display and functionality.
  108. */
  109. protected function _addJsCss($bDynamicMode = false, $sInitEditor = '')
  110. {
  111. if ($bDynamicMode)
  112. return '';
  113. if ($this->_bJsCssAdded)
  114. return '';
  115. list($aJs, $aCss) = $this->_getJsCss();
  116. $this->_oTemplate->addJs($aJs);
  117. $this->_oTemplate->addCss($aCss);
  118. $this->_bJsCssAdded = true;
  119. return '';
  120. }
  121. protected function _getJsCss($bUseUrlsForJs = false)
  122. {
  123. $sJsPrefix = $bUseUrlsForJs ? BX_DOL_URL_MODULES : BX_DIRECTORY_PATH_MODULES;
  124. $sJsSuffix = $bUseUrlsForJs ? '' : '|';
  125. $aCss = [
  126. BX_DIRECTORY_PATH_MODULES . 'boonex/editor/template/css/|main.css',
  127. ];
  128. $aJs = [
  129. $sJsPrefix . 'boonex/editor/js/' . $sJsSuffix . 'editor.js',
  130. 'https://unpkg.com/@popperjs/core@2',
  131. 'https://unpkg.com/tippy.js@6'
  132. ];
  133. return array($aJs, $aCss);
  134. }
  135. }
  136. /** @} */