Skip to content

Commit

Permalink
add wrapTextAtRange tests, cleanup wrapText style and backwards handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ianstormtaylor committed Aug 9, 2016
1 parent 17f703c commit 90d4ece
Show file tree
Hide file tree
Showing 33 changed files with 395 additions and 27 deletions.
8 changes: 4 additions & 4 deletions docs/reference/models/transform.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ Wrap the [`Block`](./block.md) nodes in the current selection with a new [`Block
Wrap the [`Inline`](./inline.md) nodes in the current selection with a new [`Inline`](./inline.md) node of `type`, with optional `data`.

### `wrapText`
`wrapText(before: String, after: String) => Transform`
`wrapText(prefix: String, [suffix: String]) => Transform`

Surround the text in the current selection.
Surround the text in the current selection with `prefix` and `suffix` strings. If the `suffix` is ommitted, the `prefix` will be used instead.


## Selection Transforms
Expand Down Expand Up @@ -378,9 +378,9 @@ Wrap the [`Block`](./block.md) nodes in a `range` with a new [`Block`](./block.m
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.

### `wrapTextAtRange`
`wrapTextAtRange(range: Selection, prefix: String, suffix: String) => Transform`
`wrapTextAtRange(range: Selection, prefix: String, [suffix: String]) => Transform`

Surround the text in a `range`.
Surround the text in a `range` with `prefix` and `suffix` strings. If the `suffix` is ommitted, the `prefix` will be used instead.

## History Transforms

Expand Down
31 changes: 19 additions & 12 deletions lib/models/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -1146,21 +1146,28 @@ class State extends new Record(DEFAULTS) {
*/

wrapText(prefix, suffix = prefix) {
let { document, selection } = this
let { startKey, endKey, startOffset, endOffset } = selection
let acrossBlocks = (startKey !== endKey)
let state = this
let { document, selection } = state
let { anchorOffset, anchorKey, focusOffset, focusKey, isBackward } = selection
let after

document = document.wrapTextAtRange(selection, prefix, suffix)
// Determine what the selection should be after wrapping.
if (anchorKey == focusKey) {
after = selection.moveForward(prefix.length)
}

selection = selection
.merge({
anchorKey: startKey,
anchorOffset: startOffset + prefix.length,
focusKey: endKey,
focusOffset: acrossBlocks ? endOffset : endOffset + prefix.length
})
else {
after = selection.merge({
anchorOffset: isBackward ? anchorOffset : anchorOffset + prefix.length,
focusOffset: isBackward ? focusOffset + prefix.length : focusOffset
})
}

return this.merge({ document, selection })
// Wrap the text and update the state.
document = document.wrapTextAtRange(selection, prefix, suffix)
selection = after
state = state.merge({ document, selection })
return state
}

/**
Expand Down
25 changes: 14 additions & 11 deletions lib/models/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ const Transforms = {
})

return node.normalize()
},
},

/**
* Wrap the text in a `range` in a prefix/suffix.
Expand All @@ -1029,16 +1029,19 @@ const Transforms = {
*/

wrapTextAtRange(range, prefix, suffix = prefix) {
let withPrefix = this.insertTextAtRange(range.collapseToAnchor(), prefix)
let acrossBlocks = (range.startKey !== range.endKey)
let withSuffix = withPrefix.insertTextAtRange(
range
.collapseToFocus()
.moveForward(acrossBlocks ? 0 : prefix.length),
suffix
)

return withSuffix
let node = this

// Insert text at the starting edge.
const { startKey, endKey } = range
const start = range.collapseToStart()
node = node.insertTextAtRange(start, prefix)

// Determine the new ending edge, and insert text there.
let end = range.collapseToEnd()
if (startKey == endKey) end = end.moveForward(prefix.length)
node = node.insertTextAtRange(end, suffix)

return node
}

}
Expand Down
18 changes: 18 additions & 0 deletions test/transforms/fixtures/wrap-text-at-range/across-blocks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = texts.last()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 2,
focusKey: last.key,
focusOffset: 2
})

return state
.transform()
.wrapTextAtRange(range, '[[', ']]')
.apply()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word
- kind: block
type: paragraph
nodes:
- kind: text
text: another
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: wo[[rd
- kind: block
type: paragraph
nodes:
- kind: text
text: an]]other
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = texts.last()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 2,
focusKey: last.key,
focusOffset: 2
})

return state
.transform()
.wrapTextAtRange(range, '[[', ']]')
.apply()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
text: word
- kind: inline
type: link
nodes:
- kind: text
text: another
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
text: wo[[rd
- kind: inline
type: link
nodes:
- kind: text
text: an]]other
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 3,
focusKey: first.key,
focusOffset: 1,
isBackward: true
})

return state
.transform()
.wrapTextAtRange(range, '[[', ']]')
.apply()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: w[[or]]d
17 changes: 17 additions & 0 deletions test/transforms/fixtures/wrap-text-at-range/empty-block/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

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()
.wrapTextAtRange(range, '[[', ']]')
.apply()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: "[[]]"
17 changes: 17 additions & 0 deletions test/transforms/fixtures/wrap-text-at-range/end-of-block/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 2,
focusKey: first.key,
focusOffset: 4
})

return state
.transform()
.wrapTextAtRange(range, '[[', ']]')
.apply()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: wo[[rd]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

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()
.wrapTextAtRange(range, '[[', ']]')
.apply()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: w[[or]]d
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

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: 2
})

return state
.transform()
.wrapTextAtRange(range, '[[', ']]')
.apply()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: "[[wo]]rd"
17 changes: 17 additions & 0 deletions test/transforms/fixtures/wrap-text-at-range/whole-block/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

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: 4
})

return state
.transform()
.wrapTextAtRange(range, '[[', ']]')
.apply()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word
Loading

0 comments on commit 90d4ece

Please sign in to comment.