App.vue 3.4 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
<template>
璃白.'s avatar
璃白. 已提交
2
  <div :class="['md_container', { active: isFocus }]">
璃白.'s avatar
璃白. 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
    <markdown-header
      :text.sync="text"
      :selectionInfo.sync="selectionInfo"
      :showPreview.sync="showPreview"
      :isFocus.sync="isFocus"
      :fullScreen.sync="fullScreen"
    />
    <markdownPreview :text="text" :html.sync="html" v-show="showPreview" />
    <markdown-editor
      :selectionInfo.sync="selectionInfo"
      :text.sync="text"
      :fileList.sync="fileList"
      :placeholder="placeholder"
      :isFocus.sync="isFocus"
      :fullScreen.sync="fullScreen"
      v-show="!showPreview"
    />
璃白.'s avatar
璃白. 已提交
20
    <markdown-footer
璃白.'s avatar
璃白. 已提交
21 22 23
      :fileList.sync="fileList"
      :canAttachFile="canAttachFile"
      :isFocus.sync="isFocus"
璃白.'s avatar
璃白. 已提交
24 25 26
      :can-attach-file="canAttachFile"
      v-if="!showPreview && canAttachFile"
    />
璃白.'s avatar
璃白. 已提交
27 28 29
  </div>
</template>
<script>
璃白.'s avatar
璃白. 已提交
30 31 32 33
import markdownHeader from "./components/header/md-header";
import markdownFooter from "./components/footer/md-footer";
import markdownEditor from "./components/content/md-textarea";
import markdownPreview from "./components/content/md-preview";
璃白.'s avatar
璃白. 已提交
34
import { formatText } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
35
export default {
璃白.'s avatar
璃白. 已提交
36 37 38 39 40
  components: {
    markdownHeader,
    markdownFooter,
    markdownEditor,
    markdownPreview
璃白.'s avatar
璃白. 已提交
41
  },
璃白.'s avatar
璃白. 已提交
42 43 44 45 46 47 48 49 50
  props: {
    placeholder: {
      type: String,
      default: "请输入内容"
    },
    canAttachFile: {
      type: Boolean,
      default: true
    }
G
guoweijia 已提交
51
  },
璃白.'s avatar
璃白. 已提交
52 53 54 55 56 57 58
  data() {
    return {
      fullScreen: false,
      isFocus: false,
      showPreview: false,
      fileList: [],
      text: "",
璃白.'s avatar
璃白. 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
//       text: `
// # 标题一标题一标题一
// ## 标题二标题二
// 666\`行内代码\`666
// \`\`\`js
// // 是注释呀
// /**
// * @params x
// */
// function fn() {
//   return null;
// }
// \`\`\`
// **粗体文字**

// _斜体文字_

// > 这段是引用的内容\n
// > 这段是引用的内容
// > 这段是引用的内容

// [链接](url)

// - 无序列表
// - 无序列表
// - 无序列表

// 1. 有序列表
// 2. 有序列表
// 3. 有序列表

// - [ ] 任务列表
// - [x] 任务列表
// - [ ] 任务列表

// | header | header |
// | ------ | ------ |
// | cell | cell |
// | cell | cell |`,
璃白.'s avatar
璃白. 已提交
98 99 100 101 102 103 104 105 106
      html: "",
      selectionInfo: {
        selectorId: "",
        selectionStart: "",
        selectionEnd: ""
      }
    };
  },

G
guoweijia 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120
  watch: {
    html: {
      immediate: true,
      handler: function(val) {
        this.$emit("change", {
          text: this.text,
          html: this.html
        });
      }
    },
    fileList: {
      immediate: false,
      deep: true,
      handler: function(val) {
璃白.'s avatar
璃白. 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        const _this = this;
        if (!val.length) return;
        this.$emit("upload", {
          val: val[0],
          callback: function(url) {
            const originalText = _this.text;
            const selectionInfo = _this.selectionInfo;
            const newText = formatText(
              originalText,
              selectionInfo,
              "\n\n![img](",
              `${url})\n`
            );
            _this.text = newText;
          }
        });
        this.fileList = [];
G
guoweijia 已提交
138 139
      }
    }
璃白.'s avatar
璃白. 已提交
140 141 142 143 144
  }
};
</script>
<style lang="less" scoped>
.md_container {
璃白.'s avatar
璃白. 已提交
145 146
  width: 100%;
  // margin: 200px auto;
璃白.'s avatar
璃白. 已提交
147
  border: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
148 149 150
  border-radius: 4px;
  padding: 10px 16px;
  box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
151 152
  transition: border 0.3s;
  &.active {
璃白.'s avatar
璃白. 已提交
153
    border: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
154 155 156
  }
}
</style>