2021-06-09 17:05:41 +08:00
|
|
|
|
import { Mark, mergeAttributes } from '@tiptap/core'
|
2021-06-01 22:18:25 +08:00
|
|
|
|
|
|
|
|
|
export interface SuperscriptExtensionOptions {
|
|
|
|
|
HTMLAttributes: Object,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
declare module '@tiptap/core' {
|
2021-06-09 17:05:41 +08:00
|
|
|
|
interface Commands<ReturnType> {
|
2021-06-01 22:18:25 +08:00
|
|
|
|
superscript: {
|
|
|
|
|
/**
|
|
|
|
|
* Set a superscript mark
|
|
|
|
|
*/
|
2021-06-09 17:05:41 +08:00
|
|
|
|
setSuperscript: () => ReturnType,
|
2021-06-01 22:18:25 +08:00
|
|
|
|
/**
|
|
|
|
|
* Toggle a superscript mark
|
|
|
|
|
*/
|
2021-06-09 17:05:41 +08:00
|
|
|
|
toggleSuperscript: () => ReturnType,
|
2021-06-01 22:18:25 +08:00
|
|
|
|
/**
|
|
|
|
|
* Unset a superscript mark
|
|
|
|
|
*/
|
2021-06-09 17:05:41 +08:00
|
|
|
|
unsetSuperscript: () => ReturnType,
|
2021-06-01 22:18:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const Superscript = Mark.create<SuperscriptExtensionOptions>({
|
|
|
|
|
name: 'superscript',
|
|
|
|
|
|
|
|
|
|
defaultOptions: {
|
|
|
|
|
HTMLAttributes: {},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
parseHTML() {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
tag: 'sup',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
style: 'vertical-align',
|
|
|
|
|
getAttrs(value) {
|
|
|
|
|
// Don’t match this rule if the vertical align isn’t super.
|
|
|
|
|
if (value !== 'super') {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If it falls through we’ll match, and this mark will be applied.
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
renderHTML({ HTMLAttributes }) {
|
|
|
|
|
return ['sup', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
|
return {
|
|
|
|
|
setSuperscript: () => ({ commands }) => {
|
|
|
|
|
return commands.setMark('superscript')
|
|
|
|
|
},
|
|
|
|
|
toggleSuperscript: () => ({ commands }) => {
|
|
|
|
|
return commands.toggleMark('superscript')
|
|
|
|
|
},
|
|
|
|
|
unsetSuperscript: () => ({ commands }) => {
|
|
|
|
|
return commands.unsetMark('superscript')
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-06-09 12:23:58 +08:00
|
|
|
|
|
|
|
|
|
addKeyboardShortcuts() {
|
|
|
|
|
return {
|
|
|
|
|
'Mod-.': () => this.editor.commands.toggleSuperscript(),
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-06-01 22:18:25 +08:00
|
|
|
|
})
|