Add hyperlink to pasted text

Hello,

if i pasting text to card, anki add hyperlink properties to this text.
The text in the clipboard pasted by a Java programm. If i pasted the text anywhere else (ms word, notepad++) its look normal. I tried anki qt5 and qt6. If i copy text from anywhere else by myself the pasting text looks normal in the card. If i copy text from the Java program textarea by myself anki add hyperlink properties to this text, too.

Update: Example Java code:

package org.example;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;

public class Main {
    public static void main(String[] args) {
        String text = "Hello";
        StringSelection selection = new StringSelection(text);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(selection, null);
    }
}

I can reproduce this using your snippet. It looks like a Qt bug or something to me, because QMimeData.urls() is actually giving Anki a URL like QUrl('Hello').

3 Likes

After analyzing the clipboard and the source code, I found a solution.

Anki try various content types in turn. First html then url etc.

Expand the clipboard with text/html mimetype and it works:

package org.example;

import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        String text = "Hello";
        HtmlSelection selection = new HtmlSelection(text);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(selection, null);
    }

    private static class HtmlSelection implements Transferable {

        private static List<DataFlavor> htmlFlavors = new ArrayList<>(3);

        static {

            try {
                htmlFlavors.add(new DataFlavor("text/html;class=java.lang.String"));
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }

        }

        private String html;

        public HtmlSelection(String html) {
            this.html = html;
        }

        public DataFlavor[] getTransferDataFlavors() {
            return (DataFlavor[]) htmlFlavors.toArray(new DataFlavor[htmlFlavors.size()]);
        }

        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return htmlFlavors.contains(flavor);
        }

        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
            if (String.class.equals(flavor.getRepresentationClass())) {
                return html;
            }
            throw new UnsupportedFlavorException(flavor);
        }
    }
}

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.