Skip to content

Commit

Permalink
Add input rule for checkboxes
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Dec 22, 2020
1 parent b92ea12 commit b5761e3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/EditorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
HardBreak,
Heading,
Code,
BulletList,
OrderedList,
Blockquote,
CodeBlock,
Expand All @@ -34,7 +33,7 @@ import {
Placeholder,
} from 'tiptap-extensions'
import { Strong, Italic, Strike, Link } from './marks'
import { Image, PlainTextDocument, ListItem } from './nodes'
import { Image, PlainTextDocument, ListItem, BulletList } from './nodes'
import MarkdownIt from 'markdown-it'
import taskLists from 'markdown-it-task-lists'
import { translate as t } from '@nextcloud/l10n'
Expand Down
32 changes: 32 additions & 0 deletions src/nodes/BulletList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

import { BulletList as TiptapBulletList } from 'tiptap-extensions'

export default class BulletList extends TiptapBulletList {

/* The bullet list input rules are handled in the ListItem node so we can make sure that "- [ ]" can still trigger todo list items */
inputRules() {
return []
}

}
19 changes: 18 additions & 1 deletion src/nodes/ListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import { ListItem as TiptapListItem } from 'tiptap-extensions'
import { Plugin } from 'tiptap'
import { toggleList } from 'tiptap-commands'
import { toggleList, wrappingInputRule } from 'tiptap-commands'
import { findParentNode, findParentNodeClosestToPos } from 'prosemirror-utils'

const TYPES = {
Expand Down Expand Up @@ -139,6 +139,23 @@ export default class ListItem extends TiptapListItem {
}
}

inputRules({ type }) {
return [
wrappingInputRule(/^\s*([-+*])\s(\[ \])\s$/, type, (match) => {
return {
type: TYPES.CHECKBOX,
}
}),
wrappingInputRule(/^\s*([-+*])\s(\[(x|X)\])\s$/, type, (match) => {
return {
type: TYPES.CHECKBOX,
done: true,
}
}),
wrappingInputRule(/^\s*([-+*])\s[^\s[]$/, type),
]
}

get plugins() {
return [
new Plugin({
Expand Down
2 changes: 2 additions & 0 deletions src/nodes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import Image from './Image'
import PlainTextDocument from './PlainTextDocument'
import ListItem from './ListItem'
import BulletList from './BulletList'

export {
Image,
PlainTextDocument,
ListItem,
BulletList,
}

0 comments on commit b5761e3

Please sign in to comment.