Skip to content

Instantly share code, notes, and snippets.

@corwin-of-amber
Created August 9, 2019 19:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corwin-of-amber/12671bc8b7ca34f69b25d40d1d6a7d3d to your computer and use it in GitHub Desktop.
Save corwin-of-amber/12671bc8b7ca34f69b25d40d1d6a7d3d to your computer and use it in GitHub Desktop.
A minimal "null-modem" Firepad adapter
const {EntityManager, RichTextCodeMirror, EditorClient, FirebaseAdapter, utils,
RichTextCodeMirrorAdapter} = Firepad.firepad;
var upstream = [];
class NullAdapter {
constructor(userId, userColor) {
this.userId_ = userId;
this.color_ = userColor;
}
sendOperation(operation, callback) {
console.log('operation', operation, callback);
upstream.push({ a: this.userId_, o: operation.toJSON() });
setTimeout(() => {
this.trigger('ack');
if (this.other)
this.other.trigger('operation', operation);
}, 0);
}
sendCursor(obj) {
console.log('cursor', obj, this.userId_, this.color_);
setTimeout(() => {
if (this.other)
this.other.trigger('cursor', this.userId_, obj, this.color_);
});
}
registerCallbacks(callbacks) {
for (var eventType in callbacks) {
this.on(eventType, callbacks[eventType]);
}
}
}
utils.makeEventEmitter(NullAdapter, ['ready', 'cursor', 'operation', 'ack', 'retry']);
function createClient(cm, userId) {
var entityManager_ = new EntityManager();
var firebaseAdapter_ = new NullAdapter(userId, '#9999FF');
var richTextCodeMirror_ = new RichTextCodeMirror(cm, entityManager_,
{ cssPrefix: 'firepad-' }),
editorAdapter_ = new RichTextCodeMirrorAdapter(richTextCodeMirror_);
var client_ = new EditorClient(firebaseAdapter_, editorAdapter_);
return client_;
}
$(() => {
var div = $('<div>').appendTo('body');
var cm1 = new CodeMirror(div[0], {lineNumbers: true});
firepad1 = createClient(cm1, 'user1');
var cm2 = new CodeMirror(div[0], {lineNumbers: true});
firepad2 = createClient(cm2, 'user2');
firepad1.serverAdapter.other = firepad2.serverAdapter;
firepad2.serverAdapter.other = firepad1.serverAdapter;
Object.assign(window, {cm1, cm2, firepad1, firepad2}); // for debugging
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment