refactoring

This commit is contained in:
Philipp Kühn
2021-01-19 10:09:32 +01:00
committed by Hans Pagel
parent ba5d38a621
commit 9af0d76d67
7 changed files with 46 additions and 22 deletions

View File

@@ -1,11 +1,6 @@
import { NodeType } from 'prosemirror-model' import { NodeType } from 'prosemirror-model'
import getNodeType from '../helpers/getNodeType' import getNodeType from '../helpers/getNodeType'
import { Command } from '../types' import { Command, Range } from '../types'
export type Range = {
from: number,
to: number,
}
/** /**
* Replaces text with a node within a range. * Replaces text with a node within a range.

View File

@@ -130,3 +130,8 @@ export type ChainedCommands = {
export type CanCommands = SingleCommands & { chain: () => ChainedCommands } export type CanCommands = SingleCommands & { chain: () => ChainedCommands }
export type FocusPosition = 'start' | 'end' | number | boolean | null export type FocusPosition = 'start' | 'end' | number | boolean | null
export type Range = {
from: number,
to: number,
}

View File

@@ -8,7 +8,6 @@ export const Mention = Node.create({
defaultOptions: <MentionOptions>{ defaultOptions: <MentionOptions>{
char: '@', char: '@',
render: () => ({}),
}, },
group: 'inline', group: 'inline',
@@ -50,6 +49,7 @@ export const Mention = Node.create({
command: ({ range }) => { command: ({ range }) => {
this.editor this.editor
.chain() .chain()
.focus()
.replace(range, 'mention') .replace(range, 'mention')
.insertText(' ') .insertText(' ')
.run() .run()

View File

@@ -1,3 +1,4 @@
import { Range } from '@tiptap/core'
import { ResolvedPos } from 'prosemirror-model' import { ResolvedPos } from 'prosemirror-model'
export interface Trigger { export interface Trigger {
@@ -8,10 +9,7 @@ export interface Trigger {
} }
export type SuggestionMatch = { export type SuggestionMatch = {
range: { range: Range,
from: number,
to: number,
},
query: string, query: string,
text: string, text: string,
} | null } | null

View File

@@ -1,4 +1,10 @@
export function getVirtualNode(node: Element) { export interface VirtualNode {
getBoundingClientRect: () => DOMRect,
clientWidth: number,
clientHeight: number,
}
export function getVirtualNode(node: Element): VirtualNode {
return { return {
getBoundingClientRect() { getBoundingClientRect() {
return node.getBoundingClientRect() return node.getBoundingClientRect()

View File

@@ -1,5 +1,7 @@
import { Suggestion } from './suggestion' import { Suggestion } from './suggestion'
export * from './findSuggestionMatch'
export * from './getVirtualNode'
export * from './suggestion' export * from './suggestion'
export default Suggestion export default Suggestion

View File

@@ -1,8 +1,8 @@
import { Editor } from '@tiptap/core' import { Editor, Range } from '@tiptap/core'
import { Plugin, PluginKey } from 'prosemirror-state' import { Plugin, PluginKey } from 'prosemirror-state'
import { Decoration, DecorationSet } from 'prosemirror-view' import { Decoration, DecorationSet, EditorView } from 'prosemirror-view'
import { findSuggestionMatch } from './findSuggestionMatch' import { findSuggestionMatch } from './findSuggestionMatch'
import { getVirtualNode } from './getVirtualNode' import { getVirtualNode, VirtualNode } from './getVirtualNode'
export interface SuggestionOptions { export interface SuggestionOptions {
editor: Editor, editor: Editor,
@@ -10,17 +10,35 @@ export interface SuggestionOptions {
allowSpaces?: boolean, allowSpaces?: boolean,
startOfLine?: boolean, startOfLine?: boolean,
suggestionClass?: string, suggestionClass?: string,
command?: (props: any) => any, command?: (props: { range: Range }) => void,
items?: (query: string) => any[], items?: (query: string) => any[],
render?: () => { render?: () => {
onStart?: (props: any) => void, onStart?: (props: SuggestionProps) => void,
onUpdate?: (props: any) => void, onUpdate?: (props: SuggestionProps) => void,
onExit?: (props: any) => void, onExit?: (props: SuggestionProps) => void,
onKeyDown?: (props: any) => boolean, onKeyDown?: (props: SuggestionKeyDownProps) => boolean,
}, },
} }
export interface SuggestionProps {
editor: Editor,
range: Range,
query: string,
text: string,
items: any[],
command: () => void,
decorationNode: Element | null,
virtualNode: VirtualNode | null,
}
export interface SuggestionKeyDownProps {
view: EditorView,
event: KeyboardEvent,
range: Range,
}
export function Suggestion({ export function Suggestion({
editor,
char = '@', char = '@',
allowSpaces = false, allowSpaces = false,
startOfLine = false, startOfLine = false,
@@ -57,8 +75,8 @@ export function Suggestion({
const state = handleExit ? prev : next const state = handleExit ? prev : next
const decorationNode = document.querySelector(`[data-decoration-id="${state.decorationId}"]`) const decorationNode = document.querySelector(`[data-decoration-id="${state.decorationId}"]`)
const props = { const props: SuggestionProps = {
view, editor,
range: state.range, range: state.range,
query: state.query, query: state.query,
text: state.text, text: state.text,