-
-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export buildTREFromConfig for testing
- Loading branch information
Showing
3 changed files
with
83 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { TransientRenderEngineConfig } from '../shared-types'; | ||
import TRenderEngine, { | ||
HTMLModelRecord, | ||
TagName | ||
} from '@native-html/transient-render-engine'; | ||
|
||
export default function buildTREFromConfig(props: TransientRenderEngineConfig) { | ||
const { | ||
allowedStyles, | ||
ignoredStyles, | ||
ignoredDomTags, | ||
ignoreDomNode, | ||
domVisitors, | ||
htmlParserOptions, | ||
baseStyle, | ||
classesStyles, | ||
tagsStyles, | ||
idsStyles, | ||
enableCSSInlineProcessing, | ||
enableUserAgentStyles, | ||
fallbackFonts, | ||
systemFonts, | ||
customHTMLElementModels = {}, | ||
emSize, | ||
setMarkersForTNode, | ||
selectDomRoot, | ||
dangerouslyDisableHoisting, | ||
dangerouslyDisableWhitespaceCollapsing | ||
} = props; | ||
const customizeHTMLModels = Object.keys(customHTMLElementModels).length | ||
? (defaultModels: HTMLModelRecord<TagName>): HTMLModelRecord<TagName> => { | ||
return { ...defaultModels, ...customHTMLElementModels }; | ||
} | ||
: undefined; | ||
const fontMap = {} as Record<string, true>; | ||
systemFonts!.forEach((font) => { | ||
fontMap[font] = true; | ||
}); | ||
const isFontSupported = (fontFamily: string) => { | ||
if (fallbackFonts![fontFamily as keyof typeof fallbackFonts]) { | ||
return fallbackFonts![fontFamily as keyof typeof fallbackFonts]; | ||
} | ||
return fontMap[fontFamily] || false; | ||
}; | ||
return new TRenderEngine({ | ||
customizeHTMLModels, | ||
cssProcessorConfig: { | ||
isFontSupported, | ||
inlinePropertiesBlacklist: ignoredStyles, | ||
inlinePropertiesWhitelist: allowedStyles, | ||
rootFontSize: emSize | ||
}, | ||
htmlParserOptions: { | ||
decodeEntities: true, | ||
...htmlParserOptions | ||
}, | ||
stylesConfig: { | ||
baseStyle, | ||
enableCSSInlineProcessing, | ||
enableUserAgentStyles, | ||
classesStyles, | ||
idsStyles, | ||
tagsStyles | ||
}, | ||
ignoredDomTags, | ||
ignoreDomNode, | ||
domVisitors, | ||
setMarkersForTNode, | ||
selectDomRoot, | ||
dangerouslyDisableHoisting, | ||
dangerouslyDisableWhitespaceCollapsing | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,88 +1,20 @@ | ||
import { useMemo } from 'react'; | ||
import TRenderEngine, { | ||
HTMLModelRecord, | ||
TagName | ||
} from '@native-html/transient-render-engine'; | ||
import { TransientRenderEngineConfig } from '../shared-types'; | ||
import buildTREFromConfig from '../helpers/buildTREFromConfig'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export default function useTRenderEngine(props: TransientRenderEngineConfig) { | ||
const { | ||
allowedStyles, | ||
ignoredStyles, | ||
ignoredDomTags, | ||
ignoreDomNode, | ||
domVisitors, | ||
htmlParserOptions, | ||
baseStyle, | ||
classesStyles, | ||
tagsStyles, | ||
idsStyles, | ||
enableCSSInlineProcessing, | ||
enableUserAgentStyles, | ||
fallbackFonts, | ||
systemFonts, | ||
customHTMLElementModels = {}, | ||
emSize, | ||
setMarkersForTNode, | ||
selectDomRoot, | ||
dangerouslyDisableHoisting, | ||
dangerouslyDisableWhitespaceCollapsing, | ||
triggerTREInvalidationPropNames | ||
} = props; | ||
const isFontSupported = useMemo(() => { | ||
const fontMap = {} as Record<string, true>; | ||
systemFonts!.forEach((font) => { | ||
fontMap[font] = true; | ||
}); | ||
return (fontFamily: string) => { | ||
if (fallbackFonts![fontFamily as keyof typeof fallbackFonts]) { | ||
return fallbackFonts![fontFamily as keyof typeof fallbackFonts]; | ||
} | ||
return fontMap[fontFamily] || false; | ||
}; | ||
}, [systemFonts, fallbackFonts]); | ||
export default function useTRenderEngine({ | ||
triggerTREInvalidationPropNames, | ||
...props | ||
}: TransientRenderEngineConfig) { | ||
const tbuilderDeps = (triggerTREInvalidationPropNames || []).map( | ||
(key) => props[key] | ||
); | ||
const customizeHTMLModels = Object.keys(customHTMLElementModels).length | ||
? (defaultModels: HTMLModelRecord<TagName>): HTMLModelRecord<TagName> => { | ||
return { ...defaultModels, ...customHTMLElementModels }; | ||
} | ||
: undefined; | ||
return useMemo( | ||
() => | ||
new TRenderEngine({ | ||
customizeHTMLModels, | ||
cssProcessorConfig: { | ||
isFontSupported, | ||
inlinePropertiesBlacklist: ignoredStyles, | ||
inlinePropertiesWhitelist: allowedStyles, | ||
rootFontSize: emSize | ||
}, | ||
htmlParserOptions: { | ||
decodeEntities: true, | ||
...htmlParserOptions | ||
}, | ||
stylesConfig: { | ||
baseStyle, | ||
enableCSSInlineProcessing, | ||
enableUserAgentStyles, | ||
classesStyles, | ||
idsStyles, | ||
tagsStyles | ||
}, | ||
ignoredDomTags, | ||
ignoreDomNode, | ||
domVisitors, | ||
setMarkersForTNode, | ||
selectDomRoot, | ||
dangerouslyDisableHoisting, | ||
dangerouslyDisableWhitespaceCollapsing | ||
}), | ||
() => buildTREFromConfig(props), | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[...tbuilderDeps, isFontSupported] | ||
tbuilderDeps | ||
); | ||
} |
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