BxBaseEditorQuill.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Quill editor representation.
  11. * @see BxDolEditor
  12. */
  13. class BxBaseEditorQuill extends BxDolEditor
  14. {
  15. /**
  16. * Common initialization params
  17. */
  18. protected static $CONF_COMMON = "
  19. var oParams = {
  20. skin: '{bx_var_skin}',
  21. name: '{bx_var_editor_name}',
  22. selector: '{bx_var_selector}',
  23. css_class: '{bx_var_css_additional_class}',
  24. toolbar: {toolbar},
  25. root_url: '{bx_url_root}'
  26. }
  27. {bx_var_editor_name} = bx_editor_init({bx_var_editor_name}, oParams);";
  28. /**
  29. * Standard view initialization params
  30. */
  31. protected static $CONF_STANDARD = "";
  32. /**
  33. * Minimal view initialization params
  34. */
  35. protected static $CONF_MINI = "";
  36. /**
  37. * Full view initialization params
  38. */
  39. protected static $CONF_FULL = "";
  40. protected $_oTemplate;
  41. protected $_bJsCssAdded = false;
  42. public function __construct ($aObject, $oTemplate)
  43. {
  44. parent::__construct ($aObject);
  45. if ($oTemplate)
  46. $this->_oTemplate = $oTemplate;
  47. else
  48. $this->_oTemplate = BxDolTemplate::getInstance();
  49. }
  50. /**
  51. * Attach editor to HTML element, in most cases - textarea.
  52. * @param $sSelector - jQuery selector to attach editor to.
  53. * @param $iViewMode - editor view mode: BX_EDITOR_STANDARD, BX_EDITOR_MINI, BX_EDITOR_FULL
  54. * @param $bDynamicMode - is AJAX mode or not, the HTML with editor area is loaded dynamically.
  55. */
  56. public function attachEditor ($sSelector, $iViewMode = BX_EDITOR_STANDARD, $bDynamicMode = false, $aAttrs = [])
  57. {
  58. // set visual mode
  59. switch ($iViewMode) {
  60. case BX_EDITOR_MINI:
  61. $sToolbarItems = getParam('sys_quill_toolbar_mini');
  62. $sCustomInit = self::$CONF_MINI;
  63. break;
  64. case BX_EDITOR_FULL:
  65. $sToolbarItems = getParam('sys_quill_toolbar_full');
  66. $sCustomInit = self::$CONF_FULL;
  67. break;
  68. case BX_EDITOR_STANDARD:
  69. default:
  70. $sToolbarItems = getParam('sys_quill_toolbar_standard');
  71. $sCustomInit = self::$CONF_STANDARD;
  72. }
  73. if ($this->_sButtonsCustom !== false) {
  74. $sToolbarItems = $this->_sButtonsCustom;
  75. }
  76. $sCss = 'editor.less';
  77. $aCss = BxDolTemplate::getInstance()->_lessCss(array(
  78. 'path' => $this->_oTemplate->getCssPath($sCss),
  79. 'url' => $this->_oTemplate->getCssUrl($sCss)
  80. ));
  81. $sEditorName = 'quill_' . str_replace(['-', ' '], '_', $aAttrs['form_id'] . '_' . $aAttrs['element_name']);
  82. $this->_oTemplate->addJsTranslation([
  83. '_sys_txt_quill_tooltip_bold',
  84. '_sys_txt_quill_tooltip_italic',
  85. '_sys_txt_quill_tooltip_underline',
  86. '_sys_txt_quill_tooltip_clean',
  87. '_sys_txt_quill_tooltip_list_ordered',
  88. '_sys_txt_quill_tooltip_list_bullet',
  89. '_sys_txt_quill_tooltip_indent_1',
  90. '_sys_txt_quill_tooltip_indent_2',
  91. '_sys_txt_quill_tooltip_blockquote',
  92. '_sys_txt_quill_tooltip_direction',
  93. '_sys_txt_quill_tooltip_script_sub',
  94. '_sys_txt_quill_tooltip_script_super',
  95. '_sys_txt_quill_tooltip_link',
  96. '_sys_txt_quill_tooltip_image',
  97. '_sys_txt_quill_tooltip_code_block',
  98. '_sys_txt_quill_tooltip_color',
  99. '_sys_txt_quill_tooltip_background',
  100. '_sys_txt_quill_tooltip_align',
  101. '_sys_txt_quill_tooltip_header',
  102. '_sys_txt_quill_tooltip_embed',
  103. '_sys_txt_quill_tooltip_embed_popup_header',
  104. '_sys_txt_quill_tooltip_link_popup_header',
  105. ]);
  106. // initialize editor
  107. $sInitEditor = $this->_replaceMarkers(self::$CONF_COMMON, array(
  108. 'bx_var_custom_init' => $sCustomInit,
  109. 'bx_var_selector' => bx_js_string($sSelector, BX_ESCAPE_STR_APOS),
  110. 'bx_var_form_id' => $aAttrs['form_id'],
  111. 'toolbar' => $sToolbarItems ? '[' . $sToolbarItems . ']' : 'false',
  112. 'bx_var_css_additional_class' => $sToolbarItems ? '' : 'bx-form-input-html-quill-empty',
  113. 'bx_var_element_name' => str_replace(['-', ' '], '_', $aAttrs['element_name']),
  114. 'bx_var_editor_name' => $sEditorName,
  115. 'bx_var_skin' => bx_js_string($this->_aObject['skin'], BX_ESCAPE_STR_APOS),
  116. 'bx_url_root' => bx_js_string(BX_DOL_URL_ROOT, BX_ESCAPE_STR_APOS),
  117. 'bx_var_css_path' => bx_js_string($aCss['url'], BX_ESCAPE_STR_APOS),
  118. ));
  119. if ($bDynamicMode) {
  120. list($aJs, $aCss) = $this->_getJsCss(true);
  121. $sCss = $this->_oTemplate->addCss($aCss, true);
  122. $sScript = $sCss . "<script>
  123. var " . $sEditorName . ";
  124. if (typeof bQuillEditorInited === 'undefined') {
  125. bx_get_scripts(" . json_encode($aJs) . ", function () {
  126. bQuillEditorInited = true;
  127. $sInitEditor
  128. });
  129. } else {
  130. setTimeout(function () {
  131. $sInitEditor
  132. }, 10); // wait while html is rendered in case of dynamic adding html with quill
  133. }
  134. if (typeof bQuillEditorInited === 'undefined')
  135. bQuillEditorInited = true;
  136. </script>";
  137. } else {
  138. $sScript = "
  139. <script>
  140. var " . $sEditorName . ";
  141. $(document).ready(function () {
  142. $sInitEditor
  143. });
  144. </script>";
  145. }
  146. return $this->_addJsCss($bDynamicMode) . $sScript;
  147. }
  148. /**
  149. * Add css/js files which are needed for editor display and functionality.
  150. */
  151. protected function _addJsCss($bDynamicMode = false, $sInitEditor = '')
  152. {
  153. if ($bDynamicMode)
  154. return '';
  155. if ($this->_bJsCssAdded)
  156. return '';
  157. list($aJs, $aCss) = $this->_getJsCss();
  158. $this->_oTemplate->addJs($aJs);
  159. $this->_oTemplate->addCss($aCss);
  160. $this->_bJsCssAdded = true;
  161. return '';
  162. }
  163. protected function _getJsCss($bUseUrlsForJs = false)
  164. {
  165. $sJsPrefix = $bUseUrlsForJs ? BX_DOL_URL_PLUGINS_PUBLIC : BX_DIRECTORY_PATH_PLUGINS_PUBLIC;
  166. $sJsPrefixRoot = $bUseUrlsForJs ? BX_DOL_URL_ROOT .'inc/js/' : BX_DIRECTORY_PATH_INC . 'js/';
  167. $sJsSuffix = $bUseUrlsForJs ? '' : '|';
  168. $aJs = array(
  169. $sJsPrefix . 'highlight/' . $sJsSuffix . 'highlight.min.js',
  170. $sJsPrefix . 'quill/' . $sJsSuffix . 'quill.min.js',
  171. $sJsPrefix . 'quill/' . $sJsSuffix . 'quill.mention.js',
  172. $sJsPrefix . 'quill/' . $sJsSuffix . 'quill.imageUploader.min.js',
  173. $sJsPrefix . 'quill/' . $sJsSuffix . 'image-resize.min.js',
  174. $sJsPrefixRoot . $sJsSuffix . 'editor.quill.js',
  175. );
  176. $aCss = array(
  177. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'quill/|quill.' . $this->_aObject['skin'] . '.css',
  178. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'quill/|quill.mention.css',
  179. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'highlight/|default.min.css',
  180. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'quill/quill.imageUploader.min.css',
  181. BX_DIRECTORY_PATH_BASE . 'css/|editor.quill.css',
  182. );
  183. return array($aJs, $aCss);
  184. }
  185. }
  186. /** @} */