-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(form): Add
ExcelPreviewWidget
and ExcelPreview
components.
- Loading branch information
1 parent
5c49c80
commit d80eca1
Showing
7 changed files
with
83 additions
and
4 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
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
43 changes: 43 additions & 0 deletions
43
schedula/utils/form/react/src/core/components/ExcelPreview/index.js
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,43 @@ | ||
import {useEffect, useState} from "react"; | ||
import xlsxPreview from 'xlsx-preview'; | ||
|
||
|
||
function dataURLtoFile(dataurl) { | ||
let arr = dataurl.split(','), | ||
mime = arr[0].match(/:(.*?);/)[1], | ||
filename = decodeURIComponent(arr[0].match(/;?name=(.*?);/)[1]), | ||
bstr = atob(arr[1]), | ||
n = bstr.length, | ||
u8arr = new Uint8Array(n); | ||
|
||
while (n--) { | ||
u8arr[n] = bstr.charCodeAt(n); | ||
} | ||
return new File([u8arr], filename, {type: mime}); | ||
} | ||
|
||
|
||
export default function ExcelPreview({children, render, uri, ...props}) { | ||
const [url, setUrl] = useState(null) | ||
useEffect(() => { | ||
if (uri) | ||
(async () => { | ||
const result = await xlsxPreview.xlsx2Html(dataURLtoFile(uri), { | ||
output: 'arrayBuffer', | ||
minimumRows: 50, | ||
minimumCols: 30, | ||
separateSheets: false | ||
}); | ||
const url = URL.createObjectURL(new Blob([result], { | ||
type: 'text/html' | ||
})); | ||
setUrl(url); | ||
})(); | ||
}, [uri]); | ||
return <>{url ? <object | ||
style={{height: '100%', width: '100%'}} | ||
className="res-obj" | ||
type="text/html" data={url} | ||
/> : null}</> | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
schedula/utils/form/react/src/core/widgets/ExcelPreviewWidget/core.js
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,5 @@ | ||
import ExcelPreview from '../../components/ExcelPreview' | ||
|
||
export default function ExcelPreviewWidget({value, options, ...props}) { | ||
return <ExcelPreview uri={value} {...options}/> | ||
} |
7 changes: 7 additions & 0 deletions
7
schedula/utils/form/react/src/core/widgets/ExcelPreviewWidget/index.js
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,7 @@ | ||
import React, {Suspense} from "react"; | ||
|
||
const Field = React.lazy(() => import('./core')); | ||
|
||
export default function ExcelPreviewWidget(props) { | ||
return <Suspense key={props.key}><Field{...props}/></Suspense> | ||
} |
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