Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add selectUpload config option to ImageBlock #1559

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/src/image-block/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ImageBlockConfig {
uploadPlaceholderText: string
captionPlaceholderText: string
onUpload: (file: File) => Promise<string>
selectUpload?: () => Promise<string>
}

export const defaultImageBlockConfig: ImageBlockConfig = {
Expand Down
17 changes: 16 additions & 1 deletion packages/components/src/image-block/view/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ export const imageComponent: Component<ImageComponentProps> = ({
e.preventDefault()
}

const handleUpload = async (e: PointerEvent) => {
if (readonly) {
return;
}
if (typeof config?.selectUpload !== 'undefined') {
e.stopPropagation();
e.preventDefault();
const url = await config.selectUpload();
if (url) {
setAttr?.('src', url)
setHidePlaceholder(true);
}
}
}

return html`<host class=${clsx(selected && 'selected')}>
<div class=${clsx('image-edit', src.length > 0 && 'hidden')}>
<div class="image-icon">
Expand All @@ -142,7 +157,7 @@ export const imageComponent: Component<ImageComponentProps> = ({
/>
<div class=${clsx('placeholder', hidePlaceholder && 'hidden')}>
<input disabled=${readonly} class="hidden" id=${uuid} type="file" accept="image/*" onchange=${onUpload} />
<label onpointerdown=${onClickUploader} class="uploader" for=${uuid}>
<label onpointerdown=${onClickUploader} onclick=${handleUpload} class="uploader" for=${uuid}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should add a new event handler here to handle both onclick and onpointerdown, a possible solution should be still use onUpload without introducting another config. We can pass event to the onUpload config to let users open their own file picker.

${config?.uploadButton()}
</label>
<span class="text" onclick=${() => linkInput.current?.focus()}>
Expand Down
29 changes: 28 additions & 1 deletion storybook/stories/components/image-block.stories.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Meta, StoryObj } from '@storybook/html'
import { imageBlockComponent } from '@milkdown/kit/component/image-block'
import { imageBlockComponent, imageBlockConfig } from '@milkdown/kit/component/image-block'

import type { CommonArgs } from '../utils/shadow'
import { setupMilkdown } from '../utils/shadow'
Expand Down Expand Up @@ -31,6 +31,33 @@ export const Empty: StoryObj<CommonArgs> = {
},
}

export const CustomUpload: StoryObj<CommonArgs> = {
render: (args) => {
return setupMilkdown([style], args, (editor) => {
editor.config((ctx) => {
ctx.update(imageBlockConfig.key, value => ({
uploadButton: (() => 'Select image'),
imageIcon: value.imageIcon,
captionIcon: value.captionIcon,
confirmButton: value.confirmButton,
captionPlaceholderText: value.captionPlaceholderText,
uploadPlaceholderText: value.uploadPlaceholderText,
onUpload: value.onUpload,
selectUpload: () => {
return new Promise(resolve => {
resolve('/milkdown-logo.png');
});
}
}))
}).use(imageBlockComponent)
})
},
args: {
readonly: false,
defaultValue: empty,
},
}

export const Logo: StoryObj<CommonArgs> = {
...Empty,
args: {
Expand Down