npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ctrv-upload

v1.0.3

Published

粘贴上传组件

Downloads

9

Readme

背景

最近要做一个ctrl+V 上传图片的组件。简单说就是,使用截屏工具截一张图,然后在某个元素上按ctrl+v操作,需要将截的图上床至服务端,服务端保存图片并返回对应的地址;所以封装了组件做这事,发布出来记录下。

ctrv-upload

手动截屏后,鼠标移动至某元素,按ctrlv进行图片上传。

兼容性说明

由于代码中使用到clipboardData对象及paste事件,目前只能兼容到高版本Chrome,FireFox及IE 11。

实现原理及过程

原理:

  • 对于webkit内核,input、textarea、contentEditable属性为true的元素可响应paste事件,可通过事件对象中的clipboardData对象属性访问粘贴板数据。
  • 对于FireFox及IE 11,在contentEditable属性为true的元素上进行ctrl+v操作时,会在目标元素中插入img元素,img元素的src属性即为截图的base64数据。
  • 获取到图片的base64数据后提交给后台程序php,可在服务端保存为图片。

实现过程

  1. 在目标元素前插入contentEditable属性为true的div元素,用于响应paste事件;
  2. 在目标元素上做ctrl+v操作时,响应paste事件,通过event.clipboardData对象或者获取在目标元素中插入的img元素得到图片的base64数据。
  3. ajax上传图片base64数据至服务端。

安装

npm install ctrv-upload

使用

支持AMD与CommonJs的方式加载模块,

直接引入:<script src="xxx/ctrv-upload/index.js">

AMD: define(['xxx/ctrv-upload/index'],function(ctrlVUtil){});

COMMONJS: var ctrlVUtil = require("ctrv-upload");

调用



var load1 = document.querySelector(".js-upload");

// 实例化即可
new ctrlVUtil({
    uploadUrl: "server.php",
    targetElement: load1,
	isCompleteImg:false,
    data:{
        name:"alanzhang"
    },
    success:function(data){
        alert("上传成功");
        console.log(data);
    },
	error: function(error){
		alert("上传失败");
	}
});

参数说明

| 名称 | 参数 | 默认值 | | :--: | :--- | :--: | | uploadUrl | 上传地址 | 需自定义 | | targetElement | 鼠标放在该元素上时,可响应ctrl+v操作 | document.querySelector(".js-upload") | | isCompleteImg | 上传图片base64的格式,false表示只上传content部分,即不包括头信息data:image/jpg;base64,true表示上传完整的base64字符串 |false | | data |需要上传的其他参数,json对象 | 空 | | success | 上传成功时的回调函数,其参数为接口返回的json对象 | - | | error | 上传成功时的回调函数,其参数为接口返回的json对象 | - |

API

ctrlVUtil(config)

构造函数,参数:

  • config: 上传图片的相关参数,json对象,具体字段见参数说明。

ctrlVUtil#alertMsg(content)

静态方法,用于弹出异常信息的提示,默认使用window.alert(content)弹出提示;提供此方法用于使用者自定义弹出提示的样式。

  • content:组件给出的异常提示信息

开发

  1. 源码位于src/index.js,开发完成后执行webpack打包,生成根目录下的index.js。
  2. 实例项目examples中ctrlVMain.js中引用了根目录下的index.js文件,在运行测试文件时也许进行gulp打包操作。

版本记录

1.0.3 2021-12-14

  • 修正readme

1.0.2 2016-08-22

  • 修正readme

1.0.1 2016-08-22

  • 修正readme

1.0.0 2016-08-22

  • 初始化版本,完成代码及文档

参考文档