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

Add transform .unwrapInlineByKey with test and documentation

This commit is contained in:
Samy Pesse
2016-10-17 01:14:47 +02:00
parent b7eb15dfad
commit fc318ec78b
6 changed files with 60 additions and 0 deletions

View File

@@ -57,6 +57,7 @@ Transform methods can either operate on the [`Document`](./document.md), the [`S
- [`setMarkByKey`](#setmarkbykey)
- [`setNodeByKey`](#setnodebykey)
- [`splitNodeByKey`](#splitnodebykey)
- [`unwrapInlineByKey`](#unwrapinlinebykey)
- [Document Transforms](#document-transforms)
- [`deleteAtRange`](#deleteatrange)
- [`deleteBackwardAtRange`](#deletebackwardatrange)
@@ -317,6 +318,11 @@ 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.
## Document Transforms

View File

@@ -193,3 +193,20 @@ 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 range = selection.moveToRangeOf(node)
return transform.unwrapInline(range, properties)
}

View File

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

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,14 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: w
- kind: inline
type: hashtag
nodes:
- kind: text
text: or
- kind: text
text: d

View File

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