|
| 1 | +var ReconnectingWebSocket = require('reconnecting-websocket'); |
| 2 | +var sharedb = require('sharedb/lib/client'); |
| 3 | +var richText = require('./rich-text'); |
| 4 | +var Quill = require('quill'); |
| 5 | +var QuillCursors = require('quill-cursors'); |
| 6 | +var tinycolor = require('tinycolor2'); |
| 7 | + |
| 8 | +sharedb.types.register(richText.type); |
| 9 | +Quill.register('modules/cursors', QuillCursors); |
| 10 | + |
| 11 | +var clients = []; |
| 12 | +var colors = {}; |
| 13 | + |
| 14 | +var collection = 'examples'; |
| 15 | +var id = 'richtext'; |
| 16 | + |
| 17 | +var editorContainer = document.querySelector('.editor-container'); |
| 18 | +document.querySelector('#add-client').addEventListener('click', function() { |
| 19 | + addClient(); |
| 20 | +}); |
| 21 | + |
| 22 | +addClient(); |
| 23 | +addClient(); |
| 24 | + |
| 25 | +function addClient() { |
| 26 | + var socket = new ReconnectingWebSocket('ws://' + window.location.host); |
| 27 | + var connection = new sharedb.Connection(socket); |
| 28 | + var doc = connection.get(collection, id); |
| 29 | + doc.subscribe(function(err) { |
| 30 | + if (err) throw err; |
| 31 | + var quill = initialiseQuill(doc); |
| 32 | + var color = '#' + tinycolor.random().toHex(); |
| 33 | + var id = 'client-' + (clients.length + 1); |
| 34 | + colors[id] = color; |
| 35 | + |
| 36 | + clients.push({ |
| 37 | + quill: quill, |
| 38 | + doc: doc, |
| 39 | + color: color |
| 40 | + }); |
| 41 | + |
| 42 | + document.querySelector('#' + id + ' h1').style.color = color; |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +function initialiseQuill(doc) { |
| 47 | + var quill = new Quill(quillContainer(), { |
| 48 | + theme: 'bubble', |
| 49 | + modules: { |
| 50 | + cursors: true |
| 51 | + } |
| 52 | + }); |
| 53 | + var cursors = quill.getModule('cursors'); |
| 54 | + var index = clients.length; |
| 55 | + |
| 56 | + quill.setContents(doc.data); |
| 57 | + |
| 58 | + quill.on('text-change', function(delta, oldDelta, source) { |
| 59 | + if (source !== 'user') return; |
| 60 | + doc.submitOp(delta); |
| 61 | + }); |
| 62 | + |
| 63 | + doc.on('op', function(op, source) { |
| 64 | + if (source) return; |
| 65 | + quill.updateContents(op); |
| 66 | + }); |
| 67 | + |
| 68 | + var presence = doc.connection.getDocPresence(collection, id); |
| 69 | + presence.subscribe(function(error) { |
| 70 | + if (error) throw error; |
| 71 | + }); |
| 72 | + var localPresence = presence.create('client-' + (index + 1)); |
| 73 | + |
| 74 | + quill.on('selection-change', function(range) { |
| 75 | + // Ignore blurring, so that we can see lots of users in the |
| 76 | + // same window |
| 77 | + if (!range) return; |
| 78 | + localPresence.submit(range, function(error) { |
| 79 | + if (error) throw error; |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + presence.on('receive', function(id, range) { |
| 84 | + cursors.createCursor(id, id, colors[id]); |
| 85 | + cursors.moveCursor(id, range); |
| 86 | + }); |
| 87 | + |
| 88 | + return quill; |
| 89 | +} |
| 90 | + |
| 91 | +function quillContainer() { |
| 92 | + var wrapper = document.createElement('div'); |
| 93 | + wrapper.classList.add('editor'); |
| 94 | + var index = clients.length; |
| 95 | + wrapper.id = 'client-' + (index + 1); |
| 96 | + |
| 97 | + wrapper.innerHTML = |
| 98 | + ' <h1>Client' + (index + 1) + '</h1>' + |
| 99 | + ' <button class="remove-client">Remove</button>' + |
| 100 | + ' <button class="client-connection connected">Disconnect</button>' + |
| 101 | + ' <div class="quill"></div>'; |
| 102 | + |
| 103 | + wrapper.querySelector('.remove-client').addEventListener('click', function() { |
| 104 | + removeClient(clients[index]); |
| 105 | + }); |
| 106 | + |
| 107 | + var connectionButton = wrapper.querySelector('.client-connection'); |
| 108 | + connectionButton.addEventListener('click', function() { |
| 109 | + toggleConnection(connectionButton, clients[index]); |
| 110 | + }); |
| 111 | + |
| 112 | + editorContainer.appendChild(wrapper); |
| 113 | + return wrapper.querySelector('.quill'); |
| 114 | +} |
| 115 | + |
| 116 | +function toggleConnection(button, client) { |
| 117 | + if (button.classList.contains('connected')) { |
| 118 | + button.classList.remove('connected'); |
| 119 | + button.textContent = 'Connect'; |
| 120 | + disconnectClient(client); |
| 121 | + } else { |
| 122 | + button.classList.add('connected'); |
| 123 | + button.textContent = 'Disconnect'; |
| 124 | + connectClient(client); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +function disconnectClient(client) { |
| 129 | + client.doc.connection.close(); |
| 130 | +} |
| 131 | + |
| 132 | +function connectClient(client) { |
| 133 | + var socket = new ReconnectingWebSocket('ws://' + window.location.host); |
| 134 | + client.doc.connection.bindToSocket(socket); |
| 135 | +} |
| 136 | + |
| 137 | +function removeClient(client) { |
| 138 | + client.quill.root.parentElement.parentElement.remove(); |
| 139 | + client.doc.destroy(function(error) { |
| 140 | + if (error) throw error; |
| 141 | + }); |
| 142 | +} |
0 commit comments