refactoring

This commit is contained in:
Philipp Kühn 2020-11-20 23:44:23 +01:00
parent 527f75f5fd
commit c433428fba
3 changed files with 21 additions and 17 deletions

View File

@ -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

View File

@ -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)

View File

@ -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: {