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

[core/models] add replaceMark method on Change, tests, docs (#1910)

* [core/models/change] add replaceMark method on Change, tests, docs

* Update change.md
This commit is contained in:
David Chang
2018-06-21 19:46:40 -07:00
committed by Ian Storm Taylor
parent 270321b5dc
commit 2652680850
12 changed files with 366 additions and 0 deletions

View File

@@ -192,6 +192,14 @@ Split the [`Inline`](./inline.md) node in the current selection by `depth` level
Remove a [`mark`](./mark.md) from the characters in the current selection. For convenience, you can pass a `type` string or `properties` object to implicitly create a [`Mark`](./mark.md) of that type.
### `replaceMark`
`replaceMark(oldMark: Mark, newMark: Mark) => Change` <br/>
`replaceMark(oldProperties: Object, newProperties: Object) => Change` <br/>
`replaceMark(oldType: String, newType: String) => Change`
Replace a [`mark`](./mark.md) in the characters in the current selection. For convenience, you can pass a `type` string or `properties` object to implicitly create a [`Mark`](./mark.md) of that type.
### `toggleMark`
`toggleMark(mark: Mark) => Change` <br/>

View File

@@ -264,6 +264,19 @@ Changes.removeMark = (change, mark) => {
}
}
/**
* Replace an `oldMark` with a `newMark` in the characters in the current selection.
*
* @param {Change} change
* @param {Mark} oldMark
* @param {Mark} newMark
*/
Changes.replaceMark = (change, oldMark, newMark) => {
change.removeMark(oldMark)
change.addMark(newMark)
}
/**
* Add or remove a `mark` from the characters in the current selection,
* depending on whether it's already there.

View File

@@ -0,0 +1,37 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.replaceMark('italic', 'bold')
}
export const input = (
<value>
<document>
<paragraph>
wo<anchor />
<i>rd</i>
</paragraph>
<paragraph>
<i>an</i>
<focus />other
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
wo<anchor />
<b>rd</b>
</paragraph>
<paragraph>
<b>an</b>
<focus />other
</paragraph>
</document>
</value>
)

View File

@@ -0,0 +1,49 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.replaceMark('italic', 'bold')
}
export const input = (
<value>
<document>
<paragraph>
<link>
wo<anchor />
<i>rd</i>
</link>
<i />
</paragraph>
<paragraph>
<i />
<link>
<i>an</i>
<focus />other
</link>
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<link>
wo<anchor />
<b>rd</b>
</link>
<b />
</paragraph>
<paragraph>
<b />
<link>
<b>an</b>
<focus />other
</link>
</paragraph>
</document>
</value>
)

View File

@@ -0,0 +1,29 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.replaceMark('italic', 'bold').insertText('a')
}
export const input = (
<value>
<document>
<paragraph>
<i />
<cursor />word
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<b>a</b>
<cursor />word
</paragraph>
</document>
</value>
)

View File

@@ -0,0 +1,33 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.replaceMark('italic', 'bold')
}
export const input = (
<value>
<document>
<paragraph>
<anchor />
<i>
wo<focus />rd
</i>
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<anchor />
<b>wo</b>
<focus />
<i>rd</i>
</paragraph>
</document>
</value>
)

View File

@@ -0,0 +1,31 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.replaceMark('italic', 'bold')
}
export const input = (
<value>
<document>
<paragraph>
<anchor />
<i>w</i>
<focus />ord
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<anchor />
<b>w</b>
<focus />ord
</paragraph>
</document>
</value>
)

View File

@@ -0,0 +1,31 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.replaceMark('italic', 'bold')
}
export const input = (
<value>
<document>
<paragraph>
wor<anchor />
<i>d</i>
<focus />
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
wor<anchor />
<b>d</b>
<focus />
</paragraph>
</document>
</value>
)

View File

@@ -0,0 +1,31 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.replaceMark('italic', 'bold')
}
export const input = (
<value>
<document>
<paragraph>
w<anchor />
<i>o</i>
<focus />rd
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
w<anchor />
<b>o</b>
<focus />rd
</paragraph>
</document>
</value>
)

View File

@@ -0,0 +1,31 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.replaceMark('italic', 'bold')
}
export const input = (
<value>
<document>
<paragraph>
<anchor />
<i>word</i>
<focus />
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<anchor />
<b>word</b>
<focus />
</paragraph>
</document>
</value>
)

View File

@@ -0,0 +1,39 @@
/** @jsx h */
import h from '../../../helpers/h'
import { Mark } from '../../../..'
export default function(change) {
change.replaceMark(
'italic',
Mark.create({
type: 'bold',
data: { thing: 'value' },
})
)
}
export const input = (
<value>
<document>
<paragraph>
<anchor />
<i>w</i>
<focus />ord
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<anchor />
<b thing="value">w</b>
<focus />ord
</paragraph>
</document>
</value>
)

View File

@@ -0,0 +1,34 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.replaceMark('italic', {
type: 'bold',
data: { thing: 'value' },
})
}
export const input = (
<value>
<document>
<paragraph>
<anchor />
<i>w</i>
<focus />ord
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<anchor />
<b thing="value">w</b>
<focus />ord
</paragraph>
</document>
</value>
)