首页 > 其他分享 >在输入框里直接粘贴图片

在输入框里直接粘贴图片

时间:2023-12-07 14:56:09浏览次数:33  
标签:insert const target url 输入框 file before 粘贴 图片

有很多 Web 编辑器支持直接复制粘贴图片,记录一下这种效果是怎么实现的

拿到粘贴板上的 image file 对象

document.querySelector('textarea').addEventListener('paste', e => {
  const file = Array.from(e.clipboardData.items)
    .find(v => v.type.includes('image'))
    ?.getAsFile()
})

paste 事件的 ClipboardEvent 对象上可以拿到。这里得到的 file 和 <input type="file" /> 上传选择文件得到的 file 对象一样。

拿到 file 之后就可以做其他操作如上传到服务器,得到 url 链接等

上传图片

通常需要将拿到的图片文件上传到图床或自己的图片服务器上。

function uploadFile(file) {
  return new Promise(resolve => {
    setTimeout(() => resolve(URL.createObjectURL(file)), 1000)
  })
}
const url = await uploadFile(file)

插回输入框

将上传得到的 image url 插入到输入框光标位置

const insert = `![](${url})` // img markdown 语法
const { target } = e
const before = target.value.substr(0, target.selectionStart)
const after = target.value.substr(target.selectionEnd)
target.value = before + insert + after
const cursorPosition = (before + insert).length
target.setSelectionRange(cursorPosition, cursorPosition) // 设置光标位置

完整代码如下:

document.querySelector('textarea').addEventListener('paste', async e => {
  // 拿到粘贴板上的 image file 对象
  const file = Array.from(e.clipboardData.items)
    .find(v => v.type.includes('image'))
    ?.getAsFile()
  // 上传到图床服务器拿到 image web url
  const url = await uploadFile(file)

  // 将 url 插入到输入框光标处
  const insert = `![](${url})` // img markdown 语法
  const { target } = e
  const before = target.value.substr(0, target.selectionStart)
  const after = target.value.substr(target.selectionEnd)
  target.value = before + insert + after
  const cursorPosition = (before + insert).length
  target.setSelectionRange(cursorPosition, cursorPosition)
})
function uploadFile(file) {
  return new Promise(resolve => {
    setTimeout(() => resolve(URL.createObjectURL(file)), 1000)
  })
}

你可以在这里查看运行示例:
https://code.juejin.cn/pen/7309685835798937610

觉得不错,点点小赞:)

标签:insert,const,target,url,输入框,file,before,粘贴,图片
From: https://www.cnblogs.com/iovec/p/17881991.html

相关文章

  • Eclipse复制(Ctrl+C)粘贴(Ctrl+V)会卡顿
    问题场景:使用Eclipse复制(Ctrl+C)粘贴(Ctrl+V)会卡顿问题原因:与Eclispe的超链接快捷键Ctrl冲突导致,在使用快捷键Ctrl+C或者Ctrl+V复制粘贴时,会触发超链接功能,然后Eclipse会先去执行超链接的工作,最后再执行我们想要的复制粘贴,导致复制粘贴时的卡顿。解决方案:将超链接快捷键从Ctrl......
  • python多线程下载图片
    urls.json文件数据格式如下["https://example.com/image1.jpg","https://example.com/image2.jpg","https://example.com/image3.jpg"] 代码如下importjsonimportrequestsimportthreadingdefdownload_image(url):respo......
  • nginx配置(图片跨域问题)
    events{worker_connections1024;}http{log_formatmain'$remote_addr-$remote_user[$time_local]"$request"''$status$body_bytes_sent"$http_referer"'&#......
  • asp.net如何生成图片验证码
    新建一个页面image.aspx,添加命名空间:usingSystem.Drawing.Imaging;usingSystem.IO;然后在Page_load事件拷入如下代码: stringtmp=RndNum(4); HttpCookiea=newHttpCookie("ImageV",tmp);  Response.Cookies.Add(a); this.ValidateCode(tmp);接下来向该页面......
  • word同一行带题注的两张图片生成的图表目录在同一行的解决办法
    本文转载自:https://blog.csdn.net/Liangontheway/article/details/120316467每张图片通过题注来编号的,同一行的两张图片生成图目录就会在同一行,这也太不美观了。解决办法一:将图片放到表格里,然后隐藏表格边框就好了再次生成目录问题解决了!!!解决办法二是插入图文框,这个操作起......
  • VUE 输入框实现光标插入,设置光标位置并删除光标内容
    参考https://blog.csdn.net/m0_62333895/article/details/127648295自己写的例子constcursorPosition=ref("");consthandleFocus=(event:any)=>{cursorPosition.value=event.srcElement.selectionStart;console.log("cursorPosition.value......
  • 【VUE】vue动态变换背景图片的实现 +背景图片铺满+ 一般路由的配置
    一、动态变换背景图片的实现代码如下:<template><divclass="body"v-loading="loading":style="{backgroundImage:'url('+bgi+')'}"></div></template><script>data(){reyurn{ b......
  • 使用ThinkPHP框架根据Excel内容批量处理图片名称详解记录
    ThinkPHP依赖以下环境Nginx+PHP,建议提前装好Composer,PHP、Composer需要设置好系统环境变量。1.通过Composer安装Laravel框架composercreate-projecttopthink/thinkthinkphp6启动服务测试cdthinkphp6phpthinkrun然后就可以在浏览器中访问http://localhost:8000如果不能显示......
  • 想在同一张图片上添加不同的文字,也就是一张图片上出现一个词
    大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Python实战的问题,一起来看看吧。问题描述:上图中也是他的代码,没有文字的代码确实看着难受,而且还是手机拍出来的模糊照片,不是截图。其实他自己也发现了问题,但是不知道怎么修改。二、实现过程这里【......
  • # yyds干货盘点 # 想在同一张图片上添加不同的文字,也就是一张图片上出现一个词
    大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Python实战的问题,一起来看看吧。问题描述:上图中也是他的代码,没有文字的代码确实看着难受,而且还是手机拍出来的模糊照片,不是截图。其实他自己也发现了问题,但是不知道怎么修改。二、实现过程这里【巭孬......