feature: add subscript and superscript extensions, docs and tests

This commit is contained in:
Hans Pagel
2021-06-01 16:18:25 +02:00
committed by Hans Pagel
parent 7387eaf51b
commit 3f57a13d19
16 changed files with 492 additions and 10 deletions

View File

@@ -0,0 +1,69 @@
import { Command, Mark, mergeAttributes } from '@tiptap/core'
export interface SubscriptExtensionOptions {
HTMLAttributes: Object,
}
declare module '@tiptap/core' {
interface Commands {
subscript: {
/**
* Set a subscript mark
*/
setSubscript: () => Command,
/**
* Toggle a subscript mark
*/
toggleSubscript: () => Command,
/**
* Unset a subscript mark
*/
unsetSubscript: () => Command,
}
}
}
export const Subscript = Mark.create<SubscriptExtensionOptions>({
name: 'subscript',
defaultOptions: {
HTMLAttributes: {},
},
parseHTML() {
return [
{
tag: 'sub',
},
{
style: 'vertical-align',
getAttrs(value) {
// Dont match this rule if the vertical align isnt sub.
if (value !== 'sub') {
return false
}
// If it falls through well match, and this mark will be applied.
},
},
]
},
renderHTML({ HTMLAttributes }) {
return ['sub', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},
addCommands() {
return {
setSubscript: () => ({ commands }) => {
return commands.setMark('subscript')
},
toggleSubscript: () => ({ commands }) => {
return commands.toggleMark('subscript')
},
unsetSubscript: () => ({ commands }) => {
return commands.unsetMark('subscript')
},
}
},
})