TEditor.vue 16 KB

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