fix: initialize autofocus selection in createView (#2212)
* initialize autofocus selection in `createView` * fix missing variable and null error * remove unused imports
This commit is contained in:
@@ -12,6 +12,7 @@ import createDocument from './helpers/createDocument'
|
||||
import getHTMLFromFragment from './helpers/getHTMLFromFragment'
|
||||
import getText from './helpers/getText'
|
||||
import isNodeEmpty from './helpers/isNodeEmpty'
|
||||
import resolveFocusPosition from './helpers/resolveFocusPosition'
|
||||
import getTextSeralizersFromSchema from './helpers/getTextSeralizersFromSchema'
|
||||
import createStyleTag from './utilities/createStyleTag'
|
||||
import isFunction from './utilities/isFunction'
|
||||
@@ -260,11 +261,14 @@ export class Editor extends EventEmitter<EditorEvents> {
|
||||
* Creates a ProseMirror view.
|
||||
*/
|
||||
private createView(): void {
|
||||
const doc = createDocument(this.options.content, this.schema, this.options.parseOptions)
|
||||
const selection = resolveFocusPosition(doc, this.options.autofocus)
|
||||
this.view = new EditorView(this.options.element, {
|
||||
...this.options.editorProps,
|
||||
dispatchTransaction: this.dispatchTransaction.bind(this),
|
||||
state: EditorState.create({
|
||||
doc: createDocument(this.options.content, this.schema, this.options.parseOptions),
|
||||
doc,
|
||||
selection,
|
||||
}),
|
||||
})
|
||||
|
||||
|
||||
@@ -1,42 +1,7 @@
|
||||
import { EditorState, Selection, TextSelection } from 'prosemirror-state'
|
||||
import { RawCommands, FocusPosition } from '../types'
|
||||
import minMax from '../utilities/minMax'
|
||||
import isTextSelection from '../helpers/isTextSelection'
|
||||
import isiOS from '../utilities/isiOS'
|
||||
|
||||
function resolveSelection(state: EditorState, position: FocusPosition = null) {
|
||||
if (!position) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (position === 'start' || position === true) {
|
||||
return {
|
||||
from: 0,
|
||||
to: 0,
|
||||
}
|
||||
}
|
||||
|
||||
const { size } = state.doc.content
|
||||
|
||||
if (position === 'end') {
|
||||
return {
|
||||
from: size,
|
||||
to: size,
|
||||
}
|
||||
}
|
||||
|
||||
if (position === 'all') {
|
||||
return {
|
||||
from: 0,
|
||||
to: size,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
from: position,
|
||||
to: position,
|
||||
}
|
||||
}
|
||||
import resolveFocusPosition from '../helpers/resolveFocusPosition'
|
||||
|
||||
declare module '@tiptap/core' {
|
||||
interface Commands<ReturnType> {
|
||||
@@ -95,13 +60,7 @@ export const focus: RawCommands['focus'] = (position = null, options) => ({
|
||||
return true
|
||||
}
|
||||
|
||||
const { from, to } = resolveSelection(editor.state, position) || editor.state.selection
|
||||
const { doc, storedMarks } = tr
|
||||
const minPos = Selection.atStart(doc).from
|
||||
const maxPos = Selection.atEnd(doc).to
|
||||
const resolvedFrom = minMax(from, minPos, maxPos)
|
||||
const resolvedEnd = minMax(to, minPos, maxPos)
|
||||
const selection = TextSelection.create(doc, resolvedFrom, resolvedEnd)
|
||||
const selection = resolveFocusPosition(editor.state.doc, position) || editor.state.selection
|
||||
const isSameSelection = editor.state.selection.eq(selection)
|
||||
|
||||
if (dispatch) {
|
||||
@@ -111,8 +70,8 @@ export const focus: RawCommands['focus'] = (position = null, options) => ({
|
||||
|
||||
// `tr.setSelection` resets the stored marks
|
||||
// so we’ll restore them if the selection is the same as before
|
||||
if (isSameSelection && storedMarks) {
|
||||
tr.setStoredMarks(storedMarks)
|
||||
if (isSameSelection && tr.storedMarks) {
|
||||
tr.setStoredMarks(tr.storedMarks)
|
||||
}
|
||||
|
||||
delayedFocus()
|
||||
|
||||
22
packages/core/src/helpers/resolveFocusPosition.ts
Normal file
22
packages/core/src/helpers/resolveFocusPosition.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Node as ProseMirrorNode } from 'prosemirror-model'
|
||||
import { Selection, TextSelection } from 'prosemirror-state'
|
||||
import { FocusPosition } from '../types'
|
||||
import minMax from '../utilities/minMax'
|
||||
|
||||
export default function resolveFocusPosition(
|
||||
doc: ProseMirrorNode,
|
||||
position: FocusPosition = null
|
||||
): Selection | null {
|
||||
|
||||
if (!position) return null
|
||||
if (position === 'start' || position === true) return Selection.atStart(doc)
|
||||
if (position === 'end') return Selection.atEnd(doc)
|
||||
if (position === 'all') return TextSelection.create(doc, 0, doc.content.size)
|
||||
|
||||
// Check if `position` is in bounds of the doc if `position` is a number.
|
||||
const minPos = Selection.atStart(doc).from
|
||||
const maxPos = Selection.atEnd(doc).to
|
||||
const resolvedFrom = minMax(position, minPos, maxPos)
|
||||
const resolvedEnd = minMax(position, minPos, maxPos)
|
||||
return TextSelection.create(doc, resolvedFrom, resolvedEnd)
|
||||
}
|
||||
Reference in New Issue
Block a user