Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit f9a0a43

Browse files
authored
Merge pull request #4848 from withspectrum/fix-messages-getting-lines-appended
Fix messages getting empty lines appended
2 parents b9ffdfb + 104ae6f commit f9a0a43

File tree

4 files changed

+96
-18
lines changed

4 files changed

+96
-18
lines changed

shared/draft-utils/add-embeds-to-draft-js.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const addEmbedsToEditorState = (
3434
if (block.type !== 'unstyled') return;
3535

3636
const embeds = getEmbedsFromText(block.text);
37-
if (!embeds.length === 0) return;
37+
if (embeds.length === 0) return;
3838

3939
embeds.forEach(embed => {
4040
lastEntityKey++;
@@ -64,19 +64,6 @@ export const addEmbedsToEditorState = (
6464
type: 'embed',
6565
};
6666
});
67-
// If this is the last block we need to add an empty block below the atomic block,
68-
// otherwise users cannot remove the embed during editing
69-
if (index === input.blocks.length - 1) {
70-
newBlocks.push({
71-
type: 'unstyled',
72-
data: {},
73-
text: ' ',
74-
depth: 0,
75-
entityRanges: [],
76-
inlineStyleRanges: [],
77-
key: genKey(),
78-
});
79-
}
8067
});
8168

8269
return {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`should add embeds 1`] = `
4+
Object {
5+
"blocks": Array [
6+
Object {
7+
"data": Object {},
8+
"depth": 0,
9+
"entityRanges": Array [],
10+
"inlineStyleRanges": Array [],
11+
"key": "g0000",
12+
"text": "https://simplecast.com/s/a1f11d11",
13+
"type": "unstyled",
14+
},
15+
Object {
16+
"data": Object {},
17+
"depth": 0,
18+
"entityRanges": Array [
19+
Object {
20+
"key": -Infinity,
21+
"length": 1,
22+
"offset": 0,
23+
},
24+
],
25+
"inlineStyleRanges": Array [],
26+
"key": "0",
27+
"text": " ",
28+
"type": "atomic",
29+
},
30+
],
31+
"entityMap": Object {
32+
"-Infinity": Object {
33+
"data": Object {
34+
"height": 200,
35+
"src": "https://embed.simplecast.com/a1f11d11",
36+
"url": "https://embed.simplecast.com/a1f11d11",
37+
},
38+
"mutability": "MUTABLE",
39+
"type": "embed",
40+
},
41+
},
42+
}
43+
`;

shared/draft-utils/test/add-embeds-to-draft-js.test.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// @flow
2-
import { getEmbedsFromText } from '../add-embeds-to-draft-js';
2+
jest.mock('draft-js/lib/generateRandomKey', () => {
3+
let last = 0;
4+
const multiplier = Math.exp(24);
5+
return () => Math.floor(last++ * multiplier).toString(32);
6+
});
7+
import {
8+
getEmbedsFromText,
9+
addEmbedsToEditorState,
10+
} from '../add-embeds-to-draft-js';
311

412
describe('sites', () => {
513
describe('<iframe>', () => {
@@ -357,3 +365,39 @@ describe('complex text', () => {
357365
]);
358366
});
359367
});
368+
369+
it('should not change anything if there are not embeds to add', () => {
370+
const input = {
371+
blocks: [
372+
{
373+
type: 'unstyled',
374+
key: 'g0000',
375+
data: {},
376+
depth: 0,
377+
inlineStyleRanges: [],
378+
entityRanges: [],
379+
text: 'Hello world!',
380+
},
381+
],
382+
entityMap: {},
383+
};
384+
expect(addEmbedsToEditorState(input)).toEqual(input);
385+
});
386+
387+
it('should add embeds', () => {
388+
const input = {
389+
blocks: [
390+
{
391+
type: 'unstyled',
392+
key: 'g0000',
393+
data: {},
394+
depth: 0,
395+
inlineStyleRanges: [],
396+
entityRanges: [],
397+
text: 'https://simplecast.com/s/a1f11d11',
398+
},
399+
],
400+
entityMap: {},
401+
};
402+
expect(addEmbedsToEditorState(input)).toMatchSnapshot();
403+
});

src/views/thread/components/threadDetail.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,17 @@ class ThreadDetailPure extends React.Component<Props, State> {
309309
uploadFiles = files => {
310310
const uploading = `![Uploading ${files[0].name}...]()`;
311311
let caretPos = this.bodyEditor.selectionStart;
312+
const { body } = this.state;
313+
if (!body) return;
312314

313315
this.setState(
314-
({ body }) => ({
316+
{
315317
isSavingEdit: true,
316318
body:
317319
body.substring(0, caretPos) +
318320
uploading +
319-
body.substring(this.bodyEditor.selectionEnd, this.state.body.length),
320-
}),
321+
body.substring(this.bodyEditor.selectionEnd, body.length),
322+
},
321323
() => {
322324
caretPos = caretPos + uploading.length;
323325
this.bodyEditor.selectionStart = caretPos;
@@ -335,6 +337,7 @@ class ThreadDetailPure extends React.Component<Props, State> {
335337
this.setState({
336338
isSavingEdit: false,
337339
});
340+
if (!this.state.body) return;
338341
this.changeBody({
339342
target: {
340343
value: this.state.body.replace(
@@ -349,6 +352,7 @@ class ThreadDetailPure extends React.Component<Props, State> {
349352
this.setState({
350353
isSavingEdit: false,
351354
});
355+
if (!this.state.body) return;
352356
this.changeBody({
353357
target: {
354358
value: this.state.body.replace(uploading, ''),

0 commit comments

Comments
 (0)