Git Product home page Git Product logo

google / transmat Goto Github PK

View Code? Open in Web Editor NEW
499.0 11.0 25.0 396 KB

Share data beyond the browser boundaries. Enable users to transfer data to external apps, and open your webapp to receive external data.

Home Page: https://google.github.io/transmat/

License: Apache License 2.0

JavaScript 6.98% HTML 4.70% TypeScript 85.50% CSS 2.81%
drag-drop datatransfer copy-paste json-ld mime-types

transmat's Introduction

Transmat

Share data beyond the boundaries of the browser.

Build status

By utilizing the DataTransfer API capabilities to transfer data to applications, Transmat is enabling your web app to interact beyond the boundaries of the browser. This technique is compatible all modern desktop browsers since IE11.

Transmat will help you setting up the necessarily drag-drop and copy-paste interactions, and help you in transferring (both transmitting and receiving) the data.

This is not an officially supported Google product.

Transmitting data

The DataTransfer payload consists of simple string-based key value pairs. When providing mime-types keys and their expected data, new integrations can happen, sometimes for free.

import {Transmat, addListeners} from 'transmat';

addListeners(myElement, 'transmit', event => {
  const transmat = new Transmat(event);
  transmat.setData({
    'text/plain': 'This will show up in text fields',
    'text/html': '<img src="https://example.com/test.jpg" />',
    'text/uri-list': 'https://example.com/foobar',
    'application/x.my-custom-type': {foo: 'bar'},
  });
});

When transferring the example from above, you can expect the following to happen depending on the receiving target;

  • WYSIWYG inputs like contentEditable, Google Docs, Gmail, Apple Pages will use text/html data, and fallback to text/plain.
  • Text inputs like textareas, VSCode, will show text/plain.
  • Browsers will navigate the URLs listed in the text/uri-list data.

Receiving data

You can receive the DataTransfer payload by listening to the drop and paste events.

import {Transmat, addReceiveListeners} from 'transmat';

addListeners(myElement, 'receive', event => {
  const myCustomMimeType = 'application/x.my-custom-type';
  const transmat = new Transmat(event);
  if(transmat.hasType(myCustomMimeType) && transmat.accept()) {
    const dataString = transmat.getData(myCustomMimeType);
    const data = JSON.parse(dataString);
    console.log(data);
  }
});

Connecting the web, and beyond

The project's vision is to connect the web. By promoting the use of JSON-LD data transfers, applications will be able to speak the same language independently from their implementation. With growing adoption, users will be able to transfer data without friction to any other applications, across the web. How cool is that?

import {Person} from 'schema-dts';
import {Transmat} from 'transmat';
import * as jsonLd from 'transmat/lib/json_ld';

// When transmitting...
const transmat = new Transmat(event);
transmat.setData(jsonLd.MIME_TYPE, jsonLd.fromObject<Person>({
  "@type": "Person",
  "name": "Rory Gilmore",
  "image": "https://example.com/rory.jpg",
  "address": {
    "@type": "PostalAddress",
    "addressCountry": "USA",
    "addressRegion": "Connecticut",
    "addressLocality": "Stars Hollow"
  },
}));

// .. and when receiving
if (transmat.hasType(jsonLd.MIME_TYPE)) {
  const person = jsonLd.getData<Person>(jsonLd.MIME_TYPE);
}

Observing transfer events

You can make use of the included TransmatObserver class to respond to drag activity. Use this to for example highlight valid drop areas.

import {TransmatObserver, Transmat} from 'transmat';

const obs = new TransmatObserver(entries => {
  for (const entry of entries) {
    const transmat = new Transmat(entry.event);
    if(transmat.hasMimeType(myCustomMimeType)) {
      entry.target.classList.toggle('drag-over', entry.isTarget);
      entry.target.classList.toggle('drag-active', entry.isActive);
    }
  }
});
obs.observe(myElement);

Some concerns

Discoverability

Drag-drop and copy-paste are native interactions, with roots back to the first GUIs about 40 years ago. On the desktop, these are common operations that users will do often. Think about organizing files, dragging files to tools to open them, etc. On the web, this is uncommon.

We will need to educate users about this new capability, and ideally come up with UX patterns to make this recognizable.

Accessibility

Drag-drop is not a very accessible interaction, but the DataTransfer API works with copy-paste too.

Security and privacy

The data stored in the DataTransfer will be available to any application on the user's device. Keep this in mind when transferring sensitive data. Web applications can only read the data payload on drop. While dragging, only the keys (mime-types) are exposed to the webpage that is being dragged over.

Receiving data should be treated like any other user input; sanitize and validate before using.

Known quirks

  • Chrome strips newlines in text/uri-list. For now, you can only use this to transfer a single URI. https://crbug.com/239745
  • Transferring files generated using new File() doesn't seem to be supported well enough.
  • Web applications can only read the payload (using getData) when finishing the transfer by dropping or pasting. While dragging, only the types can be read.
  • The DataTransfer data keys are transformed to lowercase.
  • Mobile devices have poor support. iOS ignores DataTransfer, Chrome on Android only supports text/plain.

Contact

Use GitHub issues for filing bugs. Follow @jorikdelaporik on Twitter for occasional updates around the project.

transmat's People

Contributors

jtangelder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

transmat's Issues

Browser compatibility

Goal of this issue is to list browser compatibility issues that have been discovered and should be looked into in more detail.

  • It looks like Chrome 89 on Android 11 only supports text/plain. Other mime-type keys will be dropped, even the primitives like text/html, text/uri-list.

Add a minimal drag image

Add a function to replace the default drag image with a small and minimal image that is still recognizable as a drag ghost. This to provide a way to integrate Transmat on top of existing drag and drop implementations.

Published npm package files are in /dist

The published npm package only imports the index file from dist, the others need the dist folder in the path.

Current behavior:

import {Transmat} from 'transmat';
import * as jsonLd from 'transmat/dist/json_ld';

Expected behavior:

import {Transmat} from 'transmat';
import * as jsonLd from 'transmat/json_ld';

The files should move to the package root when publishing.

Add builds for the browser

It's a shame that you can't even use Transmat from a CDN like UNPKG. I don't understand why you may even this for node because Transmat is a frontend library.

It may be very good indeed to have UMD builds.

(I would have personally done it and committed it, but Google libraries always have weird dependencies and I don't own a supercomputer yet)

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.