Merge pull request #2294 from ecency/nt/table-inline-header

Nt/table inline header
This commit is contained in:
Feruz M 2022-05-11 13:00:57 +03:00 committed by GitHub
commit c43ee6603f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 9 deletions

View File

@ -9,6 +9,7 @@ import WebView from 'react-native-webview';
import { VideoPlayer } from '..'; import { VideoPlayer } from '..';
import { useHtmlTableProps } from '@native-html/table-plugin'; import { useHtmlTableProps } from '@native-html/table-plugin';
import { ScrollView } from 'react-native-gesture-handler'; import { ScrollView } from 'react-native-gesture-handler';
import { prependChild, removeElement } from 'htmlparser2/node_modules/domutils';
interface PostHtmlRendererProps { interface PostHtmlRendererProps {
contentWidth: number; contentWidth: number;
@ -142,12 +143,49 @@ export const PostHtmlRenderer = memo(
}; };
//Does some needed dom modifications for proper rendering
const _onElement = (element: Element) => { const _onElement = (element: Element) => {
if (element.tagName === 'img' && element.attribs.src) { if (element.tagName === 'img' && element.attribs.src) {
const imgUrl = element.attribs.src; const imgUrl = element.attribs.src;
console.log('img element detected', imgUrl); console.log('img element detected', imgUrl);
onElementIsImage(imgUrl); onElementIsImage(imgUrl);
} }
//this avoids invalid rendering of first element of table pushing rest of columsn to extreme right.
if(element.tagName === 'table'){
console.log('table detected')
element.children.forEach((child)=>{
if(child.name === 'tr'){
let headerIndex = -1;
let colIndex = -1;
child.children.forEach((gChild, index) => {
//check if element of row in table is not a column while it's other siblings are columns
if(gChild.type === 'tag'){
if(gChild.name !== 'td' && headerIndex === -1){
headerIndex = index;
}else if(colIndex === -1){
colIndex = index
}
}
})
//if row contans a header with column siblings
//remove first child and place it as first separate row in table
if(headerIndex !== -1 && colIndex !== -1 && headerIndex < colIndex){
console.log("time to do some switching", headerIndex, colIndex);
const header = child.children[headerIndex];
const headerRow = new Element('tr', {}, [header]);
removeElement(header);
prependChild(element, headerRow);
}
}
})
}
}; };
@ -244,16 +282,21 @@ export const PostHtmlRenderer = memo(
//based on number of columns a table have, sets scroll enabled or disable, also adjust table full width //based on number of columns a table have, sets scroll enabled or disable, also adjust table full width
const _tableRenderer = ({ TDefaultRenderer, ...props }: CustomRendererProps<TNode>) => { const _tableRenderer = ({ InternalRenderer, ...props }: CustomRendererProps<TNode>) => {
const tableProps = useHtmlTableProps(props); // const tableProps = useHtmlTableProps(props);
const isScrollable = tableProps.numOfColumns > 3; let maxColumns = 0;
const _tableWidth = isScrollable ? tableProps.numOfColumns * _minTableColWidth : contentWidth; props.tnode.children.forEach((child)=>
maxColumns = child.children.length > maxColumns ? child.children.length : maxColumns
)
const isScrollable = maxColumns > 3;
const _tableWidth = isScrollable ? maxColumns * _minTableColWidth : contentWidth;
props.style = { width: _tableWidth }; props.style = { width: _tableWidth };
return ( return (
<ScrollView horizontal={true} scrollEnabled={isScrollable}> <ScrollView horizontal={true} scrollEnabled={isScrollable}>
<TDefaultRenderer {...props} /> <InternalRenderer {...props} />
</ScrollView> </ScrollView>
) )
} }
@ -308,6 +351,7 @@ export const PostHtmlRenderer = memo(
code: styles.code, code: styles.code,
li: styles.li, li: styles.li,
p: styles.p, p: styles.p,
h6: styles.h6
}} }}
domVisitors={{ domVisitors={{
onElement: _onElement, onElement: _onElement,

View File

@ -22,6 +22,9 @@ export default EStyleSheet.create({
alignItems:'center', alignItems:'center',
flexWrap:'wrap' flexWrap:'wrap'
} as TextStyle, } as TextStyle,
h6:{
fontSize:14,
} as TextStyle,
pLi:{ pLi:{
marginTop:0, marginTop:0,
marginBottom:0 marginBottom:0
@ -33,7 +36,8 @@ export default EStyleSheet.create({
width: '100%', width: '100%',
alignSelf:'center', alignSelf:'center',
marginTop:4, marginTop:4,
marginBottom:4 marginBottom:4,
backgroundColor:'red'
} as ImageStyle, } as ImageStyle,
th:{ th:{
flex: 1, flex: 1,
@ -55,6 +59,7 @@ export default EStyleSheet.create({
borderColor: '$tableBorderColor', borderColor: '$tableBorderColor',
backgroundColor: '$tableTrColor', backgroundColor: '$tableTrColor',
alignItems:'center', alignItems:'center',
justifyContent:'center',
} as ViewStyle, } as ViewStyle,
table:{ table:{
width: '100%', width: '100%',
@ -79,9 +84,7 @@ export default EStyleSheet.create({
justifyContent: 'center', justifyContent: 'center',
} as TextStyle, } as TextStyle,
phishy:{ phishy:{
color:'red', color:'$primaryRed',
flexDirection:'row',
flexWrap:'wrap'
} as TextStyle, } as TextStyle,
textJustify:{ textJustify:{
textAlign: Platform.select({ios:'justify', android:'auto'}), //justify with selectable on android causes ends of text getting clipped, textAlign: Platform.select({ios:'justify', android:'auto'}), //justify with selectable on android causes ends of text getting clipped,