Skip to content

Commit

Permalink
fix(filters): stop preserveAtomicBlocks resetting blocks with text
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudcolas committed Jan 15, 2018
1 parent fbd16d3 commit 04d0554
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 49 deletions.
4 changes: 4 additions & 0 deletions src/lib/filters/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ export const preserveAtomicBlocks = (
const perservedBlocks = blockMap
.filter((block) => {
const entityKey = block.getEntityAt(0)
// Use the ES6 way of counting string length to account for unicode symbols.
// See https://mathiasbynens.be/notes/javascript-unicode.
const isSingleSymbol = Array.from(block.getText()).length === 1

return (
entityKey &&
isSingleSymbol &&
entityTypes.includes(content.getEntity(entityKey).getType())
)
})
Expand Down
32 changes: 32 additions & 0 deletions src/lib/filters/blocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ describe("blocks", () => {
],
data: {},
},
{
key: "d3072",
text: "📷",
type: "unstyled",
depth: 0,
inlineStyleRanges: [],
entityRanges: [
{
offset: 0,
length: 1,
key: 4,
},
],
data: {},
},
{
key: "affm4",
text: " ",
Expand All @@ -73,6 +88,21 @@ describe("blocks", () => {
entityRanges: [],
data: {},
},
{
key: "umi67",
text: "📷 Star list",
type: "unstyled",
depth: 0,
inlineStyleRanges: [],
entityRanges: [
{
offset: 0,
length: 1,
key: 4,
},
],
data: {},
},
],
})

Expand All @@ -86,8 +116,10 @@ describe("blocks", () => {
.toJS(),
).toEqual({
d3071: "atomic",
d3072: "atomic",
affm4: "unstyled",
abbm4: "unstyled",
umi67: "unstyled",
})
})

Expand Down
69 changes: 20 additions & 49 deletions src/tests/__snapshots__/pasting.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Snapshot Diff:
"data": Object {},
"depth": 0,
"entityRanges": Array [],
@@ -433,36 +402,24 @@
@@ -433,32 +402,20 @@
"type": "unstyled",
},
Object {
Expand All @@ -190,10 +190,8 @@ Snapshot Diff:
+ "entityRanges": Array [],
"inlineStyleRanges": Array [],
"key": "6umi7",
- "text": "📷 Star list",
- "type": "unstyled",
+ "text": " ",
+ "type": "atomic",
"text": "📷 Star list",
"type": "unstyled",
},
Object {
"data": Object {},
Expand All @@ -208,16 +206,10 @@ Snapshot Diff:
+ "entityRanges": Array [],
"inlineStyleRanges": Array [],
"key": "fonu3",
- "text": "📷 Star list",
- "type": "unstyled",
+ "text": " ",
+ "type": "atomic",
"text": "📷 Star list",
"type": "unstyled",
},
Object {
"data": Object {},
"depth": 0,
"entityRanges": Array [],
@@ -481,21 +438,15 @@
@@ -481,17 +438,11 @@
"type": "unstyled",
},
Object {
Expand All @@ -233,15 +225,9 @@ Snapshot Diff:
+ "entityRanges": Array [],
"inlineStyleRanges": Array [],
"key": "nl29",
- "text": "📷 Star list",
- "type": "unstyled",
+ "text": " ",
+ "type": "atomic",
"text": "📷 Star list",
"type": "unstyled",
},
Object {
"data": Object {},
"depth": 0,
"entityRanges": Array [],
@@ -518,11 +469,11 @@
"depth": 0,
"entityRanges": Array [],
Expand Down Expand Up @@ -331,7 +317,7 @@ Snapshot Diff:
"data": Object {},
"depth": 0,
"entityRanges": Array [],
@@ -860,42 +799,24 @@
@@ -860,32 +799,20 @@
"type": "unstyled",
},
Object {
Expand All @@ -347,10 +333,8 @@ Snapshot Diff:
+ "entityRanges": Array [],
"inlineStyleRanges": Array [],
"key": "7ri7s",
- "text": "📷 ",
- "type": "unstyled",
+ "text": " ",
+ "type": "atomic",
"text": "📷 ",
"type": "unstyled",
},
Object {
"data": Object {},
Expand All @@ -362,26 +346,13 @@ Snapshot Diff:
- "offset": 0,
- },
- ],
- "inlineStyleRanges": Array [
- Object {
- "length": 5,
- "offset": 1,
- "style": "BOLD",
- },
- ],
+ "entityRanges": Array [],
+ "inlineStyleRanges": Array [],
"key": "alt7k",
- "text": "📷Shape",
- "type": "unstyled",
+ "text": " ",
+ "type": "atomic",
},
Object {
"data": Object {},
"depth": 0,
"entityRanges": Array [],
@@ -911,21 +832,15 @@
"inlineStyleRanges": Array [
Object {
"length": 5,
"offset": 1,
"style": "BOLD",
@@ -911,21 +838,15 @@
"type": "unstyled",
},
Object {
Expand All @@ -406,7 +377,7 @@ Snapshot Diff:
"data": Object {},
"depth": 0,
"entityRanges": Array [],
@@ -941,21 +856,15 @@
@@ -941,21 +862,15 @@
"type": "unstyled",
},
Object {
Expand All @@ -431,7 +402,7 @@ Snapshot Diff:
"data": Object {},
"depth": 0,
"entityRanges": Array [],
@@ -1004,17 +913,11 @@
@@ -1004,17 +919,11 @@
"type": "unstyled",
},
Object {
Expand All @@ -450,7 +421,7 @@ Snapshot Diff:
"text": "[T.C1]Comment",
"type": "unstyled",
},
@@ -1028,186 +931,50 @@
@@ -1028,186 +937,50 @@
"type": "unstyled",
},
Object {
Expand Down

0 comments on commit 04d0554

Please sign in to comment.