Repository: gchq/CyberChef
File: src/web/waiters/InputWaiter.mjs (JavaScript)

Code before:
                    // Explicitly insert a new line (using the current EOL char) rather
                    // than messing around with indenting, which does not respect EOL chars
                    { key: "Enter", run: insertNewline },
                    ...historyKeymap,
                    ...defaultKeymap,
                    ...searchKeymap
                ]),

                // Event listeners
                EditorView.updateListener.of(e => {
                    if (e.selectionSet)
                        this.manager.highlighter.selectionChange("input", e);
                    if (e.docChanged && !this.silentInputChange)
                        this.inputChange(e);
                    this.silentInputChange = false;
                }),

                // Event handlers
                EditorView.domEventHandlers({
                    paste(event, view) {
                        const clipboardData = event.clipboardData || window.clipboardData;
                        const items = clipboardData.items;
                        event.target.files = [];
                        for (let i = 0; i < items.length; i++) {
                            const item = items[i];
                            if (item.type.indexOf("image") !== -1) {
                                const file = item.getAsFile();
                                event.target.files.push(file);

                                event.preventDefault(); // Prevent the default paste behavior
                            }
                        }
                        setTimeout(() => {
                            self.afterPaste(event);
                        });
                    }
                })
            ]
        });


        if (this.inputEditorView) this.inputEditorView.destroy();
        this.inputEditorView = new EditorView({
            state: initialState,
            parent: this.inputTextEl
        });
    }

    /**
     * Handler for EOL change events
     * Sets the line separator

Review comment (suggestion):
It would be good if we could support all file types, rather than just images. Could you not instead check `item.kind === 'file'`?
https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/kind

Code after:
                    // Explicitly insert a new line (using the current EOL char) rather
                    // than messing around with indenting, which does not respect EOL chars
                    { key: "Enter", run: insertNewline },
                    ...historyKeymap,
                    ...defaultKeymap,
                    ...searchKeymap
                ]),

                // Event listeners
                EditorView.updateListener.of(e => {
                    if (e.selectionSet)
                        this.manager.highlighter.selectionChange("input", e);
                    if (e.docChanged && !this.silentInputChange)
                        this.inputChange(e);
                    this.silentInputChange = false;
                }),

                // Event handlers
                EditorView.domEventHandlers({
                    paste(event, view) {
                        const clipboardData = event.clipboardData;
                        const items = clipboardData.items;
                        const files = [];
                        for (let i = 0; i < items.length; i++) {
                            const item = items[i];
                            if (item.kind === "file") {
                                const file = item.getAsFile();
                                files.push(file);

                                event.preventDefault(); // Prevent the default paste behavior
                            }
                        }
                        setTimeout(() => {
                            self.afterPaste(files);
                        });
                    }
                })
            ]
        });


        if (this.inputEditorView) this.inputEditorView.destroy();
        this.inputEditorView = new EditorView({
            state: initialState,
            parent: this.inputTextEl
        });
    }

    /**
     * Handler for EOL change events
     * Sets the line separator