mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-01 03:11:44 +02:00
refactor wrap inline transform
This commit is contained in:
@@ -771,11 +771,11 @@ export function wrapInlineAtRange(transform, range, inline) {
|
||||
|
||||
const startOff = startChild.key == startKey
|
||||
? startOffset
|
||||
: startChild.getOffset(startKey)
|
||||
: startChild.getOffset(startKey) + startOffset
|
||||
|
||||
const endOff = endChild.key == endKey
|
||||
? endOffset
|
||||
: endChild.getOffset(endKey)
|
||||
: endChild.getOffset(endKey) + endOffset
|
||||
|
||||
if (startBlock == endBlock) {
|
||||
transform.splitNodeByKey(endChild.key, endOff)
|
||||
@@ -784,26 +784,21 @@ export function wrapInlineAtRange(transform, range, inline) {
|
||||
state = transform.state
|
||||
document = state.document
|
||||
startBlock = document.getClosestBlock(startKey)
|
||||
const startInner = document.getNextText(startKey)
|
||||
const innerParent = document.getParent(startInner)
|
||||
const startInnerIndex = innerParent.nodes.indexOf(startInner)
|
||||
const endInner = startKey == endKey ? startInner : document.getDescendant(endKey)
|
||||
startChild = startBlock.getHighestChild(startKey)
|
||||
const startInner = document.getNextSibling(startChild)
|
||||
const startInnerIndex = startBlock.nodes.indexOf(startInner)
|
||||
const endInner = startKey == endKey ? startInner : startBlock.getHighestChild(endKey)
|
||||
const inlines = startBlock.nodes
|
||||
.skipUntil(n => n == startInner)
|
||||
.takeUntil(n => n == endInner)
|
||||
.push(endInner)
|
||||
|
||||
const node = inline.merge({
|
||||
key: uid(),
|
||||
nodes: inlines
|
||||
})
|
||||
|
||||
debugger
|
||||
const node = inline.merge({ key: uid() })
|
||||
|
||||
transform.insertNodeByKey(startBlock.key, startInnerIndex, node)
|
||||
|
||||
inlines.forEach((child, i) => {
|
||||
transform.removeNodeByKey(child.key, node.key, i)
|
||||
transform.moveNodeByKey(child.key, node.key, i)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -815,11 +810,24 @@ export function wrapInlineAtRange(transform, range, inline) {
|
||||
document = state.document
|
||||
startBlock = document.getDescendant(startBlock.key)
|
||||
endBlock = document.getDescendant(endBlock.key)
|
||||
// startChild = startBlock.
|
||||
|
||||
// const startInlines = startBlock.skip(startIndex)
|
||||
const startInlines = startBlock.nodes.slice(startIndex + 1)
|
||||
const endInlines = endBlock.nodes.slice(0, endIndex + 1)
|
||||
const startNode = inline.merge({ key: uid() })
|
||||
const endNode = inline.merge({ key: uid() })
|
||||
|
||||
blocks.rest().butLast().forEach((block) => {
|
||||
transform.insertNodeByKey(startBlock.key, startIndex + 1, startNode)
|
||||
transform.insertNodeByKey(endBlock.key, endIndex + 1, endNode)
|
||||
|
||||
startInlines.forEach((child, i) => {
|
||||
transform.moveNodeByKey(child.key, startNode.key, i)
|
||||
})
|
||||
|
||||
endInlines.forEach((child, i) => {
|
||||
transform.moveNodeByKey(child.key, endNode.key, i)
|
||||
})
|
||||
|
||||
blocks.slice(1, -1).forEach((block) => {
|
||||
const node = inline.merge({ key: uid() })
|
||||
transform.insertNodeByKey(block.key, 0, node)
|
||||
|
||||
|
@@ -54,7 +54,6 @@ export function insertNodeByKey(transform, key, index, node) {
|
||||
|
||||
parent = parent.merge({ nodes })
|
||||
document = isParent ? parent : document.updateDescendant(parent)
|
||||
document = document.normalize()
|
||||
state = state.merge({ document })
|
||||
|
||||
return transform.add(state, {
|
||||
@@ -108,7 +107,6 @@ export function insertTextByKey(transform, key, offset, text, marks) {
|
||||
export function moveNodeByKey(transform, key, newKey, newIndex) {
|
||||
let { state } = transform
|
||||
let { document } = state
|
||||
debugger
|
||||
const node = document.assertDescendant(key)
|
||||
const path = document.getPath(node)
|
||||
const newPath = document.getPath(newKey)
|
||||
@@ -124,7 +122,7 @@ export function moveNodeByKey(transform, key, newKey, newIndex) {
|
||||
|
||||
target = target.insertNode(newIndex, node)
|
||||
document = isTarget ? target : document.updateDescendant(target)
|
||||
document = document.normalize()
|
||||
// document = document.normalize()
|
||||
state = state.merge({ document })
|
||||
|
||||
return transform.add(state, {
|
||||
|
@@ -16,9 +16,6 @@ nodes:
|
||||
nodes:
|
||||
- kind: text
|
||||
text: rd
|
||||
- kind: inline
|
||||
type: hashtag
|
||||
nodes:
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
|
Reference in New Issue
Block a user