Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix InputRule regex matcher ignoring non-text leaflets in textBefore #2807

Merged
merged 4 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 7 additions & 8 deletions demos/src/Nodes/Mention/React/index.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import './styles.scss'

import React from 'react'
import { useEditor, EditorContent } from '@tiptap/react'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'

import Mention from '@tiptap/extension-mention'
import { EditorContent, useEditor } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'

import suggestion from './suggestion'
import './styles.scss'

export default () => {
const editor = useEditor({
extensions: [
Document,
Paragraph,
Text,
StarterKit,
Mention.configure({
HTMLAttributes: {
class: 'mention',
Expand Down
8 changes: 2 additions & 6 deletions demos/src/Nodes/Mention/Vue/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

<script>
import { Editor, EditorContent } from '@tiptap/vue-3'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import StarterKit from '@tiptap/starter-kit'
import Mention from '@tiptap/extension-mention'
import suggestion from './suggestion'

Expand All @@ -26,9 +24,7 @@ export default {
mounted() {
this.editor = new Editor({
extensions: [
Document,
Paragraph,
Text,
StarterKit,
Mention.configure({
HTMLAttributes: {
class: 'mention',
Expand Down
21 changes: 9 additions & 12 deletions packages/core/src/InputRule.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { EditorState, Plugin, TextSelection } from 'prosemirror-state'
import { Editor } from './Editor'

import { CommandManager } from './CommandManager'
import { Editor } from './Editor'
import { createChainableState } from './helpers/createChainableState'
import { isRegExp } from './utilities/isRegExp'
import { getTextContentFromNodes } from './helpers/getTextContentFromNodes'
import {
Range,
CanCommands,
ChainedCommands,
ExtendedRegExpMatchArray,
Range,
SingleCommands,
ChainedCommands,
CanCommands,
} from './types'
import { isRegExp } from './utilities/isRegExp'

export type InputRuleMatch = {
index: number,
Expand Down Expand Up @@ -114,13 +116,8 @@ function run(config: {
}

let matched = false
const maxMatch = 500
const textBefore = $from.parent.textBetween(
Math.max(0, $from.parentOffset - maxMatch),
$from.parentOffset,
undefined,
' ',
) + text

const textBefore = getTextContentFromNodes($from) + text

rules.forEach(rule => {
if (matched) {
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/helpers/getTextContentFromNodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ResolvedPos } from 'prosemirror-model'

export const getTextContentFromNodes = ($from: ResolvedPos<any>, maxMatch = 500) => {
bdbch marked this conversation as resolved.
Show resolved Hide resolved
let textBefore = ''

$from.parent.nodesBetween(
Math.max(0, $from.parentOffset - maxMatch),
$from.parentOffset,
(node, pos, parent, index) => {
textBefore += node.type.spec.toText?.({
node, pos, parent, index,
}) || node.textContent || '%leaf%'
},
)

return textBefore
}