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

Merge remote-tracking branch 'slate/master' into schema-normalize

This commit is contained in:
Samy Pesse
2016-10-25 21:55:53 +02:00
19 changed files with 227 additions and 14 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,19 @@ 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.
>>>>>>> slate/master
## Document Transforms
### `deleteBackwardAtRange`

View File

@@ -775,7 +775,7 @@ export function unwrapInlineAtRange(transform, range, properties, options = {})
const { normalize = true } = options
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

@@ -323,3 +323,39 @@ export function splitNodeByKey(transform, key, offset, options = {}) {
return transform
}
/**
* 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

@@ -94,7 +94,8 @@ import {
setMarkByKey,
setNodeByKey,
splitNodeByKey,
unwrapInlineByKey
unwrapInlineByKey,
unwrapBlockByKey,
} from './by-key'
/**
@@ -250,6 +251,10 @@ export default {
setNodeByKey,
splitNodeByKey,
unwrapInlineByKey,
<<<<<<< HEAD
=======
unwrapBlockByKey,
>>>>>>> slate/master
/**
* 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