前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Atom插件开发-使用自己的Chevereto图床API项目简介功能分析代码实现项目地址

Atom插件开发-使用自己的Chevereto图床API项目简介功能分析代码实现项目地址

作者头像
gwk_iOS
发布2018-08-23 10:53:43
1.1K0
发布2018-08-23 10:53:43
举报
文章被收录于专栏:coding...coding...

最近一直在用 Atom 写写markdown,但无奈上传图片真实太麻烦了。找了好几个插件都是要用到 七牛 的账号,由于之前被七牛坑过一次,就没再想用他的打算了。一度放弃后使用在线的markdown,例如简书 掘金 这些,可感觉用起来还是不太舒服,最终还是折腾起atom插件了。

正好之前有建了个图床站还顺带出了个iOS的App 米米图床 (我是奸商我收费)

也写过一篇相关的文章 iOS开发-RAC+MVVM练手项目 图床App

项目简介

由于自己有图床 目前使用的是 Chevereto, 本文所涉及到内容都是根据 此API文档

所以这款插件只为解决几个问题

  • 使用自己图床的API
  • 上传图片获得URL

功能分析

功能灰常简单

  • 获取剪切板图片数据
  • 通过post上传至图床API获得回调数据
  • 生成markdown图片内容

image

根据API文档 我们用 Postman 测一下API 看看回调

image

嗯 回调的内容很多,我们就挑一个display_url这个字段吧,够用就行。

代码实现

用的是coffeescript代码

第三方依赖 request

image

代码语言:javascript
复制
module.exports =
  # setting 中的全局变量
  config:
    Api:
      title: "Your api url"
      description: "Input your api url like `https://xxx.com/api/1/upload`"
      type: 'string'
      default: "https://imgurl.xyz/api/1/upload"
    ApiKey:
      title: "ApiKey"
      description: "Input your api key, you can find it in your dashboard, default is my key ,Do not mark sure always available"
      type: 'string'
      default: "a7bb2b091e3f2961e59551a8cf6e05b2"

  activate: (state) ->
    @attachEvent()

  attachEvent: ->
    workspaceElement = atom.views.getView(atom.workspace)
    workspaceElement.addEventListener 'keydown', (e) =>
      # cmd + paste
      if (e.metaKey && e.keyCode == 86)
        # clipboard readImage
        clipboard = require('clipboard')
        img = clipboard.readImage()
        return if img.isEmpty()

        clipboard.writeText('')

        # insert loading text
        editor = atom.workspace.getActiveTextEditor()
        range = editor.insertText('uploading...');

        @postToImgur img, (imgUrl) ->
          # replace loading text to markdown img format
          markdown = "![](#{imgUrl})"
          editor.setTextInBufferRange(range[0], markdown)

  postToImgur: (img, callback) ->

    req = require('request')
    options = {
      uri: atom.config.get('image-copy-chevereto.Api')
      formData: {
        action : 'upload'
        key : atom.config.get('image-copy-chevereto.ApiKey')
        source : img.toPNG().toString('base64')
      }
      json: true
    }
    req.post options, (error, response, body) ->
      if (!error && response.statusCode == 200)
        callback(body.image.display_url) if callback && body.image.display_url
      else
        callback('error ')

项目地址

插件中默认使用的apiurl和key都是自己的 不保证会完全变,有条件的建议还是使用自己的apiurl和key 还有。。。暂不支持gif

插件安装:直接Atom perference->install 中搜索 image-copy-chevereto即可

插件地址:https://atom.io/packages/image-copy-chevereto

gayhub地址: https://github.com/gongxiaokai/image-copy-chevereto

欢迎点赞~

image

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.04.28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 项目简介
  • 功能分析
  • 代码实现
  • 项目地址
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档