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) {
|
setLinkUrl(command, url) {
|
||||||
command({ href: url })
|
command({ href: url })
|
||||||
this.hideLinkMenu()
|
this.hideLinkMenu()
|
||||||
this.editor.focus()
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ import {
|
|||||||
Emitter,
|
Emitter,
|
||||||
ExtensionManager,
|
ExtensionManager,
|
||||||
ComponentView,
|
ComponentView,
|
||||||
|
minMax,
|
||||||
} from './Utils'
|
} from './Utils'
|
||||||
import { Doc, Paragraph, Text } from './Nodes'
|
import { Doc, Paragraph, Text } from './Nodes'
|
||||||
import css from './style.css'
|
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) {
|
focus(position = null) {
|
||||||
if ((this.view.focused && position === null) || position === false) {
|
if ((this.view.focused && position === null) || position === false) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let pos = position
|
const { from, to } = this.resolveSelection(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)
|
|
||||||
|
|
||||||
|
this.setSelection(from, to)
|
||||||
setTimeout(() => this.view.focus(), 10)
|
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() {
|
blur() {
|
||||||
this.view.dom.blur()
|
this.view.dom.blur()
|
||||||
}
|
}
|
||||||
|
@ -5,4 +5,5 @@ export { default as Extension } from './Extension'
|
|||||||
export { default as ExtensionManager } from './ExtensionManager'
|
export { default as ExtensionManager } from './ExtensionManager'
|
||||||
export { default as injectCSS } from './injectCSS'
|
export { default as injectCSS } from './injectCSS'
|
||||||
export { default as Mark } from './Mark'
|
export { default as Mark } from './Mark'
|
||||||
|
export { default as minMax } from './minMax'
|
||||||
export { default as Node } from './Node'
|
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