DescEditor.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <template>
  2. <div>
  3. <div class="desc-editor-box">
  4. <div class="desc-editor-tool">
  5. <Button class="tool-button" size="small" @click="openFull">{{$L('全屏')}}</Button>
  6. </div>
  7. <div v-if="loadIng > 0" class="desc-editor-load">
  8. <WLoading/>
  9. </div>
  10. <div
  11. ref="myTextarea"
  12. class="desc-editor-content"
  13. :id="id"
  14. :placeholder="placeholder"
  15. v-html="content"
  16. @blur="handleBlur"></div>
  17. <ImgUpload
  18. ref="myUpload"
  19. class="desc-editor-upload"
  20. type="callback"
  21. :uploadIng.sync="uploadIng"
  22. @on-callback="editorImage"
  23. num="50"></ImgUpload>
  24. </div>
  25. <Spin fix v-if="uploadIng > 0">
  26. <Icon type="ios-loading" class="upload-control-spin-icon-load"></Icon>
  27. <div>{{$L('正在上传文件...')}}</div>
  28. </Spin>
  29. <Modal v-model="transfer" class="desc-editor-transfer" @on-visible-change="transferChange" footer-hide fullscreen transfer>
  30. <div slot="close">
  31. <Button type="primary" size="small">{{$L('完成')}}</Button>
  32. </div>
  33. <div class="desc-editor-transfer-body">
  34. <textarea :id="'T_' + id" :placeholder="placeholder">{{content}}</textarea>
  35. </div>
  36. <Spin fix v-if="uploadIng > 0">
  37. <Icon type="ios-loading" class="upload-control-spin-icon-load"></Icon>
  38. <div>{{$L('正在上传文件...')}}</div>
  39. </Spin>
  40. </Modal>
  41. </div>
  42. </template>
  43. <style lang="scss">
  44. .desc-editor-box {
  45. .desc-editor-content {
  46. img {
  47. max-width: 100%;
  48. max-height: 100%;
  49. }
  50. &:before {
  51. left: 8px !important;
  52. color: #cccccc !important;
  53. }
  54. }
  55. }
  56. .desc-editor-transfer {
  57. background-color: #ffffff;
  58. .tox-toolbar {
  59. > div:last-child {
  60. > button:last-child {
  61. margin-right: 64px;
  62. }
  63. }
  64. }
  65. .ivu-modal-header {
  66. display: none;
  67. }
  68. .ivu-modal-close {
  69. top: 7px;
  70. z-index: 2;
  71. }
  72. .desc-editor-transfer-body {
  73. position: absolute;
  74. top: 0;
  75. left: 0;
  76. width: 100%;
  77. height: 100%;
  78. padding: 0;
  79. margin: 0;
  80. textarea {
  81. opacity: 0;
  82. }
  83. .tox-tinymce {
  84. border: 0;
  85. .tox-statusbar {
  86. span.tox-statusbar__branding {
  87. a {
  88. display: none;
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. </style>
  96. <style lang="scss" scoped>
  97. .desc-editor-box {
  98. position: relative;
  99. &:hover {
  100. .desc-editor-tool {
  101. .tool-button {
  102. opacity: 0.9;
  103. &:hover {
  104. opacity: 1;
  105. }
  106. }
  107. }
  108. }
  109. .desc-editor-tool {
  110. display: flex;
  111. flex-direction: row;
  112. align-items: center;
  113. position: absolute;
  114. top: 5px;
  115. right: 5px;
  116. z-index: 2;
  117. .tool-button {
  118. font-size: 12px;
  119. opacity: 0;
  120. transition: all 0.2s;
  121. margin-left: 5px;
  122. background-color: #ffffff;
  123. }
  124. }
  125. .desc-editor-load {
  126. position: absolute;
  127. right: 5px;
  128. bottom: 5px;
  129. z-index: 2;
  130. width: 16px;
  131. height: 16px;
  132. }
  133. }
  134. .desc-editor-content {
  135. position: relative;
  136. margin: 10px 0 6px;
  137. border: 2px solid transparent;
  138. padding: 5px 8px;
  139. color: #172b4d;
  140. line-height: 1.5;
  141. border-radius: 4px;
  142. min-height: 56px;
  143. max-height: 182px;
  144. background: rgba(9, 30, 66, 0.04);
  145. overflow: auto;
  146. &:focus {
  147. box-shadow: 0 0 0 2px rgba(45, 140, 240, .2);
  148. }
  149. }
  150. .desc-editor-upload {
  151. display: none;
  152. width: 0;
  153. height: 0;
  154. overflow: hidden;
  155. }
  156. </style>
  157. <script>
  158. import tinymce from 'tinymce/tinymce';
  159. import ImgUpload from "../../../ImgUpload";
  160. export default {
  161. name: 'DescEditor',
  162. components: {ImgUpload},
  163. props: {
  164. taskid: {
  165. default: ''
  166. },
  167. desc: {
  168. default: ''
  169. },
  170. placeholder: {
  171. type: String,
  172. default: ''
  173. }
  174. },
  175. data() {
  176. return {
  177. loadIng: 0,
  178. uploadIng: 0,
  179. id: "tinymce_" + Math.round(Math.random() * 10000),
  180. content: '',
  181. submitContent: '',
  182. editor: null,
  183. editorT: null,
  184. cTinyMce: null,
  185. checkerTimeout: null,
  186. isTyping: false,
  187. transfer: false,
  188. };
  189. },
  190. mounted() {
  191. this.loadData((val) => {
  192. this.submitContent = val;
  193. this.content = val;
  194. this.init();
  195. });
  196. },
  197. beforeDestroy() {
  198. if (this.editor !== null) {
  199. this.editor.destroy();
  200. }
  201. if (this.editorT !== null) {
  202. this.editorT.destroy();
  203. }
  204. },
  205. watch: {
  206. desc() {
  207. this.loadData((val) => {
  208. this.submitContent = val;
  209. this.content = val;
  210. this.getEditor().setContent(val);
  211. });
  212. }
  213. },
  214. methods: {
  215. loadData(callback) {
  216. this.loadIng++;
  217. $A.apiAjax({
  218. url: 'project/task/desc',
  219. data: {
  220. taskid: this.taskid,
  221. },
  222. complete: () => {
  223. this.loadIng--;
  224. },
  225. success: (res) => {
  226. if (res.ret === 1) {
  227. callback(res.data.desc);
  228. } else {
  229. callback('');
  230. }
  231. }
  232. });
  233. },
  234. init() {
  235. this.$nextTick(() => {
  236. tinymce.init(this.options(false));
  237. });
  238. },
  239. initTransfer() {
  240. this.$nextTick(() => {
  241. tinymce.init(this.options(true));
  242. });
  243. },
  244. options(isFull) {
  245. let toolbar;
  246. if (isFull) {
  247. toolbar = 'undo redo | styleselect | uploadImages | bold italic underline forecolor backcolor | alignleft aligncenter alignright | outdent indent | link image emoticons media codesample | preview screenload';
  248. } else {
  249. toolbar = false;
  250. }
  251. return {
  252. selector: (isFull ? '#T_' : '#') + this.id,
  253. base_url: $A.serverUrl('js/build'),
  254. auto_focus: false,
  255. language: "zh_CN",
  256. toolbar: toolbar,
  257. plugins: [
  258. 'advlist autolink lists link image charmap print preview hr anchor pagebreak imagetools',
  259. 'searchreplace visualblocks code',
  260. 'insertdatetime media nonbreaking save table contextmenu directionality',
  261. 'emoticons paste textcolor colorpicker imagetools codesample'
  262. ],
  263. save_onsavecallback: (e) => {
  264. this.handleBlur(e);
  265. },
  266. menubar: isFull,
  267. inline: !isFull,
  268. inline_boundaries: false,
  269. paste_data_images: true,
  270. menu: {
  271. view: {
  272. title: 'View',
  273. items: 'code | visualaid visualchars visualblocks | spellchecker | preview fullscreen screenload | showcomments'
  274. },
  275. insert: {
  276. title: "Insert",
  277. items: "image link media addcomment pageembed template codesample inserttable | charmap emoticons hr | pagebreak nonbreaking anchor toc | insertdatetime | uploadImages browseImages"
  278. }
  279. },
  280. codesample_languages: [
  281. {text: "HTML/VUE/XML", value: "markup"},
  282. {text: "JavaScript", value: "javascript"},
  283. {text: "CSS", value: "css"},
  284. {text: "PHP", value: "php"},
  285. {text: "Ruby", value: "ruby"},
  286. {text: "Python", value: "python"},
  287. {text: "Java", value: "java"},
  288. {text: "C", value: "c"},
  289. {text: "C#", value: "csharp"},
  290. {text: "C++", value: "cpp"}
  291. ],
  292. height: isFull ? '100%' : ($A.rightExists(this.height, '%') ? this.height : ($A.runNum(this.height) || 360)),
  293. resize: !isFull,
  294. convert_urls: false,
  295. toolbar_mode: 'sliding',
  296. toolbar_drawer: 'floating',
  297. setup: (editor) => {
  298. editor.ui.registry.addMenuButton('uploadImages', {
  299. text: this.$L('图片'),
  300. tooltip: this.$L('上传/浏览 图片'),
  301. fetch: (callback) => {
  302. let items = [{
  303. type: 'menuitem',
  304. text: this.$L('上传图片'),
  305. onAction: () => {
  306. this.$refs.myUpload.handleClick();
  307. }
  308. }, {
  309. type: 'menuitem',
  310. text: this.$L('浏览图片'),
  311. onAction: () => {
  312. this.$refs.myUpload.browsePicture();
  313. }
  314. }];
  315. callback(items);
  316. }
  317. });
  318. editor.ui.registry.addMenuItem('uploadImages', {
  319. text: this.$L('上传图片'),
  320. onAction: () => {
  321. this.$refs.myUpload.handleClick();
  322. }
  323. });
  324. editor.ui.registry.addMenuItem('browseImages', {
  325. text: this.$L('浏览图片'),
  326. onAction: () => {
  327. this.$refs.myUpload.browsePicture();
  328. }
  329. });
  330. if (isFull) {
  331. editor.ui.registry.addButton('screenload', {
  332. icon: 'fullscreen',
  333. tooltip: this.$L('退出全屏'),
  334. onAction: () => {
  335. this.closeFull();
  336. }
  337. });
  338. editor.ui.registry.addMenuItem('screenload', {
  339. text: this.$L('退出全屏'),
  340. onAction: () => {
  341. this.closeFull();
  342. }
  343. });
  344. editor.on('Init', (e) => {
  345. this.editorT = editor;
  346. this.editorT.setContent(this.content);
  347. });
  348. } else {
  349. editor.ui.registry.addButton('screenload', {
  350. icon: 'fullscreen',
  351. tooltip: this.$L('全屏'),
  352. onAction: () => {
  353. this.openFull();
  354. }
  355. });
  356. editor.ui.registry.addMenuItem('screenload', {
  357. text: this.$L('全屏'),
  358. onAction: () => {
  359. this.openFull();
  360. }
  361. });
  362. editor.on('Init', (e) => {
  363. this.editor = editor;
  364. this.editor.setContent(this.content);
  365. this.$emit('editorInit', this.editor);
  366. });
  367. editor.on('KeyUp', (e) => {
  368. if (this.editor !== null) {
  369. this.submitNewContent();
  370. }
  371. });
  372. editor.on('Change', (e) => {
  373. if (this.editor !== null) {
  374. if (this.getContent() !== this.value) {
  375. this.submitNewContent();
  376. }
  377. this.$emit('editorChange', e);
  378. }
  379. });
  380. }
  381. },
  382. };
  383. },
  384. openFull() {
  385. this.content = this.getContent();
  386. this.transfer = true;
  387. this.initTransfer();
  388. },
  389. closeFull() {
  390. this.content = this.getContent();
  391. this.editor.setContent(this.content);
  392. this.transfer = false;
  393. if (this.editorT != null) {
  394. this.editorT.destroy();
  395. this.editorT = null;
  396. }
  397. },
  398. transferChange(visible) {
  399. if (!visible) {
  400. this.$refs.myTextarea.focus();
  401. if (this.editorT != null) {
  402. this.content = this.editorT.getContent();
  403. this.editor.setContent(this.content);
  404. this.editorT.destroy();
  405. this.editorT = null;
  406. }
  407. }
  408. },
  409. getEditor() {
  410. return this.transfer ? this.editorT : this.editor;
  411. },
  412. getContent() {
  413. if (this.getEditor() === null) {
  414. return "";
  415. }
  416. return this.getEditor().getContent();
  417. },
  418. submitNewContent() {
  419. this.isTyping = true;
  420. if (this.checkerTimeout !== null) {
  421. clearTimeout(this.checkerTimeout);
  422. }
  423. this.checkerTimeout = setTimeout(() => {
  424. this.isTyping = false;
  425. }, 300);
  426. },
  427. insertContent(content) {
  428. if (this.getEditor() !== null) {
  429. this.getEditor().insertContent(content);
  430. } else {
  431. this.content += content;
  432. }
  433. },
  434. insertImage(src) {
  435. this.insertContent('<img src="' + src + '">');
  436. },
  437. editorImage(lists) {
  438. for (let i = 0; i < lists.length; i++) {
  439. let item = lists[i];
  440. if (typeof item === 'object' && typeof item.url === "string") {
  441. this.insertImage(item.url);
  442. }
  443. }
  444. },
  445. handleBlur() {
  446. this.loadIng++;
  447. setTimeout(() => {
  448. this.handleSave();
  449. this.loadIng--;
  450. }, 300)
  451. },
  452. handleSave() {
  453. if (this.transfer) {
  454. return;
  455. }
  456. if (this.submitContent != this.getContent()) {
  457. const bakContent = this.submitContent;
  458. this.submitContent = this.getContent();
  459. //
  460. this.loadIng++;
  461. $A.apiAjax({
  462. url: 'project/task/edit',
  463. method: 'post',
  464. data: {
  465. act: 'desc',
  466. taskid: this.taskid,
  467. content: this.submitContent,
  468. },
  469. complete: () => {
  470. this.loadIng--;
  471. },
  472. error: () => {
  473. this.getEditor().setContent(bakContent);
  474. alert(this.$L('网络繁忙,请稍后再试!'));
  475. },
  476. success: (res) => {
  477. if (res.ret === 1) {
  478. $A.triggerTaskInfoListener('desc', res.data);
  479. $A.triggerTaskInfoChange(this.taskid);
  480. this.$Message.success(res.msg);
  481. this.$emit('save-success');
  482. } else {
  483. this.$Modal.error({title: this.$L('温馨提示'), content: res.msg});
  484. this.getEditor().setContent(bakContent);
  485. }
  486. }
  487. })
  488. }
  489. },
  490. }
  491. }
  492. </script>