1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-01 05:16:10 +01:00

Fix #1463: don't split inline tags when wrapping a range in an inline (#1876)

* Added failing test for #1463

* wip

* test passing

* cleaned up, more tests

* lint

* inside inlines working with marks

* lint

* working with leaves

* tidy

* lint issues

* lint - spacing

* using splitText
This commit is contained in:
Cris Ward 2018-07-19 20:53:25 +01:00 committed by Ian Storm Taylor
parent 3405e2f822
commit c4471a5f37
5 changed files with 166 additions and 8 deletions

View File

@ -1,6 +1,5 @@
import { List } from 'immutable'
import logger from 'slate-dev-logger'
import Block from '../models/block'
import Inline from '../models/inline'
import Mark from '../models/mark'
@ -1370,16 +1369,20 @@ Changes.wrapInlineAtRange = (change, range, inline, options = {}) => {
const blocks = document.getBlocksAtRange(range)
let startBlock = document.getClosestBlock(startKey)
let endBlock = document.getClosestBlock(endKey)
const startInline = document.getClosestInline(startKey)
const endInline = document.getClosestInline(endKey)
let startChild = startBlock.getFurthestAncestor(startKey)
let endChild = endBlock.getFurthestAncestor(endKey)
change.splitDescendantsByKey(endChild.key, endKey, endOffset, {
normalize: false,
})
if (!startInline || startInline != endInline) {
change.splitDescendantsByKey(endChild.key, endKey, endOffset, {
normalize: false,
})
change.splitDescendantsByKey(startChild.key, startKey, startOffset, {
normalize: false,
})
change.splitDescendantsByKey(startChild.key, startKey, startOffset, {
normalize: false,
})
}
document = change.value.document
startBlock = document.getDescendant(startBlock.key)
@ -1389,7 +1392,24 @@ Changes.wrapInlineAtRange = (change, range, inline, options = {}) => {
const startIndex = startBlock.nodes.indexOf(startChild)
const endIndex = endBlock.nodes.indexOf(endChild)
if (startBlock == endBlock) {
if (startInline && startInline == endInline) {
const text = startBlock
.getTextsAtRange(range)
.get(0)
.splitText(startOffset)[1]
.splitText(endOffset - startOffset)[0]
inline = inline.set('nodes', List([text]))
Changes.insertInlineAtRange(change, range, inline, { normalize: false })
const inlinekey = inline.getFirstText().key
const rng = {
anchorKey: inlinekey,
focusKey: inlinekey,
anchorOffset: 0,
focusOffset: endOffset - startOffset,
isFocused: true,
}
change.select(rng)
} else if (startBlock == endBlock) {
document = change.value.document
startBlock = document.getClosestBlock(startKey)
startChild = startBlock.getFurthestAncestor(startKey)

View File

@ -0,0 +1,33 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.wrapInline('hashtag')
}
export const input = (
<value>
<document>
<paragraph>
<link>
<anchor />hel<focus />lo
</link>
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<link>
<hashtag>
<anchor />hel<focus />
</hashtag>lo
</link>
</paragraph>
</document>
</value>
)

View File

@ -0,0 +1,33 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.wrapInline('hashtag')
}
export const input = (
<value>
<document>
<paragraph>
<link>
hel<anchor />lo<focus />
</link>
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<link>
hel<hashtag>
<anchor />lo<focus />
</hashtag>
</link>
</paragraph>
</document>
</value>
)

View File

@ -0,0 +1,39 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.wrapInline('hashtag')
}
export const input = (
<value>
<document>
<paragraph>
<link>
<b>
h<anchor />ell
</b>
<focus />o
</link>
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<link>
<b>h</b>
<hashtag>
<b>
<anchor />ell<focus />
</b>
</hashtag>o
</link>
</paragraph>
</document>
</value>
)

View File

@ -0,0 +1,33 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.wrapInline('hashtag')
}
export const input = (
<value>
<document>
<paragraph>
<link>
he<anchor />ll<focus />o
</link>
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<link>
he<hashtag>
<anchor />ll<focus />
</hashtag>o
</link>
</paragraph>
</document>
</value>
)