Skip to content
This repository was archived by the owner on Apr 16, 2026. It is now read-only.
This repository was archived by the owner on Apr 16, 2026. It is now read-only.

CodeMirror sends wrong "paste" origin with changeObj after trying to paste an image #5764

Closed

Description

@nathanlesage

Hey,

first of all, as this is my first issue here: Thank you so much for your hard work with CodeMirror!

I think I found some weird behaviour in relation with pasting events and CodeMirror. I wanted to enable a "paste image from clipboard"-functionality in my app, Zettlr, and it works extremely well except one strange issue:

If you copy an image to clipboard but provide no fallback text (for pure textareas or apps that don't support pasting clipboard images) — e.g. making a screenshot — CodeMirror acts weirdly. If you paste such an image, CodeMirror won't fire, obviously, because there's no text to paste. But after I've manually saved the image and inserted a link to the image by calling cm.replaceSelection(), the next time I type anything, CodeMirror suddenly throws out a changeObj on its beforeChange event that has its origin set to "paste", and not to "insert", as it should be.

What I think is happening is when the textarea receives the paste-event, CodeMirror sets an internal origin-flag to "paste", afterwards detects that there is no text in the past event, aborts the paste operation but leaves the flag to "paste" and as soon as the next input happens, it uses the "paste" flag and does not reset it to whatever input is happening.

Steps to Reproduce

  1. Open any CodeMirror instance that works using a <textarea>-tag (any of the demos should do)
  2. Attach an event listener to the beforeChange-event that simply console.logs the changeObj: editor.on('beforeChange', (cm, changeObj) => { console.log(changeObj) })
  3. Make a screenshot or otherwise load into the clipboard an image that has no fallback text
  4. Try to paste. Nothing should happen.
  5. Type any character.
  6. In the console, the changeObj should appear, featuring a "paste" origin, when it should say "input".

Affected Browsers

I've tested this with

  • Chrome 71
  • Safari 12.0.2
  • Firefox 64

If you need any more information, please just ask! :)

Thank you in advance for looking into this!

Activity

  1. marijnh commented on Feb 4, 2019

    @marijnh
    Member

    Your diagnosis was pretty accurate—attached patch switches to a timestamp, rather than a boolean flag, so that if no input is found after a paste event, the "paste" origin doesn't get attached to the next input event.

  2. nathanlesage commented on Feb 4, 2019

    @nathanlesage
    ContributorAuthor

    Thank you very much for your quick response! :)

    I‘m looking forward to the release!

  3. added a commit that references this issue on Jan 6, 2020
    e61138c
  4. added a commit that references this issue on Jan 21, 2020
    2e3f6d4
  5. added a commit that references this issue on Sep 5, 2024
    9c8f813
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions