golang-design / clipboard

📋 cross-platform clipboard package that supports accessing text and image in Go (macOS/Linux/Windows/Android/iOS)
https://golang.design/x/clipboard
MIT License
579 stars 64 forks source link

Allow Custom Format #17

Open changkun opened 2 years ago

changkun commented 2 years ago

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{}
dmzlingyin commented 1 year ago

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

changkun commented 1 year ago

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

dmzlingyin commented 1 year ago

Cool! May I ask, when will it start?

changkun commented 1 year ago

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

dmzlingyin commented 1 year ago

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.

MarvinJWendt commented 1 year ago

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!

changkun commented 1 year ago

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?

MarvinJWendt commented 1 year ago

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 ```html Version:1.0 StartHTML:0000000105 EndHTML:0000001957 StartFragment:0000001757 EndFragment:0000001897
Hello World
```
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 Hello World
```
CSV ```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).