tiptap/packages/extension-italic/index.ts

68 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-09-24 06:29:05 +08:00
import {
2020-10-22 18:34:49 +08:00
Command, createMark, markInputRule, markPasteRule,
2020-09-24 06:29:05 +08:00
} from '@tiptap/core'
2020-09-22 05:17:30 +08:00
2020-10-22 18:34:49 +08:00
// export type ItalicCommand = () => Command
2020-03-31 06:07:12 +08:00
2020-10-22 18:34:49 +08:00
// declare module '@tiptap/core/src/Editor' {
// interface Commands {
// italic: ItalicCommand,
// }
// }
2020-04-01 04:17:54 +08:00
2020-09-24 06:37:31 +08:00
export const starInputRegex = /(?:^|\s)((?:\*)((?:[^*]+))(?:\*))$/gm
export const starPasteRegex = /(?:^|\s)((?:\*)((?:[^*]+))(?:\*))/gm
2020-09-10 00:47:47 +08:00
export const underscoreInputRegex = /(?:^|\s)((?:_)((?:[^_]+))(?:_))$/gm
export const underscorePasteRegex = /(?:^|\s)((?:_)((?:[^_]+))(?:_))/gm
2020-10-22 18:34:49 +08:00
export default createMark({
name: 'italic',
parseHTML() {
return [
2020-10-08 09:57:45 +08:00
{
tag: 'em',
},
{
tag: 'i',
getAttrs: node => (node as HTMLElement).style.fontStyle !== 'normal' && null,
},
{
style: 'font-style=italic',
},
2020-10-22 18:34:49 +08:00
]
},
renderHTML({ attributes }) {
return ['em', attributes, 0]
},
addCommands() {
return {
italic: () => ({ commands }) => {
return commands.toggleMark('italic')
},
}
},
addKeyboardShortcuts() {
return {
'Mod-i': () => this.editor.italic(),
}
},
addInputRules() {
return [
markInputRule(starInputRegex, this.type),
markInputRule(underscoreInputRegex, this.type),
]
},
addPasteRules() {
return [
markPasteRule(starPasteRegex, this.type),
markPasteRule(underscorePasteRegex, this.type),
]
},
})