Skip to content

Commit 730947b

Browse files
authored
Merge pull request #2 from stroveio/editorFocus
Editor focus
2 parents a740912 + 96d02b1 commit 730947b

File tree

5 files changed

+90
-4
lines changed

5 files changed

+90
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Liveshare toolkit for strove.io.
44

55
Note: This extension is required for liveshare to work properly inside strove.io. It will do nothing outside of the platform.
66

7-
Current version: 0.1.4
7+
Current version: 0.1.5

extension.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,37 @@ const ws = require("ws");
1010
const {
1111
stroveLiveshareSubscription,
1212
liveshareActivity,
13+
focusEditorSubscription,
1314
} = require("./utils/queries");
1415
const { websocketEndpoint } = require("./utils/endpoints");
1516
const { handleLiveshareResponse } = require("./utils/handleLiveshareResponse");
17+
const { handleFocusEditor } = require("./utils/handleFocusEditor");
1618

1719
const environment = process.env.STROVE_ENVIRONMENT;
1820

1921
const client = new SubscriptionClient(
2022
websocketEndpoint,
2123
{
2224
reconnect: true,
25+
connectionParams: () => ({
26+
authorization: process.env.STROVE_USER_TOKEN
27+
? `Bearer ${process.env.STROVE_USER_TOKEN}`
28+
: "",
29+
}),
2330
},
2431
ws
2532
);
2633

2734
const link = new WebSocketLink(client);
2835

36+
///
37+
// context = {
38+
// headers: {
39+
// Authorization: `Bearer ${user.githubToken}`,
40+
// 'User-Agent': 'node',
41+
// },
42+
// }
43+
2944
try {
3045
const liveshareActivityUpdate = (data) => {
3146
const liveshareActivityOperation = {
@@ -72,7 +87,9 @@ try {
7287
console.log("stroveteams extension is active");
7388

7489
// First call to get cursor positions of other users
75-
liveshareActivityInit();
90+
// It doesn't seem to work - my assumption:
91+
// it gets called before subscription is set up and the info gets lost, hence setTimeout
92+
setTimeout(liveshareActivityInit, 1000);
7693

7794
vscode.window.onDidChangeTextEditorSelection(
7895
({ textEditor, selections }) => {
@@ -127,16 +144,42 @@ try {
127144

128145
handleLiveshareResponse(stroveLiveshare);
129146
},
130-
error: (error) => console.log(`received error ${error}`),
147+
error: (error) =>
148+
console.log(`received error in liveshareSubscriber ${error}`),
131149
complete: () => console.log(`complete`),
132150
}
133151
);
134152

153+
const focusEditorOperation = {
154+
query: focusEditorSubscription,
155+
variables: {
156+
userId: process.env.STROVE_USER_ID || "123",
157+
projectId: process.env.STROVE_PROJECT_ID || "123abc",
158+
},
159+
};
160+
161+
const focusEditorSubscriber = execute(link, focusEditorOperation).subscribe({
162+
next: async (data) => {
163+
const {
164+
data: { focusEditor },
165+
} = data;
166+
167+
handleFocusEditor({
168+
uri: focusEditor.documentPath,
169+
userPosition: focusEditor.selections,
170+
});
171+
},
172+
error: (error) =>
173+
console.log(`received error in focusEditorSubscriber ${error}`),
174+
complete: () => console.log(`complete`),
175+
});
176+
135177
exports.activate = activate;
136178

137179
// this method is called when your extension is deactivated
138180
function deactivate() {
139181
liveshareSubscriber.unsubscribe();
182+
focusEditorSubscriber.unsubscribe();
140183
}
141184

142185
module.exports = {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "stroveteams",
33
"displayName": "stroveTeams",
44
"description": "Teams support for Strove",
5-
"version": "0.1.4",
5+
"version": "0.1.5",
66
"engines": {
77
"vscode": "^1.39.2"
88
},

utils/handleFocusEditor.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const vscode = require("vscode");
2+
3+
const handleFocusEditor = async ({ uri, userPosition }) => {
4+
try {
5+
const editorPath = vscode.Uri.file(uri);
6+
7+
const editor = await vscode.window.showTextDocument(editorPath);
8+
9+
const range = new vscode.Range(
10+
new vscode.Position(
11+
userPosition.start.line,
12+
userPosition.start.character
13+
),
14+
new vscode.Position(userPosition.end.line, userPosition.end.character)
15+
);
16+
17+
editor.revealRange(range, 1);
18+
} catch (e) {
19+
console.log(`Error in handleFocusEditor: ${e}`);
20+
}
21+
};
22+
23+
module.exports = {
24+
handleFocusEditor,
25+
};

utils/queries.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,21 @@ mutation($userData: UserActivityInput) {
3838
liveshareActivity (userData: $userData)
3939
}
4040
`;
41+
42+
exports.focusEditorSubscription = `
43+
subscription($userId: String!, $projectId: String!) {
44+
focusEditor(userId: $userId, projectId: $projectId) {
45+
documentPath
46+
selections {
47+
start {
48+
line
49+
character
50+
}
51+
end {
52+
line
53+
character
54+
}
55+
}
56+
}
57+
}
58+
`;

0 commit comments

Comments
 (0)