Files
tiptap/packages/extension-superscript/src/superscript.ts
Philipp Kühn 9afadeb7fe feat!: Replace defaultOptions with addOptions (#2088)
* add new addOptions option

* replace defaultOptions with addOptions for all extensions

* replace defaultOptions with addOptions for all demos

* replace defaultOptions with addOptions in docs

* refactoring

* refactoring

* drop object support for addOptions

* fix optional options

* fix tests
2021-10-26 18:31:13 +02: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('superscript')
},
toggleSuperscript: () => ({ commands }) => {
return commands.toggleMark('superscript')
},
unsetSuperscript: () => ({ commands }) => {
return commands.unsetMark('superscript')
},
}
},
addKeyboardShortcuts() {
return {
'Mod-.': () => this.editor.commands.toggleSuperscript(),
}
},
})