Skip to content

ImgInterceptor

imgInterceptor lets you control what happens after an image is created or its data changes. Typical use cases include uploading images to a server, replacing temporary URLs, showing upload progress and rolling back state after failures.

Usage

vue
<template>
    <power-editor :img-interceptor="imgInterceptor" />
</template>

<script>
export default {
    methods: {
        async imgInterceptor(ctx) {
            const originalSrc = ctx.getImage();

            ctx.showStatus(true);
            ctx.updateStatus(true, 0, 'Uploading...');
            ctx.updateLock(true);

            try {
                const localPreview = ctx.interceptImage(originalSrc);
                ctx.updateStatus(true, 50, 'Processing...');

                const uploadedUrl = await uploadImage(localPreview);

                ctx.updateImage(uploadedUrl);
                ctx.updateStatus(false, 100, 'Done');
            } finally {
                ctx.updateLock(false);
                ctx.showStatus(false);
            }
        }
    }
};
</script>

Context

imgInterceptor receives a context object with the current image node and helper functions.

FieldTypeDescription
nodeobjectCurrent image node.
extensionobjectCurrent image node extension.
getPosfunctionReturns the current node position.
updateAttributesfunctionUpdates current node attributes.
deleteNodefunctionDeletes the current image node.
showStatusfunctionShows or hides the image status layer.
updateStatusfunctionUpdates upload or processing status.
getImagefunctionReturns the current image src.
interceptImagefunctionReplaces image src temporarily and returns the original src.
updateImagefunctionUpdates the final image src.
updateLockfunctionUpdates interceptor lock state.

Helper Functions

showStatus(status)

Shows or hides the image status layer.

ParamDescription
statustrue to show, false to hide.

updateStatus(loading, progress, info)

Updates the status layer.

ParamDescription
loadingWhether loading state is visible.
progressProcessing progress. Recommended range: 0 to 100.
infoStatus text.

getImage()

Returns the current image node src.

interceptImage(replaceSrc)

Temporarily replaces the image src during processing and returns the original src. This is useful for local previews or placeholders before upload is finished.

updateImage(src)

Updates the image node with the final src.

updateLock(lock)

Updates interceptor lock state. Lock during upload or async processing, then unlock after it finishes.

ParamDescription
locktrue to lock, false to unlock.

Full Signature

js
const imgInterceptor = ({
    node,
    extension,
    getPos,
    updateAttributes,
    deleteNode,
    showStatus,
    updateStatus,
    getImage,
    interceptImage,
    updateImage,
    updateLock
}) => {
    // custom upload or processing logic
};

MIT Licensed