TEditor.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <template>
  2. <div>
  3. <div class="teditor-box" :class="[spinShow?'teditor-loadstyle':'teditor-loadedstyle']">
  4. <textarea ref="myTextarea" :id="id">{{ content }}</textarea>
  5. <Spin fix v-if="spinShow">
  6. <Icon type="ios-loading" size=18 class="teditor-spin-icon-load"></Icon>
  7. <div>{{$L('加载组件中...')}}</div>
  8. </Spin>
  9. <img-upload ref="myUpload" class="teditor-upload" type="callback" @on-callback="editorImage" num="50" style="margin-top:5px;height:26px;"></img-upload>
  10. </div>
  11. <Modal v-model="transfer" class="teditor-transfer" @on-visible-change="transferChange" footer-hide fullscreen transfer>
  12. <div slot="close">
  13. <Button type="primary" size="small">{{$L('完成')}}</Button>
  14. </div>
  15. <div class="teditor-transfer-body">
  16. <textarea :id="'T_' + id">{{ content }}</textarea>
  17. </div>
  18. </Modal>
  19. </div>
  20. </template>
  21. <style lang="scss">
  22. .teditor-box {
  23. textarea {
  24. opacity: 0;
  25. }
  26. .tox-tinymce {
  27. box-shadow: none;
  28. box-sizing: border-box;
  29. border-color: #dddee1;
  30. border-radius: 4px;
  31. overflow: hidden;
  32. .tox-statusbar {
  33. span.tox-statusbar__branding {
  34. a {
  35. display: none;
  36. }
  37. }
  38. }
  39. }
  40. }
  41. .teditor-transfer {
  42. background-color: #ffffff;
  43. .ivu-modal-header {
  44. display: none;
  45. }
  46. .ivu-modal-close {
  47. top: 7px;
  48. }
  49. .teditor-transfer-body {
  50. position: absolute;
  51. top: 0;
  52. left: 0;
  53. width: 100%;
  54. height: 100%;
  55. padding: 0;
  56. margin: 0;
  57. textarea {
  58. opacity: 0;
  59. }
  60. .tox-tinymce {
  61. border: 0;
  62. .tox-statusbar {
  63. span.tox-statusbar__branding {
  64. a {
  65. display: none;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. .tox {
  73. &.tox-silver-sink {
  74. z-index: 13000;
  75. }
  76. }
  77. </style>
  78. <style lang="scss" scoped>
  79. .teditor-loadstyle {
  80. width: 100%;
  81. height: 180px;
  82. overflow: hidden;
  83. position: relative;
  84. }
  85. .teditor-loadedstyle {
  86. width: 100%;
  87. max-height: inherit;
  88. overflow: inherit;
  89. position: relative;
  90. }
  91. .teditor-spin-icon-load {
  92. animation: ani-teditor-spin 1s linear infinite;
  93. }
  94. @keyframes ani-teditor-spin {
  95. from { transform: rotate(0deg);}
  96. 50% { transform: rotate(180deg);}
  97. to { transform: rotate(360deg);}
  98. }
  99. .teditor-upload {
  100. display: none;
  101. width: 0;
  102. height: 0;
  103. overflow: hidden;
  104. }
  105. </style>
  106. <script>
  107. import tinymce from 'tinymce/tinymce';
  108. import ImgUpload from "./ImgUpload";
  109. export default {
  110. name: 'TEditor',
  111. components: {ImgUpload},
  112. props: {
  113. id: {
  114. type: String,
  115. default: () => {
  116. return "tinymce_" + Math.round(Math.random() * 10000);
  117. }
  118. },
  119. value: {
  120. default: ''
  121. },
  122. height: {
  123. default: 360,
  124. },
  125. htmlClass: {
  126. default: '',
  127. type: String
  128. },
  129. plugins: {
  130. type: Array,
  131. default: () => {
  132. return [
  133. 'advlist autolink lists link image charmap print preview hr anchor pagebreak imagetools',
  134. 'searchreplace visualblocks visualchars code',
  135. 'insertdatetime media nonbreaking save table contextmenu directionality',
  136. 'emoticons paste textcolor colorpicker imagetools codesample'
  137. ];
  138. }
  139. },
  140. toolbar: {
  141. type: String,
  142. default: ' undo redo | styleselect | uploadImages | bold italic underline forecolor backcolor | alignleft aligncenter alignright | bullist numlist outdent indent | link image emoticons media codesample | preview screenload',
  143. },
  144. other_options: {
  145. type: Object,
  146. default: () => {
  147. return {};
  148. }
  149. },
  150. readonly: {
  151. type: Boolean,
  152. default: false
  153. }
  154. },
  155. data() {
  156. return {
  157. content: '',
  158. editor: null,
  159. editorT: null,
  160. cTinyMce: null,
  161. checkerTimeout: null,
  162. isTyping: false,
  163. spinShow: true,
  164. transfer: false,
  165. };
  166. },
  167. mounted() {
  168. this.content = this.value;
  169. this.init();
  170. },
  171. activated() {
  172. this.content = this.value;
  173. this.init();
  174. },
  175. deactivated() {
  176. if (this.editor !== null) {
  177. this.editor.destroy();
  178. }
  179. this.spinShow = true;
  180. $A(this.$refs.myTextarea).show();
  181. },
  182. watch: {
  183. value(newValue) {
  184. if (newValue == null) {
  185. newValue = "";
  186. }
  187. if (!this.isTyping) {
  188. if (this.getEditor() !== null) {
  189. this.getEditor().setContent(newValue);
  190. } else{
  191. this.content = newValue;
  192. }
  193. }
  194. },
  195. readonly(value) {
  196. if (this.editor !== null) {
  197. if (value) {
  198. this.editor.setMode('readonly');
  199. } else {
  200. this.editor.setMode('design');
  201. }
  202. }
  203. }
  204. },
  205. methods: {
  206. init() {
  207. this.$nextTick(() => {
  208. tinymce.init(this.concatAssciativeArrays(this.options(false), this.other_options));
  209. });
  210. },
  211. initTransfer() {
  212. this.$nextTick(() => {
  213. tinymce.init(this.concatAssciativeArrays(this.options(true), this.other_options));
  214. });
  215. },
  216. options(isFull) {
  217. return {
  218. selector: (isFull ? '#T_' : '#') + this.id,
  219. base_url: $A.serverUrl('js/build'),
  220. language: "zh_CN",
  221. toolbar: this.toolbar,
  222. plugins: this.plugins,
  223. paste_data_images: true,
  224. menu: {
  225. view: {
  226. title: 'View',
  227. items: 'code | visualaid visualchars visualblocks | spellchecker | preview fullscreen screenload | showcomments'
  228. },
  229. insert: {
  230. title: "Insert",
  231. items: "image link media addcomment pageembed template codesample inserttable | charmap emoticons hr | pagebreak nonbreaking anchor toc | insertdatetime | uploadImages browseImages"
  232. }
  233. },
  234. codesample_languages: [
  235. {text:"HTML/VUE/XML",value:"markup"},
  236. {text:"JavaScript",value:"javascript"},
  237. {text:"CSS",value:"css"},
  238. {text:"PHP",value:"php"},
  239. {text:"Ruby",value:"ruby"},
  240. {text:"Python",value:"python"},
  241. {text:"Java",value:"java"},
  242. {text:"C",value:"c"},
  243. {text:"C#",value:"csharp"},
  244. {text:"C++",value:"cpp"}
  245. ],
  246. height: isFull ? '100%' : ($A.rightExists(this.height, '%') ? this.height : ($A.runNum(this.height) || 360)),
  247. resize: !isFull,
  248. convert_urls:false,
  249. toolbar_mode: 'sliding',
  250. toolbar_drawer: 'floating',
  251. setup: (editor) => {
  252. editor.ui.registry.addMenuButton('uploadImages', {
  253. text: this.$L('图片'),
  254. tooltip: this.$L('上传/浏览 图片'),
  255. fetch: (callback) => {
  256. let items = [{
  257. type: 'menuitem',
  258. text: this.$L('上传图片'),
  259. onAction: () => {
  260. this.$refs.myUpload.handleClick();
  261. }
  262. }, {
  263. type: 'menuitem',
  264. text: this.$L('浏览图片'),
  265. onAction: () => {
  266. this.$refs.myUpload.browsePicture();
  267. }
  268. }];
  269. callback(items);
  270. }
  271. });
  272. editor.ui.registry.addMenuItem('uploadImages', {
  273. text: this.$L('上传图片'),
  274. onAction: () => {
  275. this.$refs.myUpload.handleClick();
  276. }
  277. });
  278. editor.ui.registry.addMenuItem('browseImages', {
  279. text: this.$L('浏览图片'),
  280. onAction: () => {
  281. this.$refs.myUpload.browsePicture();
  282. }
  283. });
  284. if (isFull) {
  285. editor.ui.registry.addButton('screenload', {
  286. icon: 'fullscreen',
  287. tooltip: this.$L('退出全屏'),
  288. onAction: () => {
  289. this.closeFull();
  290. }
  291. });
  292. editor.ui.registry.addMenuItem('screenload', {
  293. text: this.$L('退出全屏'),
  294. onAction: () => {
  295. this.closeFull();
  296. }
  297. });
  298. editor.on('Init', (e) => {
  299. this.editorT = editor;
  300. this.editorT.setContent(this.content);
  301. if (this.readonly) {
  302. this.editorT.setMode('readonly');
  303. } else {
  304. this.editorT.setMode('design');
  305. }
  306. });
  307. }else{
  308. editor.ui.registry.addButton('screenload', {
  309. icon: 'fullscreen',
  310. tooltip: this.$L('全屏'),
  311. onAction: () => {
  312. this.content = editor.getContent();
  313. this.transfer = true;
  314. this.initTransfer();
  315. }
  316. });
  317. editor.ui.registry.addMenuItem('screenload', {
  318. text: this.$L('全屏'),
  319. onAction: () => {
  320. this.content = editor.getContent();
  321. this.transfer = true;
  322. this.initTransfer();
  323. }
  324. });
  325. editor.on('Init', (e) => {
  326. this.spinShow = false;
  327. this.editor = editor;
  328. this.editor.setContent(this.content);
  329. if (this.readonly) {
  330. this.editor.setMode('readonly');
  331. } else {
  332. this.editor.setMode('design');
  333. }
  334. this.$emit('editorInit', this.editor);
  335. });
  336. editor.on('KeyUp', (e) => {
  337. if (this.editor !== null) {
  338. this.submitNewContent();
  339. }
  340. });
  341. editor.on('Change', (e) => {
  342. if (this.editor !== null) {
  343. if (this.getContent() !== this.value) {
  344. this.submitNewContent();
  345. }
  346. this.$emit('editorChange', e);
  347. }
  348. });
  349. }
  350. },
  351. };
  352. },
  353. closeFull() {
  354. this.content = this.getContent();
  355. this.$emit('input', this.content);
  356. this.transfer = false;
  357. if (this.editorT != null) {
  358. this.editorT.destroy();
  359. this.editorT = null;
  360. }
  361. },
  362. transferChange(visible) {
  363. if (!visible && this.editorT != null) {
  364. this.content = this.editorT.getContent();
  365. this.$emit('input', this.content);
  366. this.editorT.destroy();
  367. this.editorT = null;
  368. }
  369. },
  370. getEditor() {
  371. return this.transfer ? this.editorT : this.editor;
  372. },
  373. concatAssciativeArrays(array1, array2) {
  374. if (array2.length === 0) return array1;
  375. if (array1.length === 0) return array2;
  376. let dest = [];
  377. for (let key in array1) {
  378. if (array1.hasOwnProperty(key)) {
  379. dest[key] = array1[key];
  380. }
  381. }
  382. for (let key in array2) {
  383. if (array2.hasOwnProperty(key)) {
  384. dest[key] = array2[key];
  385. }
  386. }
  387. return dest;
  388. },
  389. submitNewContent() {
  390. this.isTyping = true;
  391. if (this.checkerTimeout !== null) {
  392. clearTimeout(this.checkerTimeout);
  393. }
  394. this.checkerTimeout = setTimeout(() => {
  395. this.isTyping = false;
  396. }, 300);
  397. this.$emit('input', this.getContent());
  398. },
  399. insertContent(content) {
  400. if (this.getEditor() !== null) {
  401. this.getEditor().insertContent(content);
  402. }else{
  403. this.content+= content;
  404. }
  405. },
  406. getContent() {
  407. if (this.getEditor() === null) {
  408. return "";
  409. }
  410. return this.getEditor().getContent();
  411. },
  412. insertImage(src) {
  413. this.insertContent('<img src="' + src + '">');
  414. },
  415. editorImage(lists) {
  416. for (let i = 0; i < lists.length; i++) {
  417. let item = lists[i];
  418. if (typeof item === 'object' && typeof item.url === "string") {
  419. this.insertImage(item.url);
  420. }
  421. }
  422. },
  423. }
  424. }
  425. </script>