-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
75 lines (60 loc) · 1.87 KB
/
extension.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
const vscode = require('vscode');
const { Client } = require('tmi.js');
const config = vscode.workspace.getConfiguration('twitchHighlighter');
const twitch = new Client({ channels: [config.channel] });
const decoration = vscode.window.createTextEditorDecorationType({
backgroundColor: config.backgroundColor,
color: config.color,
fontWeight: config.fontWeight,
border: config.border,
isWholeLine: true,
});
let removeHighlightTimeout = false;
const highlight = (fromLine, toLine) => {
const range = new vscode.Range(
new vscode.Position(fromLine, 0),
new vscode.Position(toLine, 0));
const editor = vscode.window.activeTextEditor;
clearTimeout(removeHighlightTimeout);
editor.setDecorations(decoration, [range]);
removeHighlightTimeout = setTimeout(
() => editor.setDecorations(decoration, []),
10000);
};
const activate = context => {
twitch.connect();
vscode.window.showInformationMessage('Twitch Highlighter connected');
};
const deactivate = () => twitch.disconnect();
twitch.on('message', (channel, tags, message, self) => {
if (tags['custom-reward-id']) {
const rewardId = tags['custom-reward-id'];
if (message.trim() === '!highlight test') {
vscode.window.showInformationMessage(rewardId);
return;
}
if (rewardId != config.rewardId) return;
}
else {
return;
}
try {
const matches = /^(\d+)([-,: ]\d+)?(.*)$/.exec(message.trim());
const fromLine = parseInt(matches[1]);
const toLine = (matches[2] && parseInt(matches[2].substring(1)))
|| fromLine;
if (fromLine <= 0 || toLine < fromLine) return;
let text = matches[3].trim();
if (text.length === 0) text = 'No message';
vscode.window.showInformationMessage(
`${tags['display-name']} is highlighting lines ${fromLine}-${toLine}: ${text}`)
highlight(fromLine - 1, toLine - 1);
}
catch (err) {
vscode.window.showErrorMessage(err);
}
});
module.exports = {
activate,
deactivate,
};