expose widgetValueSerializer registry

This commit is contained in:
Shawn Erquhart 2017-06-22 17:35:47 -04:00
parent 1c0bb6a877
commit ffbd8d22cc
4 changed files with 12 additions and 11 deletions

View File

@ -6,7 +6,7 @@ import { currentBackend } from '../backends/backend';
import { getIntegrationProvider } from '../integrations';
import { getAsset, selectIntegration } from '../reducers';
import { createEntry } from '../valueObjects/Entry';
import { controlValueSerializers } from '../components/Widgets/serializers';
import registry from '../lib/registry';
const { notifSend } = notifActions;
@ -223,7 +223,7 @@ export function loadEntry(collection, slug) {
return fields.reduce((acc, field) => {
const fieldName = field.get('name');
const value = values[fieldName];
const serializer = controlValueSerializers[field.get('widget')];
const serializer = registry.getWidgetValueSerializer(field.get('widget'));
if (isArray(value) && !isEmpty(value)) {
acc[fieldName] = value.map(val => deserializeValues(val, field.get('fields')));
} else if (isObject(value) && !isEmpty(value)) {
@ -293,7 +293,7 @@ export function persistEntry(collection) {
return fields.reduce((acc, field) => {
const fieldName = field.get('name');
const value = values.get(fieldName);
const serializer = controlValueSerializers[field.get('widget')];
const serializer = registry.getWidgetValueSerializer(field.get('widget'));
if (List.isList(value)) {
return acc.set(fieldName, value.map(val => serializeValues(val, field.get('fields'))));
} else if (Map.isMap(value)) {

View File

@ -9,7 +9,6 @@ import remarkToMarkdown from 'remark-stringify';
import htmlToRehype from 'rehype-parse';
import rehypeToRemark from 'rehype-remark';
import registry from '../../../../lib/registry';
import { registerControlValueSerializer } from '../../serializers';
import { createAssetProxy } from '../../../../valueObjects/AssetProxy';
import { buildKeymap } from './keymap';
import createMarkdownParser from './parser';
@ -18,7 +17,7 @@ import { Sticky } from '../../../UI/Sticky/Sticky';
import styles from './index.css';
// Register handler to transform html to markdown before persist
registerControlValueSerializer('markdown', {
registry.registerWidgetValueSerializer('markdown', {
serialize: value => unified()
.use(htmlToRehype)
.use(htmlToRehype)

View File

@ -1,5 +0,0 @@
export const controlValueSerializers = {};
export const registerControlValueSerializer = (fieldName, serializer) => {
controlValueSerializers[fieldName] = serializer;
};

View File

@ -6,6 +6,7 @@ const _registry = {
previewStyles: [],
widgets: {},
editorComponents: Map(),
widgetValueSerializers: {},
};
export default {
@ -36,5 +37,11 @@ export default {
},
getEditorComponents() {
return _registry.editorComponents;
}
},
registerWidgetValueSerializer(widgetName, serializer) {
_registry.widgetValueSerializers[widgetName] = serializer;
},
getWidgetValueSerializer(widgetName) {
return _registry.widgetValueSerializers[widgetName];
},
};