Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/858/checkboxtoggle #861

Merged
merged 17 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/components/EditorMarkdownIt.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default {
type: String,
required: true,
},
readonly: {
type: Boolean,
required: true,
},
noteid: {
type: String,
required: true,
Expand All @@ -29,7 +33,7 @@ export default {
})

md.use(require('markdown-it-task-checkbox'), {
disabled: true,
disabled: this.readonly,
liClass: 'task-list-item',
})

Expand All @@ -51,7 +55,48 @@ export default {
methods: {
onUpdate() {
this.html = this.md.render(this.value)
if (!this.readonly) {
setTimeout(() => this.prepareOnClickListener(), 100)
}
},

prepareOnClickListener() {
const items = document.getElementsByClassName('task-list-item')
for (let i = 0; i < items.length; ++i) {
items[i].removeEventListener('click', this.onClickListItem)
items[i].addEventListener('click', this.onClickListItem)
}
},

onClickListItem(event) {
event.stopPropagation()
let idOfCheckbox = 0
const markdownLines = this.value.split('\n')
markdownLines.forEach((line, i) => {
// Regex Source: https://github.com/linsir/markdown-it-task-checkbox/blob/master/index.js#L121
// plus the '- '-string.
if (/^[-+*]\s+\[[xX \u00A0]\][ \u00A0]/.test(line.trim())) {
markdownLines[i] = this.checkLine(line, i, idOfCheckbox, event.target)
idOfCheckbox++
}
})

this.$emit('input', markdownLines.join('\n'))
},

checkLine(line, index, id, target) {
let returnValue = line
if ('cbx_' + id === target.id) {
if (target.checked) {
returnValue = returnValue.replace(/\[[ \u00A0]\]/, '[x]')
} else {
// matches [x] or [X], to prevent two occurences of uppercase and lowercase X to be replaced
returnValue = returnValue.replace(/\[[xX]\]/, '[ ]')
}
}
return returnValue
},

setImageRule(id) {
// https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer
// Remember old renderer, if overridden, or proxy to default renderer
Expand Down Expand Up @@ -181,6 +226,7 @@ export default {
list-style-type: none;
input {
min-height: initial !important;
cursor: pointer;
}
label {
cursor: default;
Expand Down
7 changes: 6 additions & 1 deletion src/components/Note.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@
<div v-show="!note.content" class="placeholder">
{{ preview ? t('notes', 'Empty note') : t('notes', 'Write …') }}
</div>
<ThePreview v-if="preview" :value="note.content" :noteid="noteId" />
<ThePreview v-if="preview"
:value="note.content"
:noteid="noteId"
:readonly="note.readonly"
@input="onEdit"
/>
<TheEditor v-else
:value="note.content"
:noteid="noteId"
Expand Down