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

@threema/compose-area

v0.4.6

Published

A compose area with support for Emoji, written with Rust + Webassembly.

Downloads

2,801

Readme

compose-area

CircleCI License

A compose area with support for Emoji, written with Rust + Webassembly.

Demo: https://threema-ch.github.io/compose-area/

Concepts

This project provides a simple text editor with support for media content (e.g. emoji), implemented on top of a content editable div.

The input handling is done entirely by the browser. The library should be notified every time the caret position is changed, so it can update its internal state. It provides methods to insert text, images or other block elements. Selection and caret position are handled automatically.

Package on npmjs.com

This project is published to npmjs.com:

https://www.npmjs.com/package/@threema/compose-area

The published package contains files for two different wasm-pack build targets:

  • The root directory contains files for the wasm-pack bundler target. You will need a bundler like webpack in order to use the library this way.
  • In the web subdirectory (i.e. node_modules/@threema/compose-area/web/) you will find files built for the wasm-pack web target.

Setup

Note: A dependency graph that contains any wasm must all be imported asynchronously. This can be done using dynamic imports.

Bootstrapping JS

The simplest way is to use a bootstrapping js as the entry point to your entire application:

// bootstrap.js
import('./index.js')
  .catch(e => console.error('Error importing `index.js`:', e));
// index.js
import * as ca from '@threema/compose-area';

Dynamic Import (Promise)

Alternatively, import the library asynchronously:

import('@threema/compose-area')
    .then((ca) => {
        // Use the library
    });

If you're in an asynchronous context, you can also use the await keyword.

const ca = await import('@threema/compose-area');

Usage

Initialization

This library requires a wrapper element with white-space set to pre or pre-wrap in order to work properly.

<div id="wrapper" style="white-space: pre-wrap;"></div>

First, bind to the wrapper element:

const area = ca.ComposeArea.bind_to(document.getElementById('wrapper'));

Because the insertion should work even when there is no selection / focus inside the compose area, the library needs to know about all selection change events. Register them using an event listener:

document.addEventListener('selectionchange', (e) => {
    area.store_selection_range();
});

Inserting

Now you can start typing inside the compose area. It behaves like a regular content editable div.

To insert text or images through code, use the following two functions:

//                src          alt   class
area.insert_image("emoji.jpg", "😀", "emoji");

//               text
area.insert_text("hello");

You can also insert HTML or a DOM node directly:

area.insert_html("<div></div>");
area.insert_node(document.createElement("span"));

(Note: Due to browser limitations, inserting a node directly will not result in a new entry in the browser's internal undo stack. This means that the node insertion cannot be undone using Ctrl+Z. If you need that, use insert_html instead.)

The insert_image method returns a reference to the inserted element, so that you can set custom attributes on it.

const img = area.insert_image(...);
img.draggable = false;
img.ondragstart = (e) => e.preventDefault();

If you want to properly handle pasting of formatted text, intercept the paste event:

wrapper.addEventListener('paste', (e) => {
    e.preventDefault();
    const clipboardData = e.clipboardData.getData('text/plain');
    if (clipboardData) {
        area.insert_text(clipboardData);
    }
});

Extracting Text

To extract the text from the area, there's also a method:

area.get_text();

By default, leading and trailing white-space is trimmed from the text. To disable this, pass true to the get_text method.

area.get_text(true /* no_trim */);

Other helpers

To test whether the compose area is empty:

area.is_empty();

By default, if the compose area contains purely white-space, this method still considers the compose area to be empty. If you want a compose area containing white-space to be treated as non-empty, pass true to the is_empty method.

area.is_empty(true /* no_trim */);

To focus the compose area programmatically:

area.focus();

To clear the contents of the compose area:

area.clear();

Dev Setup

cargo install wasm-pack

Building

# Debug build
wasm-pack build

# Release build
wasm-pack build --release -- --no-default-features

Running the testproject

# Setup npm
cd www
npm install

# Run server
npm run start

Testing

# Unit tests
cargo test

# Browser tests (headless)
wasm-pack test --headless --firefox
# ...or if you want to filter tests by name
wasm-pack test --headless --firefox . -- <filter>

# Selenium tests (test server must be started)
cd selenium
npm test firefox

Linting

# Setup
rustup component add clippy

# Run linting checks
cargo clean && cargo clippy --all-targets --all-features

License

Licensed under either of

  • Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.