Skip to content

Commit dc3672f

Browse files
authored
Allow Checkbox to be toggled in viewmode
1 parent bf34d1d commit dc3672f

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

Diff for: src/components/EditorMarkdownIt.vue

+47-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export default {
1515
type: String,
1616
required: true,
1717
},
18+
readonly: {
19+
type: Boolean,
20+
required: true,
21+
},
1822
noteid: {
1923
type: String,
2024
required: true,
@@ -29,7 +33,7 @@ export default {
2933
})
3034
3135
md.use(require('markdown-it-task-checkbox'), {
32-
disabled: true,
36+
disabled: this.readonly,
3337
liClass: 'task-list-item',
3438
})
3539
@@ -51,7 +55,48 @@ export default {
5155
methods: {
5256
onUpdate() {
5357
this.html = this.md.render(this.value)
58+
if (!this.readonly) {
59+
setTimeout(() => this.prepareOnClickListener(), 100)
60+
}
61+
},
62+
63+
prepareOnClickListener() {
64+
const items = document.getElementsByClassName('task-list-item')
65+
for (let i = 0; i < items.length; ++i) {
66+
items[i].removeEventListener('click', this.onClickListItem)
67+
items[i].addEventListener('click', this.onClickListItem)
68+
}
69+
},
70+
71+
onClickListItem(event) {
72+
event.stopPropagation()
73+
let idOfCheckbox = 0
74+
const markdownLines = this.value.split('\n')
75+
markdownLines.forEach((line, i) => {
76+
// Regex Source: https://github.com/linsir/markdown-it-task-checkbox/blob/master/index.js#L121
77+
// plus the '- '-string.
78+
if (/^[-+*]\s+\[[xX \u00A0]\][ \u00A0]/.test(line.trim())) {
79+
markdownLines[i] = this.checkLine(line, i, idOfCheckbox, event.target)
80+
idOfCheckbox++
81+
}
82+
})
83+
84+
this.$emit('input', markdownLines.join('\n'))
85+
},
86+
87+
checkLine(line, index, id, target) {
88+
let returnValue = line
89+
if ('cbx_' + id === target.id) {
90+
if (target.checked) {
91+
returnValue = returnValue.replace(/\[[ \u00A0]\]/, '[x]')
92+
} else {
93+
// matches [x] or [X], to prevent two occurences of uppercase and lowercase X to be replaced
94+
returnValue = returnValue.replace(/\[[xX]\]/, '[ ]')
95+
}
96+
}
97+
return returnValue
5498
},
99+
55100
setImageRule(id) {
56101
// https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer
57102
// Remember old renderer, if overridden, or proxy to default renderer
@@ -182,6 +227,7 @@ export default {
182227
list-style-type: none;
183228
input {
184229
min-height: initial !important;
230+
cursor: pointer;
185231
}
186232
label {
187233
cursor: default;

Diff for: src/components/Note.vue

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@
3131
<div v-show="!note.content" class="placeholder">
3232
{{ preview ? t('notes', 'Empty note') : t('notes', 'Write …') }}
3333
</div>
34-
<ThePreview v-if="preview" :value="note.content" :noteid="noteId" />
34+
<ThePreview v-if="preview"
35+
:value="note.content"
36+
:noteid="noteId"
37+
:readonly="note.readonly"
38+
@input="onEdit"
39+
/>
3540
<TheEditor v-else
3641
:value="note.content"
3742
:noteid="noteId"

0 commit comments

Comments
 (0)