2020-11-16 06:25:25 +08:00
|
|
|
import { Command, Extension } 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[],
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
declare module '@tiptap/core' {
|
|
|
|
interface Commands {
|
2021-02-11 01:05:02 +08:00
|
|
|
/**
|
|
|
|
* Set the font family
|
|
|
|
*/
|
2021-02-10 16:59:35 +08:00
|
|
|
setFontFamily: (fontFamily: string) => Command,
|
2021-02-11 01:05:02 +08:00
|
|
|
/**
|
|
|
|
* Unset the font family
|
|
|
|
*/
|
2021-02-10 16:59:35 +08:00
|
|
|
unsetFontFamily: () => Command,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 04:32:50 +08:00
|
|
|
export const FontFamily = Extension.create({
|
2020-12-02 16:44:46 +08:00
|
|
|
name: 'fontFamily',
|
|
|
|
|
2020-11-06 22:14:04 +08:00
|
|
|
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 {
|
2021-02-10 16:59:35 +08:00
|
|
|
setFontFamily: fontFamily => ({ chain }) => {
|
2020-11-06 23:21:19 +08:00
|
|
|
return chain()
|
2020-11-19 00:36:00 +08:00
|
|
|
.setMark('textStyle', { fontFamily })
|
2020-11-18 19:09:04 +08:00
|
|
|
.run()
|
|
|
|
},
|
2021-02-10 16:59:35 +08:00
|
|
|
unsetFontFamily: () => ({ chain }) => {
|
2020-11-18 19:09:04 +08:00
|
|
|
return chain()
|
2020-11-19 00:36:00 +08:00
|
|
|
.setMark('textStyle', { fontFamily: null })
|
2020-11-06 23:21:19 +08:00
|
|
|
.removeEmptyTextStyle()
|
|
|
|
.run()
|
2020-11-06 22:14:04 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|