Skip to content

Instantly share code, notes, and snippets.

POST https://firestore.googleapis.com/v1beta1/projects/firestore-fun/databases/(default)/documents:commit?key={YOUR_API_KEY}
{
"writes": [
{
"update": {
"name": "projects/firestore-fun/databases/(default)/documents/foo/bar",
"fields": {
"some-field": {
"stringValue": "some-value"
@mikelehen
mikelehen / generate-pushid.js
Created February 11, 2015 17:34
JavaScript code for generating Firebase Push IDs
/**
* Fancy ID generator that creates 20-character string identifiers with the following properties:
*
* 1. They're based on timestamp so that they sort *after* any existing ids.
* 2. They contain 72-bits of random data after the timestamp so that IDs won't collide with other clients' IDs.
* 3. They sort *lexicographically* (so the timestamp is converted to characters that will sort properly).
* 4. They're monotonically increasing. Even if you generate more than one in the same timestamp, the
* latter ones will sort after the former ones. We do this by using the previous random bits
* but "incrementing" them by 1 (only in the case of a timestamp collision).
*/
codeMirror.setOption('onDragEvent', function(cm, e) {
// Move the cursor as they drag.
var pos = codeMirror.coordsChar({left: e.x, top: e.y });
codeMirror.setCursor(pos);
codeMirror.focus();
var isImageDrop = e.type == 'drop' && e.dataTransfer.files && e.dataTransfer.files.length > 0 && e.dataTransfer.files[0].type && e.dataTransfer.files[0].type.indexOf('image/') > -1;
if (!isImageDrop) return;
event.preventDefault();
@mikelehen
mikelehen / presence-example.js
Created January 10, 2014 20:51
Per-session presence bits.
var presenceRef = new Firebase('https://mike.firebaseio.com/users/michael/presence');
presenceRef.root().child('.info/connected').on('value', function(s) {
if (s.val() === true) {
// we're connected. Set up presence with a per-session presence bit.
var ref = presenceRef.push();
ref.onDisconnect().remove();
ref.set(true);
}
});
@mikelehen
mikelehen / firepad-node.js
Created December 16, 2013 22:43
Curtesy of Clément (https://github.com/iclems), a node script that instantiates firepad in order to extract the text contents.
var jsdom = require('jsdom');
var fs = require('fs');
var Firepad = {};
Firepad.load = function(ref, callback) {
jsdom.env('<head></head><body><div id="firepad"></div></body>', function (errors, window) {
var document = document || window.document;
@mikelehen
mikelehen / firepad-insert-image.js
Created October 23, 2013 19:05
Inserting an image into firepad.
firepad.insertEntity('img', {
'src' : 'http://cdn.dashburst.com/wp-content/uploads/2013/01/Grumpy-Cat.jpg',
});
@mikelehen
mikelehen / auto-expand-firepad.css
Created September 28, 2013 23:15
Styles to make firepad auto-expand to fit its content.
.firepad {
height: auto;
}
.firepad-toolbar {
position: relative;
margin-top: 20px;
margin-left: 10px;
top: auto;
left: auto;
@mikelehen
mikelehen / entity-modification.js
Created September 28, 2013 23:05
An example of registering an 'img' entity that modifies itself on click or shift-click (see onclick handler).
firepad.registerEntity('img', {
render: function(info, handle) {
var attrs = ['src', 'alt', 'width', 'height', 'style', 'class'];
var html = '<img ';
for(var i = 0; i < attrs.length; i++) {
var attr = attrs[i];
if (attr in info) {
html += ' ' + attr + '="' + info[attr] + '"';
}
}
@mikelehen
mikelehen / firepad-drag-drop.js
Created August 29, 2013 00:30
Simple drag/drop of images into Firepad... probably not production-ready, but seems to work.
codeMirror.setOption('onDragEvent', function(cm, e) {
// Move the cursor as they drag.
var pos = codeMirror.coordsChar({left: e.x, top: e.y });
codeMirror.setCursor(pos);
codeMirror.focus();
var isImageDrop = e.type == 'drop' && e.dataTransfer.files && e.dataTransfer.files.length > 0 && e.dataTransfer.files[0].type && e.dataTransfer.files[0].type.indexOf('image/') > -1;
if (!isImageDrop) return;
event.preventDefault();
@mikelehen
mikelehen / .vimrc
Created July 29, 2013 00:04
My vimrc
set autoindent ignorecase noerrorbells ruler hlsearch sw=4 sts=4 expandtab
set textwidth=79 nowrapscan formatoptions-=t incsearch tabstop=4
set scrolloff=2 norestorescreen history=100 shortmess=ao nobackup
set matchtime=1 showmatch smartcase wildmenu wildmode=longest:full
syntax on
map Q gq
map Y y$
behave xterm