rename package folders

This commit is contained in:
Philipp Kühn
2020-03-30 10:42:59 +02:00
parent 18c5164af9
commit 14421a11fa
35 changed files with 2 additions and 2 deletions

View File

@@ -0,0 +1,13 @@
import { Editor } from '../Editor'
import { TextSelection } from 'prosemirror-state'
declare module '../Editor' {
interface Editor {
clearContent(emitUpdate?: Boolean): Editor,
}
}
export default function clearContent(next: Function, editor: Editor, emitUpdate = false): void {
editor.setContent('', emitUpdate)
next()
}

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: Position): 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 - 1, // TODO: -1 only for nodes with content
}
}
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,20 @@
import { DOMParser } from 'prosemirror-model'
import { Editor } from '../Editor'
import elementFromString from '../utils/elementFromString'
declare module '../Editor' {
interface Editor {
insertHTML(value: string): Editor,
}
}
export default function insertHTML(next: Function, editor: Editor, value: string): void {
const { view, state } = editor
const { selection } = state
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()
}

View File

@@ -0,0 +1,15 @@
import { Editor } from '../Editor'
declare module '../Editor' {
interface Editor {
insertText(value: string): Editor,
}
}
export default function insertText(next: Function, editor: Editor, value: string): void {
const { view, state } = editor
const transaction = state.tr.insertText(value)
view.dispatch(transaction)
next()
}

View File

@@ -0,0 +1,33 @@
import { Editor } from '../Editor'
import { TextSelection } from 'prosemirror-state'
declare module '../Editor' {
interface Editor {
setContent(content: string, emitUpdate?: Boolean, parseOptions?: any): Editor,
}
}
export default function setContent(
next: Function,
editor: Editor,
content = null,
emitUpdate = false,
parseOptions = {},
): void {
if (content === null) {
next()
return
}
const { view, state, createDocument } = editor
const { doc, tr } = state
const document = createDocument(content, parseOptions)
const selection = TextSelection.create(doc, 0, doc.content.size)
const transaction = tr
.setSelection(selection)
.replaceSelectionWith(document, false)
.setMeta('preventUpdate', !emitUpdate)
view.dispatch(transaction)
next()
}