tiptap/packages/extension-font-family/index.ts

56 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-11-06 22:14:04 +08:00
import { Command, createExtension } from '@tiptap/core'
type FontFamilyOptions = {
types: string[],
}
const FontFamily = createExtension({
defaultOptions: <FontFamilyOptions>{
types: ['textStyle'],
},
addGlobalAttributes() {
return [
{
types: this.options.types,
attributes: {
fontFamily: {
default: null,
2020-11-06 23:21:19 +08:00
renderHTML: attributes => {
if (!attributes.fontFamily) {
return {}
}
return {
style: `font-family: ${attributes.fontFamily}`,
}
},
2020-11-06 22:14:04 +08:00
parseHTML: element => ({
2020-11-06 23:21:19 +08:00
fontFamily: element.style.fontFamily.replace(/['"]+/g, ''),
2020-11-06 22:14:04 +08:00
}),
},
},
},
]
},
addCommands() {
return {
2020-11-06 23:21:19 +08:00
fontFamily: (fontFamily: string | null = null): Command => ({ chain }) => {
return chain()
.updateMarkAttributes('textStyle', { fontFamily })
.removeEmptyTextStyle()
.run()
2020-11-06 22:14:04 +08:00
},
}
},
})
export default FontFamily
declare module '@tiptap/core/src/Editor' {
interface AllExtensions {
FontFamily: typeof FontFamily,
}
}