mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-26 10:47:49 +08:00
60 lines
1.2 KiB
TypeScript
60 lines
1.2 KiB
TypeScript
import { Command, Extension } from '@tiptap/core'
|
|
import '@tiptap/extension-text-style'
|
|
|
|
type FontFamilyOptions = {
|
|
types: string[],
|
|
}
|
|
|
|
const FontFamily = Extension.create({
|
|
defaultOptions: <FontFamilyOptions>{
|
|
types: ['textStyle'],
|
|
},
|
|
|
|
addGlobalAttributes() {
|
|
return [
|
|
{
|
|
types: this.options.types,
|
|
attributes: {
|
|
fontFamily: {
|
|
default: null,
|
|
renderHTML: attributes => {
|
|
if (!attributes.fontFamily) {
|
|
return {}
|
|
}
|
|
|
|
return {
|
|
style: `font-family: ${attributes.fontFamily}`,
|
|
}
|
|
},
|
|
parseHTML: element => ({
|
|
fontFamily: element.style.fontFamily.replace(/['"]+/g, ''),
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
]
|
|
},
|
|
|
|
addCommands() {
|
|
return {
|
|
/**
|
|
* Update the font family
|
|
*/
|
|
fontFamily: (fontFamily: string | null = null): Command => ({ chain }) => {
|
|
return chain()
|
|
.updateMarkAttributes('textStyle', { fontFamily })
|
|
.removeEmptyTextStyle()
|
|
.run()
|
|
},
|
|
}
|
|
},
|
|
})
|
|
|
|
export default FontFamily
|
|
|
|
declare module '@tiptap/core' {
|
|
interface AllExtensions {
|
|
FontFamily: typeof FontFamily,
|
|
}
|
|
}
|