add some tests

This commit is contained in:
Philipp Kühn
2019-12-08 00:16:44 +01:00
parent d8fe4ee3f8
commit 90f86bafbb
12 changed files with 524 additions and 10 deletions

View File

@@ -0,0 +1,65 @@
import { Editor } from '../Editor'
import { TextSelection } from 'prosemirror-state'
import sleep from '../utils/sleep'
import minMax from '../utils/minMax'
declare module '../Editor' {
interface Editor {
focus(position: any): Editor
}
}
interface ResolvedSelection {
from: number,
to: number,
}
type Position = 'start' | 'end' | number | null
function resolveSelection(editor: Editor, position: Position = null): ResolvedSelection {
if (position === null) {
return editor.selection
}
if (position === 'start') {
return {
from: 0,
to: 0,
}
}
if (position === 'end') {
const { size } = editor.state.doc.content
return {
from: size,
to: size,
}
}
return {
from: position as number,
to: position as number,
}
}
export default async function focus(next: Function, editor: Editor, position: Position = null): Promise<void> {
const { view, state } = editor
if ((view.hasFocus && position === null)) {
next()
return
}
const { from, to } = resolveSelection(editor, position)
const { doc, tr } = state
const resolvedFrom = minMax(from, 0, doc.content.size)
const resolvedEnd = minMax(to, 0, doc.content.size)
const selection = TextSelection.create(doc, resolvedFrom, resolvedEnd)
const transaction = tr.setSelection(selection)
view.dispatch(transaction)
await sleep(10)
view.focus()
next()
}

View File

@@ -0,0 +1,12 @@
import { Editor } from '../Editor'
declare module "../Editor" {
interface Editor {
insertText(text: string): Editor,
}
}
export default function insertText(next: Function, { state, view }: Editor, text: string): void {
view.dispatch(state.tr.insertText(text))
next()
}