From c80418fcdbaf390e1c63e41d53fb289af01e2c6b Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Mon, 16 Oct 2017 20:20:50 +0300 Subject: [PATCH] IDEA-179890 'Copy as rich text' doesn't work when pasting to Keynote --- .../richcopy/view/HtmlTransferableData.java | 23 +++------- .../richcopy/view/RtfTransferableData.java | 23 +++------- .../editorActions/TextBlockTransferable.java | 45 +++++++++---------- .../TextBlockTransferableData.java | 26 +++++------ 4 files changed, 46 insertions(+), 71 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/openapi/editor/richcopy/view/HtmlTransferableData.java b/platform/lang-impl/src/com/intellij/openapi/editor/richcopy/view/HtmlTransferableData.java index 4b03834da829..97ccbd86b72d 100644 --- a/platform/lang-impl/src/com/intellij/openapi/editor/richcopy/view/HtmlTransferableData.java +++ b/platform/lang-impl/src/com/intellij/openapi/editor/richcopy/view/HtmlTransferableData.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.openapi.editor.richcopy.view; import com.intellij.openapi.editor.richcopy.model.ColorRegistry; @@ -32,7 +18,7 @@ import java.awt.datatransfer.DataFlavor; * @since 3/28/13 1:06 PM */ public class HtmlTransferableData extends AbstractSyntaxAwareReaderTransferableData implements MarkupHandler { - + public static final int PRIORITY = 200; @NotNull public static final DataFlavor FLAVOR = new DataFlavor("text/html; class=java.io.Reader; charset=UTF-8", "HTML text"); private final int myTabSize; @@ -215,4 +201,9 @@ public class HtmlTransferableData extends AbstractSyntaxAwareReaderTransferableD } return true; } + + @Override + public int getPriority() { + return PRIORITY; + } } diff --git a/platform/lang-impl/src/com/intellij/openapi/editor/richcopy/view/RtfTransferableData.java b/platform/lang-impl/src/com/intellij/openapi/editor/richcopy/view/RtfTransferableData.java index 2757120560e4..de8fcde7aed8 100644 --- a/platform/lang-impl/src/com/intellij/openapi/editor/richcopy/view/RtfTransferableData.java +++ b/platform/lang-impl/src/com/intellij/openapi/editor/richcopy/view/RtfTransferableData.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.openapi.editor.richcopy.view; import com.intellij.ide.MacOSApplicationProvider; @@ -28,7 +14,7 @@ import java.awt.color.ColorSpace; import java.awt.datatransfer.DataFlavor; public class RtfTransferableData extends AbstractSyntaxAwareInputStreamTransferableData { - + public static final int PRIORITY = 100; @NotNull public static final DataFlavor FLAVOR = new DataFlavor("text/rtf;class=java.io.InputStream", "RTF text"); @NotNull private static final String HEADER_PREFIX = "{\\rtf1\\ansi\\deff0"; @@ -107,6 +93,11 @@ public class RtfTransferableData extends AbstractSyntaxAwareInputStreamTransfera buffer.append("\\fs").append(Math.round(fontSize * 2)); } + @Override + public int getPriority() { + return PRIORITY; + } + private static class MyVisitor implements MarkupHandler { @NotNull private final StringBuilder myBuffer; diff --git a/platform/platform-impl/src/com/intellij/codeInsight/editorActions/TextBlockTransferable.java b/platform/platform-impl/src/com/intellij/codeInsight/editorActions/TextBlockTransferable.java index 3c378386f502..ae40b0be20f6 100644 --- a/platform/platform-impl/src/com/intellij/codeInsight/editorActions/TextBlockTransferable.java +++ b/platform/platform-impl/src/com/intellij/codeInsight/editorActions/TextBlockTransferable.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2014 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight.editorActions; @@ -21,6 +7,7 @@ import com.intellij.openapi.editor.RawText; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.text.StringUtil; +import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -29,10 +16,7 @@ import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; import java.io.StringReader; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import java.util.*; public class TextBlockTransferable implements Transferable { private final Collection myExtraData; @@ -45,19 +29,22 @@ public class TextBlockTransferable implements Transferable { myExtraData = extraData; myRawText = rawText; - List dataFlavors = new ArrayList<>(); - Collections.addAll(dataFlavors, DataFlavor.stringFlavor, DataFlavor.plainTextFlavor); + List dataFlavors = new ArrayList<>(); + Collections.addAll(dataFlavors, + new DataFlavorWithPriority(DataFlavor.stringFlavor, TextBlockTransferableData.PLAIN_TEXT_PRIORITY), + new DataFlavorWithPriority(DataFlavor.plainTextFlavor, TextBlockTransferableData.PLAIN_TEXT_PRIORITY)); final DataFlavor flavor = RawText.getDataFlavor(); if (myRawText != null && flavor != null) { - dataFlavors.add(flavor); + dataFlavors.add(new DataFlavorWithPriority(flavor, TextBlockTransferableData.PLAIN_TEXT_PRIORITY)); } for(TextBlockTransferableData data: extraData) { final DataFlavor blockFlavor = data.getFlavor(); if (blockFlavor != null) { - dataFlavors.add(blockFlavor); + dataFlavors.add(new DataFlavorWithPriority(blockFlavor, data.getPriority())); } } - myTransferDataFlavors = dataFlavors.toArray(new DataFlavor[dataFlavors.size()]); + dataFlavors.sort(Comparator.comparingInt(value -> -value.priority)); + myTransferDataFlavors = ContainerUtil.map2Array(dataFlavors, DataFlavor.class, value -> value.flavor); } @NotNull @@ -146,4 +133,14 @@ public class TextBlockTransferable implements Transferable { return StringUtil.convertLineSeparators(text, newSeparator); } } + + private static class DataFlavorWithPriority { + private final DataFlavor flavor; + private final int priority; + + private DataFlavorWithPriority(DataFlavor flavor, int priority) { + this.flavor = flavor; + this.priority = priority; + } + } } \ No newline at end of file diff --git a/platform/platform-impl/src/com/intellij/codeInsight/editorActions/TextBlockTransferableData.java b/platform/platform-impl/src/com/intellij/codeInsight/editorActions/TextBlockTransferableData.java index bdaf6f2ea546..91bd367a2c31 100644 --- a/platform/platform-impl/src/com/intellij/codeInsight/editorActions/TextBlockTransferableData.java +++ b/platform/platform-impl/src/com/intellij/codeInsight/editorActions/TextBlockTransferableData.java @@ -1,30 +1,26 @@ -/* - * Copyright 2000-2014 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight.editorActions; import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.Transferable; /** * @author yole */ public interface TextBlockTransferableData { + int PLAIN_TEXT_PRIORITY = 0; + DataFlavor getFlavor(); int getOffsetCount(); int getOffsets(final int[] offsets, final int index); int setOffsets(final int[] offsets, final int index); + + /** + * Priority defines an order in which resulting transferable will mention available DataFlavor-s. + * + * @see Transferable#getTransferDataFlavors() + */ + default int getPriority() { return PLAIN_TEXT_PRIORITY; } }