-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJEditor.vue
More file actions
377 lines (376 loc) · 9.06 KB
/
Copy pathJEditor.vue
File metadata and controls
377 lines (376 loc) · 9.06 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<template>
<div class="j-editor" :class="{ fullscreen: isFullscreen }" @keydown="onKeydown" @keyup="onKeyup" :style="'font-size:' + fontSize + ';'">
<!--工具栏-->
<div class="editor-toolbar" :style="'background-color:' + toolbarsBackground + ';'">
<ToolbarLeft
:toolbars="toolbars"
:color="color"
:imageUoload="imageUoload"
:imageComplete="imageComplete"
:fileName="fileName"
:codes="languages"
@toolbar-left-click="toolbar_left_click"
@toolbar-left-especial="toolbar_left_especial"
></ToolbarLeft>
<ToolbarRight
:toolbars="toolbars"
:preview="isPreview"
:fullscreen="isFullscreen"
:htmlcode="isHtmlcode"
@toolbar-right-click="toolbar_right_click"
></ToolbarRight>
</div>
<div class="editor-panel" :class="{ preview: isPreview }" :style="'font-size:' + fontSize + 'px;font-family:monospace;'">
<div
class="editor-edit"
@click.self="isFocus = true"
@scroll="editScroll"
:style="'background-color:' + editorBackground + ';'"
ref="edit"
>
<AutoTextarea ref="autoTextarea" :preArr="preArr" :focus.sync="isFocus" v-model="editContent" />
</div>
<div class="editor-show" v-show="isPreview" :style="'background-color:' + previewBackground + ';'" ref="preview">
<div class="show-content" v-html="html" v-show="!isHtmlcode"></div>
<div class="show-content-html" v-text="html" v-show="isHtmlcode"></div>
</div>
</div>
</div>
</template>
<script>
import ToolbarLeft from "./src/components/toolbar-left.vue";
import ToolbarRight from "./src/components/toolbar-right.vue";
import AutoTextarea from "./src/components/auto-textarea.vue";
import CONFIG from "./src/lib/config";
import { toolbarLeftClick, toolbarLeftEspecial } from "./src/lib/toolbar-left-click";
import toolbarRightClick from "./src/lib/toolbar-right-click";
import { insertTextAtCaret, insertOl, insertUl, scrollSync, keydownEvent, keyupEvent, mdParse } from "./src/lib/core";
import mdFunc from "./src/lib/markdown";
import "./src/font/iconfont.css";
import lang from "./src/lang";
export default {
name: "JEditor",
props: {
toolbars: {
//工具栏
type: Object,
default() {
return CONFIG.toolbars;
},
},
color: {
//颜色
type: Array,
default() {
return CONFIG.color;
},
},
fontSize: {
// 字体大小
type: String,
default: "14px",
},
toolbarsBackground: {
// 工具栏背景色
type: String,
default: "#ffffff",
},
editorBackground: {
// TODO: 编辑栏背景色
type: String,
default: "#ffffff",
},
previewBackground: {
// 预览栏背景色
type: String,
default: "#fbfbfb",
},
preview: {
//预览
type: Boolean,
default: true,
},
fullscreen: {
//全屏编辑
type: Boolean,
default: false,
},
value: {
// 初始 value
type: String,
default: "",
},
htmlcode: {
// 查看html
type: Boolean,
default: false,
},
imageUoload: Function, //自定义上传方法
imageComplete: Function, //覆盖图片按钮方法,使用这个将不出现下拉选项
action: {
//上传的地址
type: String,
default: "/",
},
headers: {
type: Object,
default() {
return {};
},
},
fileName: {
//上传的文件字段名
type: String,
default: "file",
},
fileData: {
//上传时附带的额外参数
type: Object,
default() {
return {};
},
},
tabSize: {
type: Number,
default: 4,
},
"on-success": Function, //上传成功回调
"on-progress": Function, //上传进度回调
"on-error": Function, //上传失败回调
save: Function,
hljs: Object,
languages: Object,
i18n: {
type: String,
default: "en",
},
},
data() {
return {
isFocus: false,
editContent: "",
isPreview: true,
isFullscreen: false,
isHtmlcode: false,
html: "",
editTimer: null,
scrollOption: [],
ctrlDown: false,
valueTimer: null,
md: {},
hljsObj: null,
hljsLang: null,
tokens: [],
preArr: "",
scrollLock: false, //滚动锁,在输入时禁止
resizeTimer: null,
};
},
watch: {
value(val) {
this.editContent = val === null ? "" : val;
},
/**
* 与父组件数据双向绑定,textarea组件传值过来后使父组件改变值将值流动到本组件
*/
editContent(val) {
this.scrollLock = true;
let dom = mdParse(this.md, val, this);
this.html = dom.html;
this.preArr = dom.pre;
this.$emit("input", val);
if (this.valueTimer) {
clearTimeout(this.valueTimer);
this.valueTimer = null;
}
this.valueTimer = setTimeout(() => {
this.$nextTick(() => {
this.textOffset();
});
}, 40);
},
preview(val) {
this.isPreview = val;
},
fullscreen(val) {
this.isFullscreen = val;
},
htmlcode(val) {
this.isHtmlcode = val;
},
},
mounted() {
this.initValue();
this.$nextTick(() => {
this.textOffset();
});
window.addEventListener("resize", this.windowResize);
},
created() {
lang(this.i18n);
this.hljsObj = this.$j_hljs && this.$j_hljs.hljs ? this.$j_hljs.hljs : this.hljs;
this.hljsLang = this.$j_hljs && this.$j_hljs.languages ? this.$j_hljs.languages : this.languages;
this.md = mdFunc(this.hljsObj, this.hljsLang);
},
methods: {
initValue() {
this.editContent = this.value === null ? "" : this.value;
this.isPreview = this.preview;
this.isFullscreen = this.fullscreen;
this.isHtmlcode = this.htmlcode;
},
/**
* 执行右工具栏中的点击事件
* @param {[type]} type 点击类型
*/
toolbar_right_click(type) {
toolbarRightClick(type, this);
},
/**
* 执行左工具栏中的点击事件
* @param {[type]} type 点击类型
*/
toolbar_left_click(type) {
toolbarLeftClick(type, this);
},
/**
* 执行左工具栏中的自定义标签操作
* @param {[type]} op 操作参数
* @param {[type]} op.type 点击类型
* @param {[type]} op.val 值
*/
toolbar_left_especial(op) {
toolbarLeftEspecial(op, this);
},
/**
* 切换预览
* @param {[type]} val 是否预览
*/
previewtoggle(val) {
this.isPreview = val;
this.$emit("update:preview", val);
},
/**
* 切换全屏
* @param {[type]} val 是否全屏
*/
fullscreentoggle(val) {
this.isFullscreen = val;
this.$emit("update:fullscreen", val);
},
/**
* 查看输入结果html
* @param {[type]} val 是否查看输入结果html
*/
htmlcodetoggle(val) {
this.isHtmlcode = val;
this.$emit("update:htmlcode", val);
},
/**
* 获取textarea节点
* @return 返回一个节点
*/
getAutoTextarea() {
return this.$refs.autoTextarea.$refs.textarea;
},
/**
* 编辑器插入文案
* @param {[type]} dom 编辑器textarea节点
* @param {[type]} prefix 输入文案前缀
* @param {[type]} subfix 输入文案后缀
* @param {[type]} str 输入文案
*/
insertText(insertText) {
insertTextAtCaret(this.getAutoTextarea(), insertText, this);
},
/**
* 插入有序列表
*/
insertOl() {
insertOl(this.getAutoTextarea(), this);
},
/**
* 插入无序列表
*/
insertUl() {
insertUl(this.getAutoTextarea(), this);
},
/**
* 获取节点数
*/
textOffset() {
let option = {
editRow: document.querySelectorAll(".auto-textarea .code pre.isblock"),
showRow: document.querySelector(".editor-show").querySelectorAll("p,h1,h2,h3,h4,h5,h6,table,pre,ul,ol"),
editEndTop: document.querySelector(".auto-textarea .code").clientHeight,
showEndTop: document.querySelector(".editor-show .show-content").clientHeight,
preOffset: [],
};
this.inputScroll(option);
for (let i = 0; i < option.editRow.length; i++) {
option.preOffset.push(option.editRow[i].offsetTop);
}
option.preOffset.push(option.editEndTop);
this.scrollOption = option;
this.scrollLock = false;
},
/**
* 监听编辑输入联动预览滚动距离
*/
inputScroll(option) {
let currentRow = document.querySelector(".auto-textarea .code .isblock.current");
if (currentRow) {
let index = currentRow.dataset.index,
rowPre = (currentRow.dataset.row - 1) / currentRow.dataset.rows;
this.$refs.preview.scrollTop = option.showRow[index - 1].offsetTop + currentRow.offsetHeight * rowPre - 40;
}
},
/**
* 监听编辑栏滚动
*/
editScroll(e) {
if (this.isHtmlcode) return false;
if (this.editTimer) {
clearTimeout(this.editTimer);
this.editTimer = null;
}
this.editTimer = setTimeout(() => {
scrollSync(e, this);
}, 10);
},
/**
* 监听键盘按下
*/
onKeydown(e) {
if (e.keyCode == 17 || e.keyCode == 91) this.ctrlDown = true;
keydownEvent(e, this);
},
/**
* 监听键盘抬起
*/
onKeyup(e) {
if (e.keyCode == 17 || e.keyCode == 91) this.ctrlDown = false;
keyupEvent(e, this);
},
/**
* 窗口大小改变
*/
windowResize() {
if (this.resizeTimer) {
clearTimeout(this.resizeTimer);
}
this.resizeTimer = setTimeout(() => {
this.textOffset();
}, 300);
},
},
beforeDestroy() {
window.removeEventListener("resize", this.windowResize);
},
components: {
ToolbarLeft,
ToolbarRight,
AutoTextarea,
},
};
</script>