From 9f8a5a988fbd267c0e7763978e4bf17364043747 Mon Sep 17 00:00:00 2001 From: atanasster Date: Sun, 23 Feb 2020 17:50:56 -0500 Subject: [PATCH] feat: viewMode for Source --- blocks/core/src/Source/Source.tsx | 99 +++++++++++++++++-------------- 1 file changed, 55 insertions(+), 44 deletions(-) diff --git a/blocks/core/src/Source/Source.tsx b/blocks/core/src/Source/Source.tsx index f9cff7a5e..071ea8f87 100644 --- a/blocks/core/src/Source/Source.tsx +++ b/blocks/core/src/Source/Source.tsx @@ -97,6 +97,8 @@ export interface SourceProps { fileSource?: string; } +type ViewStyle = 'plain' | 'tags' | 'values'; + export const Source: FC = ({ children = '', language = 'jsx', @@ -110,8 +112,8 @@ export const Source: FC = ({ parentTheme || 'nightowl-light', ); - const [showMerged, setShowMerged] = React.useState( - !!controls && !!args, + const [viewStyle, setViewStyle] = React.useState( + !!controls && !!args ? 'values' : 'tags', ); const [showFileSource, setShowFileSource] = React.useState(false); @@ -128,7 +130,14 @@ export const Source: FC = ({ setThemeName(themeKeys[newIdx] as ThemeType); }; - const onMergeValues = () => setShowMerged(!showMerged); + const onMergeValues = () => + setViewStyle( + viewStyle === 'plain' + ? 'tags' + : viewStyle === 'tags' + ? 'values' + : 'plain', + ); const onShowFileSource = () => setShowFileSource(!showFileSource); const onCopy = (e: MouseEvent) => { @@ -149,7 +158,7 @@ export const Source: FC = ({ actions.push({ title: themeName, onClick: onRotateTheme }); } if (controls && args) { - actions.push({ title: 'values', onClick: onMergeValues }); + actions.push({ title: viewStyle, onClick: onMergeValues }); } actions.push({ title: copied ? 'copied' : 'copy', onClick: onCopy }); @@ -167,7 +176,7 @@ export const Source: FC = ({ let source: string; if (!showFileSource) { let code = typeof children === 'string' ? children : ''; - if (showMerged && args && controls) { + if (viewStyle === 'values' && args && controls) { code = mergeControlValues(code, args, controls); } source = @@ -197,45 +206,47 @@ export const Source: FC = ({ {tokens.map((line, i) => (
{line.map((token, key) => { - const paramIdx = parameters - ? parameters.indexOf(token.content.trim()) - : -1; - const isParameterDefiinition = - paramIdx > -1 && token.types.indexOf('parameter') > -1; - const isParameterUsage = - paramIdx > -1 && - ((token.types.indexOf('attr-value') > -1 && - token.types.indexOf('spread') > -1) || - (token.types.indexOf('tag') > -1 && - token.types.indexOf('script') > -1)); - const isParam = isParameterDefiinition || isParameterUsage; - if (isParam) { - const splitToken = getTokenProps({ - token, - key, - }).children.split(/(\s+)/); - console.log(splitToken); - return splitToken.map(s => - s.trim().length ? ( - - {s} - - ) : ( - s - ), - ); + if (viewStyle === 'tags') { + const paramIdx = parameters + ? parameters.indexOf(token.content.trim()) + : -1; + const isParameterDefiinition = + paramIdx > -1 && token.types.indexOf('parameter') > -1; + const isParameterUsage = + paramIdx > -1 && + ((token.types.indexOf('attr-value') > -1 && + token.types.indexOf('spread') > -1) || + (token.types.indexOf('tag') > -1 && + token.types.indexOf('script') > -1)); + const isParam = isParameterDefiinition || isParameterUsage; + if (isParam) { + const splitToken = getTokenProps({ + token, + key, + }).children.split(/(\s+)/); + console.log(splitToken); + return splitToken.map(s => + s.trim().length ? ( + + {s} + + ) : ( + s + ), + ); + } } return (