-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollaborative_editor.js
171 lines (142 loc) · 5.44 KB
/
collaborative_editor.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"use strict";
const AceRange = ace.require('ace/range').Range;
class CollaborativeEditor extends React.Component {
constructor(props) {
super(props);
this.name = 'editor';
this.id = Math.random();
this.client = null;
this.editor = null;
this.skipChange = false;
}
createOTClient(revision) {
this.client = new ot.Client(revision);
window.client = this.client;
bus('cursors').to_fetch = (key) => {
const connections = bus.fetch('/connections').all || [];
const cursors = connections
.filter(el => el.cursor !== undefined && el.id != this.id)
.map(el => el.cursor);
return {
all: cursors,
}
}
bus.fetch('/ops', (ops) => {
ops.all = ops.all || [];
ops.ids = ops.ids || [];
for (let i = this.client.revision; i < ops.all.length; i++) {
const operation = ot.TextOperation.fromJSON(ops.all[i]);
if (ops.ids[i] == this.client.currentId) {
this.client.serverAck();
} else {
this.client.applyServer(operation);
}
}
});
this.client.applyOperation = (operation) => {
let currentIndex = 0;
const doc = this.editor.getSession().getDocument();
for (const op of operation.ops) {
if (ot.TextOperation.isRetain(op)) {
currentIndex += op;
} else if (ot.TextOperation.isInsert(op)) {
this.skipChange = true;
doc.insert(doc.indexToPosition(currentIndex), op);
this.skipChange = false;
currentIndex += op.length;
} else {
const start = doc.indexToPosition(currentIndex);
const end = doc.indexToPosition(currentIndex - op); // op is negative
const range = new AceRange(start.row, start.column, end.row, end.column);
this.skipChange = true;
doc.remove(range);
this.skipChange = false;
}
}
};
this.client.sendOperation = (revision, operation) => {
this.client.currentId = Math.random().toString(36).substring(7);
bus.save({
key: '/op',
revision: revision,
operation: operation,
id: this.client.currentId,
});
};
}
createEditor(text) {
this.editor = ace.edit(this.name);
window.editor = this.editor;
this.editor.setValue(text, 1);
this.editor.getSession().setMode("ace/mode/javascript");
this.editor.setTheme("ace/theme/twilight");
this.editor.focus();
this.editor.on('change', (changeObj) => {
if (this.skipChange) {
return;
}
const totalLength = this.editor.getValue().length;
const doc = this.editor.getSession().getDocument();
const startPosition = doc.positionToIndex(changeObj.start);
const diff = changeObj.lines.join('\n');
const operation = new ot.TextOperation().retain(startPosition);
if (changeObj.action == 'insert') {
operation.insert(diff).retain(totalLength - (startPosition + diff.length));
} else if (changeObj.action == 'remove') {
operation.delete(diff).retain(totalLength - startPosition);
} else {
throw Error(`Unexpected action ${changeObj.action}`);
}
this.client.applyClient(operation);
});
this.editor.on('changeSelection', () => {
const doc = this.editor.getSession().getDocument();
bus.save({
key: '/connection',
id: this.id,
cursor: doc.positionToIndex(this.editor.getCursorPosition()),
});
});
bus.fetch('cursors', (cursors) => {
const session = this.editor.getSession();
const doc = session.getDocument();
for (const markerid in session.getMarkers(true)) {
session.removeMarker(markerid);
}
for (const cursorPos of cursors.all) {
const cursorRowCol = doc.indexToPosition(cursorPos);
const nextChar = {
row: cursorRowCol.row,
column: cursorRowCol.column + 1,
}
const cursorRange = new AceRange(
cursorRowCol.row, cursorRowCol.column,
cursorRowCol.row, cursorRowCol.column + 1);
session.addMarker(cursorRange, 'cursor green-cursor', 'text', true);
}
});
}
componentDidMount() {
const handlr = (document) => {
if (document.text === undefined) {
return;
}
this.createOTClient(document.revision);
this.createEditor(document.text);
bus.forget('/document', handlr);
};
bus.fetch('/document', handlr);
}
render() {
return React.DOM.div({
id: this.name,
style: {
position: 'absolute',
top: 0,
bottom: 0,
right: 0,
left: 0,
},
});
}
}