common.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import {saveFile} from '../utils';
  2. import defaultTools from '../config/tools';
  3. export default {
  4. name: 'markdown',
  5. props: {
  6. value: {
  7. type: [String, Number],
  8. default: ''
  9. },
  10. theme: {// 默认主题
  11. type: String,
  12. default: 'light'
  13. },
  14. width: {// 初始化宽度
  15. type: [Number, String],
  16. default: 'auto'
  17. },
  18. height: {// 初始化高度
  19. type: [Number, String],
  20. default: 600
  21. },
  22. toolbars: {// 工具栏
  23. type: Object,
  24. default() {
  25. return {};
  26. }
  27. },
  28. bordered: {//是否有边框
  29. type: Boolean,
  30. default: true
  31. },
  32. autoSave: {// 是否自动保存
  33. type: Boolean,
  34. default: false
  35. },
  36. interval: {// 自动保存间隔 mm
  37. type: Number,
  38. default: 10000
  39. },
  40. exportFileName: {// 默认导出文件名称
  41. type: String,
  42. default: 'unnamed'
  43. },
  44. markedOptions: {//marked.js配置项
  45. type: Object,
  46. default() {
  47. return {};
  48. }
  49. },
  50. copyCode: {// 复制代码
  51. type: Boolean,
  52. default: true
  53. },
  54. copyBtnText: {// 复制代码按钮文字
  55. type: String,
  56. default: '复制代码'
  57. },
  58. isPreview: {//是否是预览模式
  59. type: Boolean,
  60. default: false
  61. },
  62. isCustomFullscreen: {//是否全部(自定义参数)
  63. type: Boolean,
  64. default: false
  65. }
  66. },
  67. data() {
  68. return {
  69. currentValue: '', // 输入框内容
  70. timeoutId: null,
  71. indexLenth: 1,
  72. html: '',// 预览的html
  73. preview: false, // 是否是预览状态
  74. split: true, //分屏显示
  75. fullscreen: false, // 是否是全屏
  76. scrollSide: '', // 哪个半栏在滑动
  77. lastInsert: '', //最后一次插入的内容
  78. timerId: null, // 定时器id
  79. themeName: '',
  80. themeSlideDown: false,
  81. imgSlideDown: false,
  82. imgs: [],
  83. scrolling: true, // 同步滚动
  84. editorScrollHeight: 0,
  85. previewImgModal: false,
  86. previewImgSrc: '',
  87. previewImgMode: ''
  88. };
  89. },
  90. computed: {
  91. tools() {
  92. const {toolbars = {}} = this;
  93. return {
  94. ...defaultTools,
  95. ...toolbars
  96. };
  97. }
  98. },
  99. methods: {
  100. insertLink() {// 插入链接
  101. this.insertContent('\n[link](href)');
  102. },
  103. insertImage() {// 插入图片
  104. this.insertContent('\n![image](imgUrl)');
  105. },
  106. insertTable() {// 插入表格
  107. this.insertContent(
  108. '\nheader 1 | header 2\n---|---\nrow 1 col 1 | row 1 col 2\nrow 2 col 1 | row 2 col 2\n\n'
  109. );
  110. },
  111. handleSave() {// 保存操作
  112. const {currentValue, themeName, html} = this;
  113. this.$emit('on-save', {
  114. theme: themeName,
  115. value: currentValue,
  116. html
  117. });
  118. },
  119. toggleSlideDown() {// 显示主题选项
  120. this.slideDown = !this.slideDown;
  121. },
  122. setThemes(name) {// 设置主题
  123. this.themeName = name;
  124. this.themeSlideDown = false;
  125. this.$emit('on-theme-change', name);
  126. },
  127. exportFile() {// 导出为.md格式
  128. saveFile(this.currentValue, this.exportFileName + '.md');
  129. },
  130. importFile(e) {// 导入本地文件
  131. const file = e.target.files[0];
  132. if (!file) {
  133. return;
  134. }
  135. const {type} = file;
  136. if (!['text/markdown', 'text/src'].includes(type)) {
  137. return;
  138. }
  139. const reader = new FileReader();
  140. reader.readAsText(file, {
  141. encoding: 'utf-8'
  142. });
  143. reader.onload = () => {
  144. this.currentValue = reader.result;
  145. e.target.value = '';
  146. if (this.pro) {// 专业版,手动set value
  147. this.editor.setOption('value', this.currentValue);
  148. }
  149. };
  150. reader.onerror = err => {
  151. console.error(err);
  152. }
  153. },
  154. handlePaste(_, e) {// 粘贴图片
  155. const {clipboardData = {}} = e;
  156. const {types = [], items} = clipboardData;
  157. let item = null;
  158. for (let i = 0; i < types.length; i++) {
  159. if (types[i] === 'Files') {
  160. item = items[i];
  161. break;
  162. }
  163. }
  164. if (item) {
  165. const file = item.getAsFile();
  166. if (/image/gi.test(file.type)) {
  167. this.$emit('on-upload-image', file);
  168. e.preventDefault();
  169. }
  170. }
  171. },
  172. mousescrollSide(side) {// 设置究竟是哪个半边在主动滑动
  173. this.scrollSide = side;
  174. },
  175. addImageClickListener() {// 监听查看大图
  176. const {imgs = []} = this;
  177. if (imgs.length > 0) {
  178. for (let i = 0, len = imgs.length; i < len; i++) {
  179. imgs[i].onclick = null;
  180. }
  181. }
  182. setTimeout(() => {
  183. this.imgs = this.$refs.preview.querySelectorAll('img');
  184. for (let i = 0, len = this.imgs.length; i < len; i++) {
  185. this.imgs[i].onclick = () => {
  186. const src = this.imgs[i].getAttribute('src');
  187. this.previewImage(src);
  188. };
  189. }
  190. }, 600);
  191. },
  192. previewImage(src) {// 预览图片
  193. const img = new Image();
  194. img.src = src;
  195. img.onload = () => {
  196. const width = img.naturalWidth;
  197. const height = img.naturalHeight;
  198. if (height / width > 1.4) {
  199. this.previewImgMode = 'horizontal';
  200. } else {
  201. this.previewImgMode = 'vertical';
  202. }
  203. this.previewImgSrc = src;
  204. this.previewImgModal = true;
  205. };
  206. },
  207. chooseImage() {// 选择图片
  208. const input = document.createElement('input');
  209. input.type = 'file';
  210. input.accept = 'image/*';
  211. input.onchange = ()=>{
  212. const files = input.files;
  213. if(files[0]){
  214. this.$emit('on-upload-image', files[0]);
  215. input.value = '';
  216. }
  217. }
  218. input.click();
  219. },
  220. addCopyListener() {// 监听复制操作
  221. setTimeout(() => {
  222. const btns = document.querySelectorAll(
  223. '.code-block .copy-code'
  224. );
  225. this.btns = btns;
  226. for (let i = 0, len = btns.length; i < len; i++) {
  227. btns[i].onclick = () => {
  228. const code = btns[i].parentNode.querySelectorAll('pre')[0].innerText;
  229. const aux = document.createElement('input');
  230. aux.setAttribute('value', code);
  231. document.body.appendChild(aux);
  232. aux.select();
  233. document.execCommand('copy');
  234. document.body.removeChild(aux);
  235. this.$emit('on-copy', code);
  236. };
  237. }
  238. }, 600);
  239. },
  240. /**
  241. * 自定义事件
  242. * @param act
  243. */
  244. onCustom(act) {
  245. this.$emit('on-custom', act);
  246. }
  247. },
  248. destroyed() {// 销毁时清除定时器
  249. clearInterval(this.timerId);
  250. }
  251. };