refactoring
This commit is contained in:
@@ -14,7 +14,12 @@ import createStyleTag from './utils/createStyleTag'
|
|||||||
import CommandManager from './CommandManager'
|
import CommandManager from './CommandManager'
|
||||||
import ExtensionManager from './ExtensionManager'
|
import ExtensionManager from './ExtensionManager'
|
||||||
import EventEmitter from './EventEmitter'
|
import EventEmitter from './EventEmitter'
|
||||||
import { EditorOptions, EditorContent, CommandSpec } from './types'
|
import {
|
||||||
|
EditorOptions,
|
||||||
|
EditorContent,
|
||||||
|
CommandSpec,
|
||||||
|
EditorSelection,
|
||||||
|
} from './types'
|
||||||
import * as extensions from './extensions'
|
import * as extensions from './extensions'
|
||||||
import style from './style'
|
import style from './style'
|
||||||
|
|
||||||
@@ -39,7 +44,7 @@ export class Editor extends EventEmitter {
|
|||||||
|
|
||||||
public view!: EditorView
|
public view!: EditorView
|
||||||
|
|
||||||
public selection = { from: 0, to: 0 }
|
public selection: EditorSelection = { from: 0, to: 0 }
|
||||||
|
|
||||||
public isFocused = false
|
public isFocused = false
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,10 @@
|
|||||||
import { TextSelection } from 'prosemirror-state'
|
import { EditorState, TextSelection } from 'prosemirror-state'
|
||||||
import { Editor } from '../Editor'
|
|
||||||
import { Command, FocusPosition } from '../types'
|
import { Command, FocusPosition } from '../types'
|
||||||
import minMax from '../utils/minMax'
|
import minMax from '../utils/minMax'
|
||||||
|
|
||||||
interface ResolvedSelection {
|
function resolveSelection(state: EditorState, position: FocusPosition = null) {
|
||||||
from: number,
|
if (!position) {
|
||||||
to: number,
|
return null
|
||||||
}
|
|
||||||
|
|
||||||
function resolveSelection(editor: Editor, position: FocusPosition = null): ResolvedSelection {
|
|
||||||
if (position === null) {
|
|
||||||
return editor.selection
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (position === 'start' || position === true) {
|
if (position === 'start' || position === true) {
|
||||||
@@ -21,17 +15,17 @@ function resolveSelection(editor: Editor, position: FocusPosition = null): Resol
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (position === 'end') {
|
if (position === 'end') {
|
||||||
const { size } = editor.state.doc.content
|
const { size } = state.doc.content
|
||||||
|
|
||||||
return {
|
return {
|
||||||
from: size,
|
from: size,
|
||||||
to: size - 1, // TODO: -1 only for nodes with content
|
to: size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
from: position as number,
|
from: position,
|
||||||
to: position as number,
|
to: position,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +42,7 @@ export const focus = (position: FocusPosition = null): Command => ({
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
const { from, to } = resolveSelection(editor, position)
|
const { from, to } = resolveSelection(editor.state, position) || editor.selection
|
||||||
const { doc } = tr
|
const { doc } = tr
|
||||||
const resolvedFrom = minMax(from, 0, doc.content.size)
|
const resolvedFrom = minMax(from, 0, doc.content.size)
|
||||||
const resolvedEnd = minMax(to, 0, doc.content.size)
|
const resolvedEnd = minMax(to, 0, doc.content.size)
|
||||||
|
|||||||
@@ -32,6 +32,11 @@ export interface EditorOptions {
|
|||||||
onBlur: (props: { event: FocusEvent }) => void,
|
onBlur: (props: { event: FocusEvent }) => void,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type EditorSelection = {
|
||||||
|
from: number,
|
||||||
|
to: number,
|
||||||
|
}
|
||||||
|
|
||||||
export type EditorContent = string | JSON | null
|
export type EditorContent = string | JSON | null
|
||||||
|
|
||||||
export type Command = (props: {
|
export type Command = (props: {
|
||||||
|
|||||||
Reference in New Issue
Block a user