Git Product home page Git Product logo

megadraft-uploadable-image-plugin's Introduction

Megadraft Upload Plugin - Megadraft Plugin

Usage

Include the plugin in the plugins prop of your Megadraft instance.

import React from 'react';
import ReactDOM from 'react-dom';
import { MegadraftEditor } from 'megadraft';

import plugin from 'megadraft-uploadable-image-plugin';

class Example extends React.Component {
  /**
   * The success responses are expected to contain a `src` property
   * and the errors are generic JS errors. `new Error('an error message')`
   *
   * An example uploading to imgur
   */
  handleUpload(fileList) {
    const [file] = fileList;
    return new Promise((resolve, reject) => {
      const xhr = new XMLHttpRequest();
      xhr.open('POST', 'https://api.imgur.com/3/image');
      xhr.setRequestHeader(
        'Authorization',
        `Client-ID ${process.env.REACT_APP_IMGUR_CLIENT_ID}`
      );
      const data = new FormData();
      data.append('image', file);
      xhr.send(data);
      xhr.addEventListener('load', () => {
        const response = JSON.parse(xhr.responseText);
        if (response.success) {
          resolve({ src: response.data.link });
        } else {
          reject(new Error(response.data.error));
        }
      });
    });
  }

  render() {
    return (
      <MegadraftEditor
        plugins={[plugin({ handleUpload: this.handleUpload })]}
      />
    );
  }
}

ReactDOM.render(<Example />, document.getElementById('container'));

Contributing

Install, run, test.

# Install npm dependencies
make setup

# Gulp dev-server task with webpack + sass running on http://localhost:8080/
make run

# Run mocha tests + eslint
make test

If you're constantly running tests, there's a faster alternative using mocha's watch feature:

make watch_unit

Releasing

There's a prepublish script entry on package.json that runs build tasks before publishing the package.

npm publish

Third Party

The sample plugin uses the extension icon from https://design.google.com/icons/ under Apache License Version 2.0.

megadraft-uploadable-image-plugin's People

Contributors

kevinsperrine avatar

Watchers

 avatar James Cloos avatar

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.