Created
May 3, 2022 22:34
-
-
Save gregveres/64ec1d8a733feb735b7dd4c46331abae to your computer and use it in GitHub Desktop.
font-size for tiptap 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Extension } from "@tiptap/core"; | |
import "@tiptap/extension-text-style"; | |
export type FontSizeOptions = { | |
types: string[]; | |
}; | |
declare module "@tiptap/core" { | |
interface Commands<ReturnType> { | |
fontSize: { | |
/** | |
* Set the font size | |
*/ | |
setFontSize: (fontSize: string) => ReturnType; | |
/** | |
* Unset the font size | |
*/ | |
unsetFontSize: () => ReturnType; | |
}; | |
} | |
} | |
export const FontSize = Extension.create<FontSizeOptions>({ | |
name: "fontSize", | |
addOptions() { | |
return { | |
types: ["textStyle"], | |
}; | |
}, | |
addGlobalAttributes() { | |
return [ | |
{ | |
types: this.options.types, | |
attributes: { | |
fontSize: { | |
default: null, | |
parseHTML: (element) => | |
element.style.fontSize.replace(/['"]+/g, ""), | |
renderHTML: (attributes) => { | |
if (!attributes.fontSize) { | |
return {}; | |
} | |
return { | |
style: `font-size: ${attributes.fontSize}`, | |
}; | |
}, | |
}, | |
}, | |
}, | |
]; | |
}, | |
addCommands() { | |
return { | |
setFontSize: | |
(fontSize) => | |
({ chain }) => { | |
return chain().setMark("textStyle", { fontSize }).run(); | |
}, | |
unsetFontSize: | |
() => | |
({ chain }) => { | |
return chain() | |
.setMark("textStyle", { fontSize: null }) | |
.removeEmptyTextStyle() | |
.run(); | |
}, | |
}; | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, would it be possible to get a license added to this gist?