Skip to content

Feature/858/checkboxtoggle #861

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

Merged
merged 17 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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
49 changes: 48 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: false,
liClass: 'task-list-item',
})

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

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

setListener(item) {
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 (/^(- )\[[xX \u00A0]\][ \u00A0]/.test(line.trim())) {
markdownLines[i] = this.checkLine(line, i, idOfCheckbox, item.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('[ ]', '[x]')
returnValue = returnValue.replace('[\u00A0]', '[x]')
} else {
returnValue = returnValue.replace('[x]', '[ ]')
returnValue = returnValue.replace('[X]', '[ ]')
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work correctly if there is a [ ] or [x] in the item's text, e.g. - [x] this is an example [x] with checkbox. In this case, the text is also changed. In order to fix this, it must be assured, that only a single replace is done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as i know the .replace() function only replaces the first occurence, when no regex is used. Even if a regex is applied, when the global-flag is missing it will also only chamge the first occurence. So this should be fine, did your test yield different results?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try my example. It doesn't work correctly. But I did not found out why.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works just fine for me, have you cleared the cache? (I had problems when the developer console wasnt open, it was using some broken hybrid version from cache)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only has the behaviour you describe if we have both an uppercase and a lowercase X in the line. I will have to fix this particular issue though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've found out that it happens on sub-items only, see the following example:

- [ ] check [ ] box (works)
 - [ ] sub check [ ] box (does not work)
- [ ] check [ ] box (works)

Copy link
Contributor Author

@newhinton newhinton Jul 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • check [ ] box (works)
  • sub check [ ] box (does not work)
  • check [ ] box (works)

I also cannot reproduce this. Is the sub-element supposed to be indented? Because on my build it behaves identical to the other two (including the checkbox behaviour)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the second item should be indented. See this screenshot:

Screenshot

}
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 +227,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