clipboard icon indicating copy to clipboard operation
clipboard copied to clipboard

Allow Custom Format

Open changkun opened this issue 3 years ago • 9 comments

Currently, we only support UTF-8 text and PNG-encoded image data. However, there are much more formats than that.

Let's support registering custom format and handlers so that this package can help any other unregistered format. API design could be:

package clipboard

type Format int

// Add this.
func Register[T any](format clipboard.Format, read func([]byte) (T, error), write func() []byte) error { ... }

func Read(format clipboard.Format) []byte
func Write(format clipboard.Format, data []byte)
func Watch(format clipboard.Format) <-chan struct{}

changkun avatar Jan 06 '22 08:01 changkun

Excuse me, is there currently a plan that supports file(.pdf, .txt...) types?

dmzlingyin avatar Nov 20 '22 08:11 dmzlingyin

We can actually start case by case. But that would be a different issue.

changkun avatar Nov 20 '22 08:11 changkun

Cool! May I ask, when will it start?

dmzlingyin avatar Nov 20 '22 09:11 dmzlingyin

There is no official plan for it. But things can get prioritized if we find this a high demand.

changkun avatar Nov 20 '22 09:11 changkun

There is no official plan for it. But things can get prioritized if we find this a high demand.

I got it. Thanks a lot.

dmzlingyin avatar Nov 20 '22 09:11 dmzlingyin

There is no official plan for it. But things can get prioritized if we find this a high demand.

I think custom formats would be awesome. Maybe also some functions to inspect the format, currently in the clipboard? For my application, I want to generate some Excel data, which should be styled. So if I could copy the data in the style I like, and then replicate the format, but change the content, that would be awesome!

MarvinJWendt avatar Nov 23 '22 08:11 MarvinJWendt

I want to generate some Excel data, which should be styled. So if I could copy the data in the style I like, and then replicate the format, but change the content, that would be awesome!

This is an interesting case. Would you mind creating a different issue specifically for this type of data and elaborating a little bit more on the use case then cite to this issue?

changkun avatar Nov 23 '22 08:11 changkun

Would you mind creating a different issue specifically for this type of data and elaborating a little bit more on the use case then cite to this issue?

I thought about creating a new issue, but I think it's a bit more complicated, as the user would have to change the format, depending on his input. Custom formats would be perfect for that.

Excel uses a combination of many formats, when copying cells. For this example, I will copy those two cells:

image

Those cells have bold and centered text styling.

This is the clipboard:

screenshot

Clipboard content

Click to expand

CF_TEXT
Hello	World
HTML
Version:1.0
StartHTML:0000000105
EndHTML:0000001957
StartFragment:0000001757
EndFragment:0000001897

<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 15">
<link id=Main-File rel=Main-File
href="file:///C:/Users/marvi/AppData/Local/Temp/msohtmlclip1/01/clip.htm">
<link rel=File-List
href="file:///C:/Users/marvi/AppData/Local/Temp/msohtmlclip1/01/clip_filelist.xml">
<style>
<!--table
	{mso-displayed-decimal-separator:"\,";
	mso-displayed-thousand-separator:"\.";}
@page
	{margin:.75in .7in .75in .7in;
	mso-header-margin:.3in;
	mso-footer-margin:.3in;}
tr
	{mso-height-source:auto;}
col
	{mso-width-source:auto;}
br
	{mso-data-placement:same-cell;}
td
	{padding-top:1px;
	padding-right:1px;
	padding-left:1px;
	mso-ignore:padding;
	color:black;
	font-size:11.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	font-family:Calibri, sans-serif;
	mso-font-charset:0;
	mso-number-format:General;
	text-align:general;
	vertical-align:bottom;
	border:none;
	mso-background-source:auto;
	mso-pattern:auto;
	mso-protection:locked visible;
	white-space:nowrap;
	mso-rotate:0;}
.xl65
	{font-weight:700;
	text-align:center;}
-->
</style>
</head>

<body link="#0563C1" vlink="#954F72">

<table border=0 cellpadding=0 cellspacing=0 width=128 style='border-collapse:
 collapse;width:96pt'>
 <col width=64 span=2 style='width:48pt'>
 <tr height=19 style='height:14.5pt'>
<!--StartFragment-->
  <td height=19 class=xl65 width=64 style='height:14.5pt;width:48pt'>Hello</td>
  <td class=xl65 width=64 style='width:48pt'>World</td>
<!--EndFragment-->
 </tr>
</table>

</body>

</html>
RTF
{\rtf1\ansi \ansicpg1252
{\fonttbl{\f0\fnil Calibri;}{\f1\fnil Calibri;}{\f2\fnil Calibri;}{\f3\fnil Calibri;}{\f4\fnil Calibri;}{\f5\fnil Calibri;}{\f6\fnil Calibri Light;}{\f7\fnil Calibri;}{\f8\fnil Calibri;}{\f9\fnil Calibri;}{\f10\fnil Calibri;}{\f11\fnil Calibri;}{\f12\fnil Calibri;}{\f13\fnil Calibri;}{\f14\fnil Calibri;}{\f15\fnil Calibri;}{\f16\fnil Calibri;}{\f17\fnil Calibri;}{\f18\fnil Calibri;}{\f19\fnil Calibri;}{\f20\fnil Calibri;}{\f21\fnil Calibri;}{\f22\fnil Segoe UI;}{\f23\fnil Calibri;}{\f24\fnil Calibri;}}
{\info{\id220}}\plain {\colortbl\red0\green0\blue0;\red255\green255\blue255;\red255\green0\blue0;\red0\green255\blue0;\red0\green0\blue255;\red255\green255\blue0;\red255\green0\blue255;\red0\green255\blue255;\red0\green0\blue0;\red255\green255\blue255;\red255\green0\blue0;\red0\green255\blue0;\red0\green0\blue255;\red255\green255\blue0;\red255\green0\blue255;\red0\green255\blue255;\red128\green0\blue0;\red0\green128\blue0;\red0\green0\blue128;\red128\green128\blue0;\red128\green0\blue128;\red0\green128\blue128;\red192\green192\blue192;\red128\green128\blue128;\red153\green153\blue255;\red153\green51\blue102;\red255\green255\blue204;\red204\green255\blue255;\red102\green0\blue102;\red255\green128\blue128;\red0\green102\blue204;\red204\green204\blue255;\red0\green0\blue128;\red255\green0\blue255;\red255\green255\blue0;\red0\green255\blue255;\red128\green0\blue128;\red128\green0\blue0;\red0\green128\blue128;\red0\green0\blue255;\red0\green204\blue255;\red204\green255\blue255;\red204\green255\blue204;\red255\green255\blue153;\red153\green204\blue255;\red255\green153\blue204;\red204\green153\blue255;\red255\green204\blue153;\red51\green102\blue255;\red51\green204\blue204;\red153\green204\blue0;\red255\green204\blue0;\red255\green153\blue0;\red255\green102\blue0;\red102\green102\blue153;\red150\green150\blue150;\red0\green51\blue102;\red51\green153\blue102;\red0\green51\blue0;\red51\green51\blue0;\red153\green51\blue0;\red153\green51\blue102;\red51\green51\blue153;\red51\green51\blue51;;\red255\green255\blue255;\red100\green100\blue100;\red240\green240\blue240;\red0\green0\blue0;\red255\green255\blue255;\red160\green160\blue160;\red0\green120\blue215;\red0\green0\blue0;\red200\green200\blue200;\red55\green55\blue55;\red255\green255\blue255;\red100\green100\blue100;\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue0;\red255\green255\blue225;\red0\green0\blue0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\red192\green192\blue192;\red150\green150\blue150;\red128\green128\blue128;\red102\green102\blue102;\red51\green51\blue51;\red119\green198\blue253;\red255\green167\blue154;\red213\green173\blue252;\red0\green219\blue87;\red254\green153\blue255;\red255\green173\blue18;\red87\green210\blue218;\red51\green102\blue153;\red128\green0\blue0;\red0\green128\blue0;\red0\green0\blue128;\red128\green128\blue0;\red128\green0\blue128;\red0\green128\blue128;\red0\green0\blue208;\red224\green224\blue224;\red50\green106\blue199;\red0\green120\blue212;\red136\green23\blue152;\red227\green0\blue140;\red0\green78\blue140;\red209\green52\blue56;\red202\green80\blue16;\red3\green131\blue135;\red152\green111\blue11;\red164\green38\blue44;\red194\green57\blue179;\red57\green57\blue57;\red79\green107\blue237;\red117\green11\blue28;\red135\green100\blue184;\red122\green117\blue116;\red0\green91\blue112;\red92\green46\blue145;\red105\green121\blue126;\red142\green86\blue46;\red170\green170\blue170;\red0\green107\blue255;}
\trowd \trgaph30\trleft-30\trrh288\cellx946\cellx1922\pard \intbl \qc \f20\fs22 \b \cf8 Hello\cell \qc World\cell \pard \intbl \row}
XML Spreadsheet
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s65">
   <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"
    ss:Bold="1"/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="1"
   ss:DefaultRowHeight="14.5">
   <Row>
    <Cell ss:StyleID="s65"><Data ss:Type="String">Hello</Data></Cell>
    <Cell ss:StyleID="s65"><Data ss:Type="String">World</Data></Cell>
   </Row>
  </Table>
 </Worksheet>
</Workbook>
CSV
Hello;World

As we can see here, excel uses multiple formats, to make pasting in different applications possible. The XML Spreadsheet format, seems to be excels own. Outside of Excel, either HTML or RTF will be used to give the pasted content the styling.

Use Case

I suggest that something is added to inspect those different formats, and to modify them. In my case, I want to create a tool, that helps me to fill out an Excel sheet. I have to follow some style guidelines. So what I want to do is, get the information of the clipboard formats, and replace specific text. That way, if I paste into Excel, I will have the same style as the cells that I copied (with placeholders).

As an example: If I copy those cells:

image

I want to replace %NAME% with Marvin Wendt and %AGE% with 21.

Then, when I paste into Excel, it will look like this:

image

Another use case would be custom HTML styling. That way, we could put styled text into the users clipboard, which then can be pasted in programs like Word or other editors (maybe also online Editors like WordPress).

MarvinJWendt avatar Nov 23 '22 16:11 MarvinJWendt