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

Commit

Permalink
Merge branch 'alpha' of github.com:withspectrum/spectrum into 2.8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlovin committed Mar 9, 2019
2 parents a2f8f2d + f9a0a43 commit 73303e8
Show file tree
Hide file tree
Showing 24 changed files with 327 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import type { FileUpload } from 'shared/types';
import { events } from 'shared/analytics';
import { trackQueue } from 'shared/bull/queues';
import { isAuthedResolver as requireAuth } from '../../utils/permissions';
import { messageTypeObj } from 'shared/draft-utils/process-message-content';
import type { MessageType } from 'shared/draft-utils/process-message-content';
import { messageTypeObj } from 'shared/draft-utils/message-types';
import type { MessageType } from 'shared/draft-utils/message-types';

export type CreateDirectMessageThreadInput = {
input: {
Expand Down
2 changes: 1 addition & 1 deletion api/mutations/message/addMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { validateRawContentState } from '../../utils/validate-draft-js-input';
import processMessageContent, {
messageTypeObj,
} from 'shared/draft-utils/process-message-content';
import type { MessageType } from 'shared/draft-utils/process-message-content';
import type { MessageType } from 'shared/draft-utils/message-types';

type Input = {
message: {
Expand Down
2 changes: 1 addition & 1 deletion api/mutations/message/editMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { validateRawContentState } from '../../utils/validate-draft-js-input';
import processMessageContent, {
messageTypeObj,
} from 'shared/draft-utils/process-message-content';
import type { MessageType } from 'shared/draft-utils/process-message-content';
import type { MessageType } from 'shared/draft-utils/message-types';

type Args = {
input: {
Expand Down
15 changes: 13 additions & 2 deletions api/queries/thread/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,16 @@
import type { GraphQLContext } from '../../';
import type { DBThread } from 'shared/types';

export default ({ channelId }: DBThread, _: any, { loaders }: GraphQLContext) =>
loaders.channel.load(channelId);
export default async (root: DBThread, _: any, ctx: GraphQLContext) => {
const { channelId, id } = root;
const { loaders } = ctx;
const channel = await loaders.channel.load(channelId);
if (!channel) {
console.error(
'User queried thread of non-existent/deleted channel: ',
channelId
);
console.error('Thread queried: ', id);
}
return channel;
};
18 changes: 13 additions & 5 deletions api/queries/thread/community.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
import type { GraphQLContext } from '../../';
import type { DBThread } from 'shared/types';

export default (
{ communityId }: DBThread,
_: any,
{ loaders }: GraphQLContext
) => loaders.community.load(communityId);
export default async (root: DBThread, _: any, ctx: GraphQLContext) => {
const { communityId, id } = root;
const { loaders } = ctx;
const community = await loaders.community.load(communityId);
if (!community) {
console.error(
'User queried thread of non-existent/deleted community: ',
communityId
);
console.error('Thread queried: ', id);
}
return community;
};
53 changes: 53 additions & 0 deletions flow-typed/npm/snarkdown_vx.x.x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// flow-typed signature: 8f063755e65f032485439ac4cefffded
// flow-typed version: <<STUB>>/snarkdown_vx.x.x/flow_v0.66.0

/**
* This is an autogenerated libdef stub for:
*
* 'snarkdown'
*
* Fill this stub out by replacing all the `any` types.
*
* Once filled out, we encourage you to share your work with the
* community by sending a pull request to:
* https://github.com/flowtype/flow-typed
*/

declare module 'snarkdown' {
declare module.exports: any;
}

/**
* We include stubs for each file inside this npm package in case you need to
* require those files directly. Feel free to delete any files that aren't
* needed.
*/
declare module 'snarkdown/dist/snarkdown.es' {
declare module.exports: any;
}

declare module 'snarkdown/dist/snarkdown' {
declare module.exports: any;
}

declare module 'snarkdown/dist/snarkdown.umd' {
declare module.exports: any;
}

declare module 'snarkdown/src/index' {
declare module.exports: any;
}

// Filename aliases
declare module 'snarkdown/dist/snarkdown.es.js' {
declare module.exports: $Exports<'snarkdown/dist/snarkdown.es'>;
}
declare module 'snarkdown/dist/snarkdown.js' {
declare module.exports: $Exports<'snarkdown/dist/snarkdown'>;
}
declare module 'snarkdown/dist/snarkdown.umd.js' {
declare module.exports: $Exports<'snarkdown/dist/snarkdown.umd'>;
}
declare module 'snarkdown/src/index.js' {
declare module.exports: $Exports<'snarkdown/src/index'>;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
"slate": "^0.20.1",
"slate-markdown": "0.1.1",
"slugg": "^1.1.0",
"snarkdown": "^1.2.2",
"stopword": "^0.1.13",
"string-replace-to-array": "^1.0.3",
"string-similarity": "^2.0.0",
Expand Down Expand Up @@ -293,4 +294,4 @@
]
},
"pre-commit": "lint:staged"
}
}
9 changes: 2 additions & 7 deletions shared/clients/draft-js/utils/isShort.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
// @flow
// Determine whether a DraftJS message is short
import { toPlainText, toState } from '../../../draft-utils';

import type { RawDraftContentState } from 'draft-js';
import { toPlainText } from './plaintext';
import type { MessageInfoType } from '../../../graphql/fragments/message/messageInfo';

export const isShort = (message: MessageInfoType): boolean => {
if (message.messageType === 'media') return false;
const jsonBody = JSON.parse(message.content.body);
return (
jsonBody.blocks.length <= 1 && toPlainText(toState(jsonBody)).length <= 170
);
return jsonBody.blocks.length <= 1 && toPlainText(jsonBody).length <= 170;
};
9 changes: 9 additions & 0 deletions shared/clients/draft-js/utils/plaintext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @flow
import type { RawContentState } from 'draft-js';

export const toPlainText = (raw: RawContentState) => {
return raw.blocks
.filter(block => block.type === 'unstyled')
.map(block => block.text)
.join('\n');
};
15 changes: 1 addition & 14 deletions shared/draft-utils/add-embeds-to-draft-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const addEmbedsToEditorState = (
if (block.type !== 'unstyled') return;

const embeds = getEmbedsFromText(block.text);
if (!embeds.length === 0) return;
if (embeds.length === 0) return;

embeds.forEach(embed => {
lastEntityKey++;
Expand Down Expand Up @@ -64,19 +64,6 @@ export const addEmbedsToEditorState = (
type: 'embed',
};
});
// If this is the last block we need to add an empty block below the atomic block,
// otherwise users cannot remove the embed during editing
if (index === input.blocks.length - 1) {
newBlocks.push({
type: 'unstyled',
data: {},
text: ' ',
depth: 0,
entityRanges: [],
inlineStyleRanges: [],
key: genKey(),
});
}
});

return {
Expand Down
8 changes: 8 additions & 0 deletions shared/draft-utils/message-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @flow

export const messageTypeObj = {
text: 'text',
media: 'media',
draftjs: 'draftjs',
};
export type MessageType = $Keys<typeof messageTypeObj>;
8 changes: 2 additions & 6 deletions shared/draft-utils/process-message-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@
import { stateFromMarkdown } from 'draft-js-import-markdown';
import { convertFromRaw, convertToRaw, EditorState } from 'draft-js';
import { addEmbedsToEditorState } from './add-embeds-to-draft-js';
import { messageTypeObj, type MessageType } from './message-types';

export const messageTypeObj = {
text: 'text',
media: 'media',
draftjs: 'draftjs',
};
export type MessageType = $Keys<typeof messageTypeObj>;
export { messageTypeObj };

export default (type: MessageType, body: string): string => {
let newBody = body;
Expand Down
8 changes: 4 additions & 4 deletions shared/draft-utils/process-thread-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { stateFromMarkdown } from 'draft-js-import-markdown';
import { convertFromRaw, convertToRaw, EditorState } from 'draft-js';
import { addEmbedsToEditorState } from './add-embeds-to-draft-js';

export default (type: 'TEXT' | 'DRAFTJS', body: string): string => {
export default (type: 'TEXT' | 'DRAFTJS', body: ?string): string => {
let newBody = body;
if (type === 'TEXT') {
// workaround react-mentions bug by replacing @[username] with @username
// @see withspectrum/spectrum#4587
newBody = newBody.replace(/@\[([a-z0-9_-]+)\]/g, '@$1');
newBody = newBody ? newBody.replace(/@\[([a-z0-9_-]+)\]/g, '@$1') : '';
newBody = JSON.stringify(
convertToRaw(
stateFromMarkdown(newBody, {
Expand Down Expand Up @@ -43,10 +43,10 @@ export default (type: 'TEXT' | 'DRAFTJS', body: string): string => {

// Add automatic embeds to body
try {
return JSON.stringify(addEmbedsToEditorState(JSON.parse(newBody)));
return JSON.stringify(addEmbedsToEditorState(JSON.parse(newBody || '')));
// Ignore errors during automatic embed detection
} catch (err) {
console.error(err);
return newBody;
return newBody || '';
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should add embeds 1`] = `
Object {
"blocks": Array [
Object {
"data": Object {},
"depth": 0,
"entityRanges": Array [],
"inlineStyleRanges": Array [],
"key": "g0000",
"text": "https://simplecast.com/s/a1f11d11",
"type": "unstyled",
},
Object {
"data": Object {},
"depth": 0,
"entityRanges": Array [
Object {
"key": -Infinity,
"length": 1,
"offset": 0,
},
],
"inlineStyleRanges": Array [],
"key": "0",
"text": " ",
"type": "atomic",
},
],
"entityMap": Object {
"-Infinity": Object {
"data": Object {
"height": 200,
"src": "https://embed.simplecast.com/a1f11d11",
"url": "https://embed.simplecast.com/a1f11d11",
},
"mutability": "MUTABLE",
"type": "embed",
},
},
}
`;
46 changes: 45 additions & 1 deletion shared/draft-utils/test/add-embeds-to-draft-js.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// @flow
import { getEmbedsFromText } from '../add-embeds-to-draft-js';
jest.mock('draft-js/lib/generateRandomKey', () => {
let last = 0;
const multiplier = Math.exp(24);
return () => Math.floor(last++ * multiplier).toString(32);
});
import {
getEmbedsFromText,
addEmbedsToEditorState,
} from '../add-embeds-to-draft-js';

describe('sites', () => {
describe('<iframe>', () => {
Expand Down Expand Up @@ -357,3 +365,39 @@ describe('complex text', () => {
]);
});
});

it('should not change anything if there are not embeds to add', () => {
const input = {
blocks: [
{
type: 'unstyled',
key: 'g0000',
data: {},
depth: 0,
inlineStyleRanges: [],
entityRanges: [],
text: 'Hello world!',
},
],
entityMap: {},
};
expect(addEmbedsToEditorState(input)).toEqual(input);
});

it('should add embeds', () => {
const input = {
blocks: [
{
type: 'unstyled',
key: 'g0000',
data: {},
depth: 0,
inlineStyleRanges: [],
entityRanges: [],
text: 'https://simplecast.com/s/a1f11d11',
},
],
entityMap: {},
};
expect(addEmbedsToEditorState(input)).toMatchSnapshot();
});
4 changes: 2 additions & 2 deletions shared/generate-meta-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* so it chokes on the Flow syntax.
* More info: https://flow.org/en/docs/types/comments/
*/
var draft = require('./draft-utils');
var truncate = require('./truncate');
var striptags = require('striptags');
var toPlainText = require('./clients/draft-js/utils/plaintext').toPlainText;

var DEFAULT_META = {
title: 'Spectrum',
Expand Down Expand Up @@ -104,7 +104,7 @@ function generateMetaInfo(input /*: Input */) /*: Meta */ {
data &&
data.body &&
(data.type === 'DRAFTJS'
? draft.toPlainText(draft.toState(JSON.parse(data.body)))
? toPlainText(JSON.parse(data.body))
: data.body);
return setDefault({
title: data && data.title + ' · ' + data.communityName,
Expand Down
Loading

0 comments on commit 73303e8

Please sign in to comment.