Files
tiptap/packages/extension-superscript/src/superscript.ts
2021-12-02 14:58:15 +01:00

78 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Mark, mergeAttributes } from '@tiptap/core'
export interface SuperscriptExtensionOptions {
HTMLAttributes: Object,
}
declare module '@tiptap/core' {
interface Commands<ReturnType> {
superscript: {
/**
* Set a superscript mark
*/
setSuperscript: () => ReturnType,
/**
* Toggle a superscript mark
*/
toggleSuperscript: () => ReturnType,
/**
* Unset a superscript mark
*/
unsetSuperscript: () => ReturnType,
}
}
}
export const Superscript = Mark.create<SuperscriptExtensionOptions>({
name: 'superscript',
addOptions() {
return {
HTMLAttributes: {},
}
},
parseHTML() {
return [
{
tag: 'sup',
},
{
style: 'vertical-align',
getAttrs(value) {
// Dont match this rule if the vertical align isnt super.
if (value !== 'super') {
return false
}
// If it falls through well match, and this mark will be applied.
},
},
]
},
renderHTML({ HTMLAttributes }) {
return ['sup', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},
addCommands() {
return {
setSuperscript: () => ({ commands }) => {
return commands.setMark(this.name)
},
toggleSuperscript: () => ({ commands }) => {
return commands.toggleMark(this.name)
},
unsetSuperscript: () => ({ commands }) => {
return commands.unsetMark(this.name)
},
}
},
addKeyboardShortcuts() {
return {
'Mod-.': () => this.editor.commands.toggleSuperscript(),
}
},
})