public onPluginEvent()

in packages/roosterjs-react-common/lib/plugins/PasteImagePlugin.ts [27:69]


    public onPluginEvent(event: PluginEvent): void {
        if (event.eventType === PluginEventType.ExtractContent) {
            const extractContentEvent = event as ExtractContentEvent;
            const content = extractContentEvent.content;
            const runRemove = hasPlaceholder(content);
            if (runRemove) {
                extractContentEvent.content = content.replace(PlaceholderRegex, "");
            }

            return;
        }

        if (event.eventType !== PluginEventType.BeforePaste) {
            return;
        }
        const beforePasteEvent = event as BeforePasteEvent;
        if (beforePasteEvent.pasteOption !== PasteOption.PasteImage) {
            return;
        }

        // handle only before paste and image paste
        const editor = this.getEditor();
        if (!editor) {
            return;
        }

        // prevent pasting of image by telling the handler to interpret the paste as text
        if (this.preventImagePaste) {
            beforePasteEvent.pasteOption = PasteOption.PasteText;
            return;
        }

        const image = beforePasteEvent.clipboardData.image;
        const placeholder: HTMLElement = this.imageManager.upload(editor, image, true);
        if (placeholder === null) {
            return;
        }

        // modify the pasting content and option so Paste plugin won't handle
        beforePasteEvent.fragment.appendChild(placeholder);
        beforePasteEvent.clipboardData.html = placeholder.outerHTML;
        beforePasteEvent.pasteOption = PasteOption.PasteHtml;
    }