peerpad-core
Peerpad core API
Install
$ npm install peerpad-core --save
Import
const PeerpadBackend =
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 =
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.
backend.network
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
options
:
name
: string that uniquely identifies thistype
: string that identifies type of document. Currently supportstext
,richtext
ormath
.readKey
: string containing the read keywriteKey
: string containing the write key (optional)peerAlias
: string identifying the current author. Defaults to the IPFS peerId
Document
document.peers
Peers: document.peers.all()
Returns an array of peers:
documentpeersall// 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:
documentpeers
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:
const editor = '#editor' document
Example for CodeMirror:
const editor = CodeMirror document
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
peerpadsnapshots
document.network
document.network.observe()
Returns an Event Emitter that emits the following events:
received message
const emitter = documentnetworkemitter
sent message
const emitter = documentnetworkemitter
peer joined
const emitter = documentnetworkemitter
peer left
const emitter = documentnetworkemitter
emitter.stop()
Stop observing events. No network events get emitted after calling this.
Want to hack on Peerpad?
License
MIT