Git Product home page Git Product logo

code-fragment's Introduction

在issue中记录一些代码片段

  1. 简单实现copy
  2. 一个简单地轮播插件
  3. es5中的继承
  4. js-xlsx的使用记录
  5. 数组降维
  6. 从少数派的markdown修改过来的markdown样式,用于公众号

code-fragment's People

Contributors

feng-fu avatar

Stargazers

Yan ZHOU avatar

Watchers

James Cloos avatar  avatar

code-fragment's Issues

The solution of "regeneratorRuntime is not defined".

In webpack:

  1. add package.
npm install --save regenerator-runtime
  1. Add this package to webpack entry
entry: ["regenerator-runtime/runtime.js", "<your enter js file>"]
  1. Import this package in entry file. via app.ts
import 'regenerator-runtime/runtime'

If you are not use webpack, just ignore step 2.

view more

js-xlsx使用记录

var tableBody = [{
  'name': 'aaa',
  'age': 11,
  'sex': '男'
}]

var tableHeader = {
  'name': '姓名',
  'age': '年龄',
  'sex': '性别'
}

function s2ab(s) { // 字符串转字符流
  var buf = new ArrayBuffer(s.length)
  var view = new Uint8Array(buf)
  for (var i = 0; i !== s.length; ++i) {
    view[i] = s.charCodeAt(i) & 0xFF
  }
  return buf
}
function getCharCol (n) {
  let s = ''
  let m = 0
  while (n > 0) {
    m = n % 26 + 1
    s = String.fromCharCode(m + 64) + s
    n = (n - m) / 26
  }
  return s
}

function fileTransDownload(json, merges) {
  
  let keyMap = []
  for (let k in json[0]) {
    keyMap.push(k)
  }
  let tmpdata = [] // 用来保存转换好的json
  json.map((v, i) => keyMap.map((k, j) => Object.assign({}, {
    v: v[k],
    position: (j > 25 ? getCharCol(j) : String.fromCharCode(65 + j)) + (i + 1),
    t: 't'
  }))).reduce((prev, next) => prev.concat(next)).forEach(function (v) {
    tmpdata[v.position] = {
      v: v.v,
      t: v.t || 'e'
    }
  })
  let outputPos = Object.keys(tmpdata)  // 设置区域,比如表格从A1到D10
  let tmpWB = {
    SheetNames: ['mySheet'], // 保存的表标题
    Sheets: {
      'mySheet': Object.assign({},
        tmpdata, // 内容
        {
          '!ref': outputPos[0] + ':' + outputPos[outputPos.length - 1], // 设置填充区域
        })
    }
  }
  let tmpDown = new Blob([s2ab(XLSX.write(tmpWB,
    {bookType: 'xlsx', bookSST: false, type: 'binary'} // 这里的数据是用来定义导出的格式类型
  ))], {
    type: ''
  })  // 创建二进制对象写入转换好的字节流
  var href = URL.createObjectURL(tmpDown)  // 创建对象超链接
  return {
    tmpDown,
    href
  }
}
function download() {
  const fileName = `表格a`
  downloadExl(tableBody.concat([tableHeader]).reverse(), fileName)
}
function downloadExl(json, downName) {  // 导出到excel
  const { href, tmpDown } = fileTransDownload(json)
  outputFile.download = downName + '.xlsx'  // 下载名称
  outputFile.href = href  // 绑定a标签
  outputFile.click()  // 模拟点击实现下载
  setTimeout(function () {  // 延时释放
    URL.revokeObjectURL(tmpDown) // 用URL.revokeObjectURL()来释放这个object URL
  }, 100)
}


outputBtn.addEventListener('click', download)

es5 extend.

function Base() {}

Base.extend = function (methods, staticMethods) {
  var Base = this
  methods = methods || {}
  staticMethods = staticMethods || {}
  function S() {
    Base.call(this)
  }
  S.prototype = new Base // 原型链继承

  copy(S.prototype, methods) // 添加方法
  copy(S, Base) // 静态方法继承
  copy(S, staticMethods) // 添加静态方法
  return S
}
  // 用法
  Base.extend(methods, staticMethods)

少数派markdown.css

.markdown-here-wrapper {
	font-size: 18px;
	color: #333;
	background: #fff;
	font-family: "PingFang SC","Helvetica Neue", Helvetica, Arial, "Microsoft YaHei", "WenQuanYi Micro Hei", "tohoma,sans-serif";
	margin: 0;
	line-height: 1.6;
	background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg,rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%);
	background-size: 20px 20px;
	background-position: center center;
}

h1 {
	font-size: 2.2em;
	font-weight: 700;
	line-height: 1.1;
	padding-top: 16px;
	margin-bottom: 1em;
	text-align: center;
	color: #130f13;
}

h2,
h3,
h4,
h5,
h6 {
	line-height: 1.5em;
	margin-top: 2.2em;
	margin-bottom: 1em;
	color: #130f13;
	text-align: center;
}

h2 {
	font-size: 1.3em;
	line-height: 1.4;
}

h3 {
	font-size: 1.2em;
	line-height: 1.4;
}

h4 {
	font-size: 1.1em;
	line-height: 1.4;
}

h5,
h6 {
	font-size: .9em;
}

h5 {
	font-weight: bold;
	text-transform: uppercase;
}

h6 {
	font-weight: normal;
	color: #AAA;
}

img {
	width: 100%;
	border-radius: 5px;
	display: block;
	margin-bottom: 15px;
	height: auto;
}

dl,
ol,
ul {
	margin-top: 12px;
	margin-bottom: 20px;
	padding-left: 4%;
	line-height: 1.8;
	li
}

p {
	margin: 0 0 20px;
	padding: 0 10px;
	line-height: 1.8;
	list-style: circle;
}

a {
	color: #f22f27;
	text-decoration: none;
}

a:hover {
	color: #f55852;
	text-decoration: underline;
}

a:focus {
	outline-offset: -2px;
}

blockquote {
	font-size: .9em;
	font-style: normal;
	padding: 1em;
	margin: 0 0 15px;
	position: relative;
	line-height: 1.8;
	text-indent: 0;
	border: none;
	color: #888;
	background: rgba(224, 224, 224, .2);
	border-left: 6px solid rgba(242, 47, 39, .4);
}

strong,
dfn {
	font-weight: 700;
	color: rgb(0, 122, 170);
}

em,
dfn {
	font-style: italic;
	font-weight: 400;
}

del {
	text-decoration: line-through;
}

pre {
	margin: 0 0 10px;
	font-size: 13px;
	line-height: 1.42857;
	word-break: break-all;
	word-wrap: break-word;
	border-radius: 4px;
	white-space: pre-wrap;
	display: block;
	background: #f8f8f8;
	padding: 10px 20px;
	border: none;
	margin-bottom: 25px;
	color: #666;
	font-family: Courier, sans-serif;
}

code {
	color: #c7254e;
	background-color: #f9f2f4;
	border-radius: 4px;
	font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
	padding: 2px 4px;
	font-size: 90%;
}

p>code {
	color: #c7264e;
	background-color: #f9f2f4;
	font-size: .95em;
	border-radius: 3px;
	-moz-border-radius: 3px;
	-webkit-border-radius: 3px;
}

figure {
	margin: 1em 0;
}

figcaption {
	font-size: 0.75em;
	padding: 0.5em 2em;
	margin-bottom: 2em;
}

figure img {
	margin-bottom: 0px;
}

hr {
	margin-top: 20px;
	margin-bottom: 20px;
	border: 0;
	border-top: 1px solid #eee;
}

ol p,
ul p {
	margin-bottom: 0px;
}

li {
	margin-bottom: 0.75em;
	margin-top: 0.75em;
}

ol#footnotes {
	font-size: 0.95em;
	padding-top: 1em;
	margin-top: 1em;
	margin-left: 0;
	border-top: 1px solid #eaeaea;
	counter-reset: footer-counter;
	list-style: none;
	color: #555;
	padding-left: 5%;
	margin: 20px 0;
}

ol#footnotes li {
	margin-bottom: 10px;
	margin-left: 16px;
	font-weight: 400;
	line-height: 2;
	list-style-type: none;
}

ol#footnotes li:before {
	content: counter(footer-counter) ". ";
	counter-increment: footer-counter;
	font-weight: 800;
	font-size: .95em;
}

@keyframes highfade {
	0% {
		background-color: none;
	}

	20% {
		background-color: yellow;
	}

	100% {
		background-color: none;
	}
}

@-webkit-keyframes highfade {
	0% {
		background-color: none;
	}

	20% {
		background-color: yellow;
	}

	100% {
		background-color: none;
	}
}

a:target,
ol#footnotes li:target,
sup a:target {
	animation-name: highfade;
	animation-duration: 2s;
	animation-iteration-count: 1;
	animation-timing-function: ease-in-out;
	-webkit-animation-name: highfade;
	-webkit-animation-duration: 2s;
	-webkit-animation-iteration-count: 1;
	-webkit-animation-timing-function: ease-in-out;
}

a:target {
	border: 0;
	outline: 0;
}

Carousel

function Carousel(obj) {
  this.element = obj.element;
  this.singleInstance = obj.instance;
  this.speed = obj.speed || 2000;
  this.childNode = this.element.querySelectorAll(obj.childNode || 'li');
  this.count = this.childNode.length;
  this.playPreTimer = null;
  this.playNextTimer = null;
  this.direction = obj.direction == 'left' ? 'left' : 'top';
}
Carousel.prototype.init = function () {
  var first = this.childNode[0];
  var last = this.childNode[this.count - 1];
  this.first = first.cloneNode(true);
  this.last = last.cloneNode(true);
  this.element.appendChild(this.first);
  this.element.insertBefore(this.last, first);
  this.element.style[this.direction] = -this.singleInstance + 'px';
  this.element.transition = 'all .5s';
};
Carousel.prototype.autoPlayNext = function () {
  var _this = this;
  this.playNextTimer = setInterval(function () {
      _this.playNext();
  }, this.speed);
};
Carousel.prototype.autoPlayPre = function () {
  var _this = this;
  this.playPreTimer = setInterval(function () {
      _this.playPre();
  }, this.speed);
};
Carousel.prototype.clearAutoPlay = function () {
  clearInterval(this.playNextTimer);
  clearInterval(this.playNextTimer);
  this.playNextTimer = null;
  this.playPreTimer = null;
};
Carousel.prototype.playNext = function () {
  var _this = this;
  var instance = parseInt(this.element.style[this.direction] || 0);
  var newInstnace = instance - this.singleInstance;
  this.element.style[this.direction] = newInstnace + 'px';
  if (newInstnace + this.singleInstance * (this.count + 1) <= 0) {
      setTimeout(function () {
          _this.element.style.transition = 'height .5s';
          _this.element.style[_this.direction] = -_this.singleInstance + 'px';
          setTimeout(function () {
              _this.element.style.transition = 'all .5s';
          }, 200);
      }, 500);
  }
};
Carousel.prototype.playNextOneDirection = function () {
  var _this = this;
  var instance = parseInt(this.element.style[this.direction] || 0);
  var newInstnace = instance - this.singleInstance;
  if(newInstnace + this.singleInstance * (this.count - 9) <= 0) return
  this.element.style[this.direction] = newInstnace + 'px';
};
Carousel.prototype.playPreOneDirection = function () {
  var _this = this;
  var instance = parseInt(this.element.style[this.direction] || 0);
  var newInstnace = instance + this.singleInstance;
  if (newInstnace >= this.singleInstance) return
  this.element.style[this.direction] = newInstnace + 'px';
};
Carousel.prototype.playPre = function () {
  var _this = this;
  var instance = parseInt(this.element.style[this.direction] || 0);
  var newInstnace = instance + this.singleInstance;
  this.element.style[this.direction] = newInstnace + 'px';
  if (newInstnace >= 0) {
      setTimeout(function () {
          _this.element.style.transition = 'height .5s';
          _this.element.style[_this.direction] = -_this.singleInstance * _this.count + 'px';
          setTimeout(function () {
              _this.element.style.transition = 'all .5s';
          }, 200);
      }, 500);
  }
};
Carousel.prototype.delete = function () { 
  var _this = this;
  _this.element.removeChild(this.first)
  _this.element.removeChild(this.last)
}

数组降维

var deepFlatten = (arr) => {
  var result = []
  var stack = arr
  while(stack.length) {
    var last = stack.pop()
    if (Array.isArray(last)) {
      stack = stack.concat(last)
    } else {
      result.unshift(last)
    }
  }
  return result
}

copy code.

function(element) {
  var textarea = document.createElement('textarea')
  textarea.textContent = document.querySelector(element).innerText
  textarea.style.opacity = 0
  textarea.style.top = 0
  textarea.style.left = 0
  textarea.style.position = 'fixed'
  document.body.appendChild(textarea)
  textarea.select()
  document.execCommand('copy')
  document.body.removeChild(textarea)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.