Editormd.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <template>
  2. <div :id="id" class="main-editor">
  3. <link href="static/editor.md/css/editormd.min.css" rel="stylesheet">
  4. <textarea v-html="content" style="display:none;"></textarea>
  5. <!-- 放大图片 -->
  6. <BigImg v-if="showImg" @clickit="showImg = false" :imgSrc="imgSrc"></BigImg>
  7. </div>
  8. </template>
  9. <style scoped>
  10. </style>
  11. <script>
  12. import BigImg from '@/components/common/BigImg'
  13. if (typeof window !== 'undefined') {
  14. var $s = require('scriptjs');
  15. }
  16. export default {
  17. name: 'Editor',
  18. props: {
  19. width: '',
  20. content:{
  21. type: String,
  22. default: '###初始化成功'
  23. },
  24. type: {
  25. type:String,
  26. default: 'editor'
  27. },
  28. id: {
  29. type: String,
  30. default: 'editor-md'
  31. },
  32. editorPath: {
  33. type: String,
  34. default: 'static/editor.md',
  35. },
  36. editorConfig: {
  37. type: Object,
  38. default() {
  39. return {
  40. path: 'static/editor.md/lib/',
  41. height: 1000,
  42. taskList : true,
  43. tex : true, // 默认不解析
  44. flowChart : true, // 默认不解析
  45. sequenceDiagram : true, // 默认不解析
  46. syncScrolling: "single",
  47. htmlDecode: 'style,script,iframe|filterXSS',
  48. imageUpload: true,
  49. imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp", "JPG", "JPEG", "GIF", "PNG", "BMP", "WEBP"],
  50. imageUploadURL: DocConfig.server+"/api/page/uploadImg",
  51. onload: () => {
  52. console.log('onload');
  53. },
  54. };
  55. },
  56. },
  57. },
  58. components:{
  59. BigImg
  60. },
  61. data() {
  62. return {
  63. instance: null,
  64. showImg:false,
  65. imgSrc: ''
  66. };
  67. },
  68. computed: {
  69. },
  70. mounted() {
  71. //加载依赖""
  72. $s([`${this.editorPath}/../jquery.min.js`,
  73. `${this.editorPath}/lib/raphael.min.js`,
  74. `${this.editorPath}/lib/flowchart.min.js`,
  75. ],()=>{
  76. $s([
  77. `${this.editorPath}/../xss.min.js`,
  78. `${this.editorPath}/lib/marked.min.js`,
  79. `${this.editorPath}/lib/prettify.min.js`,
  80. `${this.editorPath}/lib/underscore.min.js`,
  81. `${this.editorPath}/lib/sequence-diagram.min.js`,
  82. `${this.editorPath}/lib/jquery.flowchart.min.js`,
  83. ], () => {
  84. $s(`${this.editorPath}/editormd.js`, () => {
  85. this.initEditor();
  86. });
  87. $s(`${this.editorPath}/../highlight/highlight.min.js`, () => {
  88. hljs.initHighlightingOnLoad();
  89. });
  90. });
  91. });
  92. },
  93. beforeDestroy() {
  94. //清理所有定时器
  95. for (var i = 1; i < 999; i++){
  96. window.clearInterval(i);
  97. };
  98. //window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))
  99. },
  100. methods: {
  101. initEditor() {
  102. this.$nextTick((editorMD = window.editormd) => {
  103. if (editorMD) {
  104. if (this.type == 'editor'){
  105. this.instance = editorMD(this.id, this.editorConfig);
  106. //草稿
  107. this.draft();
  108. //window.addEventListener('beforeunload', e => this.beforeunloadHandler(e));
  109. } else {
  110. this.instance = editorMD.markdownToHTML(this.id, this.editorConfig);
  111. }
  112. this.deal_with_content();
  113. }
  114. });
  115. },
  116. //插入数据到编辑器中。插入到光标处
  117. insertValue(insertContent){
  118. this.instance.insertValue(insertContent);
  119. },
  120. getMarkdown(){
  121. return this.instance.getMarkdown();
  122. },
  123. clear(){
  124. return this.instance.clear();
  125. },
  126. //草稿
  127. draft(){
  128. var that = this ;
  129. //定时保存文本内容到localStorage
  130. setInterval(()=>{
  131. localStorage.page_content= that.getMarkdown() ;
  132. }, 60000);
  133. //检测是否有定时保存的内容
  134. var page_content = localStorage.page_content ;
  135. if (page_content && page_content.length > 0) {
  136. localStorage.removeItem("page_content");
  137. that.$confirm(that.$t('draft_tips'),
  138. ).then(()=>{
  139. that.clear() ;
  140. that.insertValue(page_content) ;
  141. localStorage.removeItem("page_content");
  142. });
  143. };
  144. },
  145. //关闭前提示
  146. beforeunloadHandler(e){
  147. e = e || window.event;
  148. // 兼容IE8和Firefox 4之前的版本
  149. if (e) {
  150. e.returnValue = '提示';
  151. }
  152. // Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
  153. return '提示';
  154. },
  155. //对内容做些定制化改造
  156. deal_with_content(){
  157. var that = this ;
  158. //当表格列数过长时将自动出现滚动条
  159. $.each($("#"+this.id+' table'), function() {
  160. $(this).prop('outerHTML', '<div style="width: 100%;overflow-x: auto;">'+$(this).prop('outerHTML')+'</div>');
  161. });
  162. //超链接都在新窗口打开
  163. $("#"+this.id+' a[href^="http"]').each(function() {
  164. $(this).attr('target', '_blank');
  165. });
  166. //对表格进行一些改造
  167. $("#"+this.id+" table tbody tr").each(function(){
  168. var tr_this = $(this) ;
  169. var td1 = tr_this.find("td").eq(1).html() ;
  170. var td2 = tr_this.find("td").eq(2).html() ;
  171. if(td1 =="object" || td1 =="array[object]" || td2 =="object" || td2 =="array[object]"){
  172. tr_this.css({"background-color":"#F8F8F8"});
  173. }else{
  174. tr_this.css("background-color","#fff");
  175. }
  176. //设置表格hover
  177. tr_this.hover(function(){
  178. tr_this.css("background-color","#F8F8F8");
  179. },function(){
  180. if(td1 =="object" || td1 =="array[object]" || td2 =="object" || td2 =="array[object]"){
  181. tr_this.css({"background-color":"#F8F8F8"});
  182. }else{
  183. tr_this.css("background-color","#fff");
  184. }
  185. });
  186. });
  187. $("th").css("width","180px");
  188. //图片点击放大
  189. $("#"+this.id+" img").click(function(){
  190. var img_url = $(this).attr("src");
  191. that.showImg = true;
  192.       // 获取当前图片地址
  193. that.imgSrc = img_url;
  194. });
  195. //表格头颜色
  196. $("#"+this.id+" table thead tr").css("background-color","#409eff") ;
  197. $("#"+this.id+" table thead tr").css("color","#fff") ;
  198. //代码块美化
  199. $("#"+this.id+" .linenums").css("padding-left","5px") ;
  200. $("#"+this.id+" .linenums li").css("list-style-type","none") ;
  201. $("#"+this.id+" .linenums li").css("background-color","#fcfcfc") ;
  202. $("#"+this.id+" pre").css("background-color","#fcfcfc") ;
  203. $("#"+this.id+" pre").css("border","1px solid #e1e1e8") ;
  204. $("#"+this.id+" code").css("color","#d14");
  205. },
  206. }
  207. };
  208. </script>