Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created September 17, 2020 22:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sibelius/fa3d5210e1138391828a5aa121d7c745 to your computer and use it in GitHub Desktop.
Save sibelius/fa3d5210e1138391828a5aa121d7c745 to your computer and use it in GitHub Desktop.
Transform an object to a Relay Record Proxy
export const richTextToRelayRecordProxy = (bodyRichText, store) => {
const bodyRichTextId = 'client:RichText:' + tempID++;
const bodyRichTextRecord = store.create(bodyRichTextId, 'RichText');
bodyRichTextRecord.setValue(bodyRichText.text, 'text');
bodyRichTextRecord.setValue(bodyRichText.html, 'html');
const { contentState } = bodyRichText;
if (contentState) {
const contentStateId = 'client:DraftContentState:' + tempID++;
const contentStateRecord = store.create(
contentStateId,
'DraftContentState',
);
const blocks = contentState.blocks.map((block) => {
const blockId = 'client:DraftBlock:' + tempID++;
const blockRecord = store.create(blockId, 'DraftBlock');
blockRecord.setValue(block.key, 'key');
blockRecord.setValue(block.text, 'text');
blockRecord.setValue(block.type, 'type');
blockRecord.setValue(block.depth, 'depth');
const entityRanges = block.entityRanges.map((entityRange) => {
const entityRangeId = 'client:DraftEntityRange:' + tempID++;
const entityRangeRecord = store.create(
entityRangeId,
'DraftEntityRange',
);
entityRangeRecord.setValue(entityRange.offset, 'offset');
entityRangeRecord.setValue(entityRange.length, 'length');
entityRangeRecord.setValue(entityRange.key, 'key');
return entityRangeRecord;
});
const inlineStyleRanges = block.inlineStyleRanges.map(
(inlineStyleRange) => {
const inlineStyleRangesId =
'client:DraftInlineStyleRange:' + tempID++;
const inlineStyleRangesRecord = store.create(
inlineStyleRangesId,
'DraftInlineStyleRange',
);
inlineStyleRangesRecord.setValue(inlineStyleRange.offset, 'offset');
inlineStyleRangesRecord.setValue(inlineStyleRange.length, 'length');
inlineStyleRangesRecord.setValue(inlineStyleRange.style, 'style');
return inlineStyleRangesRecord;
},
);
blockRecord.setLinkedRecords(entityRanges, 'entityRanges');
blockRecord.setLinkedRecords(inlineStyleRanges, 'inlineStyleRanges');
return blockRecord;
});
contentStateRecord.setLinkedRecords(blocks, 'blocks');
const entityMap = contentState.entityMap.map((entity) => {
const entityId = 'client:DraftEntity:' + tempID++;
const entityRecord = store.create(entityId, 'DraftEntity');
entityRecord.setValue(entity.type, 'type');
entityRecord.setValue(entity.mutability, 'mutability');
if (entity.data) {
const dataId = 'client:DraftData:' + tempID++;
const dataRecord = store.create(dataId, 'DraftData');
dataRecord.setValue(entity.data.href, 'href');
dataRecord.setValue(entity.data.url, 'url');
entityRecord.setLinkedRecord(dataRecord, 'data');
}
return entityRecord;
});
contentStateRecord.setLinkedRecords(entityMap, 'entityMap');
bodyRichTextRecord.setLinkedRecord(contentStateRecord, 'contentState');
}
return bodyRichTextRecord;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment