This repository was archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 881
Expand file tree
/
Copy pathheadless.js
More file actions
149 lines (121 loc) · 4.2 KB
/
headless.js
File metadata and controls
149 lines (121 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var firepad = firepad || { };
/**
* Instance of headless Firepad for use in NodeJS. Supports get/set on text/html.
*/
firepad.Headless = (function() {
var TextOperation = firepad.TextOperation;
var FirebaseAdapter = firepad.FirebaseAdapter;
var EntityManager = firepad.EntityManager;
var ParseHtml = firepad.ParseHtml;
function Headless(refOrPath) {
// Allow calling without new.
if (!(this instanceof Headless)) { return new Headless(refOrPath); }
if (typeof refOrPath === 'string') {
if (typeof Firebase !== 'function') {
var firebase = require('firebase');
} else {
var firebase = Firebase;
}
var ref = new firebase(refOrPath);
} else {
var ref = refOrPath;
}
this.entityManager_ = new EntityManager();
this.firebaseAdapter_ = new FirebaseAdapter(ref);
this.ready_ = false;
this.zombie_ = false;
}
Headless.prototype.getDocument = function(callback) {
var self = this;
if (self.ready_) {
return callback(self.firebaseAdapter_.getDocument());
}
self.firebaseAdapter_.on('ready', function() {
self.ready_ = true;
callback(self.firebaseAdapter_.getDocument());
});
}
Headless.prototype.getText = function(callback) {
if (this.zombie_) {
throw new Error('You can\'t use a firepad.Headless after calling dispose()!');
}
this.getDocument(function(doc) {
var text = doc.apply('');
// Strip out any special characters from Rich Text formatting
for (key in firepad.sentinelConstants) {
text = text.replace(new RegExp(firepad.sentinelConstants[key], 'g'), '');
}
callback(text);
});
}
Headless.prototype.setText = function(text, callback) {
if (this.zombie_) {
throw new Error('You can\'t use a firepad.Headless after calling dispose()!');
}
var op = TextOperation().insert(text);
this.sendOperationWithRetry(op, callback);
}
Headless.prototype.initializeFakeDom = function(callback) {
if (typeof document === 'object' || typeof firepad.document === 'object') {
callback();
} else {
require('jsdom').env('<head></head><body></body>', function(err, window) {
if (firepad.document) {
// Return if we've already made a jsdom to avoid making more than one
// This would be easier with promises but we want to avoid introducing
// another dependency for just headless mode.
window.close();
return callback();
}
firepad.document = window.document;
callback();
});
}
}
Headless.prototype.getHtml = function(callback) {
var self = this;
if (this.zombie_) {
throw new Error('You can\'t use a firepad.Headless after calling dispose()!');
}
self.initializeFakeDom(function() {
self.getDocument(function(doc) {
callback(firepad.SerializeHtml(doc, self.entityManager_));
});
});
}
Headless.prototype.setHtml = function(html, callback) {
var self = this;
if (this.zombie_) {
throw new Error('You can\'t use a firepad.Headless after calling dispose()!');
}
self.initializeFakeDom(function() {
var textPieces = ParseHtml(html, self.entityManager_, self.codeMirror_);
var inserts = firepad.textPiecesToInserts(true, textPieces);
var op = new TextOperation();
for (var i = 0; i < inserts.length; i++) {
op.insert(inserts[i].string, inserts[i].attributes);
}
self.sendOperationWithRetry(op, callback);
});
}
Headless.prototype.sendOperationWithRetry = function(operation, callback) {
var self = this;
self.getDocument(function(doc) {
var op = operation.clone()['delete'](doc.targetLength);
self.firebaseAdapter_.sendOperation(op, function(err, committed) {
if (committed) {
if (typeof callback !== "undefined") {
callback(null, committed);
}
} else {
self.sendOperationWithRetry(operation, callback);
}
});
});
}
Headless.prototype.dispose = function() {
this.zombie_ = true; // We've been disposed. No longer valid to do anything.
this.firebaseAdapter_.dispose();
};
return Headless;
})();