Skip to content

Commit edfff0a

Browse files
committed
feat: add tabSize prop
1 parent 10f8f91 commit edfff0a

File tree

11 files changed

+77
-33
lines changed

11 files changed

+77
-33
lines changed

dev/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import VueMarkdownEditor from '@/codemirror-editor';
66
import Codemirror from 'codemirror';
77
// mode
88
import 'codemirror/mode/markdown/markdown';
9+
import 'codemirror/mode/javascript/javascript';
10+
import 'codemirror/mode/css/css';
11+
import 'codemirror/mode/htmlmixed/htmlmixed';
12+
import 'codemirror/mode/vue/vue';
13+
// edit
14+
import 'codemirror/addon/edit/closebrackets';
15+
import 'codemirror/addon/edit/closetag';
16+
import 'codemirror/addon/edit/matchbrackets';
917
// placeholder
1018
import 'codemirror/addon/display/placeholder';
1119
// active-line

src/assets/css/theme/vuepress-markdown.css

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
word-wrap: normal;
1010
word-break: normal;
1111
word-spacing: normal;
12-
-moz-tab-size: 4;
13-
-o-tab-size: 4;
14-
tab-size: 4;
1512
-webkit-hyphens: none;
1613
-ms-hyphens: none;
1714
hyphens: none;

src/base-editor.vue

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
>
4242
<v-md-preview
4343
:text="text"
44+
:tab-size="tabSize"
4445
:scroll-container="getPreviewScrollContainer"
4546
@change="handleChange"
4647
ref="preview"
@@ -73,7 +74,7 @@ const component = {
7374
this.setFocusEnd();
7475
},
7576
// Must implement
76-
editorFocusEnd () {
77+
editorFocusEnd() {
7778
this.focus();
7879
7980
this.$refs.editorEgine.setRange({
@@ -82,7 +83,7 @@ const component = {
8283
});
8384
},
8485
// Must implement
85-
delLineLeft () {
86+
delLineLeft() {
8687
const { editorEgine } = this.$refs;
8788
const { start } = editorEgine.getRange();
8889
@@ -91,25 +92,25 @@ const component = {
9192
this.replaceSelectionText('\n');
9293
},
9394
// Must implement
94-
getCursorLineLeftText () {
95+
getCursorLineLeftText() {
9596
const { start, end } = this.$refs.editorEgine.getRange();
9697
9798
return start === end ? this.text.slice(0, start).split('\n').pop() : null;
9899
},
99100
// Must implement
100-
editorRegisterHotkeys (...arg) {
101+
editorRegisterHotkeys(...arg) {
101102
this.$refs.editorEgine.registerHotkeys(...arg);
102103
},
103104
// Must implement
104-
editorScrollToTop (scrollTop) {
105+
editorScrollToTop(scrollTop) {
105106
this.$refs.editorScroller.scrollTo(scrollTop);
106107
},
107108
// Must implement
108-
getScrollInfo () {
109+
getScrollInfo() {
109110
return this.$refs.editorScroller.getScrollInfo();
110111
},
111112
// Must implement
112-
heightAtLine (...arg) {
113+
heightAtLine(...arg) {
113114
return this.$refs.editorEgine.heightAtLine(...arg);
114115
},
115116
// Must implement
@@ -135,7 +136,7 @@ const component = {
135136
this.$refs.editorEgine.insertText(text);
136137
},
137138
// Must implement
138-
getCurrentSelectedStr () {
139+
getCurrentSelectedStr() {
139140
const { start, end } = this.$refs.editorEgine.getRange();
140141
141142
return end > start ? this.text.slice(start, end) : null;

src/codemirror-editor.vue

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
>
3535
<v-md-preview
3636
:text="text"
37+
:tab-size="tabSize"
3738
:scroll-container="getPreviewScrollContainer"
3839
@change="handleChange"
3940
ref="preview"
@@ -84,15 +85,20 @@ const component = {
8485
}
8586
8687
this.codemirrorInstance = new this.Codemirror(this.$refs.codemirrorEditor, {
87-
tabSize: 2,
8888
lineNumbers: true,
8989
styleActiveLine: true,
90+
autoCloseTags: true,
91+
matchBrackets: true,
92+
indentWithTabs: true,
93+
autoCloseBrackets: true,
94+
...this.codemirrorConfig,
95+
tabSize: this.tabSize,
96+
indentUnit: this.tabSize,
9097
value: this.text,
9198
placeholder: this.placeholder,
9299
mode: 'markdown',
93-
lineWrapping: 'wrap',
100+
lineWrapping: true,
94101
scrollbarStyle: 'overlay',
95-
...this.codemirrorConfig,
96102
});
97103
98104
this.codemirrorInstance.on('change', () => {

src/mixins/common.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export default {
3131
},
3232
autofocus: Boolean,
3333
placeholder: String,
34+
tabSize: {
35+
type: Number,
36+
default: 2,
37+
},
3438
},
3539
data() {
3640
return {

src/mixins/hotkeys.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ export default function (Component) {
1919
});
2020
},
2121
methods: {
22-
registerHotkeys({ modifier, key, action }) {
23-
this.editorRegisterHotkeys({ modifier, key, action: (...arg) => action(this, ...arg) });
22+
registerHotkeys({ modifier, key, action, preventDefault = true }) {
23+
this.editorRegisterHotkeys({
24+
modifier,
25+
key,
26+
preventDefault,
27+
action: (...arg) => action(this, ...arg),
28+
});
2429
},
2530
},
2631
};

src/mixins/list.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default {
1010

1111
this.registerHotkeys({
1212
key: 'enter',
13+
preventDefault: false,
1314
action: (editor, e) => {
1415
if (e.isComposing) return;
1516

@@ -20,11 +21,14 @@ export default {
2021
if (ol.test(cursorLineLeftText)) {
2122
suffix = 'x. ';
2223
syntax = olSyntax;
24+
25+
e.preventDefault();
2326
} else if (ul.test(cursorLineLeftText)) {
2427
suffix = '- ';
2528
syntax = ulSyntax;
29+
30+
e.preventDefault();
2631
} else {
27-
this.replaceSelectionText('\n', 'end');
2832
return;
2933
}
3034

@@ -39,10 +43,6 @@ export default {
3943
}
4044

4145
this.replaceSelectionText(`\n${beforeText}`, 'end');
42-
} else {
43-
// break
44-
this.delLineLeft();
45-
this.replaceSelectionText('\n', 'end');
4646
}
4747
},
4848
});

src/mixins/preview.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export default {
1313
type: Number,
1414
default: 0,
1515
},
16+
tabSize: {
17+
type: Number,
18+
default: 2,
19+
},
1620
},
1721
components: {
1822
[ImagePreview.name]: ImagePreview,

src/preview-html.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<template>
22
<div
33
class="v-md-editor-preview"
4+
:style="{
5+
tabSize,
6+
'-moz-tab-size': tabSize,
7+
'-o-tab-size': tabSize
8+
}"
49
@click="handlePreviewClick"
510
>
611
<div

src/preview.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<template>
22
<div
33
class="v-md-editor-preview"
4+
:style="{
5+
tabSize,
6+
'-moz-tab-size': tabSize,
7+
'-o-tab-size': tabSize
8+
}"
49
@click="handlePreviewClick"
510
>
611
<div

0 commit comments

Comments
 (0)