-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
249 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { DocBuilder, PropDocPropsUnknown, TDocContext, TPreviewPropsItem } from '@epam/uui-docs'; | ||
import { TPropInputDataAll } from './propDocUtils'; | ||
import { TTheme } from '../docsConstants'; | ||
|
||
export type TPreviewRef = { link: string, error: string | undefined }; | ||
type TBuildPreviewLinkParams = { | ||
context: TDocContext, | ||
inputData: TPropInputDataAll, | ||
theme: TTheme, | ||
isSkin: boolean, | ||
componentId: string, | ||
docs: DocBuilder<PropDocPropsUnknown> | ||
}; | ||
|
||
const INLINE_PP_PREFIX = 'inline:'; | ||
|
||
export function buildPreviewRef(params: TBuildPreviewLinkParams): TPreviewRef { | ||
const { context, inputData, theme, isSkin, componentId, docs } = params; | ||
const unableToSerialize: string[] = []; | ||
const initialValue = { | ||
id: '', | ||
context, | ||
matrix: {}, | ||
}; | ||
|
||
const previewProps = Object.keys(inputData).reduce<TPreviewPropsItem<any>>((acc, name) => { | ||
const { value, exampleId } = inputData[name]; | ||
if (exampleId !== undefined) { | ||
const exObject = Object.values(docs.getPropExamplesMap(name)).find(({ id }) => exampleId === id); | ||
if (exObject) { | ||
if (!exObject?.isDefault) { | ||
Object.assign(acc.matrix, { | ||
[name]: { | ||
examples: [exObject.name], | ||
}, | ||
}); | ||
} | ||
} else { | ||
console.error(`Unable to find example of property=${name} by exampleId=${exampleId}. The property will be ignored.`); | ||
} | ||
} else if (value !== undefined) { | ||
if (['string', 'boolean', 'number'].indexOf(typeof value) !== -1) { | ||
Object.assign(acc.matrix, { | ||
[name]: { | ||
values: [value], | ||
}, | ||
}); | ||
} else { | ||
unableToSerialize.push(name); | ||
} | ||
} | ||
return acc; | ||
}, initialValue); | ||
|
||
const link = `/preview?theme=${theme}&isSkin=${isSkin}&componentId=${componentId}&previewId=${encodeInlinePreviewPropsForUrl(previewProps)}`; | ||
let error; | ||
if (unableToSerialize.length) { | ||
error = `Next props cannot be serialized for URL and will be excluded: ${unableToSerialize.join(', ')}.`; | ||
} | ||
return { link, error }; | ||
} | ||
|
||
function encodeInlinePreviewPropsForUrl(pp: object) { | ||
const str = JSON.stringify(pp); | ||
return encodeURIComponent(`${INLINE_PP_PREFIX}${str}`); | ||
} | ||
|
||
export function isPreviewIdInline(previewId: string) { | ||
return previewId.indexOf('inline:') === 0; | ||
} | ||
|
||
export function decodeInlinePreviewIdFromUrl(previewId: string): TPreviewPropsItem<unknown> { | ||
if (isPreviewIdInline(previewId)) { | ||
const json = previewId.substring(INLINE_PP_PREFIX.length); | ||
try { | ||
return JSON.parse(json) as TPreviewPropsItem<unknown>; | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
throw new Error(`Unsupported format of inline preview props: ${previewId}`); | ||
} | ||
|
||
export function formatPreviewId(previewId: string): string { | ||
if (isPreviewIdInline(previewId)) { | ||
const decoded = decodeInlinePreviewIdFromUrl(previewId); | ||
return JSON.stringify(decoded, undefined, 1); | ||
} | ||
return previewId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.