1
1
<template >
2
2
<div class =" editor" >
3
- <editor
4
- :value = " editorText "
5
- :options = " editorOptions "
6
- :html = " editorHtml "
7
- :visible = " editorVisible "
8
- height = " 500px "
9
- mode = " markdown "
10
- previewStyle = " vertical "
3
+ <codemirror
4
+ ref = " cm "
5
+ v-model = " text "
6
+ :hidden = " !editorVisible "
7
+ :options = " editorOptions "
8
+ @blur = " onCmBlur "
9
+ @focus = " onCmFocus "
10
+ @ready = " onCmReady "
11
11
/>
12
+ <div :hidden =" editorVisible" id =" renderContainer" >
13
+ <VueShowdown :extensions =" [enableCheckBoxExt]" :markdown =" text" />
14
+ </div >
12
15
</div >
13
16
</template >
14
17
<script lang="ts">
15
- import { codemirror } from ' vue-codemirror' ;
16
- import ' codemirror/lib/codemirror.css' ;
17
18
import { Component , Prop , Vue , Watch } from " vue-property-decorator" ;
19
+ import { codemirror } from " vue-codemirror" ;
20
+ import " codemirror/lib/codemirror.css" ;
21
+ import " codemirror/mode/gfm/gfm.js" ;
22
+ import " codemirror/addon/edit/continuelist.js" ;
23
+ import VueShowdown from " vue-showdown" ;
24
+ import { clearTimeout } from " timers" ;
25
+ import { constants } from ' http2' ;
26
+
27
+ var myext2 = {
28
+ type: " lang" ,
29
+ regex: / markdown/ g ,
30
+ replace: " showdown"
31
+ };
32
+ Vue .use (VueShowdown , {
33
+ flavor: " github" ,
34
+ options: {
35
+ emoji: true ,
36
+ tasklists: true
37
+ }
38
+ });
18
39
@Component ({
19
- components: {
20
- codemirror
21
- }
40
+ components: {
41
+ codemirror
42
+ }
22
43
})
23
- export default class TuiEditor extends Vue {
24
- data() {
25
- return {
26
- editorText: ' This is initialValue. ' ,
27
- editorOptions : {
28
- hideModeSwitch: true ,
29
- toolbarItems:[]
30
- } ,
31
- editorHtml: ' ' ,
32
- editorVisible : true
33
- };
34
- }
35
- onEditorLoad() {
36
- console . log ( " Test " );
37
- }
44
+ export default class CodeMirrorEditor extends Vue {
45
+ private cm;
46
+ private readonly editorOptions = {
47
+ lineNumbers: false ,
48
+ mode : {
49
+ name: " gfm " ,
50
+ tokenTypeOverrides: {
51
+ emoji: true ,
52
+ taskLists: true ,
53
+ strikethrough : true ,
54
+ gitHubSpice: true
55
+ }
56
+ },
57
+ extraKeys: { Enter: " newlineAndIndentContinueMarkdownList " }
58
+ };
38
59
39
- onEditorFocus() {
40
- // implement your code
41
- }
60
+ private enableCheckBoxExt = () => {
61
+ var c = 0 ;
62
+ return [
63
+ {
64
+ type: " lang" ,
65
+ regex: / $ / g ,
66
+ replace : () => {
67
+ c = 0 ;
68
+ return " " ;
69
+ }
70
+ },
71
+ {
72
+ type: " output" ,
73
+ regex: / disabled/ gm ,
74
+ replace : (match , $1 ) => {
75
+ return ` id="checkbox-${c ++ }" ` ;
76
+ }
77
+ }
78
+ ];
79
+ };
42
80
43
- onEditorBlur() {
44
- // implement your code
45
- }
81
+ public text = " - [ ] test\n - [ ] test2\n blah blah\n - [ ] test\n - [ ] test2" ;
46
82
47
- onEditorChange() {
48
- // implement your code
49
- }
83
+ public get editorText() {
84
+ return this .text ;
85
+ }
86
+ public set editorText(text ) {
87
+ this .text = text ;
88
+ }
89
+ private editorVisible = true ;
90
+
91
+ onCmReady(cm ) {
92
+ this .cm = cm ;
93
+ }
50
94
51
- onEditorStateChange() {
52
- // implement your code
95
+ mounted() {
96
+ document .addEventListener (
97
+ " click" ,
98
+ e => {
99
+ var target = <any >e .target ;
100
+ if (target .type == " checkbox" ) {
101
+ var i = - 1 ;
102
+ this .editorText = this .editorText .replace (/ \[ [\s xX] \] / g , match => {
103
+ i ++ ;
104
+ return i == target .id .replace (" checkbox-" ," " ) ?
105
+ match .replace (/ \[ [\s xX] \] / g , target .checked ? " [x]" : " [ ]" ) : match ;
106
+ });
107
+ }
108
+ else if (! target .className .includes (" cm" ) && ! target .className .includes (" CodeMirror" )){
109
+ console .log (target .className )
110
+ this .onCmBlur ();
111
+ }
112
+ },
113
+ false
114
+ );
115
+ }
116
+
117
+ onCmFocus() {
118
+ this .cm .setCursor (this .cm .lineCount (), 0 );
119
+ }
120
+
121
+ onCmBlur() {
122
+ this .editorVisible = ! this .editorVisible ;
123
+ if (this .editorVisible ) {
124
+ var timeout = setTimeout (() => {
125
+ console .log (" focused" );
126
+ this .cm .focus ();
127
+ }, 100 );
128
+ clearTimeout (timeout );
53
129
}
54
- };
130
+ }
131
+ }
55
132
</script >
0 commit comments