add ordered list extension

This commit is contained in:
Philipp Kühn
2020-09-22 20:45:30 +02:00
parent 2c9973af8c
commit 9229de8881
2 changed files with 68 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
import { Command, Node } from '@tiptap/core'
import { wrappingInputRule } from 'prosemirror-inputrules'
export type OrderedListCommand = () => Command
declare module '@tiptap/core/src/Editor' {
interface Commands {
orderedList: OrderedListCommand,
}
}
export default new Node()
.name('ordered_list')
.schema(() => ({
attrs: {
order: {
default: 1,
},
},
content: 'list_item+',
group: 'block',
parseDOM: [{
tag: 'ol',
getAttrs: node => ({
order: (node as HTMLElement).hasAttribute('start')
? +(node as HTMLElement).getAttribute('start')
: 1,
}),
}],
toDOM: node => (node.attrs.order === 1
? ['ol', 0]
: ['ol', { start: node.attrs.order }, 0]
),
}))
.commands(({ name }) => ({
orderedList: () => ({ commands }) => {
return commands.toggleList(name, 'list_item')
},
}))
.keys(({ editor }) => ({
'Shift-Ctrl-9': () => editor.orderedList(),
}))
.inputRules(({ type }) => [
wrappingInputRule(
/^(\d+)\.\s$/,
type,
match => ({ order: +match[1] }),
(match, node) => node.childCount + node.attrs.order === +match[1],
),
])
.create()

View File

@@ -0,0 +1,17 @@
{
"name": "@tiptap/extension-ordered-list",
"version": "1.0.0",
"source": "index.ts",
"main": "dist/tiptap-extension-ordered-list.js",
"umd:main": "dist/tiptap-extension-ordered-list.umd.js",
"module": "dist/tiptap-extension-ordered-list.mjs",
"unpkg": "dist/tiptap-extension-ordered-list.js",
"jsdelivr": "dist/tiptap-extension-ordered-list.js",
"files": [
"src",
"dist"
],
"peerDependencies": {
"@tiptap/core": "2.x"
}
}