File size: 5,485 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
// @flow
import genKey from 'draft-js/lib/generateRandomKey';
import type { RawDraftContentState } from 'draft-js/lib/RawDraftContentState.js';
const FIGMA_URLS = /\b((?:https?:\/\/)?(?:www\.)?figma.com\/(file|proto)\/([0-9a-zA-Z]{22,128})(?:\/.*)?)/gi;
const YOUTUBE_URLS = /\b(?:https?:\/\/)?(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?[\w\?=]*)?/gi;
const VIMEO_URLS = /\b(?:https?:\/\/)?(?:www\.)?vimeo.com\/(?:channels\/[0-9a-z-_]+\/)?([0-9a-z\-_]+)/gi;
const IFRAME_TAG = /<iframe.+?src=['"](.+?)['"]/gi;
const FRAMER_URLS = /\b(?:https?:\/\/)?(?:www\.)?(?:framer\.cloud|share\.framerjs\.com)\/([A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?/gi;
const CODEPEN_URLS = /\b(?:https?:\/\/)?(?:www\.)?codepen\.io(\/[A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?/gi;
const CODESANDBOX_URLS = /\b(?:https?:\/\/)?(?:www\.)?codesandbox\.io(\/[A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?/gi;
const SIMPLECAST_URLS = /\b(?:https?:\/\/)?(?:www\.)?simplecast\.com(\/[A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?/gi;
const THREAD_URLS = /(?:(?:https?:\/\/)?|\B)(?:spectrum\.chat|localhost:3000)\/.*?(?:~|(?:\?|&)t=|(?:\?|&)thread=|thread\/)([^&\s]*)/gi;
const REGEXPS = {
figma: FIGMA_URLS,
youtube: YOUTUBE_URLS,
vimeo: VIMEO_URLS,
iframe: IFRAME_TAG,
framer: FRAMER_URLS,
codepen: CODEPEN_URLS,
codesandbox: CODESANDBOX_URLS,
simplecast: SIMPLECAST_URLS,
internal: THREAD_URLS,
};
export type InternalEmbedData = {
type: 'internal',
entity: 'thread',
id: string,
};
export type ExternalEmbedData = {
url: string,
type:
| 'figma'
| 'youtube'
| 'vimeo'
| 'iframe'
| 'framer'
| 'codepen'
| 'codesandbox'
| 'simplecast',
aspectRatio?: string,
width?: number,
height?: number,
};
export type EmbedData = InternalEmbedData | ExternalEmbedData;
export const addEmbedsToEditorState = (
input: RawDraftContentState
): RawDraftContentState => {
let lastEntityKey = Math.max(...Object.keys(input.entityMap));
if (lastEntityKey === -Infinity || lastEntityKey === Infinity)
lastEntityKey = -1;
let newEntityMap = input.entityMap || {};
let newBlocks = [];
// Detect the embeds and add an atomic block and an entity to the
// raw content state for each one
input.blocks.forEach((block, blockIndex) => {
newBlocks.push(block);
if (block.type !== 'unstyled') return;
const embeds = getEmbedsFromText(block.text);
if (embeds.length === 0) return;
embeds.forEach(embed => {
lastEntityKey++;
const entityKey = lastEntityKey;
newEntityMap[entityKey] = {
data: {
...embed,
...(embed.url ? { src: embed.url } : {}),
},
mutability: 'MUTABLE',
type: 'embed',
};
const regexp = new RegExp(REGEXPS[embed.type], 'ig');
const text = block.text;
var match;
while ((match = regexp.exec(text)) !== null) {
const offset = match.index;
const length = match[0].length;
newBlocks[blockIndex].entityRanges = newBlocks[
blockIndex
].entityRanges.filter(
entity => entity.offset !== offset || entity.length !== length
);
newBlocks[blockIndex].entityRanges.push({
offset,
length,
key: entityKey,
});
}
});
});
return {
...input,
entityMap: newEntityMap,
blocks: newBlocks,
};
};
// Utility function to return the first capturing group of each unique match
// of a regex in a string
const match = (regex: RegExp, text: string) => {
const matches = text.match(regex);
if (!matches) return [];
return [...new Set(matches)].map(match => {
return regex.exec(match)[1];
});
};
export const getEmbedsFromText = (text: string): Array<EmbedData> => {
let embeds = [];
match(IFRAME_TAG, text).forEach(url => {
embeds.push({ type: 'iframe', url });
});
match(FIGMA_URLS, text).forEach(url => {
embeds.push({
type: 'figma',
url: `https://www.figma.com/embed?embed_host=spectrum&url=${url}`,
aspectRatio: '56.25%', // 16:9 aspect ratio
});
});
match(YOUTUBE_URLS, text).forEach(id => {
embeds.push({
type: 'youtube',
url: `https://www.youtube.com/embed/${id}`,
aspectRatio: '56.25%', // 16:9 aspect ratio
});
});
match(VIMEO_URLS, text).forEach(id => {
embeds.push({
type: 'vimeo',
url: `https://player.vimeo.com/video/${id}`,
aspectRatio: '56.25%', // 16:9 aspect ratio
});
});
match(FRAMER_URLS, text).forEach(id => {
embeds.push({
type: 'framer',
url: `https://share.framerjs.com/${id}`,
width: 600,
height: 800,
});
});
match(CODEPEN_URLS, text).forEach(path => {
embeds.push({
type: 'codepen',
url: `https://codepen.io${path.replace(/(pen|full|details)/, 'embed')}`,
height: 300,
});
});
match(CODESANDBOX_URLS, text).forEach(path => {
embeds.push({
type: 'codesandbox',
url: `https://codesandbox.io${path.replace('/s/', '/embed/')}`,
height: 500,
});
});
match(SIMPLECAST_URLS, text).forEach(path => {
embeds.push({
type: 'simplecast',
url: `https://embed.simplecast.com/${path
.replace('/s/', '')
.replace('/', '')}`,
height: 200,
});
});
match(THREAD_URLS, text).forEach(id => {
embeds.push({
type: 'internal',
id,
entity: 'thread',
});
});
return embeds;
};
|