mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-07 05:30:41 +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
|
const startOff = startChild.key == startKey
|
||||||
? startOffset
|
? startOffset
|
||||||
: startChild.getOffset(startKey)
|
: startChild.getOffset(startKey) + startOffset
|
||||||
|
|
||||||
const endOff = endChild.key == endKey
|
const endOff = endChild.key == endKey
|
||||||
? endOffset
|
? endOffset
|
||||||
: endChild.getOffset(endKey)
|
: endChild.getOffset(endKey) + endOffset
|
||||||
|
|
||||||
if (startBlock == endBlock) {
|
if (startBlock == endBlock) {
|
||||||
transform.splitNodeByKey(endChild.key, endOff)
|
transform.splitNodeByKey(endChild.key, endOff)
|
||||||
@@ -784,26 +784,21 @@ export function wrapInlineAtRange(transform, range, inline) {
|
|||||||
state = transform.state
|
state = transform.state
|
||||||
document = state.document
|
document = state.document
|
||||||
startBlock = document.getClosestBlock(startKey)
|
startBlock = document.getClosestBlock(startKey)
|
||||||
const startInner = document.getNextText(startKey)
|
startChild = startBlock.getHighestChild(startKey)
|
||||||
const innerParent = document.getParent(startInner)
|
const startInner = document.getNextSibling(startChild)
|
||||||
const startInnerIndex = innerParent.nodes.indexOf(startInner)
|
const startInnerIndex = startBlock.nodes.indexOf(startInner)
|
||||||
const endInner = startKey == endKey ? startInner : document.getDescendant(endKey)
|
const endInner = startKey == endKey ? startInner : startBlock.getHighestChild(endKey)
|
||||||
const inlines = startBlock.nodes
|
const inlines = startBlock.nodes
|
||||||
.skipUntil(n => n == startInner)
|
.skipUntil(n => n == startInner)
|
||||||
.takeUntil(n => n == endInner)
|
.takeUntil(n => n == endInner)
|
||||||
.push(endInner)
|
.push(endInner)
|
||||||
|
|
||||||
const node = inline.merge({
|
const node = inline.merge({ key: uid() })
|
||||||
key: uid(),
|
|
||||||
nodes: inlines
|
|
||||||
})
|
|
||||||
|
|
||||||
debugger
|
|
||||||
|
|
||||||
transform.insertNodeByKey(startBlock.key, startInnerIndex, node)
|
transform.insertNodeByKey(startBlock.key, startInnerIndex, node)
|
||||||
|
|
||||||
inlines.forEach((child, i) => {
|
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
|
document = state.document
|
||||||
startBlock = document.getDescendant(startBlock.key)
|
startBlock = document.getDescendant(startBlock.key)
|
||||||
endBlock = document.getDescendant(endBlock.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() })
|
const node = inline.merge({ key: uid() })
|
||||||
transform.insertNodeByKey(block.key, 0, node)
|
transform.insertNodeByKey(block.key, 0, node)
|
||||||
|
|
||||||
|
@@ -54,7 +54,6 @@ export function insertNodeByKey(transform, key, index, node) {
|
|||||||
|
|
||||||
parent = parent.merge({ nodes })
|
parent = parent.merge({ nodes })
|
||||||
document = isParent ? parent : document.updateDescendant(parent)
|
document = isParent ? parent : document.updateDescendant(parent)
|
||||||
document = document.normalize()
|
|
||||||
state = state.merge({ document })
|
state = state.merge({ document })
|
||||||
|
|
||||||
return transform.add(state, {
|
return transform.add(state, {
|
||||||
@@ -108,7 +107,6 @@ export function insertTextByKey(transform, key, offset, text, marks) {
|
|||||||
export function moveNodeByKey(transform, key, newKey, newIndex) {
|
export function moveNodeByKey(transform, key, newKey, newIndex) {
|
||||||
let { state } = transform
|
let { state } = transform
|
||||||
let { document } = state
|
let { document } = state
|
||||||
debugger
|
|
||||||
const node = document.assertDescendant(key)
|
const node = document.assertDescendant(key)
|
||||||
const path = document.getPath(node)
|
const path = document.getPath(node)
|
||||||
const newPath = document.getPath(newKey)
|
const newPath = document.getPath(newKey)
|
||||||
@@ -124,7 +122,7 @@ export function moveNodeByKey(transform, key, newKey, newIndex) {
|
|||||||
|
|
||||||
target = target.insertNode(newIndex, node)
|
target = target.insertNode(newIndex, node)
|
||||||
document = isTarget ? target : document.updateDescendant(target)
|
document = isTarget ? target : document.updateDescendant(target)
|
||||||
document = document.normalize()
|
// document = document.normalize()
|
||||||
state = state.merge({ document })
|
state = state.merge({ document })
|
||||||
|
|
||||||
return transform.add(state, {
|
return transform.add(state, {
|
||||||
|
@@ -16,9 +16,6 @@ nodes:
|
|||||||
nodes:
|
nodes:
|
||||||
- kind: text
|
- kind: text
|
||||||
text: rd
|
text: rd
|
||||||
- kind: inline
|
|
||||||
type: hashtag
|
|
||||||
nodes:
|
|
||||||
- kind: inline
|
- kind: inline
|
||||||
type: link
|
type: link
|
||||||
nodes:
|
nodes:
|
||||||
|
Reference in New Issue
Block a user