1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 10:29:48 +02:00

change wrapping transform arguments for consistency

This commit is contained in:
Ian Storm Taylor
2016-07-28 16:24:24 -07:00
parent c74b527d33
commit a892c6b800
50 changed files with 67 additions and 454 deletions

View File

@@ -317,24 +317,24 @@ Remove a [`mark`](./mark.md) from the characters in a `range`. For convenience,
Add or remove a [`mark`](./mark.md) from the characters in a `range`, depending on whether any of them already have the mark. For convenience, you can pass a `type` string or `properties` object to implicitly create a [`Mark`](./mark.md) of that type.
### `unwrapBlockAtRange`
`unwrapBlockAtRange(range: Selection, [type: String], [data: Data]) => Transform`
`unwrapBlockAtRange(range: Selection, properties: Object || String) => Transform`
Unwrap all [`Block`](./block.md) nodes in a `range` that match a `type` and/or `data`.
Unwrap all [`Block`](./block.md) nodes in a `range` that match `properties`. For convenience, you can pass a `type` string or `properties` object.
### `unwrapInlineAtRange`
`unwrapInlineAtRange(range: Selection, [type: String], [data: Data]) => Transform`
`unwrapInlineAtRange(range: Selection, properties: Object || String) => Transform`
Unwrap all [`Inline`](./inline.md) nodes in a `range` that match a `type` and/or `data`.
Unwrap all [`Inline`](./inline.md) nodes in a `range` that match `properties`. For convenience, you can pass a `type` string or `properties` object.
### `wrapBlockAtRange`
`wrapBlockAtRange(range: Selection, type: String, [data: Data]) => Transform`
`wrapBlockAtRange(range: Selection, properties: Object || String) => Transform`
Wrap the [`Block`](./block.md) nodes in a `range` with a new [`Block`](./block.md) node of `type`, with optional `data`.
Wrap the [`Block`](./block.md) nodes in a `range` with a new [`Block`](./block.md) node with `properties`. For convenience, you can pass a `type` string or `properties` object.
### `wrapInlineAtRange`
`wrapInlineAtRange(range: Selection, type: String, [data: Data]) => Transform`
`wrapInlineAtRange(range: Selection, properties: Object || String) => Transform`
Wrap the [`Inline`](./inline.md) nodes in a `range` with a new [`Inline`](./inline.md) node of `type`, with optional `data`.
Wrap the [`Inline`](./inline.md) nodes in a `range` with a new [`Inline`](./inline.md) node with `properties`. For convenience, you can pass a `type` string or `properties` object.
## History Transforms

View File

@@ -745,26 +745,17 @@ const Transforms = {
},
/**
* Unwrap all of the block nodes in a `range` from a block node of `type.`
* Unwrap all of the block nodes in a `range` from a block with `properties`.
*
* @param {Selection} range
* @param {String} type (optional)
* @param {Data or Object} data (optional)
* @param {String or Object} properties
* @return {Node} node
*/
unwrapBlockAtRange(range, type, data) {
unwrapBlockAtRange(range, properties) {
properties = normalizeProperties(properties)
let node = this
// Allow for only data.
if (typeof type == 'object') {
data = type
type = null
}
// Ensure that data is immutable.
if (data) data = Data.create(data)
// Get the deepest blocks in the range.
const blocks = node.getBlocksAtRange(range)
@@ -772,8 +763,8 @@ const Transforms = {
const wrappers = blocks.reduce((memo, text) => {
const match = node.getClosest(text, (parent) => {
if (parent.kind != 'block') return false
if (type && parent.type != type) return false
if (data && !parent.data.isSuperset(data)) return false
if (properties.type && parent.type != properties.type) return false
if (properties.data && !parent.data.isSuperset(properties.data)) return false
return true
})
@@ -846,34 +837,25 @@ const Transforms = {
},
/**
* Unwrap the inline nodes in a `range` from an parent inline with `type`.
* Unwrap the inline nodes in a `range` from an inline with `properties`.
*
* @param {Selection} range
* @param {String} type (optional)
* @param {Data} data (optional)
* @param {String or Object} properties
* @return {Node} node
*/
unwrapInlineAtRange(range, type, data) {
unwrapInlineAtRange(range, properties) {
properties = normalizeProperties(properties)
let node = this
let blocks = node.getInlinesAtRange(range)
// Allow for no type.
if (typeof type == 'object') {
data = type
type = null
}
// Ensure that data is immutable.
if (data) data = Data.create(data)
// Find the closest matching inline wrappers of each text node.
const texts = this.getTexts()
const wrappers = texts.reduce((memo, text) => {
const match = node.getClosest(text, (parent) => {
if (parent.kind != 'inline') return false
if (type && parent.type != type) return false
if (data && !parent.data.isSuperset(data)) return false
if (properties.type && parent.type != properties.type) return false
if (properties.data && !parent.data.isSuperset(properties.data)) return false
return true
})
@@ -900,16 +882,15 @@ const Transforms = {
},
/**
* Wrap all of the blocks in a `range` in a new block node of `type`.
* Wrap all of the blocks in a `range` in a new block with `properties`.
*
* @param {Selection} range
* @param {String} type
* @param {Data} data (optional)
* @param {String or Object} properties
* @return {Node} node
*/
wrapBlockAtRange(range, type, data) {
data = Data.create(data)
wrapBlockAtRange(range, properties) {
properties = normalizeProperties(properties)
let node = this
// Get the block nodes, sorted by depth.
@@ -936,8 +917,8 @@ const Transforms = {
// Wrap the siblings in a new block.
const wrapper = Block.create({
nodes: siblings,
type,
data
type: properties.type,
data: properties.data
})
// Replace the siblings with the wrapper.
@@ -958,16 +939,15 @@ const Transforms = {
},
/**
* Wrap the text and inline nodes in a `range` with a new inline node.
* Wrap the text and inlines in a `range` in a new inline with `properties`.
*
* @param {Selection} range
* @param {String} type
* @param {Data} data (optional)
* @param {String or Object} properties
* @return {Node} node
*/
wrapInlineAtRange(range, type, data) {
data = Data.create(data)
wrapInlineAtRange(range, properties) {
properties = normalizeProperties(properties)
let node = this
// If collapsed, there's nothing to wrap.
@@ -1007,11 +987,11 @@ const Transforms = {
// Iterate each of the child nodes, wrapping them.
children.forEach((child) => {
const obj = {}
obj.nodes = [child]
obj.type = type
if (data) obj.data = data
const wrapper = Inline.create(obj)
const wrapper = Inline.create({
nodes: [child],
type: properties.type,
data: properties.data
})
// Replace the child in it's parent with the wrapper.
const parent = node.getParent(child)

View File

@@ -1,19 +0,0 @@
import { Data } from '../../../../..'
export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 0,
focusKey: first.key,
focusOffset: 0
})
return state
.transform()
.unwrapBlockAtRange(range, 'quote', Data.create({ key: 'value' }))
.apply()
}

View File

@@ -12,6 +12,9 @@ export default function (state) {
return state
.transform()
.wrapBlockAtRange(range, 'quote', { key: 'value' })
.unwrapBlockAtRange(range, {
type: 'quote',
data: { key: 'value' }
})
.apply()
}

View File

@@ -1,12 +0,0 @@
nodes:
- kind: block
type: quote
data:
key: value
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word

View File

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

View File

@@ -1,28 +0,0 @@
import { Data } from '../../../../..'
import assert from 'assert'
export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 0,
focusKey: first.key,
focusOffset: 0
})
const next = state
.transform()
.moveTo(range)
.unwrapBlock('quote', Data.create({ key: 'value' }))
.apply()
assert.deepEqual(
next.selection.toJS(),
range.toJS()
)
return next
}

View File

@@ -1,12 +0,0 @@
nodes:
- kind: block
type: quote
data:
key: value
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word

View File

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

View File

@@ -15,7 +15,10 @@ export default function (state) {
const next = state
.transform()
.moveTo(range)
.unwrapBlock('quote', { key: 'value' })
.unwrapBlock({
type: 'quote',
data: { key: 'value' }
})
.apply()
assert.deepEqual(

View File

@@ -1,19 +0,0 @@
import { Data } from '../../../../..'
export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 1,
focusKey: first.key,
focusOffset: 3
})
return state
.transform()
.unwrapInlineAtRange(range, 'hashtag', Data.create({ key: 'one' }))
.apply()
}

View File

@@ -12,6 +12,9 @@ export default function (state) {
return state
.transform()
.wrapInlineAtRange(range, 'hashtag', { key: 'value' })
.unwrapInlineAtRange(range, {
type: 'hashtag',
data: { key: 'one' }
})
.apply()
}

View File

@@ -1,21 +0,0 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: w
- kind: inline
type: hashtag
data:
key: one
nodes:
- kind: inline
type: hashtag
data:
key: two
nodes:
- kind: text
text: or
- kind: text
text: d

View File

@@ -1,16 +0,0 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: w
- kind: inline
type: hashtag
data:
key: two
nodes:
- kind: text
text: or
- kind: text
text: d

View File

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

View File

@@ -1,21 +0,0 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: w
- kind: inline
type: hashtag
data:
key: one
nodes:
- kind: inline
type: hashtag
data:
key: two
nodes:
- kind: text
text: or
- kind: text
text: d

View File

@@ -1,16 +0,0 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: w
- kind: inline
type: hashtag
data:
key: two
nodes:
- kind: text
text: or
- kind: text
text: d

View File

@@ -15,7 +15,10 @@ export default function (state) {
const next = state
.transform()
.moveTo(range)
.unwrapInline('hashtag', { key: 'one' })
.unwrapInline({
type: 'hashtag',
data: { key: 'one' }
})
.apply()
assert.deepEqual(

View File

@@ -1,19 +0,0 @@
import { Data } from '../../../../..'
export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 0,
focusKey: first.key,
focusOffset: 0
})
return state
.transform()
.wrapBlockAtRange(range, 'quote', Data.create({ key: 'value' }))
.apply()
}

View File

@@ -12,6 +12,9 @@ export default function (state) {
return state
.transform()
.unwrapBlockAtRange(range, 'quote', { key: 'value' })
.wrapBlockAtRange(range, {
type: 'quote',
data: { key: 'value' }
})
.apply()
}

View File

@@ -1,12 +0,0 @@
nodes:
- kind: block
type: quote
data:
key: value
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word

View File

@@ -1,28 +0,0 @@
import { Data } from '../../../../..'
import assert from 'assert'
export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 0,
focusKey: first.key,
focusOffset: 0
})
const next = state
.transform()
.moveTo(range)
.wrapBlock('quote', Data.create({ key: 'value' }))
.apply()
assert.deepEqual(
next.selection.toJS(),
range.toJS()
)
return next
}

View File

@@ -1,12 +0,0 @@
nodes:
- kind: block
type: quote
data:
key: value
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word

View File

@@ -15,7 +15,10 @@ export default function (state) {
const next = state
.transform()
.moveTo(range)
.wrapBlock('quote', { key: 'value' })
.wrapBlock({
type: 'quote',
data: { key: 'value' }
})
.apply()
assert.deepEqual(

View File

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

View File

@@ -1,19 +0,0 @@
import { Data } from '../../../../..'
export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 1,
focusKey: first.key,
focusOffset: 3
})
return state
.transform()
.wrapInlineAtRange(range, 'hashtag', Data.create({ key: 'value' }))
.apply()
}

View File

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

View File

@@ -12,6 +12,9 @@ export default function (state) {
return state
.transform()
.unwrapInlineAtRange(range, 'hashtag', { key: 'one' })
.wrapInlineAtRange(range, {
type: 'hashtag',
data: { key: 'value' }
})
.apply()
}

View File

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

View File

@@ -1,16 +0,0 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: w
- kind: inline
type: hashtag
data:
key: value
nodes:
- kind: text
text: or
- kind: text
text: d

View File

@@ -1,35 +0,0 @@
import { Data } from '../../../../..'
import assert from 'assert'
export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 1,
focusKey: first.key,
focusOffset: 3
})
const next = state
.transform()
.moveTo(range)
.wrapInline('hashtag', Data.create({ key: 'value' }))
.apply()
const updated = next.document.getTexts().get(1)
assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: updated.key,
anchorOffset: 0,
focusKey: updated.key,
focusOffset: updated.length
}).toJS()
)
return next
}

View File

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

View File

@@ -1,16 +0,0 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: w
- kind: inline
type: hashtag
data:
key: value
nodes:
- kind: text
text: or
- kind: text
text: d

View File

@@ -15,7 +15,10 @@ export default function (state) {
const next = state
.transform()
.moveTo(range)
.wrapInline('hashtag', { key: 'value' })
.wrapInline({
type: 'hashtag',
data: { key: 'value' }
})
.apply()
const updated = next.document.getTexts().get(1)