style: move node text content into helper function
This commit is contained in:
committed by
Dominik
parent
30c39c94c9
commit
0597e474af
@@ -1,18 +1,17 @@
|
||||
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 suggestion from './suggestion'
|
||||
import './styles.scss'
|
||||
|
||||
import React from 'react'
|
||||
|
||||
import Mention from '@tiptap/extension-mention'
|
||||
import { EditorContent, useEditor } from '@tiptap/react'
|
||||
import StarterKit from '@tiptap/starter-kit'
|
||||
|
||||
import suggestion from './suggestion'
|
||||
|
||||
export default () => {
|
||||
const editor = useEditor({
|
||||
extensions: [
|
||||
Document,
|
||||
Paragraph,
|
||||
Text,
|
||||
StarterKit,
|
||||
Mention.configure({
|
||||
HTMLAttributes: {
|
||||
class: 'mention',
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -26,9 +24,7 @@ export default {
|
||||
mounted() {
|
||||
this.editor = new Editor({
|
||||
extensions: [
|
||||
Document,
|
||||
Paragraph,
|
||||
Text,
|
||||
StarterKit,
|
||||
Mention.configure({
|
||||
HTMLAttributes: {
|
||||
class: 'mention',
|
||||
|
||||
@@ -3,6 +3,7 @@ import { EditorState, Plugin, TextSelection } from 'prosemirror-state'
|
||||
import { CommandManager } from './CommandManager'
|
||||
import { Editor } from './Editor'
|
||||
import { createChainableState } from './helpers/createChainableState'
|
||||
import { getTextContentFromNodes } from './helpers/getTextContentFromNodes'
|
||||
import {
|
||||
CanCommands,
|
||||
ChainedCommands,
|
||||
@@ -115,26 +116,15 @@ function run(config: {
|
||||
}
|
||||
|
||||
let matched = false
|
||||
const maxMatch = 500
|
||||
|
||||
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%'
|
||||
},
|
||||
)
|
||||
const textBefore = getTextContentFromNodes($from) + text
|
||||
|
||||
rules.forEach(rule => {
|
||||
if (matched) {
|
||||
return
|
||||
}
|
||||
|
||||
const match = inputRuleMatcherHandler(textBefore + text, rule.find)
|
||||
const match = inputRuleMatcherHandler(textBefore, rule.find)
|
||||
|
||||
if (!match) {
|
||||
return
|
||||
|
||||
17
packages/core/src/helpers/getTextContentFromNodes.ts
Normal file
17
packages/core/src/helpers/getTextContentFromNodes.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { ResolvedPos } from 'prosemirror-model'
|
||||
|
||||
export const getTextContentFromNodes = ($from: ResolvedPos<any>, maxMatch = 500) => {
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user