Skip to content

Latest commit

Β 

History

History
255 lines (165 loc) Β· 5.02 KB

README.md

File metadata and controls

255 lines (165 loc) Β· 5.02 KB

peerpad-core

Not being used, DEPRECATED IN FAVOR OF peer-base

made by Protocol Labs Freenode

Peerpad core API

Install

$ npm install peerpad-core --save

Import

const PeerpadBackend = require('peerpad-core')

PeerpadBackend.generateRandomName()

Returns a random pad name (string).

PeerpadBackend.generateRandomKeys()

Generates a set of read and write random keys.

Returns a promise that resolves to:

{
  "read": "base58-encoded string",
  "write": "base58-encoded string",
}

PeerpadBackend(options)

Creates a Peerpad backend.

const backend = PeerpadBackend(options)

Options:

  • ipfs: IPFS (version 0.27 or higher) node that is already created (optional). If not passed in, one will be created for you.

PeerpadBackend

backend.ipfs

The IPFS node.

async backend.start()

Starts the backend. Returns a promise that resolves once the backend has started.

async backend.stop()

Stops the backend.

Network: backend.network

backend.network.hasStarted()

Returns a boolean, true if the IPFS node has started.

backend.network.once('started', fn)

Emitted once the IPFS node starts. If you passed in an IPFS node that is already started (via options.ipfs), this event doesn't get emitted.

backend.createDocument(options)

const options = {
  name: 'name of the pad',
  type: 'richtext',
  readKey: 'gobelegook',
  writeKey: 'moregobelegook'
}

const document = backend.createDocument(options)

options:

  • name: string that uniquely identifies this
  • type: string that identifies type of document. Currently supports text, richtext or math.
  • readKey: string containing the read key
  • writeKey: string containing the write key (optional)
  • peerAlias: string identifying the current author. Defaults to the IPFS peerId

Document

Peers: document.peers

document.peers.all()

Returns an array of peers:

document.peers.all()
// returns:
[
  {
    id: 'QmHashHash1',
    permissions: {
      admin: false,
      write: true,
      read: true
    }
  },
  {
    id: 'QmHashHash2',
    permissions: {
      admin: false,
      write: false,
      read: true
    }
  }
]

document.peers.on('change', fn)

Emitted when there is a change in the peer list:

document.peers.on('change', () => {
  console.log('peers changed and now are', peerpad.peers.all())
})

document.setPeerAlias(peerAlias)

Sets the current peer alias. peerAlias must be a string.

document.bindEditor(editor)

Bind CodeMirror editor (for pad of type markdown or text) or Quill editor (for pad of type richtext).

Two-way bind to a editor. Example for Quill:

import Quill from 'quill'

const editor = new Quill('#editor')

document.bindEditor(editor)

Example for CodeMirror:

import Codemirror from 'codemirror'

const editor = CodeMirror.fromTextArea(myTextArea)

document.bindEditor(editor)

document.unbindEditor(editor)

Unbinds editor.

document.bindTitle(element)

Bind the document title to an editing element (like a textarea or a text input field).

document.unbindTitle(element)

Unbind the document title from an editing element.

async document.convertMarkdown(markdown, type)

Converts markdown to HTML.

document.on('change', fn)

Emitted when the document changes. fn is called with the arguments:

  • peer (a Peer object)
  • operation (object of type Operation, see further down)

document.snapshots

document.snapshots.take()

Returns a promise

peerpad.snapshots.take().then((hash) => {
  console.log('snapshot hash: ', hash)
})

document.network

document.network.observe()

Returns an Event Emitter that emits the following events:

received message

const emitter = document.network.observe()
emitter.on('received message', (fromPeer, message) => {
  console.log('received message from %s: %j', fromPeer, message)
})

sent message

const emitter = document.network.observe()
emitter.on('sent message', (toPeer, message) => {
  console.log('sent message to %s: %j', toPeer, message)
})

peer joined

const emitter = document.network.observe()
emitter.on('peer joined', (peer) => {
  console.log('peer %s joined room', peer)
})

peer left

const emitter = document.network.observe()
emitter.on('peer left', (peer) => {
  console.log('peer %s left room', peer)
})

emitter.stop()

Stop observing events. No network events get emitted after calling this.

Want to hack on Peerpad?

License

MIT