mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-15 19:29:02 +08:00
add setSelection method
This commit is contained in:
parent
d5c08c8876
commit
fabb6124cf
@ -105,7 +105,6 @@ export default {
|
||||
setLinkUrl(command, url) {
|
||||
command({ href: url })
|
||||
this.hideLinkMenu()
|
||||
this.editor.focus()
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import {
|
||||
Emitter,
|
||||
ExtensionManager,
|
||||
ComponentView,
|
||||
minMax,
|
||||
} from './Utils'
|
||||
import { Doc, Paragraph, Text } from './Nodes'
|
||||
import css from './style.css'
|
||||
@ -358,32 +359,53 @@ export default class Editor extends Emitter {
|
||||
})
|
||||
}
|
||||
|
||||
resolveSelection(position = null) {
|
||||
if (this.selection && position === null) {
|
||||
return this.selection
|
||||
}
|
||||
|
||||
if (position === 'start') {
|
||||
return {
|
||||
from: 0,
|
||||
to: 0,
|
||||
}
|
||||
}
|
||||
|
||||
if (position === 'end') {
|
||||
const { doc } = this.state
|
||||
return {
|
||||
from: doc.content.size,
|
||||
to: doc.content.size,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
from: position,
|
||||
to: position,
|
||||
}
|
||||
}
|
||||
|
||||
focus(position = null) {
|
||||
if ((this.view.focused && position === null) || position === false) {
|
||||
return
|
||||
}
|
||||
|
||||
let pos = position
|
||||
|
||||
if (this.selection && position === null) {
|
||||
pos = this.selection.from
|
||||
} else if (position === 'start') {
|
||||
pos = 0
|
||||
} else if (position === 'end') {
|
||||
pos = this.state.doc.nodeSize - 2
|
||||
}
|
||||
|
||||
// selection should be inside of the document range
|
||||
pos = Math.max(0, pos)
|
||||
pos = Math.min(this.state.doc.content.size, pos)
|
||||
|
||||
const selection = TextSelection.near(this.state.doc.resolve(pos))
|
||||
const transaction = this.state.tr.setSelection(selection)
|
||||
this.view.dispatch(transaction)
|
||||
const { from, to } = this.resolveSelection(position)
|
||||
|
||||
this.setSelection(from, to)
|
||||
setTimeout(() => this.view.focus(), 10)
|
||||
}
|
||||
|
||||
setSelection(from = 0, to = 0) {
|
||||
const { doc, tr } = this.state
|
||||
const resolvedFrom = minMax(from, 0, doc.content.size)
|
||||
const resolvedEnd = minMax(to, 0, doc.content.size)
|
||||
const selection = TextSelection.create(doc, resolvedFrom, resolvedEnd)
|
||||
const transaction = tr.setSelection(selection)
|
||||
|
||||
this.view.dispatch(transaction)
|
||||
}
|
||||
|
||||
blur() {
|
||||
this.view.dom.blur()
|
||||
}
|
||||
|
@ -5,4 +5,5 @@ export { default as Extension } from './Extension'
|
||||
export { default as ExtensionManager } from './ExtensionManager'
|
||||
export { default as injectCSS } from './injectCSS'
|
||||
export { default as Mark } from './Mark'
|
||||
export { default as minMax } from './minMax'
|
||||
export { default as Node } from './Node'
|
||||
|
3
packages/tiptap/src/Utils/minMax.js
Normal file
3
packages/tiptap/src/Utils/minMax.js
Normal file
@ -0,0 +1,3 @@
|
||||
export default function minMax(value = 0, min = 0, max = 0) {
|
||||
return Math.min(Math.max(parseInt(value, 10), min), max)
|
||||
}
|
Loading…
Reference in New Issue
Block a user