1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-18 21:21:21 +02:00

Merge branch 'pr393' into schema-normalize

This commit is contained in:
Samy Pessé
2016-10-25 15:48:46 +02:00
4 changed files with 62 additions and 16 deletions

View File

@@ -66,8 +66,8 @@ export function deleteAtRange(transform, range, options = {}) {
let ancestor = document.getCommonAncestor(startKey, endKey) let ancestor = document.getCommonAncestor(startKey, endKey)
let startChild = ancestor.getHighestChild(startKey) let startChild = ancestor.getHighestChild(startKey)
let endChild = ancestor.getHighestChild(endKey) let endChild = ancestor.getHighestChild(endKey)
const startOff = startChild.getOffset(startKey) + startOffset const startOff = (startChild.key === startKey ? 0 : startChild.getOffset(startKey)) + startOffset
const endOff = endChild.getOffset(endKey) + endOffset const endOff = (endChild.key === endKey ? 0 : endChild.getOffset(endKey)) + endOffset
transform = transform transform = transform
.splitNodeByKey(startChild.key, startOff, { normalize: false }) .splitNodeByKey(startChild.key, startOff, { normalize: false })
@@ -78,30 +78,37 @@ export function deleteAtRange(transform, range, options = {}) {
ancestor = document.getCommonAncestor(startKey, endKey) ancestor = document.getCommonAncestor(startKey, endKey)
const startBlock = document.getClosestBlock(startKey) const startBlock = document.getClosestBlock(startKey)
const endBlock = document.getClosestBlock(document.getNextText(endKey)) const endBlock = document.getClosestBlock(document.getNextText(endKey))
startChild = ancestor.getHighestChild(startBlock) startChild = ancestor.getHighestChild(startKey)
endChild = ancestor.getHighestChild(endBlock) endChild = ancestor.getHighestChild(endKey)
const startIndex = ancestor.nodes.indexOf(startChild) const startIndex = ancestor.nodes.indexOf(startChild)
const endIndex = ancestor.nodes.indexOf(endChild) const endIndex = ancestor.nodes.indexOf(endChild)
const middles = ancestor.nodes.slice(startIndex + 1, endIndex) const middles = ancestor.nodes.slice(startIndex + 1, endIndex + 1)
middles.forEach((child) => { if (middles.size) {
transform.removeNodeByKey(child.key, { normalize: false }) // remove first nodes directly so the document is not normalized
}) middles.butLast().forEach(child => {
transform.removeNodeOperation(transform.state.document.getPath(child.key))
})
endBlock.nodes.forEach((child, i) => { // remove last node so the document is normalized
const newKey = startBlock.key transform.removeNodeByKey(middles.last().key)
const newIndex = startBlock.nodes.size + i }
transform.moveNodeByKey(child.key, newKey, newIndex, { normalize: false })
})
const lonely = document.getFurthest(endBlock, p => p.nodes.size == 1) || endBlock if (startBlock.key !== endBlock.key) {
transform.removeNodeByKey(lonely.key, { normalize: false }) endBlock.nodes.forEach((child, i) => {
const newKey = startBlock.key
const newIndex = startBlock.nodes.size + i
transform.moveNodeByKey(child.key, newKey, newIndex)
})
const lonely = document.getFurthest(endBlock, p => p.nodes.size == 1) || endBlock
transform.removeNodeByKey(lonely.key)
}
if (normalize) { if (normalize) {
transform.normalizeNodeByKey(ancestor.key) transform.normalizeNodeByKey(ancestor.key)
} }
return transform return transform
} }

View File

@@ -0,0 +1,18 @@
export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = texts.last()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 1,
focusKey: last.key,
focusOffset: last.length - 1
})
return state
.transform()
.deleteAtRange(range)
.apply()
}

View File

@@ -0,0 +1,14 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: before
- kind: inline
type: link
nodes:
- kind: text
text: word
- kind: text
text: after

View File

@@ -0,0 +1,7 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: "br"