forked from josephg/ShareJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etherpad.coffee
107 lines (92 loc) · 3.18 KB
/
etherpad.coffee
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
# This is the type for etherpad changesets
# The snapshot has a JSON structure of
# {
# "text" - the text of the pad
# "attribs" - attributes
# "pool" - the attribute pool
# }
# The Changesets have the structure
# {
# "changeset" - serialized version of the changeset
# "pool" - the pool
# }
if WEB?
if window.ShareJS? && window.ShareJS.Changeset?
Changeset = window.ShareJS.Changeset
AttributePool = window.ShareJS.AttributePool
else
console.log("Etherpad library not found. Make sure to include Attributepool.js and Changeset.js in your javascript sourcecode");
else
Changeset = require("./../lib-etherpad/Changeset");
AttributePool = require("./../lib-etherpad/AttributePool");
etherpad =
Changeset : Changeset
AttributePool : AttributePool
etherpad.name = "etherpad"
etherpad.create = ->
"text" : "",
"attribs" : Changeset.makeAttribution(""),
"pool" : new AttributePool()
etherpad.tryDeserializeSnapshot = (snapshot) ->
if snapshot.pool.clone
return snapshot
snapshot.pool = new AttributePool().fromJsonable(snapshot.pool)
return snapshot;
etherpad.tryDeserializeOp = (op) ->
if op.pool.clone
return op;
if op.pool.numToAttrib
op.pool = new AttributePool().fromJsonable(op.pool)
else
op.pool = new AttributePool().fromJsonable(JSON.parse(op.pool))
return op
etherpad.apply = (snapshot, op) ->
snapshot = etherpad.tryDeserializeSnapshot(snapshot)
op = etherpad.tryDeserializeOp(op)
result = {}
result.pool = snapshot.pool.clone();
newCS = Changeset.moveOpsToNewPool(op.changeset, op.pool, result.pool);
result.text = Changeset.applyToText(newCS, snapshot.text);
result.attribs = Changeset.applyToAttribution(newCS, snapshot.attribs, result.pool);
return result
etherpad.transform = (op1, op2, side) ->
op1 = etherpad.tryDeserializeOp(op1)
op2 = etherpad.tryDeserializeOp(op2)
result = {}
# join the operation pools into a new one
newPool = op1.pool.clone();
# newPool will hold the combined pool
# op2cs will hold the rewritten op2 cs
op2cs = Changeset.moveOpsToNewPool(op2.changeset, op2.pool, newPool);
result.changeset = Changeset.follow(op1.changeset, op2cs, side=="right", newPool);
result.pool = newPool
return result
etherpad.compose = (op1, op2) ->
op1 = etherpad.tryDeserializeOp(op1)
op2 = etherpad.tryDeserializeOp(op2)
result = {}
# join the operation pools into a new one
newPool = op1.pool.clone();
# newPool will hold the combined pool
# op2cs will hold the rewritten op2 cs
op2cs = Changeset.moveOpsToNewPool(op2.changeset, op2.pool, newPool);
result.changeset = Changeset.compose(op1.changeset, op2cs, newPool);
result.pool = newPool
return result
etherpad.serialize = (snapshot) ->
text = snapshot.text
attribs = snapshot.attribs
pool = snapshot.pool.toJsonable()
etherpad.serializeOp = (snapshot) ->
changeset = snapshot.changeset
pool = JSON.stringify(snapshot.pool)
etherpad.deserialize = (obj) ->
text = obj.text
attribs = obj.attribs
pool = new AttributePool().fromJsonable(obj.pool)
if WEB?
exports.types ||= {}
# [] is used to prevent closure from renaming types.text
exports.types.etherpad = etherpad
else
module.exports = etherpad