add basic new chaining

This commit is contained in:
Philipp Kühn
2020-09-20 23:19:27 +02:00
parent 26af779d22
commit fbdc156981
7 changed files with 159 additions and 60 deletions

View File

@@ -1,8 +1,8 @@
import { Editor } from '../Editor'
import { Editor, Command } from '../Editor'
import { TextSelection } from 'prosemirror-state'
import minMax from '../utils/minMax'
type FocusCommand = (position?: Position) => Editor
type FocusCommand = (position?: Position) => Command
declare module '../Editor' {
interface Editor {
@@ -44,25 +44,45 @@ function resolveSelection(editor: Editor, position: Position = null): ResolvedSe
}
}
export default (next: Function, editor: Editor) => (position = null) => {
const { view, state } = editor
// export default (next: Function, editor: Editor) => (position = null) => {
// const { view, state } = editor
// if ((view.hasFocus() && position === null) || position === false) {
// 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)
// view.focus()
// //@ts-ignore
// // console.log(bla)
// // return 'FOCUS'
// next()
// }
export const focus: FocusCommand = (position = null) => ({ editor, tr }) => {
const { view } = editor
if ((view.hasFocus() && position === null) || position === false) {
next()
return
return true
}
const { from, to } = resolveSelection(editor, position)
const { doc, tr } = state
const { doc } = tr
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)
tr.setSelection(selection)
view.focus()
//@ts-ignore
// console.log(bla)
// return 'FOCUS'
next()
return true
}