-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.js
259 lines (206 loc) · 7.02 KB
/
main.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* global define, brackets, window */
define(function (require, exports, module) {
var AppInit = brackets.getModule('utils/AppInit')
var EditorManager = brackets.getModule('editor/EditorManager')
var ProjectManager = brackets.getModule('project/ProjectManager')
var ExtensionUtils = brackets.getModule('utils/ExtensionUtils')
var PreferencesManager = brackets.getModule('preferences/PreferencesManager')
var prefs = PreferencesManager.getExtensionPrefs('multihack-brackets')
var DEFAULT_HOSTNAME = 'https://quiet-shelf-57463.herokuapp.com'
var DEFAULT_ROOM = '$%#NONE/RANDOM#%$'
var DEFAULT_REMOTE_SELECTION_COLOR = '#3f5d38'
var DEFAULT_REMOTE_CARET_COLOR = '#7fb971'
var RemoteManager = require('./lib/npm/multihack-core')
var EditorWrapper = require('./lib/editor')
var FileSystemWrapper = require('./lib/filesystem')
var UI = require('./lib/ui')(prefs)
var Voice = require('./lib/voice')
var remote = null
var isSyncing = false
var isInCall = false
ExtensionUtils.loadStyleSheet(module, 'widget/css/main.css')
UI.injectButton()
UI.on('voiceToggle', handleVoiceToggle)
UI.on('stop', handleStop)
var button = document.querySelector('#edc-multihack-btn')
button.addEventListener('click', function () {
console.log('click')
if (isSyncing) {
UI.openModal(isInCall, remote)
} else {
handleStart()
}
})
var inited = false // Brackets sometimes tries to init multiple times
function init () {
if (inited) return
inited = true
setupPreferences()
setupEventListeners()
}
function setupPreferences () {
prefs.definePreference('hostname', 'string', DEFAULT_HOSTNAME)
prefs.definePreference('lastRoom', 'string', DEFAULT_ROOM)
prefs.definePreference('remoteSelectionColor', 'string', DEFAULT_REMOTE_SELECTION_COLOR)
prefs.definePreference('remoteCaretColor', 'string', DEFAULT_REMOTE_CARET_COLOR)
prefs.save()
ExtensionUtils.addEmbeddedStyleSheet(
'.remoteSelection { background-color: ' + prefs.get('remoteSelectionColor')+'; }' +
'.remoteCaret { background-color: ' + prefs.get('remoteCaretColor') + '; }'
)
}
function setupEventListeners () {
FileSystemWrapper.on('createFile', handleLocalCreateFile)
FileSystemWrapper.on('renameFile', handleLocalRenameFile)
FileSystemWrapper.on('deleteFile', handleLocalDeleteFile)
EditorWrapper.on('changeFile', handleLocalChangeFile)
EditorWrapper.on('changeSelection', handleLocalSelection)
EditorWrapper.on('alert', handleAlert)
ProjectManager.on('projectOpen', handleStop) // Stop sync on project open
}
function handleAlert (alert) {
UI.showAlert(button, alert)
}
function handleVoiceToggle () {
if (isInCall) {
handleVoiceLeave()
} else {
handleVoiceJoin()
}
}
function handleVoiceJoin () {
if (!remote.voice) return
remote.voice.join()
}
function handleVoiceLeave () {
if (!remote.voice) return
remote.voice.leave()
isInCall = false
button.className = 'active'
}
function handleStart () {
console.log('handleStart')
UI.getRoomAndNickname(function (room, nickname) {
console.log(room, nickname)
if (!room) return
remote = new RemoteManager({
hostname: prefs.get('hostname'),
room: room,
nickname: nickname,
wrtc: null,
voice: Voice
})
remote.posFromIndex = function (filePath, index, cb) {
EditorWrapper.posFromIndex(fromWebPath(filePath), index, cb)
}
// setup remote listeners
remote.once('voice', function () {
console.log('voice')
remote.voice.on('join', function () {
console.log('voice join')
button.className = 'active voice'
isInCall = true
})
})
remote.once('ready', function () {
console.log('yjs ready')
FileSystemWrapper.getProject(function (filePath, content, status) {
UI.showSyncProgress(button, status)
handleLocalCreateFile(toWebPath(filePath), content)
})
})
remote.on('changeFile', handleRemoteChangeFile)
remote.on('changeSelection', handleRemoteSelection)
remote.on('createFile', handleRemoteCreateFile)
remote.on('createDir', handleRemoteCreateDir)
remote.on('deleteFile', handleRemoteDeleteFile)
remote.on('lostPeer', handleLostPeer)
remote.on('gotPeer', handleGotPeer)
isSyncing = true
button.className = 'active'
EditorWrapper.setupListeners()
FileSystemWrapper.setupListeners()
console.log('MH started')
})
}
function handleStop () {
isSyncing = false
button.className = ''
if (remote) {
console.log(remote)
remote.destroy()
remote = null
}
EditorWrapper.removeListeners()
FileSystemWrapper.removeListeners()
console.log('MH stopped')
}
/* Local Listeners */
function handleLocalSelection (filePath, selection) {
console.log('local selection')
remote.changeSelection({
filePath: toWebPath(filePath),
change: selection
})
}
function handleLocalChangeFile (filePath, change) {
console.log('local change')
console.log(filePath, change)
remote.changeFile(toWebPath(filePath), change)
}
function handleLocalCreateFile (filePath, content) {
console.log('local create file')
// destroy any useless save messages
remote.createFile(toWebPath(filePath), content)
}
function handleLocalCreateDir (path) {
console.log('local create dir')
// TODO remote.createDir(path)
}
function handleLocalRenameFile (oldPath, newPath) {
console.log('local rename file')
// TODO remote.renameFile(toWebPath(filePath), toWebPath(newPath))
}
function handleLocalDeleteFile (filePath) {
console.log('local delete file')
remote.deleteFile(toWebPath(filePath))
}
/* Remote listeners */
function handleRemoteChangeFile (data) {
console.log('remote change', data)
EditorWrapper.change(fromWebPath(data.filePath), data.change)
}
function handleRemoteSelection (selections) {
console.log('remote seleciton')
EditorWrapper.highlight(selections)
}
function handleRemoteCreateFile (data) {
console.log('remote create file')
EditorWrapper.createFile(fromWebPath(data.filePath), data.content)
}
function handleRemoteCreateDir (data) {
console.log('remote create dir')
EditorWrapper.createDirectory(fromWebPath(data.filePath))
}
function handleRemoteDeleteFile (data) {
console.log('remote delete file')
EditorWrapper.deleteFile(fromWebPath(data.filePath))
}
function handleLostPeer (peer) {
if (!isSyncing) return
UI.showLostConnection(button, peer.metadata.nickname)
}
function handleGotPeer (peer) {
if (!isSyncing) return
// TODO
}
/* Utilities to convert path formats */
function toWebPath (path) {
return path[0] === '/' ? path : '/' + path
}
function fromWebPath (path) {
return path[0] === '/' ? path.slice(1) : path
}
function noop () {}
AppInit.appReady(init)
})