UeditorWidget.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace app\common\widget;
  3. use yii\base\Widget;
  4. use app\assets\CoreAsset;
  5. use app\assets\PluginsAsset;
  6. use Yii;
  7. //编辑器
  8. class UeditorWidget extends Widget
  9. {
  10. public $id;//容器ID
  11. public $name;//变量名
  12. public $toolBar='full';//全能型 full , 标准型 basic, 前台发布 publish, 简约:simple
  13. public $width='100%';
  14. public $height='200';
  15. public $defaultValue='';//初始值
  16. public $lang;//语言
  17. public $allowPageBreak=1; //允许使用分页符 1:允许 0:不允许
  18. public $saveRemoteImage=1; //保存远程图片,默认开启保存
  19. public $allowUploadAttachment=0;//允许上传附件 1:允许 0:不允许
  20. public $allowInsertCode=0;//允许插入源代码
  21. public $map;//是否开启百度地图
  22. public $theme = "gray";//皮肤
  23. private $pageBreakTag;//分页符
  24. private $imageUrl;//图片上传提交地址
  25. private $imgPath;//图片修正地址
  26. private $imageManagerUrl;//在线管理图片url
  27. private $imageManagerPath;//图片修正地址
  28. private $scrawlUrl;//涂鸦上传地址
  29. private $scrawlPath;//图片修正地址
  30. private $wordImageUrl;//word转存提交地址
  31. private $wordImagePath;
  32. private $fileUrl;//附件上传提交地址
  33. private $filePath;
  34. private $saveRemotImageUrl;//处理远程图片抓取的程序地址
  35. private $saveRemotImagePath;//保存远程图片的路径
  36. private $getMovieUrl; //视频数据获取程序处理地址
  37. public function init()
  38. {
  39. //id 和 name
  40. if(empty($this->id))
  41. {
  42. echo '缺少ID';
  43. exit;
  44. }
  45. if(empty($this->name))$this->name = $this->id;
  46. //语言设定
  47. $this->lang = 'zh-cn';
  48. //分页符号
  49. $this->pageBreakTag=Yii::$app->params['ueditor']['pageBreakTag'];
  50. }
  51. public function run()
  52. {
  53. $assetsUrl = $this->registerClientScript();
  54. echo "<script src='".$assetsUrl['pluginUrl']."/ueditor1_4_3/ueditor.all.min.js?v=12'></script>";
  55. echo "<script src='".$assetsUrl['pluginUrl']."/ueditor1_4_3/lang/".$this->lang."/".$this->lang.".js'></script>";
  56. echo "<script type='text/plain' id='".$this->id."' name='".$this->name."' style='width:100%' class='kiivoeditor'>".htmlspecialchars_decode($this->defaultValue)."</script>";
  57. echo "<script type=\"text/javascript\">
  58. window.UEDITOR_HOME_URL = '".$assetsUrl['pluginUrl']."/ueditor1_4_3/';";
  59. echo "var URL = window.UEDITOR_HOME_URL || getUEBasePath();";
  60. echo "UE.getEditor('".$this->id."',{theme:'".$this->theme."',lang:'".$this->lang."',";
  61. echo "UEDITOR_HOME_URL : URL,";
  62. echo "listiconpath:'".$assetsUrl['pluginUrl']."/ueditor1_4_3/themes/".$this->theme."/ueditor-list/',";
  63. echo "initialStyle:'ol.custom_cn li,ol.custom_cn1 li,ol.custom_cn2 li,ol.custom_num li,ol.custom_num1 li,ol.custom_num2 li{list-style:none;}',";
  64. echo "serverUrl:URL + 'php/controller.php?base_path=".sys_auth(BASE_PATH)."&app_path=".sys_auth(APP_PATH)."&upload_path=".sys_auth(UPLOAD_PATH)."&auth_key=".AUTH_KEY."',";
  65. //echo "serverUrl: 'https://www.zhitiku.cn/assets/39418df8/ueditor1_4_3/php/controller.php?base_path=".urlencode(BASE_PATH)."&app_path=".urlencode(APP_PATH)."&upload_path=".urlencode(UPLOAD_PATH)."',";
  66. /*
  67. echo "imageUrl:'".$this->imageUrl."',"; //图片上传提交地址
  68. echo "imagePath:'".$this->imgPath."',";
  69. echo "wordImageUrl:'".$this->wordImageUrl."',"; //work转存上传提交地址
  70. echo "wordImagePath:'".$this->wordImagePath."',";
  71. echo "imageManagerUrl:'".$this->imageManagerUrl."',"; //图片管理提交地址
  72. echo "imageManagerPath:'".$this->imageManagerPath."',";
  73. echo "scrawlUrl:'".$this->scrawlUrl."',"; //涂鸦图片提交地址
  74. echo "scrawlPath:'".$this->scrawlPath."',";
  75. echo "fileUrl:'".$this->fileUrl."',"; //附件上传提交地址
  76. echo "filePath:'".$this->filePath."',";
  77. echo "catcherUrl:'".$this->saveRemotImageUrl."',"; //抓取远程图片地址
  78. echo "catcherPath:'".$this->saveRemotImagePath."',";
  79. echo "getMovieUrl:'".$this->getMovieUrl."',";
  80. */
  81. //宽度
  82. if(!empty($this->width))echo "initialFrameWidth:'".$this->width."',";
  83. //高度
  84. if(!empty($this->height))echo "initialFrameHeight:'".$this->height."',";
  85. //后台时支持源码查看
  86. $strSource = defined('IN_ADMIN')?'"source",':'';
  87. //判断是否支持附件上传和图片上传
  88. $strAttachment = $this->allowUploadAttachment?'"simpleupload", "insertimage","attachment",':'';
  89. //判断是否支持分页
  90. $strPage = $this->allowPageBreak?'"pagebreak",':'';
  91. //判断是否支持百度地图
  92. $strMap = $this->map?'"map",':'';
  93. //判断是否支持插入代码
  94. $strInsertCode = $this->allowInsertCode?'"insertcode",':'';
  95. if($this->toolBar=='publish')
  96. {
  97. $this->toolBar = '"fullscreen","undo","redo", "removeformat","|","bold","italic","underline","strikethrough","|","insertunorderedlist","insertorderedlist","justifyleft","justifycenter","justifyright","justifyjustify","|","customstyle","fontsize","fontfamily","forecolor","backcolor","|","unlink", "link","blockquote",'.$strInsertCode.'"inserttable","|",'.$strAttachment.'"pasteplain"';
  98. }
  99. else if($this->toolBar=='simple')
  100. {
  101. $this->toolBar = '"undo","redo", "removeformat","|","bold","italic","underline","strikethrough","|","insertunorderedlist","insertorderedlist","justifyleft","justifycenter","justifyright","justifyjustify","|","unlink", "link","blockquote","|","simpleupload"';
  102. }
  103. else if($this->toolBar=='mobile')
  104. {
  105. $this->toolBar = '"undo","redo", "removeformat","|","bold","italic","underline","strikethrough","|","insertunorderedlist","insertorderedlist","justifyleft","justifycenter","justifyright","justifyjustify","|","simpleupload"';
  106. }
  107. else if($this->toolBar=='basic')
  108. {
  109. $this->toolBar = '"fullscreen",'.$strSource.'"undo","redo", "removeformat","cleardoc","|","bold","italic","underline","strikethrough","|","insertunorderedlist","insertorderedlist","justifyleft","justifycenter","justifyright","justifyjustify","|","rowspacingtop", "rowspacingbottom", "lineheight", "|","indent","customstyle","fontsize","fontfamily","forecolor","backcolor","|","unlink", "link","blockquote",'.$strInsertCode.'"inserttable", "|", "horizontal",'.$strPage.$strAttachment.$strMap.'"pasteplain"';
  110. }
  111. else if($this->toolBar=='full')
  112. {
  113. $this->toolBar = ' "fullscreen", '.$strSource.', "|", "undo", "redo", "|",
  114. "bold", "italic", "underline", "fontborder", "strikethrough", "superscript", "subscript", "removeformat", "formatmatch", "autotypeset", "blockquote", "pasteplain", "|", "forecolor", "backcolor", "insertorderedlist", "insertunorderedlist", "selectall", "cleardoc", "|",
  115. "rowspacingtop", "rowspacingbottom", "lineheight", "|",
  116. "customstyle", "paragraph", "fontfamily", "fontsize", "|",
  117. "directionalityltr", "directionalityrtl", "indent", "|",
  118. "justifyleft", "justifycenter", "justifyright", "justifyjustify", "|", "touppercase", "tolowercase", "|",
  119. "link", "unlink", "anchor", "|", "imagenone", "imageleft", "imageright", "imagecenter", "|",
  120. '.$strAttachment.' "emotion", "scrawl", "insertvideo", "music", '.$strMap.' "insertframe", "insertcode", "webapp", '.$strPage.' "template", "background", "|",
  121. "horizontal", "date", "time", "spechars", "snapscreen", "wordimage", "|",
  122. "inserttable", "deletetable", "insertparagraphbeforetable", "insertrow", "deleterow", "insertcol", "deletecol", "mergecells", "mergeright", "mergedown", "splittocells", "splittorows", "splittocols", "charts", "|",
  123. "print", "preview", "searchreplace", "drafts" ,"help"';
  124. }
  125. //判断是否保存远程图片
  126. if(!$this->saveRemoteImage)
  127. {
  128. echo "catchRemoteImageEnable:false,";
  129. }
  130. else
  131. {
  132. echo "catchRemoteImageEnable:true,";
  133. }
  134. //禁止高度自适应
  135. echo "autoHeightEnabled:false,";
  136. echo "zIndex:9999,";
  137. echo "toolbars:[[".$this->toolBar."]],";
  138. echo "pageBreakTag:'".$this->pageBreakTag."',";
  139. if(defined('IN_ADMIN'))
  140. {
  141. echo "
  142. //focus时自动清空初始化时的内容
  143. autoClearinitialContent:false,
  144. //关字数统计
  145. wordCount:true,
  146. //关闭elementPath
  147. elementPathEnabled:false
  148. //更多其他参数,请参考editor_config.js中的配置项
  149. });
  150. </script>";
  151. }
  152. else
  153. {
  154. echo "
  155. //focus时自动清空初始化时的内容
  156. autoClearinitialContent:false,
  157. //关闭字数统计
  158. wordCount:false,
  159. //关闭elementPath
  160. elementPathEnabled:false
  161. //更多其他参数,请参考editor_config.js中的配置项
  162. });
  163. </script>";
  164. }
  165. }
  166. protected function registerClientScript()
  167. {
  168. $view = $this->getView();
  169. $coreAsset = CoreAsset::register($view);
  170. $coreAssetsUrl = $coreAsset->baseUrl;
  171. $assetPlugin = PluginsAsset::register($view);
  172. $pluginUrl = $assetPlugin->baseUrl;
  173. return array('coreAssetsUrl'=>$coreAssetsUrl,'pluginUrl'=>$pluginUrl);
  174. }
  175. }