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
}

View File

@@ -1,18 +1,18 @@
export { default as blur } from './blur'
export { default as clearContent } from './clearContent'
export { default as deleteSelection } from './deleteSelection'
export { default as focus } from './focus'
export { default as insertHTML } from './insertHTML'
export { default as insertText } from './insertText'
export { default as liftListItem } from './liftListItem'
export { default as removeMark } from './removeMark'
export { default as removeMarks } from './removeMarks'
export { default as replaceWithNode } from './replaceWithNode'
export { default as selectAll } from './selectAll'
export { default as selectParentNode } from './selectParentNode'
export { default as setContent } from './setContent'
export { default as sinkListItem } from './sinkListItem'
export { default as splitListItem } from './splitListItem'
export { default as toggleMark } from './toggleMark'
export { default as toggleNode } from './toggleNode'
export { default as updateMark } from './updateMark'
// export { default as blur } from './blur'
// export { default as clearContent } from './clearContent'
// export { default as deleteSelection } from './deleteSelection'
export { focus } from './focus'
export { insertHTML } from './insertHTML'
export { insertText } from './insertText'
// export { default as liftListItem } from './liftListItem'
// export { default as removeMark } from './removeMark'
// export { default as removeMarks } from './removeMarks'
// export { default as replaceWithNode } from './replaceWithNode'
// export { default as selectAll } from './selectAll'
// export { default as selectParentNode } from './selectParentNode'
// export { default as setContent } from './setContent'
// export { default as sinkListItem } from './sinkListItem'
// export { default as splitListItem } from './splitListItem'
// export { default as toggleMark } from './toggleMark'
// export { default as toggleNode } from './toggleNode'
// export { default as updateMark } from './updateMark'

View File

@@ -1,8 +1,10 @@
import { DOMParser } from 'prosemirror-model'
import { Editor } from '../Editor'
import { Selection } from 'prosemirror-state'
import { Command } from '../Editor'
import elementFromString from '../utils/elementFromString'
import {ReplaceStep, ReplaceAroundStep} from "prosemirror-transform"
type InsertHTMLCommand = (value: string) => Editor
type InsertHTMLCommand = (value: string) => Command
declare module '../Editor' {
interface Editor {
@@ -10,13 +12,28 @@ declare module '../Editor' {
}
}
export default (next: Function, editor: Editor) => (value: string) => {
const { view, state } = editor
const { selection } = state
// TODO: move to utils
// https://github.com/ProseMirror/prosemirror-state/blob/master/src/selection.js#L466
function selectionToInsertionEnd(tr, startLen, bias) {
let last = tr.steps.length - 1
if (last < startLen) return
let step = tr.steps[last]
if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep)) return
let map = tr.mapping.maps[last], end
map.forEach((_from, _to, _newFrom, newTo) => { if (end == null) end = newTo })
tr.setSelection(Selection.near(tr.doc.resolve(end), bias))
}
export const insertHTML: InsertHTMLCommand = value => ({ tr, editor }) => {
console.log({tr })
const { state } = editor
const { selection } = tr
const element = elementFromString(value)
const slice = DOMParser.fromSchema(state.schema).parseSlice(element)
const transaction = state.tr.insert(selection.anchor, slice.content)
view.dispatch(transaction)
next()
tr.insert(selection.anchor, slice.content)
// TODO: set correct bias by content
selectionToInsertionEnd(tr, tr.steps.length - 1, -1)
return true
}

View File

@@ -1,6 +1,6 @@
import { Editor } from '../Editor'
import { Command } from '../Editor'
type InsertTextCommand = (value: string) => Editor
type InsertTextCommand = (value: string) => Command
declare module '../Editor' {
interface Editor {
@@ -8,10 +8,8 @@ declare module '../Editor' {
}
}
export default (next: Function, editor: Editor) => (value: string) => {
const { view, state } = editor
const transaction = state.tr.insertText(value)
export const insertText: InsertTextCommand = value => ({ tr }) => {
tr.insertText(value)
view.dispatch(transaction)
next()
return true
}