mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-07 03:48:15 +08:00
9afadeb7fe
* add new addOptions option * replace defaultOptions with addOptions for all extensions * replace defaultOptions with addOptions for all demos * replace defaultOptions with addOptions in docs * refactoring * refactoring * drop object support for addOptions * fix optional options * fix tests
111 lines
2.2 KiB
TypeScript
111 lines
2.2 KiB
TypeScript
import {
|
|
Mark,
|
|
markInputRule,
|
|
markPasteRule,
|
|
mergeAttributes,
|
|
} from '@tiptap/core'
|
|
|
|
export interface BoldOptions {
|
|
HTMLAttributes: Record<string, any>,
|
|
}
|
|
|
|
declare module '@tiptap/core' {
|
|
interface Commands<ReturnType> {
|
|
bold: {
|
|
/**
|
|
* Set a bold mark
|
|
*/
|
|
setBold: () => ReturnType,
|
|
/**
|
|
* Toggle a bold mark
|
|
*/
|
|
toggleBold: () => ReturnType,
|
|
/**
|
|
* Unset a bold mark
|
|
*/
|
|
unsetBold: () => ReturnType,
|
|
}
|
|
}
|
|
}
|
|
|
|
export const starInputRegex = /(?:^|\s)((?:\*\*)((?:[^*]+))(?:\*\*))$/
|
|
export const starPasteRegex = /(?:^|\s)((?:\*\*)((?:[^*]+))(?:\*\*))/g
|
|
export const underscoreInputRegex = /(?:^|\s)((?:__)((?:[^__]+))(?:__))$/
|
|
export const underscorePasteRegex = /(?:^|\s)((?:__)((?:[^__]+))(?:__))/g
|
|
|
|
export const Bold = Mark.create<BoldOptions>({
|
|
name: 'bold',
|
|
|
|
addOptions() {
|
|
return {
|
|
HTMLAttributes: {},
|
|
}
|
|
},
|
|
|
|
parseHTML() {
|
|
return [
|
|
{
|
|
tag: 'strong',
|
|
},
|
|
{
|
|
tag: 'b',
|
|
getAttrs: node => (node as HTMLElement).style.fontWeight !== 'normal' && null,
|
|
},
|
|
{
|
|
style: 'font-weight',
|
|
getAttrs: value => /^(bold(er)?|[5-9]\d{2,})$/.test(value as string) && null,
|
|
},
|
|
]
|
|
},
|
|
|
|
renderHTML({ HTMLAttributes }) {
|
|
return ['strong', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
|
|
},
|
|
|
|
addCommands() {
|
|
return {
|
|
setBold: () => ({ commands }) => {
|
|
return commands.setMark('bold')
|
|
},
|
|
toggleBold: () => ({ commands }) => {
|
|
return commands.toggleMark('bold')
|
|
},
|
|
unsetBold: () => ({ commands }) => {
|
|
return commands.unsetMark('bold')
|
|
},
|
|
}
|
|
},
|
|
|
|
addKeyboardShortcuts() {
|
|
return {
|
|
'Mod-b': () => this.editor.commands.toggleBold(),
|
|
}
|
|
},
|
|
|
|
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,
|
|
}),
|
|
]
|
|
},
|
|
})
|