Skip to content

Commit

Permalink
fix: emoji replacement when pasting (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthur-fontaine authored Jan 5, 2023
1 parent d4e9956 commit bd1b9fb
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions composables/tiptap/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,39 @@ import {
Node,
mergeAttributes,
nodeInputRule,
nodePasteRule,
} from '@tiptap/core'
import { emojiRegEx, getEmojiAttributes } from '~/config/emojis'

const createEmojiRule = <NR extends typeof nodeInputRule | typeof nodePasteRule>(
nodeRule: NR,
type: Parameters<NR>[0]['type'],
): ReturnType<NR>[] => {
const rule = nodeRule({
find: emojiRegEx as RegExp,
type,
getAttributes: (match) => {
const [native] = match
return getEmojiAttributes(native)
},
}) as ReturnType<NR>

// Error catch for unsupported emoji
const handler = rule.handler.bind(rule)
rule.handler = (...args) => {
try {
return handler(...args)
}
catch (e) {
return null
}
}

return [
rule,
]
}

export const Emoji = Node.create({
name: 'em-emoji',

Expand Down Expand Up @@ -50,26 +80,10 @@ export const Emoji = Node.create({
},

addInputRules() {
const inputRule = nodeInputRule({
find: emojiRegEx as RegExp,
type: this.type,
getAttributes: (match) => {
const [native] = match
return getEmojiAttributes(native)
},
})
// Error catch for unsupported emoji
const handler = inputRule.handler.bind(inputRule)
inputRule.handler = (...args) => {
try {
return handler(...args)
}
catch (e) {
return null
}
}
return [
inputRule,
]
return createEmojiRule(nodeInputRule, this.type)
},

addPasteRules() {
return createEmojiRule(nodePasteRule, this.type)
},
})

0 comments on commit bd1b9fb

Please sign in to comment.