1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-20 23:22:56 +01:00

Add transform "unwrapBlockByKey" and "unwrapInlineByKey" (#390)

* Add transform .unwrapInlineByKey with test and documentation

* Add transform .unwrapBlockByKey with test and documentation

* add failing test for unwrap limited to a range

* Improve creation of selection in unwrap[Inline|Block]ByKey

* Adapt unwrapInlineAtRange to take range in consideration

* Add test to unwrapInline only one in the document
This commit is contained in:
Samy Pessé 2016-10-25 21:04:12 +02:00 committed by Ian Storm Taylor
parent f1a5d6f3b4
commit f380943926
19 changed files with 223 additions and 13 deletions

View File

@ -57,6 +57,8 @@ Transform methods can either operate on the [`Document`](./document.md), the [`S
- [`setMarkByKey`](#setmarkbykey)
- [`setNodeByKey`](#setnodebykey)
- [`splitNodeByKey`](#splitnodebykey)
- [`unwrapInlineByKey`](#unwrapinlinebykey)
- [`unwrapBlockByKey`](#unwrapblockbykey)
- [Document Transforms](#document-transforms)
- [`deleteAtRange`](#deleteatrange)
- [`deleteBackwardAtRange`](#deletebackwardatrange)
@ -317,6 +319,17 @@ Set a dictionary of `properties` on a [`Node`](./node.md) by its `key`. For conv
Split a node by its `key` at an `offset`.
### `unwrapInlineByKey`
`unwrapInlineByKey(key: String, properties: Object) => Transform` <br/>
`unwrapInlineByKey(key: String, type: String) => Transform`
Unwrap all inner content of an [`Inline`](./inline.md) node that match `properties`. For convenience, you can pass a `type` string or `properties` object.
### `unwrapBlockByKey`
`unwrapBlockByKey(key: String, properties: Object) => Transform` <br/>
`unwrapBlockByKey(key: String, type: String) => Transform`
Unwrap all inner content of a [`Block`](./block.md) node that match `properties`. For convenience, you can pass a `type` string or `properties` object.
## Document Transforms

View File

@ -690,7 +690,7 @@ export function unwrapInlineAtRange(transform, range, properties) {
const { state } = transform
const { document } = state
const texts = document.getTexts()
const texts = document.getTextsAtRange(range)
const inlines = texts
.map((text) => {
return document.getClosest(text, (parent) => {

View File

@ -193,3 +193,39 @@ export function splitNodeByKey(transform, key, offset) {
const path = document.getPath(key)
return transform.splitNodeOperation(path, offset)
}
/**
* Unwrap content from an inline parent with `properties`.
*
* @param {Transform} transform
* @param {String} key
* @param {Object or String} properties
* @return {Transform}
*/
export function unwrapInlineByKey(transform, key, properties) {
const { state } = transform
const { document, selection } = state
const node = document.assertDescendant(key)
const texts = node.getTexts()
const range = selection.moveToRangeOf(texts.first(), texts.last())
return transform.unwrapInlineAtRange(range, properties)
}
/**
* Unwrap content from a block parent with `properties`.
*
* @param {Transform} transform
* @param {String} key
* @param {Object or String} properties
* @return {Transform}
*/
export function unwrapBlockByKey(transform, key, properties) {
const { state } = transform
const { document, selection } = state
const node = document.assertDescendant(key)
const texts = node.getTexts()
const range = selection.moveToRangeOf(texts.first(), texts.last())
return transform.unwrapBlockAtRange(range, properties)
}

View File

@ -92,6 +92,8 @@ import {
setMarkByKey,
setNodeByKey,
splitNodeByKey,
unwrapInlineByKey,
unwrapBlockByKey
} from './by-key'
/**
@ -238,6 +240,8 @@ export default {
setMarkByKey,
setNodeByKey,
splitNodeByKey,
unwrapInlineByKey,
unwrapBlockByKey,
/**
* On selection.

View File

@ -5,11 +5,12 @@ export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = texts.get(1)
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 1,
focusKey: first.key,
focusOffset: 3
focusKey: last.key,
focusOffset: 2
})
const next = state

View File

@ -0,0 +1,28 @@
import assert from 'assert'
export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.get(2)
const last = texts.get(3)
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 2,
focusKey: last.key,
focusOffset: 2
})
const next = state
.transform()
.moveTo(range)
.unwrapInline('hashtag')
.apply()
assert.deepEqual(
next.selection.toJS(),
range.toJS()
)
return next
}

View File

@ -0,0 +1,21 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: he
- kind: inline
type: hashtag
nodes:
- kind: text
text: ll
- kind: text
text: "o w"
- kind: inline
type: hashtag
nodes:
- kind: text
text: or
- kind: text
text: d

View File

@ -0,0 +1,14 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: he
- kind: inline
type: hashtag
nodes:
- kind: text
text: ll
- kind: text
text: "o word"

View File

@ -5,11 +5,12 @@ export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = texts.get(1)
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 1,
focusKey: first.key,
focusOffset: 3
focusKey: last.key,
focusOffset: 2
})
const next = state

View File

@ -5,11 +5,12 @@ export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = texts.get(1)
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 1,
focusKey: first.key,
focusOffset: 3
focusKey: last.key,
focusOffset: 2
})
const next = state

View File

@ -3,11 +3,12 @@ export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = texts.get(1)
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 1,
focusKey: first.key,
focusOffset: 3
focusKey: last.key,
focusOffset: 2
})
return state

View File

@ -3,11 +3,12 @@ export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = texts.get(1)
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 1,
focusKey: first.key,
focusOffset: 3
focusKey: last.key,
focusOffset: 2
})
return state

View File

@ -3,11 +3,12 @@ export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = texts.get(1)
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 1,
focusKey: first.key,
focusOffset: 3
focusKey: last.key,
focusOffset: 2
})
return state

View File

@ -0,0 +1,10 @@
export default function (state) {
const { document, selection } = state
const block = document.nodes.get(0)
return state
.transform()
.unwrapBlockByKey(block, 'quote')
.apply()
}

View File

@ -0,0 +1,18 @@
nodes:
- kind: block
type: quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word
- kind: block
type: quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word

View File

@ -0,0 +1,15 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word
- kind: block
type: quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word

View File

@ -0,0 +1,14 @@
import assert from 'assert'
export default function (state) {
const { document, selection } = state
const inline = document.assertPath([0, 1])
const next = state
.transform()
.unwrapInlineByKey(inline.key, 'hashtag')
.apply()
return next
}

View File

@ -0,0 +1,19 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: w
- kind: inline
type: hashtag
nodes:
- kind: text
text: or
- kind: text
text: d
- kind: inline
type: hashtag
nodes:
- kind: text
text: another

View File

@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word
- kind: inline
type: hashtag
nodes:
- kind: text
text: another