mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-21 15:28:09 +08:00
68 lines
1.4 KiB
TypeScript
68 lines
1.4 KiB
TypeScript
import {
|
|
Command, createMark, markInputRule, markPasteRule,
|
|
} from '@tiptap/core'
|
|
|
|
export const starInputRegex = /(?:^|\s)((?:\*)((?:[^*]+))(?:\*))$/gm
|
|
export const starPasteRegex = /(?:^|\s)((?:\*)((?:[^*]+))(?:\*))/gm
|
|
export const underscoreInputRegex = /(?:^|\s)((?:_)((?:[^_]+))(?:_))$/gm
|
|
export const underscorePasteRegex = /(?:^|\s)((?:_)((?:[^_]+))(?:_))/gm
|
|
|
|
const Italic = createMark({
|
|
name: 'italic',
|
|
|
|
parseHTML() {
|
|
return [
|
|
{
|
|
tag: 'em',
|
|
},
|
|
{
|
|
tag: 'i',
|
|
getAttrs: node => (node as HTMLElement).style.fontStyle !== 'normal' && null,
|
|
},
|
|
{
|
|
style: 'font-style=italic',
|
|
},
|
|
]
|
|
},
|
|
|
|
renderHTML({ attributes }) {
|
|
return ['em', attributes, 0]
|
|
},
|
|
|
|
addCommands() {
|
|
return {
|
|
italic: (): Command => ({ 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),
|
|
]
|
|
},
|
|
})
|
|
|
|
export default Italic
|
|
|
|
declare module '@tiptap/core/src/Editor' {
|
|
interface AllExtensions {
|
|
Italic: typeof Italic,
|
|
}
|
|
}
|