add ordered list extension
This commit is contained in:
51
packages/extension-ordered-list/index.ts
Normal file
51
packages/extension-ordered-list/index.ts
Normal 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()
|
||||
17
packages/extension-ordered-list/package.json
Normal file
17
packages/extension-ordered-list/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user