rename package folders
This commit is contained in:
13
packages/core/src/commands/clearContent.ts
Normal file
13
packages/core/src/commands/clearContent.ts
Normal 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()
|
||||
}
|
||||
65
packages/core/src/commands/focus.ts
Normal file
65
packages/core/src/commands/focus.ts
Normal 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()
|
||||
}
|
||||
20
packages/core/src/commands/insertHTML.ts
Normal file
20
packages/core/src/commands/insertHTML.ts
Normal 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()
|
||||
}
|
||||
15
packages/core/src/commands/insertText.ts
Normal file
15
packages/core/src/commands/insertText.ts
Normal 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()
|
||||
}
|
||||
33
packages/core/src/commands/setContent.ts
Normal file
33
packages/core/src/commands/setContent.ts
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user