2020-11-06 22:14:04 +08:00
|
|
|
import { Command, createExtension } from '@tiptap/core'
|
2020-11-07 01:25:33 +08:00
|
|
|
import '@tiptap/extension-text-style'
|
2020-11-06 22:14:04 +08:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|