diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index f64597b9..16f9d8f9 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -14,7 +14,12 @@ import createStyleTag from './utils/createStyleTag' import CommandManager from './CommandManager' import ExtensionManager from './ExtensionManager' import EventEmitter from './EventEmitter' -import { EditorOptions, EditorContent, CommandSpec } from './types' +import { + EditorOptions, + EditorContent, + CommandSpec, + EditorSelection, +} from './types' import * as extensions from './extensions' import style from './style' @@ -39,7 +44,7 @@ export class Editor extends EventEmitter { public view!: EditorView - public selection = { from: 0, to: 0 } + public selection: EditorSelection = { from: 0, to: 0 } public isFocused = false diff --git a/packages/core/src/commands/focus.ts b/packages/core/src/commands/focus.ts index c2c3057b..fb7f970e 100644 --- a/packages/core/src/commands/focus.ts +++ b/packages/core/src/commands/focus.ts @@ -1,16 +1,10 @@ -import { TextSelection } from 'prosemirror-state' -import { Editor } from '../Editor' +import { EditorState, TextSelection } from 'prosemirror-state' import { Command, FocusPosition } from '../types' import minMax from '../utils/minMax' -interface ResolvedSelection { - from: number, - to: number, -} - -function resolveSelection(editor: Editor, position: FocusPosition = null): ResolvedSelection { - if (position === null) { - return editor.selection +function resolveSelection(state: EditorState, position: FocusPosition = null) { + if (!position) { + return null } if (position === 'start' || position === true) { @@ -21,17 +15,17 @@ function resolveSelection(editor: Editor, position: FocusPosition = null): Resol } if (position === 'end') { - const { size } = editor.state.doc.content + const { size } = state.doc.content return { from: size, - to: size - 1, // TODO: -1 only for nodes with content + to: size, } } return { - from: position as number, - to: position as number, + from: position, + to: position, } } @@ -48,7 +42,7 @@ export const focus = (position: FocusPosition = null): Command => ({ return true } - const { from, to } = resolveSelection(editor, position) + const { from, to } = resolveSelection(editor.state, position) || editor.selection const { doc } = tr const resolvedFrom = minMax(from, 0, doc.content.size) const resolvedEnd = minMax(to, 0, doc.content.size) diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 2f44a5c3..245ffd22 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -32,6 +32,11 @@ export interface EditorOptions { onBlur: (props: { event: FocusEvent }) => void, } +export type EditorSelection = { + from: number, + to: number, +} + export type EditorContent = string | JSON | null export type Command = (props: {