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

quill-audio-embed

v2.1.0

Published

Embed and play audio in quill editor.

Downloads

21

Readme

Introduction

A Quill module for embedding audio files that you can play! You can also validate and upload files!

Image

How to Use

Install the package: npm install quill-audio-embed

Script

You can import the script tag from the node_modules/quill-audio-embed/dist/quill-audio-embed.umd.cjs

<head>
  ...
    <script type="text/javascript" src="/dist/umd/index.umd.cjs"></script>
  ...
</head>


<script>
  // Pass in the Quill constructor
  QuillAudioEmbed.default.setQuillConstructor(Quill);

  // Register the module
  Quill.register('modules/audioEmbed', QuillAudioEmbed.default);

  const quill = new Quill('#editor', {
    modules: {
      toolbar: [
        [{ header: [1, 2, false] }],
        ['bold', 'italic', 'underline'],
        ['image', 'code-block'],
      ],
      audioEmbed: {
        alignment: 'left' // Alignment of the play button
      }
    },
    placeholder: 'Compose an epic...',
    theme: 'snow',
  });

quill.on('text-change', () => {
  console.log(quill.getSemanticHTML());
});
</script>

Module

You can also import it as a module

import Quill from "quill";
import QuilAudioEmbed, { setQuillConstructor } from "quill-audio-embed";

setQuillConstructor(Quill);
Quill.register('modules/audioEmbed', QuillAudioEmbed);
  const quill = new Quill('#editor', {
    modules: {
      toolbar: [
        [{ header: [1, 2, false] }],
        ['bold', 'italic', 'underline'],
        ['image', 'code-block'],
      ],
      audioEmbed: {
        alignment: 'left' // Alignment of the play button
      }
    },
    placeholder: 'Compose an epic...',
    theme: 'snow',
});

ReactQuill

import React from 'react';
import ReactQuill, { Quill } from 'react-quill';
import 'react-quill/dist/quill.snow.css';
import QuillAudioEmbed from "quill-audio-embed";

QuillAudioEmbed.setQuillConstructor(Quill);
Quill.register('modules/audioEmbed', QuillAudioEmbed);

function QuillEditor() {

  return (
    <ReactQuill theme="snow" modules={{
      audioEmbed: {
        alignment: 'left' // Setting default alignment
      }
    }}/>
  );
}

export default QuillEditor;

CommonJs

The common js library is located inside quill-audio-embed/dist/lib.

File Uploading

You can also upload files right from the module and embed them into quill editor. You can read all the types here.

onLoad: Executed once the quill-audio-embed is successfully rendered into the browser.

validate: Executed after a file upload and before onUpload. Here you can write you validation logic. You must return a boolean for success (true) and failure (false) case. You can access all the input fields of the quill-audio-embed using the AudioInputs or through the IQuillAudioEmbed object.


const quill = new Quill('#editor', {
    modules: {
      toolbar: [ ... ],
      audioEmbed: {
        validate: (inputs, audioEmbed) => {
          // Check if file is larger than 5 mb
          const file = inputs.file.files[0];

          if ((file / (1024 * 1024)) > 5) {
            // Write your own error message displaying logic
            document.querySelector('#upload-error').innerHTML = "An error occured!";
            return false;
          }

          if (!file.type.split('/').includes('image')) {
            // Write your own error message displaying logic
            document.querySelector('#upload-error').innerHTML = "An error occured!";
            return false;
          }


          return true;
        },
        },
        alignment: 'left' // Default alignment of the play button
      }
    },
    placeholder: 'Compose an epic...',
    theme: 'snow',
  });

onUpload: Executed after successful validation, here you can write your logic for uploading files. The function must return an AudioBlotValue in case of a successful run or a false in case of failure. In the return value is AudioBlotValue then, it will automatically be injected at the selection area or at the top of the quill content.

const quill = new Quill('#editor', {
  modules: {
    toolbar: [ ... ],
    audioEmbed: {
      validate: (inputs, audioEmbed) => {
        ...
        return true;
      },
      onUpload: async (inputs, audioEmbed) => {
        const file = inputs.file.files[0];  // Getting file from the inputs

        // Uploading file to an api
        const res = await fetch('/api/file', {
          method: 'POST',
          body: file
        });
        const json = await res.json();

        if (!json.sucesss) return false;
        
        return {
          label: json.data.name,
          url: json.data.url,
          alignment: 'right'
        }
      },
      alignment: 'left' // Default alignment of the play button
    }
  },
  placeholder: 'Compose an epic...',
  theme: 'snow',
});

onDelete: Executed when you try to delete an audio embed. You can add your own deletion logic for uploaded files. Return true in case of success and the quill-audio-embed will automatically remove the embed from the editor. Return false in case of failure and embed will not be removed.



const quill = new Quill('#editor', {
    modules: {
      toolbar: [
        [{ header: [1, 2, false] }],
        ['bold', 'italic', 'underline'],
        ['image', 'code-block'],
      ],
      audioEmbed: {
        onUpload: async (inputs, audioEmbed) => {
          // File uploading
        },
        onDelete: async (data, audioEmbed) => {

          // Doing a delete request to external api
          const res = await fetch(`https://www.example.com/api/file?id=${data.id}`, {
            method: 'DELETE',
          });

          const json = await res.json();

          if (json.success) {
            // File successfully deleted
            return true;
          }

          // Failed to delete the file
          return false;
        },
        alignment: 'left' // Default alignment of the play button
      }
    },
    placeholder: 'Compose an epic...',
    theme: 'snow',
  });

Types


interface IQuillAudioEmbed  {
  quill: any;
  options: Options;
  audioButton: HTMLDivElement;
  container: HTMLDivElement;
  popup: HTMLDivElement;
  inputs: AudioInputs;
  open: boolean = false;
}

type Options = {
  onLoad?: (audioEmbed: IQuillAudioEmbed) => any,
  validate?: (inputs: AudioInputs, audioEmbed: IQuillAudioEmbed) => Promise<boolean>,
  onUpload?: (inputs: AudioInputs, audioEmbed: IQuillAudioEmbed) => Promise<AudioBlotValue|false>,
  onDelete?: (data: AudioBlotValue, audioEmbed: IQuillAudioEmbed) => Promise<boolean>,
  alignment?: 'center' | 'left' | 'right'
};

type AudioInputs = {
  label: HTMLInputElement, 
  url: HTMLInputElement,
  alignment: HTMLSelectElement,
  file: HTMLInputElement
}

type AudioBlotValue = { id?: string, url: string, label?: string, alignment?: string };

Contribute

Contribution can help a long way ✌️