index.vue
6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<template>
<div id="markdowm">
<div class="md-title">
<ul class="cf">
<li>
<span>图片</span>
<input type="file" class="uploadFile" @change="insertImg" />
</li>
<li @click="insertCode">
<span>代码块</span>
</li>
<li @click="setCursorPosition($refs.text, '***')">
<span>分割线</span>
</li>
<li @click="setCursorPosition($refs.text, '****', 2)">
<span>粗体</span>
</li>
<li @click="setCursorPosition($refs.text, '**', 1)">
<span>斜体</span>
</li>
<li @click="setCursorPosition($refs.text, '> ', 2)">
<span>引用</span>
</li>
</ul>
</div>
<textarea v-model="val" ref="text" @keydown.tab="tabMarkdown"></textarea>
<div class="render fmt" v-html="renderHtml"></div>
</div>
</template>
<script>
import marked from "marked";
import highlightJs from "highlight.js";
import { apiUploadImg } from "src/api/upload";
export default {
props: ["value"],
computed: {
renderHtml() {
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true, //允许 Git Hub标准的markdown.
tables: true, //允许支持表格语法。该选项要求 gfm 为true。
breaks: true, //允许回车换行。该选项要求 gfm 为true。
pedantic: false, //尽可能地兼容 markdown.pl的晦涩部分。不纠正原始模型任何的不良行为和错误。
sanitize: true, //对输出进行过滤(清理),将忽略任何已经输入的html代码(标签)
smartLists: true, //使用比原生markdown更时髦的列表。 旧的列表将可能被作为pedantic的处理内容过滤掉.
smartypants: false, //使用更为时髦的标点,比如在引用语法中加入破折号。
highlight: function (code) {
return highlightJs.highlightAuto(code).value;
},
});
return marked(this.val);
},
},
watch: {
val(newVal) {
this.handleModelInput(newVal);
},
},
data() {
return {
val: this.value,
link: "",
textarea: null,
};
},
mounted() {
this.textarea = this.$refs.text;
},
methods: {
handleModelInput(newVal) {
this.$emit("input", newVal);
},
tabMarkdown(e) {
// tab键
e.preventDefault();
let indent = " ";
let start = this.textarea.selectionStart;
let end = this.textarea.selectionEnd;
let selected = window.getSelection().toString();
selected = indent + selected.replace(/\n/g, "\n" + indent);
this.textarea.value =
this.textarea.value.substring(0, start) +
selected +
this.textarea.value.substring(end);
this.textarea.setSelectionRange(
start + indent.length,
start + selected.length
);
},
insertImg(e) {
// 插入图片
let formData = new FormData(),
img = "";
formData.append("markdown_img", e.target.files[0]);
return apiUploadImg(formData)
.then((res) => {
img = res.data.markdown_img;
let val = `![图片描述](${img})`;
this.setCursorPosition(this.$refs.text, val, 6);
})
.catch((err) => {
console.log(err);
})
.finally(() => {});
},
insertCode() {
let val = `
\`\`\`
\`\`\``;
this.setCursorPosition(this.$refs.text, val, val.length - 8);
},
setCursorPosition(dom, val, posLen) {
// 设置光标位置
var cursorPosition = 0;
if (dom.selectionStart) {
cursorPosition = dom.selectionStart;
}
this.insertAtCursor(dom, val);
dom.focus();
dom.setSelectionRange(
dom.value.length,
cursorPosition + (posLen || val.length)
);
this.val = dom.value;
},
insertAtCursor(dom, val) {
// 光标所在位置插入字符
if (document.selection) {
dom.focus();
sel = document.selection.createRange();
sel.text = val;
sel.select();
} else if (dom.selectionStart || dom.selectionStart == "0") {
let startPos = dom.selectionStart;
let endPos = dom.selectionEnd;
let restoreTop = dom.scrollTop;
dom.value =
dom.value.substring(0, startPos) +
val +
dom.value.substring(endPos, dom.value.length);
if (restoreTop > 0) {
dom.scrollTop = restoreTop;
}
dom.focus();
dom.selectionStart = startPos + val.length;
dom.selectionEnd = startPos + val.length;
} else {
dom.value += val;
dom.focus();
}
},
},
};
</script>
<style lang="less" scoped>
@import "./markdown.less";
@import "../../../../../node_modules/highlight.js/styles/tomorrow-night-eighties.css";
@md-bd-color: #dcdfe6;
@md-title-color: rgb(233, 234, 237);
@md-bg-color: #fff;
@btn-hover: #3b7cff;
#markdowm {
width: 100%;
height: 500px;
text-align: left;
overflow: hidden;
border: 1px solid @md-bd-color;
position: relative;
.md-title {
width: 100%;
height: 40px;
border-bottom: 1px solid #dcdfe6;
background: @md-title-color;
position: absolute;
left: 0;
top: 0;
z-index: 99;
li {
width: 100px;
height: 100%;
text-align: center;
position: relative;
float: left;
cursor: pointer;
color: #606266;
&:hover {
color: @btn-hover;
}
&:after {
content: "";
position: absolute;
left: 100%;
top: 50%;
transform: translateY(-50%);
width: 1px;
height: 20px;
background: @borderBoldColor;
}
&:last-child::after {
content: none;
}
.uploadFile {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
cursor: pointer;
}
}
}
textarea,
.render {
float: left;
width: 50%;
height: 100%;
vertical-align: top;
box-sizing: border-box;
line-height: 22px;
padding: 0 20px;
}
textarea {
border: none;
border-right: 1px solid @md-bd-color;
resize: none;
outline: none;
background-color: @md-bg-color;
color: @mainColor;
font-size: 14px;
line-height: 22px;
padding: 20px;
padding-top: 50px;
}
.render {
background-color: @md-title-color;
overflow-y: scroll;
padding-top: 50px;
}
.mask {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 10;
}
.link-text {
width: 500px;
text-align: center;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
.link-input {
width: 400px;
}
}
}
</style>