-
Notifications
You must be signed in to change notification settings - Fork 37
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
1 parent
248cbfe
commit 7b54885
Showing
6 changed files
with
171 additions
and
1 deletion.
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
155 changes: 155 additions & 0 deletions
155
packages/sn-editor-react/src/components/controls/html-editor-control.tsx
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,155 @@ | ||
import { | ||
Button, | ||
CircularProgress, | ||
createStyles, | ||
Dialog, | ||
DialogActions, | ||
DialogContent, | ||
DialogTitle, | ||
IconButton, | ||
IconButtonProps, | ||
makeStyles, | ||
Tooltip, | ||
} from '@material-ui/core' | ||
import CodeIcon from '@material-ui/icons/Code' | ||
import { Editor } from '@tiptap/react' | ||
import React, { FC, useRef, useState } from 'react' | ||
import { useLocalization } from '../../hooks' | ||
|
||
const useStyles = makeStyles(() => { | ||
return createStyles({ | ||
image: { | ||
maxWidth: '100%', | ||
height: 'auto', | ||
}, | ||
fileName: { | ||
marginTop: '0.5rem', | ||
textAlign: 'center', | ||
}, | ||
}) | ||
}) | ||
|
||
interface ImageControlProps { | ||
editor: Editor | ||
buttonProps?: Partial<IconButtonProps> | ||
} | ||
|
||
type ImageFile = File & { src?: string } | ||
|
||
export const HtmlEditorControl: FC<ImageControlProps> = ({ buttonProps, editor }) => { | ||
const [open, setOpen] = useState(false) | ||
const [uploadedFile, setUploadedFile] = useState<ImageFile>() | ||
const [isUploadInProgress, setIsUploadInProgress] = useState(false) | ||
const fileInput = useRef<HTMLInputElement>(null) | ||
|
||
const classes = useStyles() | ||
const localization = useLocalization() | ||
|
||
const handleClickOpen = () => { | ||
if (isUploadInProgress) { | ||
return | ||
} | ||
fileInput.current && fileInput.current.click() | ||
} | ||
|
||
const handleClose = () => { | ||
setOpen(false) | ||
} | ||
|
||
const addImage = () => { | ||
if (uploadedFile?.src) { | ||
editor.chain().focus().setImage({ src: uploadedFile.src }).run() | ||
} | ||
} | ||
|
||
const imageToBase64 = (image: File) => { | ||
return new Promise<string>((resolve, reject) => { | ||
const reader = new FileReader() | ||
reader.onload = (e) => { | ||
if (e.target?.result) { | ||
resolve(e.target.result as string) | ||
} else { | ||
reject(e) | ||
} | ||
} | ||
reader.readAsDataURL(image) | ||
}) | ||
} | ||
|
||
const upload = async (fileToUpload: File) => { | ||
if (!fileToUpload) { | ||
return | ||
} | ||
|
||
setIsUploadInProgress(true) | ||
|
||
const imageSrc = await imageToBase64(fileToUpload) | ||
const image: ImageFile = new File([fileToUpload], fileToUpload.name, { type: fileToUpload.type }) | ||
image.src = imageSrc | ||
|
||
setIsUploadInProgress(false) | ||
setUploadedFile(image) | ||
} | ||
|
||
return ( | ||
<> | ||
<Tooltip title={`${localization.menubar.EditHtml}`}> | ||
<IconButton | ||
onClick={() => editor.chain().focus().toggleCode().run()} | ||
color={editor.isActive('code') ? 'primary' : 'default'} | ||
{...buttonProps}> | ||
<CodeIcon style={{ marginTop: '-7px' }} /> | ||
<span style={{ position: 'absolute', fontSize: '12px', top: '13px', fontWeight: 'bold' }}>html</span> | ||
</IconButton> | ||
</Tooltip> | ||
<input | ||
onChange={(ev) => { | ||
setOpen(true) | ||
|
||
ev.target.files && upload(ev.target.files[0]) | ||
}} | ||
style={{ display: 'none' }} | ||
ref={fileInput} | ||
type="file" | ||
accept="image/*" | ||
multiple={false} | ||
/> | ||
<Dialog | ||
open={open} | ||
onClose={handleClose} | ||
aria-labelledby="form-dialog-title" | ||
onExited={() => { | ||
setUploadedFile(undefined) | ||
|
||
if (fileInput.current) { | ||
fileInput.current.value = '' | ||
} | ||
}}> | ||
<DialogTitle id="form-dialog-title">{localization.imageControl.title}</DialogTitle> | ||
<DialogContent> | ||
{uploadedFile ? ( | ||
<> | ||
<img src={uploadedFile.src} alt="" className={classes.image} /> | ||
<div className={classes.fileName}>{uploadedFile.name}</div> | ||
</> | ||
) : ( | ||
<div style={{ textAlign: 'center' }}> | ||
<CircularProgress /> | ||
</div> | ||
)} | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={handleClose}>{localization.common.cancel}</Button> | ||
<Button | ||
onClick={() => { | ||
handleClose() | ||
addImage() | ||
}} | ||
color="primary"> | ||
{localization.imageControl.submit} | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
</> | ||
) | ||
} |
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