Skip to content

Commit

Permalink
port over RenderExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
jschuler committed Jun 24, 2021
1 parent 62c5502 commit 47bc82b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"serve": "serve public"
},
"dependencies": {
"@patternfly/quickstarts": "1.0.0-rc.20",
"@patternfly/quickstarts": "1.0.0-rc.21",
"@patternfly/react-core": "^4.101.3",
"asciidoctor": "^2.2.1",
"react": "^16.14.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/module/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@patternfly/quickstarts",
"version": "1.0.0-rc.20",
"version": "1.0.0-rc.21",
"description": "PatternFly quick starts",
"files": [
"dist"
Expand Down
62 changes: 49 additions & 13 deletions packages/module/src/ConsoleInternal/components/markdown-view.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as React from 'react';
import { Converter } from 'showdown';
import { QuickStartContext, QuickStartContextValues } from '../../utils/quick-start-context';
// import _truncate from 'lodash-es/truncate.js';
// import _uniqueId from 'lodash-es/uniqueId.js';
import cx from 'classnames';
import { useForceRender } from '@console/shared';

import './_markdown-view.scss';

Expand Down Expand Up @@ -97,18 +96,11 @@ export const SyncMarkdownView: React.FC<SyncMarkdownProps> = ({
}) => {
const { getResource } = React.useContext<QuickStartContextValues>(QuickStartContext);
const markup = React.useMemo(() => {
const truncatedContent = /*truncateContent
? _truncate(content, {
length: 256,
separator: ' ',
omission: '\u2026',
})
: */ content;
return markdownConvert(
truncatedContent || emptyMsg || getResource('Not available'),
content || emptyMsg || getResource('Not available'),
extensions,
);
}, [content, emptyMsg, extensions, getResource /*, truncateContent*/]);
}, [content, emptyMsg, extensions, getResource]);
const innerProps: InnerSyncMarkdownProps = {
renderExtension: extensions?.length > 0 ? renderExtension : undefined,
exactHeight,
Expand All @@ -128,6 +120,43 @@ const uniqueId = (function () {
};
})();

type RenderExtensionProps = {
renderExtension: (contentDocument: HTMLDocument, rootSelector: string) => React.ReactNode;
selector: string;
markup: string;
docContext?: HTMLDocument;
};

const RenderExtension: React.FC<RenderExtensionProps> = ({
renderExtension,
selector,
markup,
docContext,
}) => {
const forceRender = useForceRender();
const markupRef = React.useRef<string>(null);
const shouldRenderExtension = React.useCallback(() => {
if (markupRef.current === markup) {
return true;
}
markupRef.current = markup;
return false;
}, [markup]);
/**
* During a render cycle in which markup changes, renderExtension receives an old copy of document
* because react is still updating the dom using `dangerouslySetInnerHTML` with latest markdown markup
* which causes the component rendered by renderExtension to receive old copy of document
* use forceRender to delay the rendering of extension by one render cycle
*/
React.useEffect(() => {
renderExtension && forceRender();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [markup]);
return (
<>{shouldRenderExtension() ? renderExtension?.(docContext ?? document, selector) : null}</>
);
};

const InlineMarkdownView: React.FC<InnerSyncMarkdownProps> = ({
markup,
isEmpty,
Expand All @@ -138,7 +167,7 @@ const InlineMarkdownView: React.FC<InnerSyncMarkdownProps> = ({
return (
<div className={cx('co-markdown-view', { ['is-empty']: isEmpty }, className)} id={id}>
<div dangerouslySetInnerHTML={{ __html: markup }} />
{renderExtension && renderExtension(document, `#${id}`)}
{renderExtension && <RenderExtension renderExtension={renderExtension} selector={`#${id}`} markup={markup} />}
</div>
);
};
Expand Down Expand Up @@ -232,7 +261,14 @@ const IFrameMarkdownView: React.FC<InnerSyncMarkdownProps> = ({
onLoad={() => onLoad()}
className={className}
/>
{loaded && frame && renderExtension && renderExtension(frame.contentDocument, '')}
{loaded && frame && renderExtension && (
<RenderExtension
markup={markup}
selector={''}
renderExtension={renderExtension}
docContext={frame.contentDocument}
/>
)}
</>
);
};
1 change: 1 addition & 0 deletions packages/module/src/ConsoleShared/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './scroll';
export * from './useResizeObserver';
export * from './useScrollShadows';
export * from './useBoundingClientRect';
export * from './useForceRender';
6 changes: 6 additions & 0 deletions packages/module/src/ConsoleShared/src/hooks/useForceRender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as React from 'react';

/**
* React hook that forces component render.
*/
export const useForceRender = () => React.useReducer((s: boolean) => !s, false)[1] as VoidFunction;

0 comments on commit 47bc82b

Please sign in to comment.