BxBaseEditorQuill.php 8.4 KB

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