The style definition of ClickToCopy
can be found in the following stylesheet
FileNameJoin@{$InstallationDirectory,"SystemFiles","FrontEnd","StyleSheets","Core.nb"}
and uses CopyToClipboard
...
ButtonBox[
...
CopyToClipboard[Cell[BoxData[#2],"Input"]]
...
]
...
where the double quotes come from.
Hence we can try to define a new function by slightly modifying ClickToCopy
, and this has almost been done.
The undocumented function CloudObject`Private`customClickToCopy
has essentially the same definition but different options as CopyToClipboard
,
Needs["CloudObject`"];
CloudObject`Private`customClickToCopy[#,Hold[#]]&@"test"
With slight efforts we have
clickToCopyText[displayedText_] :=
clickToCopyText`kernel[displayedText,displayedText];
clickToCopyText[displayedText_,copiedText_] :=
clickToCopyText`kernel[displayedText,copiedText];
clickToCopyText`kernel[displayedText_,copiedText_] :=
Mouseover[
clickToCopyText`makeClickToCopyOverlay[displayedText,None],
DynamicModule[{Typeset`boxobj,Typeset`cellobj},
EventHandler[
Button[
clickToCopyText`makeClickToCopyOverlay[displayedText,clickToCopyText`aliceBlue],
CopyToClipboard@copiedText;
NotebookDelete@Typeset`cellobj;
Typeset`cellobj =
MathLink`CallFrontEnd@FrontEnd`AttachCell[
Typeset`boxobj,
Cell@BoxData@TemplateBox[{"Copied"},"ClickToCopyTooltip"],
{1,{Center,Bottom}},
{Center,Top},
"ClosingActions"->{"ParentChanged"}
],
Appearance->FrontEndResource["FEExpressions","SuppressMouseDownNinePatchAppearance"],
BaseStyle->{},
Alignment->{Left,Automatic},
DefaultBaseStyle->{},
BaselinePosition->Baseline,
FrameMargins->0,
Method->"Preemptive"
],
{
"MouseEntered":>(
Typeset`cellobj =
MathLink`CallFrontEnd@FrontEnd`AttachCell[
Typeset`boxobj,
Cell@BoxData@TemplateBox[{"Copy"},"ClickToCopyTooltip"],
{1,{Center,Bottom}},
{Center,Top},
"ClosingActions"->{"ParentChanged"}
]
),
"MouseExited":>NotebookDelete[Typeset`cellobj]
},
PassEventsDown->True
]//MouseAppearance[#,"LinkHand"]&,
Initialization:>(
Typeset`boxobj = EvaluationBox[]
),
UnsavedVariables:>{Typeset`boxobj,Typeset`cellobj},
BaseStyle->{Editable->False}
]
];
clickToCopyText`makeClickToCopyOverlay[text_,bgColor_] :=
Framed[
text,
Background->bgColor,
FrameStyle->bgColor,
"BoxExpandable"->True,
BaseStyle->{},
DefaultBaseStyle->{},
Alignment->{Left,Automatic},
BaselinePosition->Baseline,
FrameMargins->0
];
clickToCopyText`aliceBlue = N@RGBColor[{232,243,255}/255];
Now let's test,
testString=(SeedRandom[1];RandomWord[20]//StringRiffle)
test={{"clickToCopyText",clickToCopyText@testString},{"ClickToCopy",ClickToCopy@testString},{"plain",testString}};
test//Grid
and the pasted text contains no double quotes as expected.
Drawback
I take several hours on the compatibility of clickToCopyText|ClickToCopy
with Dataset
, but failed,
test//Dataset
The text font can be easily fixed by the options
BaseStyle->{FontFamily->"Source Sans Pro",FontSize->14,FontWeight->Plain}
But for ClickToCopy
, sometimes (maybe according to the type system of Dataset
) there are double quotes appearing in the dataset by the cell option
ShowStringCharacters->True
For my clickToCopyText
, the text will be hidden if too long, and I have no idea how to fix it by clickToCopyText
or options of Dataset
.
ClickToCopy["display text",Defer[ file name]]
? $\endgroup$ClickToCopy["display text",ToExpression["file name",StandardForm, HoldForm]]
? $\endgroup$ClickToCopy["display text", Defer[Evaluate[ToExpression["file name"]]]]
and produces the text of the string on copy. $\endgroup$Button["display text",CopyToClipboard["file name"], Appearance->None]
works? $\endgroup$