头图

The last article introduced the magical use of Clipboard that I learned from Typora: When we copy text from a web page, two formats of data will be inserted into the clipboard: plain text and rich text. When we paste into other editors, the text editor will use plain text data, and the rich text editor will use rich text data (the corresponding text will be extracted into MarkDown format in Typora).

The specific content and simple code implementation can be viewed: of Clipboard I learned from Typora.md 1619f428ecd2d6

Picture processing method in Typora

People who use Typora frequently should know that Typora also has an excellent experience in processing pictures. When inserting a picture, you can specify the picture storage method:

Typora中处理图片上传方式

When we insert a picture, the upload service will automatically upload the picture in the clipboard or the dragged picture and replace it with the network address. This way, when you send a document to others or publish a document to a blog, you will not be unable to view the document because it is locally.

本地文件自动上传并替换为在线地址

I also wrote a small tool : 1619f428ecd40a file-uploader-cli , which can process files and upload them to the corresponding storage service, such as github , ftp , object storage, etc. You can visit the github address to view the specific configuration method (uPic and Picgo also Support Typora).

For a custom upload service, when an image is inserted, Typora will call the upload service and pass in the file path, and the upload service can print the upload result on the console.

Read the picture data in the clipboard

We still perform a quick test in the console, open any webpage, find any tag in the element panel and add the contenteditable attribute, the current tag becomes an editable element.

<p contenteditable>
  xxxx
</p>

Execute code in the console:

document.addEventListener('paste', function(e){
    console.log(e)
    const items = e.clipboardData.items;
    console.log(items)
    const fileList = e.clipboardData.files
    console.log(fileList)
    for(let i=0;i< items.length;i++){
        console.log(items[i])
        let f = items[i].getAsFile()
        console.log(f)
    }
    e.preventDefault();
})
local files

Copy the local file (can be a picture or other format file), and perform the paste operation in the label:

复制本地文件

As can be seen from the above figure, there are two ways to retrieve image data from clipboardData:

  • e.clipboardData.items, is a DataTransferItemList type data, each element is a DataTransferItem type, you can get the File object through DataTransferItem.getAsFile()
  • e.clipboardData.files is a FileList, each element is a File
DataTransferItem.getAsFile can get the data only when the local file is pasted, otherwise it is null.
Web picture

When the copied data is a network picture, there are two situations:

  • Only include web images
  • Contains network pictures and text

When copying a network picture (right-click to copy the picture from a webpage), the clipboard will get two kinds of data: text/html and image/png . For details, please refer to: The magical use of Clipboard I learned from Typora.md

只复制网络图片

When including network pictures and text, the clipboard will get: text/plain and text/html .
包含网络图片和文字

So for the two ways of including network pictures, the data obtained in the paste event is also different:

只包含图片

包含图片和文本

Get picture files

From the above information, we can understand: Only when the pasted is a local image or network image, we can get the image object through DataTransferItem.getAsFile; when it contains text, we can get the plain text and rich text data through DataTransferItem.getAsString.

How Typora handles image data
Paste the picture into Typora

If it is a local file, Typora will save it according to the picture settings in the preferences:

  • When "Upload Service" is selected, the local image address will be passed to the custom image upload service. If the upload is successful, it will be replaced with the network address; if the upload fails, the local image will be kept referenced.
  • If another configuration is selected, the picture will be copied to the corresponding configuration folder.

If it is a non-local picture in the clipboard (such as a screenshot copy), it will be saved as a local file before uploading or copying.

If it is rich text data, it will be rendered directly ( not checked to apply the above rules to pictures in the network location).

Drag and drop the picture to Typora

Typora supports automatic upload of paste images, and also supports drag-and-drop image upload, we can get the File object through Drop Event

document.addEventListener('drop', function(e){
    console.log(e)
    const items = e.dataTransfer.items;
    console.log(items)
    const fileList = e.dataTransfer.files
    console.log(fileList)
    e.preventDefault();
})

The drag data is stored in DragEvent.dataTransfer:

拖拽插入图片

chaos-fe


一颗小行星
56 声望5 粉丝

@混沌前端


引用和评论

0 条评论