You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.3 KiB

3 years ago
import { Mark, mergeAttributes, markInputRule, markPasteRule } from '@tiptap/core';
const starInputRegex = /(?:^|\s)((?:\*)((?:[^*]+))(?:\*))$/;
const starPasteRegex = /(?:^|\s)((?:\*)((?:[^*]+))(?:\*))/g;
const underscoreInputRegex = /(?:^|\s)((?:_)((?:[^_]+))(?:_))$/;
const underscorePasteRegex = /(?:^|\s)((?:_)((?:[^_]+))(?:_))/g;
const Italic = Mark.create({
name: 'italic',
addOptions() {
return {
HTMLAttributes: {},
};
},
parseHTML() {
return [
{
tag: 'em',
},
{
tag: 'i',
getAttrs: node => node.style.fontStyle !== 'normal' && null,
},
{
style: 'font-style=italic',
},
];
},
renderHTML({ HTMLAttributes }) {
return ['em', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0];
},
addCommands() {
return {
setItalic: () => ({ commands }) => {
return commands.setMark(this.name);
},
toggleItalic: () => ({ commands }) => {
return commands.toggleMark(this.name);
},
unsetItalic: () => ({ commands }) => {
return commands.unsetMark(this.name);
},
};
},
addKeyboardShortcuts() {
return {
'Mod-i': () => this.editor.commands.toggleItalic(),
'Mod-I': () => this.editor.commands.toggleItalic(),
};
},
addInputRules() {
return [
markInputRule({
find: starInputRegex,
type: this.type,
}),
markInputRule({
find: underscoreInputRegex,
type: this.type,
}),
];
},
addPasteRules() {
return [
markPasteRule({
find: starPasteRegex,
type: this.type,
}),
markPasteRule({
find: underscorePasteRegex,
type: this.type,
}),
];
},
});
export { Italic, Italic as default, starInputRegex, starPasteRegex, underscoreInputRegex, underscorePasteRegex };
//# sourceMappingURL=tiptap-extension-italic.esm.js.map