Skip to content

Commit 3310b82

Browse files
committed
new editor mostly implemented using codemirror and showdown, syncs checkbox state
1 parent b9f9c9c commit 3310b82

File tree

4 files changed

+123
-100
lines changed

4 files changed

+123
-100
lines changed

public/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ body {
1111
border-radius: 5px;
1212
}
1313

14+
.cm-s-default{
15+
background:transparent !important;
16+
}
1417
/* .editor .tuieditor .CodeMirror .tui-editor-defaultUI */
1518
#app #home{
1619
font-family: 'Avenir', Helvetica, Arial, sans-serif;
Lines changed: 117 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,132 @@
11
<template>
22
<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"
1111
/>
12+
<div :hidden="editorVisible" id="renderContainer">
13+
<VueShowdown :extensions="[enableCheckBoxExt]" :markdown="text"/>
14+
</div>
1215
</div>
1316
</template>
1417
<script lang="ts">
15-
import { codemirror } from 'vue-codemirror';
16-
import 'codemirror/lib/codemirror.css';
1718
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+
});
1839
@Component({
19-
components: {
20-
codemirror
21-
}
40+
components: {
41+
codemirror
42+
}
2243
})
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+
};
3859
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+
};
4280
43-
onEditorBlur() {
44-
// implement your code
45-
}
81+
public text = "- [ ] test\n- [ ] test2\nblah blah\n- [ ] test\n- [ ] test2";
4682
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+
}
5094
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(/\[[\sxX]\]/g, match => {
103+
i++;
104+
return i == target.id.replace("checkbox-","") ?
105+
match.replace(/\[[\sxX]\]/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);
53129
}
54-
};
130+
}
131+
}
55132
</script>

src/components/TUIEditor.vue

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/views/Home.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@
2727
<i class="fas fa-trash"></i>
2828
</a>
2929
</NavBar>
30-
<TuiEditor v-on:finished="handleClose()" v-on:fileLoaded="setFileName"></TuiEditor>
30+
<CodeMirrorEditor v-on:finished="handleClose()" v-on:fileLoaded="setFileName"></CodeMirrorEditor>
3131
</div>
3232
</template>
3333

3434
<script lang="ts">
3535
import { Component, Vue, Watch } from "vue-property-decorator";
3636
//import Editor from "@/components/Editor.vue"; // @ is an alias to /src
37-
import TuiEditor from "@/components/TUIEditor.vue";
37+
import CodeMirrorEditor from "@/components/CodeMirrorEditor.vue";
3838
import NavBar from "@/components/NavBar.vue";
3939
import { unlink, writeFile, readdirSync } from "fs";
4040
import * as path from "path";
4141
import Configuration from "@/utility/Configuration";
4242
4343
@Component({
4444
components: {
45-
TuiEditor,
45+
CodeMirrorEditor,
4646
NavBar
4747
}
4848
})

0 commit comments

Comments
 (0)