TEditor.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. language: "zh_CN",
  220. toolbar: this.toolbar,
  221. plugins: this.plugins,
  222. paste_data_images: true,
  223. menu: {
  224. view: {
  225. title: 'View',
  226. items: 'code | visualaid visualchars visualblocks | spellchecker | preview fullscreen screenload | showcomments'
  227. },
  228. insert: {
  229. title: "Insert",
  230. items: "image link media addcomment pageembed template codesample inserttable | charmap emoticons hr | pagebreak nonbreaking anchor toc | insertdatetime | uploadImages browseImages"
  231. }
  232. },
  233. codesample_languages: [
  234. {text:"HTML/VUE/XML",value:"markup"},
  235. {text:"JavaScript",value:"javascript"},
  236. {text:"CSS",value:"css"},
  237. {text:"PHP",value:"php"},
  238. {text:"Ruby",value:"ruby"},
  239. {text:"Python",value:"python"},
  240. {text:"Java",value:"java"},
  241. {text:"C",value:"c"},
  242. {text:"C#",value:"csharp"},
  243. {text:"C++",value:"cpp"}
  244. ],
  245. height: isFull ? '100%' : ($A.rightExists(this.height, '%') ? this.height : ($A.runNum(this.height) || 360)),
  246. resize: !isFull,
  247. convert_urls:false,
  248. toolbar_mode: 'sliding',
  249. toolbar_drawer: 'floating',
  250. setup: (editor) => {
  251. editor.ui.registry.addMenuButton('uploadImages', {
  252. text: this.$L('图片'),
  253. tooltip: this.$L('上传/浏览 图片'),
  254. fetch: (callback) => {
  255. let items = [{
  256. type: 'menuitem',
  257. text: this.$L('上传图片'),
  258. onAction: () => {
  259. this.$refs.myUpload.handleClick();
  260. }
  261. }, {
  262. type: 'menuitem',
  263. text: this.$L('浏览图片'),
  264. onAction: () => {
  265. this.$refs.myUpload.browsePicture();
  266. }
  267. }];
  268. callback(items);
  269. }
  270. });
  271. editor.ui.registry.addMenuItem('uploadImages', {
  272. text: this.$L('上传图片'),
  273. onAction: () => {
  274. this.$refs.myUpload.handleClick();
  275. }
  276. });
  277. editor.ui.registry.addMenuItem('browseImages', {
  278. text: this.$L('浏览图片'),
  279. onAction: () => {
  280. this.$refs.myUpload.browsePicture();
  281. }
  282. });
  283. if (isFull) {
  284. editor.ui.registry.addButton('screenload', {
  285. icon: 'fullscreen',
  286. tooltip: this.$L('退出全屏'),
  287. onAction: () => {
  288. this.closeFull();
  289. }
  290. });
  291. editor.ui.registry.addMenuItem('screenload', {
  292. text: this.$L('退出全屏'),
  293. onAction: () => {
  294. this.closeFull();
  295. }
  296. });
  297. editor.on('Init', (e) => {
  298. this.editorT = editor;
  299. this.editorT.setContent(this.content);
  300. if (this.readonly) {
  301. this.editorT.setMode('readonly');
  302. } else {
  303. this.editorT.setMode('design');
  304. }
  305. });
  306. }else{
  307. editor.ui.registry.addButton('screenload', {
  308. icon: 'fullscreen',
  309. tooltip: this.$L('全屏'),
  310. onAction: () => {
  311. this.content = editor.getContent();
  312. this.transfer = true;
  313. this.initTransfer();
  314. }
  315. });
  316. editor.ui.registry.addMenuItem('screenload', {
  317. text: this.$L('全屏'),
  318. onAction: () => {
  319. this.content = editor.getContent();
  320. this.transfer = true;
  321. this.initTransfer();
  322. }
  323. });
  324. editor.on('Init', (e) => {
  325. this.spinShow = false;
  326. this.editor = editor;
  327. this.editor.setContent(this.content);
  328. if (this.readonly) {
  329. this.editor.setMode('readonly');
  330. } else {
  331. this.editor.setMode('design');
  332. }
  333. this.$emit('editorInit', this.editor);
  334. });
  335. editor.on('KeyUp', (e) => {
  336. if (this.editor !== null) {
  337. this.submitNewContent();
  338. }
  339. });
  340. editor.on('Change', (e) => {
  341. if (this.editor !== null) {
  342. if (this.getContent() !== this.value) {
  343. this.submitNewContent();
  344. }
  345. this.$emit('editorChange', e);
  346. }
  347. });
  348. }
  349. },
  350. };
  351. },
  352. closeFull() {
  353. this.content = this.getContent();
  354. this.$emit('input', this.content);
  355. this.transfer = false;
  356. if (this.editorT != null) {
  357. this.editorT.destroy();
  358. this.editorT = null;
  359. }
  360. },
  361. transferChange(visible) {
  362. if (!visible && this.editorT != null) {
  363. this.content = this.editorT.getContent();
  364. this.$emit('input', this.content);
  365. this.editorT.destroy();
  366. this.editorT = null;
  367. }
  368. },
  369. getEditor() {
  370. return this.transfer ? this.editorT : this.editor;
  371. },
  372. concatAssciativeArrays(array1, array2) {
  373. if (array2.length === 0) return array1;
  374. if (array1.length === 0) return array2;
  375. let dest = [];
  376. for (let key in array1) {
  377. if (array1.hasOwnProperty(key)) {
  378. dest[key] = array1[key];
  379. }
  380. }
  381. for (let key in array2) {
  382. if (array2.hasOwnProperty(key)) {
  383. dest[key] = array2[key];
  384. }
  385. }
  386. return dest;
  387. },
  388. submitNewContent() {
  389. this.isTyping = true;
  390. if (this.checkerTimeout !== null) {
  391. clearTimeout(this.checkerTimeout);
  392. }
  393. this.checkerTimeout = setTimeout(() => {
  394. this.isTyping = false;
  395. }, 300);
  396. this.$emit('input', this.getContent());
  397. },
  398. insertContent(content) {
  399. if (this.getEditor() !== null) {
  400. this.getEditor().insertContent(content);
  401. }else{
  402. this.content+= content;
  403. }
  404. },
  405. getContent() {
  406. if (this.getEditor() === null) {
  407. return "";
  408. }
  409. return this.getEditor().getContent();
  410. },
  411. insertImage(src) {
  412. this.insertContent('<img src="' + src + '">');
  413. },
  414. editorImage(lists) {
  415. for (let i = 0; i < lists.length; i++) {
  416. let item = lists[i];
  417. if (typeof item === 'object' && typeof item.url === "string") {
  418. this.insertImage(item.url);
  419. }
  420. }
  421. },
  422. }
  423. }
  424. </script>