add basic new chaining
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user