parent
9f04e17e57
commit
0c15aa55d8
|
@ -1,8 +1,10 @@
|
||||||
import type { ExtendedRegExpMatchArray } from '@tiptap/core'
|
import type { ExtendedRegExpMatchArray, InputRuleFinder, nodeInputRule } from '@tiptap/core'
|
||||||
|
import type { NodeType } from '@tiptap/pm/model'
|
||||||
import {
|
import {
|
||||||
|
InputRule,
|
||||||
Node,
|
Node,
|
||||||
|
callOrReturn,
|
||||||
mergeAttributes,
|
mergeAttributes,
|
||||||
nodeInputRule,
|
|
||||||
nodePasteRule,
|
nodePasteRule,
|
||||||
} from '@tiptap/core'
|
} from '@tiptap/core'
|
||||||
import { emojiRegEx, getEmojiAttributes } from '~/config/emojis'
|
import { emojiRegEx, getEmojiAttributes } from '~/config/emojis'
|
||||||
|
@ -80,7 +82,34 @@ export const TiptapPluginEmoji = Node.create({
|
||||||
},
|
},
|
||||||
|
|
||||||
addInputRules() {
|
addInputRules() {
|
||||||
return createEmojiRule(nodeInputRule, this.type)
|
function emojiInputRule(config: {
|
||||||
|
find: InputRuleFinder
|
||||||
|
type: NodeType
|
||||||
|
getAttributes?:
|
||||||
|
| Record<string, any>
|
||||||
|
| ((match: ExtendedRegExpMatchArray) => Record<string, any>)
|
||||||
|
| false
|
||||||
|
| null
|
||||||
|
}) {
|
||||||
|
return new InputRule({
|
||||||
|
find: config.find,
|
||||||
|
handler: ({ state, range, match }) => {
|
||||||
|
const attributes = callOrReturn(config.getAttributes, undefined, match) || {}
|
||||||
|
const { tr } = state
|
||||||
|
const start = range.from
|
||||||
|
const end = range.to
|
||||||
|
|
||||||
|
tr.insert(start, config.type.create(attributes)).delete(
|
||||||
|
tr.mapping.map(start),
|
||||||
|
tr.mapping.map(end),
|
||||||
|
)
|
||||||
|
|
||||||
|
tr.scrollIntoView()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return createEmojiRule(emojiInputRule, this.type)
|
||||||
},
|
},
|
||||||
|
|
||||||
addPasteRules() {
|
addPasteRules() {
|
||||||
|
|
|
@ -13,5 +13,7 @@ export const nodePasteRule = proxy
|
||||||
export const VueNodeViewRenderer = proxy
|
export const VueNodeViewRenderer = proxy
|
||||||
export const findChildren = proxy
|
export const findChildren = proxy
|
||||||
export const VueRenderer = proxy
|
export const VueRenderer = proxy
|
||||||
|
export const callOrReturn = proxy
|
||||||
|
export const InputRule = proxy
|
||||||
|
|
||||||
export { proxy as default }
|
export { proxy as default }
|
||||||
|
|
Loading…
Reference in New Issue