Skip to content

Instantly share code, notes, and snippets.

View dmcgrath's full-sized avatar

Dan McGrath dmcgrath

View GitHub Profile
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ComputasBrann Notes</title>
<!-- update the version number as needed -->
<script defer src="/__/firebase/4.8.0/firebase.js"></script>
<!-- include only the Firebase features as you need -->
service cloud.firestore {
match /databases/{database}/documents {
match /notes/{doc} {
allow list: if false;
allow get: if true;
allow create: if true;
allow delete: if true;
allow update: if true;
}
}
@dmcgrath
dmcgrath / gist:d233237c887b474ae7277cd4633b707d
Created December 15, 2017 02:46
Cloud Firestore JavaScript
// https://stackoverflow.com/a/2117523
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(
/[018]/g,
c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
document.addEventListener('DOMContentLoaded', function() {
// // 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
// https://stackoverflow.com/a/2117523
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(
/[018]/g,
c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
html{
background: #CCC;
font-family: Monaco, monospace;
font-size: 14px;
color: #444;
line-height: 1.4;
max-width: 60rem;
margin: 0 auto;
height: 100%;
}
@dmcgrath
dmcgrath / gist:a7796731c6a9553bf2dbf825b0460a20
Created December 15, 2017 03:05
Initialize Cloud Firestore
noteId = window.location.hash; // was h
project = "computasbrann-example";
// f = firebase
firebase.initializeApp({
authDomain:project+".firebaseapp.com",
projectId:project
});
var dbClient=null;
var uuid = uuidv4();
noteElement = document.getElementById("note");
noteDocument = dbClient.collection("notes").doc(noteId);
noteElement.oninput = function(){
noteDocument.set({
owner: uuid,
text: this.innerHTML
});
};
noteDocument.set({owner:0}, {merge: true});
noteDocument.onSnapshot(function(documentSnapshot){
noteData = documentSnapshot.data();
if(noteData.owner !== uuid ) {
noteElement.innerHTML = noteData.text || '';
}
});
@dmcgrath
dmcgrath / gist:edaad606007d461c9850689e16c87763
Created December 15, 2017 03:19
Full initial Cloud Firestore page
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ComputasBrann Notes</title>
<!-- update the version number as needed -->
<script defer src="/__/firebase/4.8.0/firebase.js"></script>
<!-- include only the Firebase features as you need -->
var user = null;
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().useDeviceLanguage();
firebase.auth().signInWithPopup(provider).then(function(result) {
user = result.user;
// All the Firestore code with 'enablePersistence()' goes here