style: move node text content into helper function

This commit is contained in:
Dominik Biedebach
2022-05-20 17:10:30 +02:00
committed by Dominik
parent 30c39c94c9
commit 0597e474af
4 changed files with 31 additions and 29 deletions

View File

@@ -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 './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 () => { export default () => {
const editor = useEditor({ const editor = useEditor({
extensions: [ extensions: [
Document, StarterKit,
Paragraph,
Text,
Mention.configure({ Mention.configure({
HTMLAttributes: { HTMLAttributes: {
class: 'mention', class: 'mention',

View File

@@ -6,9 +6,7 @@
<script> <script>
import { Editor, EditorContent } from '@tiptap/vue-3' import { Editor, EditorContent } from '@tiptap/vue-3'
import Document from '@tiptap/extension-document' import StarterKit from '@tiptap/starter-kit'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import Mention from '@tiptap/extension-mention' import Mention from '@tiptap/extension-mention'
import suggestion from './suggestion' import suggestion from './suggestion'
@@ -26,9 +24,7 @@ export default {
mounted() { mounted() {
this.editor = new Editor({ this.editor = new Editor({
extensions: [ extensions: [
Document, StarterKit,
Paragraph,
Text,
Mention.configure({ Mention.configure({
HTMLAttributes: { HTMLAttributes: {
class: 'mention', class: 'mention',

View File

@@ -3,6 +3,7 @@ import { EditorState, Plugin, TextSelection } from 'prosemirror-state'
import { CommandManager } from './CommandManager' import { CommandManager } from './CommandManager'
import { Editor } from './Editor' import { Editor } from './Editor'
import { createChainableState } from './helpers/createChainableState' import { createChainableState } from './helpers/createChainableState'
import { getTextContentFromNodes } from './helpers/getTextContentFromNodes'
import { import {
CanCommands, CanCommands,
ChainedCommands, ChainedCommands,
@@ -115,26 +116,15 @@ function run(config: {
} }
let matched = false let matched = false
const maxMatch = 500
let textBefore = '' const textBefore = getTextContentFromNodes($from) + text
$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%'
},
)
rules.forEach(rule => { rules.forEach(rule => {
if (matched) { if (matched) {
return return
} }
const match = inputRuleMatcherHandler(textBefore + text, rule.find) const match = inputRuleMatcherHandler(textBefore, rule.find)
if (!match) { if (!match) {
return return

View 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
}