Skip to content

Commit

Permalink
feat: initial image path state to allow for the path to be different … (
Browse files Browse the repository at this point in the history
#242)

* Added initial image path state to allow for the path to be different than the image source, for example the path might be a file path and the image source may be base64 encoded file

* Update image preview hook example with base64

---------

Co-authored-by: Finlay Sawyer <[email protected]>
  • Loading branch information
colemorgan-bp and FinlayBP authored Dec 20, 2023
1 parent aa684b7 commit a1104bf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/examples/images.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ export function ImageWithPreviewHook() {
return (
<>
<MDXEditor
markdown={markdownWithHtmlImages}
markdown="Preview hook that returns static base64: ![alt text](/attachments/my_image.png)"
plugins={[
imagePlugin({
disableImageResize: true,
imagePreviewHandler: async (imageSource) => Promise.resolve(`${imageSource}?grayscale`)
imagePreviewHandler: async () =>
Promise.resolve(
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC0AAAAYCAYAAABurXSEAAAAAXNSR0IArs4c6QAAAGJlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAABJKGAAcAAAASAAAAUKABAAMAAAABAAEAAKACAAQAAAABAAAALaADAAQAAAABAAAAGAAAAABBU0NJSQAAAFNjcmVlbnNob3QGyMkKAAAB1GlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iWE1QIENvcmUgNi4wLjAiPgogICA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczpleGlmPSJodHRwOi8vbnMuYWRvYmUuY29tL2V4aWYvMS4wLyI+CiAgICAgICAgIDxleGlmOlBpeGVsWURpbWVuc2lvbj4yNDwvZX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8ODhIxQLcXGqykvBeSMxLZ1h3YZNDBMmT2VIycxmMDTQZ3Cyt0PRhjN5MKJFM7IuRkZGMBdGg3L7qqWLGEoraxhqGprAcqC02FBTxZAUH4uslYEBTS/MjKjwUIavX78yzJo7H+xYkCYvDzeGvs4OBvSMzwiq5FFNpYz3C1gcfvzwkUFYWAgjfRNr8vsPHxh4gZ5mYcEeplR3NLEOo0QdVdI0JQ4gR++oo8kJNXL0AADsUIxP1kwKcwAAAABJRU5ErkJggg=='
)
}),
diffSourcePlugin(),
jsxPlugin(),
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/image/ImageEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export function ImageEditor({ src, title, alt, nodeKey, width, height }: ImageEd
const [disableImageResize] = imagePluginHooks.useEmitterValues('disableImageResize')
const [imagePreviewHandler] = imagePluginHooks.useEmitterValues('imagePreviewHandler')
const [imageSource, setImageSource] = React.useState<string | null>(null)
const [initialImagePath, setInitialImagePath] = React.useState<string | null>(null)
const openEditImageDialog = imagePluginHooks.usePublisher('openEditImageDialog')
const [iconComponentFor] = corePluginHooks.useEmitterValues('iconComponentFor')

Expand Down Expand Up @@ -148,6 +149,7 @@ export function ImageEditor({ src, title, alt, nodeKey, width, height }: ImageEd
React.useEffect(() => {
if (imagePreviewHandler) {
const callPreviewHandler = async () => {
if (!initialImagePath) setInitialImagePath(src)
const updatedSrc = await imagePreviewHandler(src)
setImageSource(updatedSrc)
}
Expand Down Expand Up @@ -267,7 +269,7 @@ export function ImageEditor({ src, title, alt, nodeKey, width, height }: ImageEd
openEditImageDialog({
nodeKey: nodeKey,
initialValues: {
src: imageSource,
src: !initialImagePath ? imageSource : initialImagePath,
title: title || '',
altText: alt || ''
}
Expand Down

0 comments on commit a1104bf

Please sign in to comment.