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 getHTMLFromFragment from './helpers/getHTMLFromFragment'
|
||||||
import getText from './helpers/getText'
|
import getText from './helpers/getText'
|
||||||
import isNodeEmpty from './helpers/isNodeEmpty'
|
import isNodeEmpty from './helpers/isNodeEmpty'
|
||||||
|
import resolveFocusPosition from './helpers/resolveFocusPosition'
|
||||||
import getTextSeralizersFromSchema from './helpers/getTextSeralizersFromSchema'
|
import getTextSeralizersFromSchema from './helpers/getTextSeralizersFromSchema'
|
||||||
import createStyleTag from './utilities/createStyleTag'
|
import createStyleTag from './utilities/createStyleTag'
|
||||||
import isFunction from './utilities/isFunction'
|
import isFunction from './utilities/isFunction'
|
||||||
@@ -260,11 +261,14 @@ export class Editor extends EventEmitter<EditorEvents> {
|
|||||||
* Creates a ProseMirror view.
|
* Creates a ProseMirror view.
|
||||||
*/
|
*/
|
||||||
private createView(): void {
|
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.view = new EditorView(this.options.element, {
|
||||||
...this.options.editorProps,
|
...this.options.editorProps,
|
||||||
dispatchTransaction: this.dispatchTransaction.bind(this),
|
dispatchTransaction: this.dispatchTransaction.bind(this),
|
||||||
state: EditorState.create({
|
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 { RawCommands, FocusPosition } from '../types'
|
||||||
import minMax from '../utilities/minMax'
|
|
||||||
import isTextSelection from '../helpers/isTextSelection'
|
import isTextSelection from '../helpers/isTextSelection'
|
||||||
import isiOS from '../utilities/isiOS'
|
import isiOS from '../utilities/isiOS'
|
||||||
|
import resolveFocusPosition from '../helpers/resolveFocusPosition'
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
declare module '@tiptap/core' {
|
declare module '@tiptap/core' {
|
||||||
interface Commands<ReturnType> {
|
interface Commands<ReturnType> {
|
||||||
@@ -95,13 +60,7 @@ export const focus: RawCommands['focus'] = (position = null, options) => ({
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
const { from, to } = resolveSelection(editor.state, position) || editor.state.selection
|
const selection = resolveFocusPosition(editor.state.doc, 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 isSameSelection = editor.state.selection.eq(selection)
|
const isSameSelection = editor.state.selection.eq(selection)
|
||||||
|
|
||||||
if (dispatch) {
|
if (dispatch) {
|
||||||
@@ -111,8 +70,8 @@ export const focus: RawCommands['focus'] = (position = null, options) => ({
|
|||||||
|
|
||||||
// `tr.setSelection` resets the stored marks
|
// `tr.setSelection` resets the stored marks
|
||||||
// so we’ll restore them if the selection is the same as before
|
// so we’ll restore them if the selection is the same as before
|
||||||
if (isSameSelection && storedMarks) {
|
if (isSameSelection && tr.storedMarks) {
|
||||||
tr.setStoredMarks(storedMarks)
|
tr.setStoredMarks(tr.storedMarks)
|
||||||
}
|
}
|
||||||
|
|
||||||
delayedFocus()
|
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