tiled icon indicating copy to clipboard operation
tiled copied to clipboard

Change Clipboard Mime-Type to Support Text

Open indianajson opened this issue 1 month ago • 5 comments

What is the current behavior?

Tiled checks for text/tmx as the mime-type of your clipboard before enabling the Paste function (as defined in src/tiled/clipboardmanager.cpp). However, the content copied from and pasted into Tiled (as held in the clipboard) is simply Tiled XML in plain text.

Why is this a problem?

Using the mime-type text/tmx means it is only possible to copy and paste within Tiled itself. External programs (specifically web browsers) are not typically able to set their clipboard to uncommon mime-types (like text/tmx) thus eliminating any copy/paste interoperability between web browsers and Tiled which would otherwise be possible.

For example, I would like to create a web application that will generate Tiled XML objects with Custom Properties that I can paste into Tiled (without having to manually configure them in Tiled), but no modern web browser will allow the clipboard mime-type to be set to text/tmx thus the web app would be pointless (as you would have to download the generated TMX, open it in Tiled, copy the objects, then paste them in the destination TMX).

What is your proposed solution?

Change the mime-type on Line 50 in clipboardmanager.cpp to text/plain so programs like browsers are able to general valid Tiled XML that can be pasted directly into Tiled. Please see Pull Request 4289.

In my testing, simply changing the mime-type to text/plain allows Tiled XML to be directly pasted into open TMX maps without any obvious side effects (attempting to paste invalid XML or gibberish text fails and does not affect the Tiled map in any way).

indianajson avatar Nov 25 '25 03:11 indianajson

External programs (specifically web browsers) are not typically able to set their clipboard to uncommon mime-types (like text/tmx)

I'm not sure whether that is true, at least not anymore. I see the ClipboardItem supports specifying the MIME type in its constructor, as in:

async function copyTmx(tmx) {
  const clipboardItem = new ClipboardItem({
    ["text/tmx"]: tmx,
  });
  await navigator.clipboard.write([clipboardItem]);
}

Along with the downsides I wrote in #4289 I'm not sure whether we should change the MIME type used by Tiled.

bjorn avatar Nov 26 '25 20:11 bjorn

Based on my testing neither Safari nor Chrome will allow text/tmx as a mime-type. I did test prior to posting this request including with ClipboardItem. Safari acts like it copied it (but it doesn't) while Chrome outright refuses based on the custom mime-type. You can set the mime-type but it has to be one of the common mime-types the browser expects. If someone is able to overcome that issue then I would gladly welcome that alternative, but I was unable in my testing.

Your downsides are completely valid. The third downside is definitely the most serious, and something I didn't initially think about as my main use case was copying/pasting points/rectangles/etc with custom properties (so they didn't have any images or essential references). However, I do have an idea which may overcome all three issues.

As an alternative solution, would you be open to adding an option in Settings labeled Enable plain text copy and paste (only enable if needed) which would swap the mime-type value in clipboardmanager.cpp between text/tmx and text/plain (setting would be disabled by default). This would allow those of us who would utilize the interoperability to enable it on the fly, while not affecting any other users and keeping the backward compatibility.

Downside one feels like an acceptable limitation in this case if it is only a problem for those of us that enable the setting (and expect Tiled to accept whatever clipboard data we throw at it) and downside two is eliminated as copy/paste would work between older versions if the setting is disabled. Downside three is still an issue if you leave the setting enabled all the time, but (at least in my use case) I would see myself enabling the setting, doing whatever I need to do, then disabling it.

indianajson avatar Nov 27 '25 14:11 indianajson

Hmm, indeed the list of accepted MIME types is limited for security reasons (there are only three mandatory data types plus a limited list of optional data types documented in the standard). That's a bummer, but understandable.

But I like the idea to put this behind an option! Are you going to adjust the PR to add this feature that way?

bjorn avatar Nov 28 '25 16:11 bjorn

I'll certainly give it a try, I'll push an updated PR once I get it working on my end.

indianajson avatar Nov 28 '25 16:11 indianajson

PR has been updated and appears to have passed the checks.

The new option has been added to preferences under the name "Set clipboard to use plain text" which defaults to false. I also added the necessary code to update and store the value of the option in Storage.

When this option is toggled in Preferences, an event is emitted that triggers ClipboardManager's update() to ensure the Paste and Paste In Place menu options are correctly enabled or disabled when the option is changed. (Without the event they only updated when new data was copied to the clipboard).

Based on my testing the new option behaves consistently.

indianajson avatar Nov 28 '25 19:11 indianajson