aboutsummaryrefslogtreecommitdiff
path: root/xP/public/xP.js
blob: 4eebb7a17b1656db38f72f3adfbab8f382e46355 (plain)
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
// TODO: Probably reset state on disconnect, and indicate to user.
let socket = new WebSocket(proxy)

let commandSeq = 0
function send(command) {
	socket.send(JSON.stringify({commandSeq: ++commandSeq, data: command}))
}

socket.onopen = function(event) {
	send({command: 'Hello', version: 1})
}

let buffers = new Map()
let bufferCurrent = undefined

socket.onmessage = function(event) {
	console.log(event.data)

	let e = JSON.parse(event.data).data
	switch (e.event) {
	case 'BufferUpdate':
	{
		let b = buffers.get(e.bufferName)
		if (b === undefined) {
			b = {lines: []}
			buffers.set(e.bufferName, b)
		}
		// TODO: Update any buffer properties.
		break
	}
	case 'BufferRename':
		buffers.set(e.new, buffers.get(e.bufferName))
		buffers.delete(e.bufferName)
		break
	case 'BufferRemove':
		buffers.delete(e.bufferName)
		break
	case 'BufferActivate':
		bufferCurrent = e.bufferName
		// TODO: Somehow scroll to the end of it immediately.
		// TODO: Focus the textarea.
		break
	case 'BufferLine':
	{
		let b = buffers.get(e.bufferName)
		if (b !== undefined)
			b.lines.push({when: e.when, rendition: e.rendition, items: e.items})
		break
	}
	case 'BufferClear':
	{
		let b = buffers.get(e.bufferName)
		if (b !== undefined)
			b.lines.length = 0
		break
	}
	}

	m.redraw()
}

let BufferList = {
	view: vnode => {
		let items = []
		buffers.forEach((b, name) => {
			let attrs = {
				onclick: e => {
					send({command: 'BufferActivate', bufferName: name})
				},
			}
			if (name == bufferCurrent)
				attrs.class = 'active'
			items.push(m('.item', attrs, name))
		})
		return m('.list', {}, items)
	},
}

let Content = {
	view: vnode => {
		let line = vnode.children[0]
		let content = []
		switch (line.rendition) {
		case 'Indent': content.push(m('span.mark',       {}, ''));  break
		case 'Status': content.push(m('span.mark',       {}, '–')); break
		case 'Error':  content.push(m('span.mark.error', {}, '⚠')); break
		case 'Join':   content.push(m('span.mark.join',  {}, '→')); break
		case 'Part':   content.push(m('span.mark.part',  {}, '←')); break
		}

		let classes = new Set()
		let flip = c => {
			if (classes.has(c))
				classes.delete(c)
			else
				classes.add(c)
		}
		line.items.forEach(item => {
			// TODO: Colours.
			switch (item.kind) {
			case 'Text':
				// TODO: Detect and transform links.
				content.push(m('span', {
					class: Array.from(classes.keys()).join(' '),
				}, item.text))
				break
			case 'Reset':
				classes.clear()
				break
			case 'FlipBold':       flip('b'); break
			case 'FlipItalic':     flip('i'); break
			case 'FlipUnderline':  flip('u'); break
			case 'FlipInverse':    flip('i'); break
			case 'FlipCrossedOut': flip('s'); break
			case 'FlipMonospace':  flip('m'); break
			}
		})
		return m('.content', {}, content)
	},
}

let Buffer = {
	view: vnode => {
		let lines = []
		let b = buffers.get(bufferCurrent)
		if (b === undefined)
			return

		let lastDateMark = undefined
		b.lines.forEach(line => {
			let date = new Date(line.when * 1000)
			let dateMark = date.toLocaleDateString()
			if (dateMark !== lastDateMark) {
				lines.push(m('.date', {}, dateMark))
				lastDateMark = dateMark
			}

			lines.push(m('.time', {}, date.toLocaleTimeString()))
			lines.push(m(Content, {}, line))
		})
		return m('.buffer-container', {}, [
			m('.filler'),
			m('.buffer', {}, lines),
		])
	},
}

// TODO: This should be remembered across buffer switches,
// and we'll probably have to intercept /all/ key presses.
let Input = {
	view: vnode => {
		return m('textarea', {
			rows: 1,
			onkeydown: e => {
				// TODO: And perhaps on other actions, too.
				send({command: 'Active'})
				if (e.keyCode !== 13)
					return

				send({
					command: 'BufferInput',
					bufferName: bufferCurrent,
					text: e.currentTarget.value,
				})
				e.preventDefault()
				e.currentTarget.value = ''
			},
		})
	},
}

let Main = {
	view: vnode => {
		return m('.xP', {}, [
			m('.title', {}, "xP"),
			m('.middle', {}, [m(BufferList), m(Buffer)]),
			m('.status', {}, bufferCurrent),
			m(Input),
		])
	},
}

// TODO: Buffer names should work as routes.
window.addEventListener('load', () => {
	m.route(document.body, '/', {
		'/': Main,
	})
})