Skip to content

Commit

Permalink
feat: new extendRenderer utility
Browse files Browse the repository at this point in the history
This function allows to extend default renderers' model. This is
especially useful for content one wishes to be rendered inline.
  • Loading branch information
jsamr committed Jun 4, 2021
1 parent 7e67473 commit d7bb1da
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
2 changes: 2 additions & 0 deletions packages/render-html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export type { TNodeChildrenRendererProps } from './TNodeChildrenRenderer';
export { default as TNodeRenderer } from './TNodeRenderer';
export { default as useTRenderEngine } from './hooks/useTRenderEngine';
export { default as useTTree } from './hooks/useTTree';
export { default as defaultRenderers } from './render/defaultRenderers';
export { default as extendRenderer } from './render/extendRenderer';
11 changes: 1 addition & 10 deletions packages/render-html/src/render/RenderRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { TNode } from '@native-html/transient-render-engine';
import { ComponentType } from 'react';
import { RendererProps } from '..';
import lookupRecord from '../helpers/lookupRecord';
import AnchorRenderer from '../renderers/AnchorRenderer';
import ImageRenderer from '../renderers/ImageRenderer';
import LineBreakRenderer from '../renderers/LineBreakRenderer';
import ListRenderer from '../renderers/ListRenderer';
import WordBreakRenderer from '../renderers/WordBreakRenderer';
import defaultRenderers from './defaultRenderers';
import {
InternalTextContentRenderer,
LiteRendererDeclaration,
Expand All @@ -18,13 +16,6 @@ const internalTextRenderers: Record<string, InternalTextContentRenderer> = {
wbr: WordBreakRenderer
};

const defaultRenderers: RendererRecord = {
img: ImageRenderer,
ul: ListRenderer,
ol: ListRenderer,
a: AnchorRenderer
};

export default class RenderRegistry {
private readonly renderers: RendererRecord = defaultRenderers;

Expand Down
13 changes: 13 additions & 0 deletions packages/render-html/src/render/defaultRenderers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import AnchorRenderer from '../renderers/AnchorRenderer';
import ImageRenderer from '../renderers/ImageRenderer';
import ListRenderer from '../renderers/ListRenderer';
import { RendererRecord } from './render-types';

const defaultRenderers: RendererRecord = {
img: ImageRenderer,
ul: ListRenderer,
ol: ListRenderer,
a: AnchorRenderer
};

export default defaultRenderers;
33 changes: 33 additions & 0 deletions packages/render-html/src/render/extendRenderer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { HTMLContentModel } from '@native-html/transient-render-engine';
import {
LiteRendererDeclaration,
RendererDeclaration,
RendererFromContentModel,
RendererSpecs
} from './render-types';

/**
* Extend a default renderer to specialize its element model.
*
* @param Renderer - The component to extend.
* @param model - The new element model.
*/
export default function extendRenderer<N extends HTMLContentModel>(
Renderer: RendererDeclaration<any>,
model: RendererSpecs<N>['model']
) {
if (typeof Renderer === 'function') {
const localRenderer = Renderer;
const newRenderer: RendererFromContentModel<N> = function () {
return React.createElement(localRenderer as any, arguments[0]);
} as any;
newRenderer.displayName = Renderer.displayName;
newRenderer.model = model as any;
return newRenderer;
}
return {
...Renderer,
model
} as LiteRendererDeclaration<N>;
}

0 comments on commit d7bb1da

Please sign in to comment.