import type { Ctx } from '@milkdown/ctx' import type { Fragment, Node, Schema } from '@milkdown/prose/model' import type { EditorState } from '@milkdown/prose/state' import type { EditorView } from '@milkdown/prose/view' import { schemaCtx } from '@milkdown/core' import { Plugin, PluginKey } from '@milkdown/prose/state' import { Decoration, DecorationSet } from '@milkdown/prose/view' import { $ctx, $prose } from '@milkdown/utils' import { defaultUploader } from './default-uploader' /// @internal export type Uploader = UploadOptions['uploader'] interface Spec { id: symbol pos: number } /// The configuration for upload. export interface UploadOptions { /// The uploader for the upload plugin. It takes the files, the schema, /// the ctx and the insert position. It returns a `Promise` of a /// prosemirror `Fragment`, `Node` or `Node[]`. uploader: ( files: FileList, schema: Schema, ctx: Ctx, insertPos: number ) => Promise /// Whether to enable the html file uploader. A paste of files from /// html, for example an image copied through the right-click context /// menu, then uploads the copied image instead of keeping the original /// link. enableHtmlFileUploader: boolean /// The factory for upload widget. /// The widget will be displayed when the file is uploading. /// It takes the position and spec as parameters. /// It should return a `Decoration` of Prosemirror. /// By default, it will return `Upload in progress...`. uploadWidgetFactory: ( pos: number, spec: Parameters[2] ) => Decoration // A custom calculator for new nodes to insert getInsertPos?: ( event: ClipboardEvent | DragEvent, ctx: Ctx, defaultInsertPos: number ) => number } /// A slice that contains the configuration for upload. /// It should be typed of `UploadConfig`. export const uploadConfig = $ctx( { uploader: defaultUploader, enableHtmlFileUploader: false, uploadWidgetFactory: (pos, spec) => { const widgetDOM = document.createElement('span') widgetDOM.textContent = 'Upload in progress...' return Decoration.widget(pos, widgetDOM, spec) }, }, 'uploadConfig' ) uploadConfig.meta = { package: '@milkdown/plugin-upload', displayName: 'Ctx', } /// The prosemirror plugin for upload. export const uploadPlugin = $prose((ctx) => { const pluginKey = new PluginKey('MILKDOWN_UPLOAD') const findPlaceholder = (state: EditorState, id: symbol): number => { const decorations = pluginKey.getState(state) if (!decorations) return -1 const found = decorations.find( undefined, undefined, (spec: Spec) => spec.id === id ) if (!found.length) return -1 return found[0]?.from ?? -1 } const handleUpload = ( view: EditorView, event: DragEvent | ClipboardEvent, files: FileList | undefined ) => { if (!files || files.length <= 0) return false const id = Symbol('upload symbol') const schema = ctx.get(schemaCtx) const { uploader, getInsertPos } = ctx.get(uploadConfig.key) const { tr } = view.state const defaultInsertPos = event instanceof DragEvent ? (view.posAtCoords({ left: event.clientX, top: event.clientY })?.pos ?? tr.selection.from) : tr.selection.from const insertPos = typeof getInsertPos === 'function' ? getInsertPos(event, ctx, defaultInsertPos) : defaultInsertPos view.dispatch(tr.setMeta(pluginKey, { add: { id, pos: insertPos } })) uploader(files, schema, ctx, insertPos) .then((fragment) => { const pos = findPlaceholder(view.state, id) if (pos < 0) return view.dispatch( view.state.tr .replaceWith(pos, pos, fragment) .setMeta(pluginKey, { remove: { id } }) ) }) .catch((e) => { console.error(e) }) return true } return new Plugin({ key: pluginKey, state: { init() { return DecorationSet.empty }, apply(this: Plugin, tr, set) { const _set = set.map(tr.mapping, tr.doc) const action = tr.getMeta(this) if (!action) return _set if (action.add) { const { uploadWidgetFactory } = ctx.get(uploadConfig.key) const decoration = uploadWidgetFactory(action.add.pos, { id: action.add.id, }) return _set.add(tr.doc, [decoration]) } if (action.remove) { const target = _set.find( undefined, undefined, (spec: Spec) => spec.id === action.remove.id ) return _set.remove(target) } return _set }, }, props: { decorations(this: Plugin, state) { return this.getState(state) }, handlePaste: (view, event) => { const { enableHtmlFileUploader } = ctx.get(uploadConfig.key) if (!(event instanceof ClipboardEvent)) return false if ( !enableHtmlFileUploader && event.clipboardData?.getData('text/html') ) return false return handleUpload(view, event, event.clipboardData?.files) }, handleDrop: (view, event) => { if (!(event instanceof DragEvent)) return false return handleUpload(view, event, event.dataTransfer?.files) }, }, }) }) uploadPlugin.meta = { package: '@milkdown/plugin-upload', displayName: 'Prose', }