forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add transform wrapText (ianstormtaylor#227)
* Add basic implementation of wrapText * Default suffix for wrapText to prefix * Add more tests for afterText * Add tests "whole-block" and "empty-block" for wrapText * Add tests for across-blocks and across-inlines * Preserve selection on wrapText * Remove comment about cursor position * Document transformation "wratTextAtRange"
- Loading branch information
1 parent
10cfb6e
commit 17f703c
Showing
28 changed files
with
490 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
import assert from 'assert' | ||
|
||
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 | ||
}) | ||
|
||
const next = state | ||
.transform() | ||
.moveTo(range) | ||
.wrapText('[[', ']]') | ||
.apply() | ||
|
||
|
||
const updated = next.document.getTexts() | ||
|
||
assert.deepEqual( | ||
next.selection.toJS(), | ||
range.merge({ | ||
anchorKey: updated.get(0).key, | ||
anchorOffset: 4, | ||
focusKey: updated.get(1).key, | ||
focusOffset: 2, | ||
isBackward: false | ||
}).toJS() | ||
) | ||
|
||
return next | ||
} |
12 changes: 12 additions & 0 deletions
12
test/transforms/fixtures/wrap-text/across-blocks/input.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
12 changes: 12 additions & 0 deletions
12
test/transforms/fixtures/wrap-text/across-blocks/output.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
37 changes: 37 additions & 0 deletions
37
test/transforms/fixtures/wrap-text/across-inlines/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
import assert from 'assert' | ||
|
||
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 | ||
}) | ||
|
||
const next = state | ||
.transform() | ||
.moveTo(range) | ||
.wrapText('[[', ']]') | ||
.apply() | ||
|
||
|
||
const updated = next.document.getTexts() | ||
|
||
assert.deepEqual( | ||
next.selection.toJS(), | ||
range.merge({ | ||
anchorKey: updated.get(0).key, | ||
anchorOffset: 4, | ||
focusKey: updated.get(1).key, | ||
focusOffset: 2, | ||
isBackward: false | ||
}).toJS() | ||
) | ||
|
||
return next | ||
} |
15 changes: 15 additions & 0 deletions
15
test/transforms/fixtures/wrap-text/across-inlines/input.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
15 changes: 15 additions & 0 deletions
15
test/transforms/fixtures/wrap-text/across-inlines/output.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
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) | ||
.wrapText('[[', ']]') | ||
.apply() | ||
|
||
|
||
const updated = next.document.getTexts().get(0) | ||
|
||
assert.deepEqual( | ||
next.selection.toJS(), | ||
range.merge({ | ||
anchorKey: updated.key, | ||
anchorOffset: 2, | ||
focusKey: updated.key, | ||
focusOffset: 2, | ||
isBackward: false | ||
}).toJS() | ||
) | ||
|
||
return next | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
nodes: | ||
- kind: block | ||
type: paragraph | ||
nodes: | ||
- kind: text | ||
text: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
nodes: | ||
- kind: block | ||
type: paragraph | ||
nodes: | ||
- kind: text | ||
text: "[[]]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
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: 2, | ||
focusKey: first.key, | ||
focusOffset: 4 | ||
}) | ||
|
||
const next = state | ||
.transform() | ||
.moveTo(range) | ||
.wrapText('[[', ']]') | ||
.apply() | ||
|
||
|
||
const updated = next.document.getTexts().get(0) | ||
|
||
assert.deepEqual( | ||
next.selection.toJS(), | ||
range.merge({ | ||
anchorKey: updated.key, | ||
anchorOffset: 4, | ||
focusKey: updated.key, | ||
focusOffset: 6, | ||
isBackward: false | ||
}).toJS() | ||
) | ||
|
||
return next | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
nodes: | ||
- kind: block | ||
type: paragraph | ||
nodes: | ||
- kind: text | ||
text: word |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] |
36 changes: 36 additions & 0 deletions
36
test/transforms/fixtures/wrap-text/middle-of-block/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
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) | ||
.wrapText('[[', ']]') | ||
.apply() | ||
|
||
|
||
const updated = next.document.getTexts().get(0) | ||
|
||
assert.deepEqual( | ||
next.selection.toJS(), | ||
range.merge({ | ||
anchorKey: updated.key, | ||
anchorOffset: 3, | ||
focusKey: updated.key, | ||
focusOffset: 5, | ||
isBackward: false | ||
}).toJS() | ||
) | ||
|
||
return next | ||
} |
Oops, something went wrong.