2020-09-24 06:29:05 +08:00
|
|
|
import {
|
2020-11-13 23:44:22 +08:00
|
|
|
Command,
|
2020-11-16 18:19:43 +08:00
|
|
|
Mark,
|
2020-11-13 23:44:22 +08:00
|
|
|
markInputRule,
|
|
|
|
markPasteRule,
|
2020-11-25 16:50:54 +08:00
|
|
|
mergeAttributes,
|
2020-09-24 06:29:05 +08:00
|
|
|
} from '@tiptap/core'
|
2020-09-22 05:17:30 +08:00
|
|
|
|
2020-11-13 23:44:22 +08:00
|
|
|
export interface ItalicOptions {
|
2021-04-21 15:43:31 +08:00
|
|
|
HTMLAttributes: Record<string, any>,
|
2020-11-13 23:44:22 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
declare module '@tiptap/core' {
|
2021-02-17 01:39:37 +08:00
|
|
|
interface Commands {
|
2021-02-16 18:27:58 +08:00
|
|
|
italic: {
|
|
|
|
/**
|
|
|
|
* Set an italic mark
|
|
|
|
*/
|
|
|
|
setItalic: () => Command,
|
|
|
|
/**
|
|
|
|
* Toggle an italic mark
|
|
|
|
*/
|
|
|
|
toggleItalic: () => Command,
|
|
|
|
/**
|
|
|
|
* Unset an italic mark
|
|
|
|
*/
|
|
|
|
unsetItalic: () => Command,
|
|
|
|
}
|
2021-02-10 16:59:35 +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
|
|
|
|
|
2021-02-11 01:25:08 +08:00
|
|
|
export const Italic = Mark.create<ItalicOptions>({
|
2020-10-22 18:34:49 +08:00
|
|
|
name: 'italic',
|
|
|
|
|
2021-02-11 01:25:08 +08:00
|
|
|
defaultOptions: {
|
2020-11-13 23:44:22 +08:00
|
|
|
HTMLAttributes: {},
|
|
|
|
},
|
|
|
|
|
2020-10-22 18:34:49 +08:00
|
|
|
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
|
|
|
]
|
|
|
|
},
|
|
|
|
|
2020-11-13 23:07:20 +08:00
|
|
|
renderHTML({ HTMLAttributes }) {
|
2020-11-25 16:50:54 +08:00
|
|
|
return ['em', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2021-02-10 16:59:35 +08:00
|
|
|
setItalic: () => ({ commands }) => {
|
2020-11-19 00:36:00 +08:00
|
|
|
return commands.setMark('italic')
|
2020-11-18 04:49:23 +08:00
|
|
|
},
|
2021-02-10 16:59:35 +08:00
|
|
|
toggleItalic: () => ({ commands }) => {
|
2020-10-22 18:34:49 +08:00
|
|
|
return commands.toggleMark('italic')
|
|
|
|
},
|
2021-02-10 16:59:35 +08:00
|
|
|
unsetItalic: () => ({ commands }) => {
|
2020-11-19 00:38:16 +08:00
|
|
|
return commands.unsetMark('italic')
|
2020-11-18 04:49:23 +08:00
|
|
|
},
|
2020-10-22 18:34:49 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addKeyboardShortcuts() {
|
|
|
|
return {
|
2020-11-18 04:49:23 +08:00
|
|
|
'Mod-i': () => this.editor.commands.toggleItalic(),
|
2020-10-22 18:34:49 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addInputRules() {
|
|
|
|
return [
|
|
|
|
markInputRule(starInputRegex, this.type),
|
|
|
|
markInputRule(underscoreInputRegex, this.type),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
addPasteRules() {
|
|
|
|
return [
|
|
|
|
markPasteRule(starPasteRegex, this.type),
|
|
|
|
markPasteRule(underscorePasteRegex, this.type),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
})
|