Skip to content

Commit 043c8e0

Browse files
committed
clipboard now works across tabs
1 parent 01130a7 commit 043c8e0

File tree

5 files changed

+53
-82
lines changed

5 files changed

+53
-82
lines changed

frontend/src/main/javascript/src/js/components/views/symbol-view/symbol-view.component.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import remove from 'lodash/remove';
1818
import uniqueId from 'lodash/uniqueId';
1919
import {AlphabetSymbol} from '../../../entities/alphabet-symbol';
2020
import {ParametrizedSymbol} from '../../../entities/parametrized-symbol';
21+
import {ClipboardService} from '../../../services/clipboard.service';
2122
import {Selectable} from '../../../utils/selectable';
2223

2324
/**
@@ -222,7 +223,7 @@ class SymbolViewComponent {
222223
steps.forEach(step => {
223224
delete step._id;
224225
});
225-
this.ClipboardService.copy('symbolSteps', steps);
226+
this.ClipboardService.copy(this.project.id, 'symbolSteps', steps);
226227
this.ToastService.info(steps.length + ' steps copied to clipboard');
227228
}
228229
}
@@ -231,7 +232,7 @@ class SymbolViewComponent {
231232
const s = AlphabetSymbol.stepsToJson(step);
232233
delete s._id;
233234

234-
this.ClipboardService.copy('symbolSteps', [s]);
235+
this.ClipboardService.copy(this.project.id, 'symbolSteps', [s]);
235236
this.ToastService.info('The action has been copied to the clipboard.');
236237
}
237238

@@ -243,7 +244,7 @@ class SymbolViewComponent {
243244
cpy.forEach(step => {
244245
delete step._id;
245246
});
246-
this.ClipboardService.cut('symbolSteps', cpy);
247+
this.ClipboardService.copy(this.project.id, 'symbolSteps', cpy, ClipboardService.Mode.CUT);
247248
this.deleteSteps(steps);
248249
this.ToastService.info(steps.length + ' steps cut to clipboard');
249250
}
@@ -253,7 +254,7 @@ class SymbolViewComponent {
253254
const s = AlphabetSymbol.stepsToJson(step);
254255
delete s._id;
255256

256-
this.ClipboardService.cut('symbolSteps', [s]);
257+
this.ClipboardService.copy(this.project.id, 'symbolSteps', [s], ClipboardService.Mode.CUT);
257258
this.deleteSteps([step]);
258259
this.ToastService.info('The action has been copied to the clipboard.');
259260
}
@@ -262,7 +263,7 @@ class SymbolViewComponent {
262263
* Pastes the actions from the clipboard to the end of of the action list.
263264
*/
264265
pasteSteps() {
265-
let steps = this.ClipboardService.paste('symbolSteps');
266+
let steps = this.ClipboardService.paste(this.project.id, 'symbolSteps');
266267
if (steps != null) {
267268
steps.forEach(step => {
268269
step._id = uniqueId();

frontend/src/main/javascript/src/js/components/views/test-suite-view/test-suite-view.component.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,15 +386,15 @@ export const testSuiteViewComponent = {
386386
if (tests.length > 0) {
387387
tests = JSON.parse(JSON.stringify(tests));
388388
tests = this.TestService.exportTests(tests);
389-
this.ClipboardService.copy('tests', tests);
389+
this.ClipboardService.copy(this.project.id, 'tests', tests);
390390
this.ToastService.info('Tests copied to clipboard.');
391391
} else {
392392
this.ToastService.info('You have to select at least one test');
393393
}
394394
}
395395

396396
pasteTests() {
397-
const tests = this.ClipboardService.paste('tests');
397+
const tests = this.ClipboardService.paste(this.project.id, 'tests');
398398
if (tests != null) {
399399
this.TestService.importTests(this.project.id, tests, this.testSuite.id)
400400
.then((importedTests) => {

frontend/src/main/javascript/src/js/services/clipboard.service.js

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,59 +19,65 @@
1919
*/
2020
export class ClipboardService {
2121

22-
/**
23-
* Constructor.
24-
*/
22+
/** Constructor. */
2523
constructor() {
26-
27-
/**
28-
* The map of clipboard entries.
29-
* @type {Object}
30-
*/
31-
this.entries = {};
24+
// initialize clipboard
25+
const clipboard = localStorage.getItem('clipboard');
26+
if (clipboard == null) {
27+
localStorage.setItem('clipboard', JSON.stringify({}));
28+
}
3229
}
3330

3431
/**
3532
* Copies an item to the Clipboard.
3633
*
37-
* @param {string} key - The key the data is saved under.
38-
* @param {any} data - the data to copy to the clipboard.
34+
* @param {number} projectId The ID of the project.
35+
* @param {string} key The key the data is saved under.
36+
* @param {any} data The data to copy to the clipboard.
37+
* @param {string} mode The mode for copying.
3938
*/
40-
copy(key, data) {
41-
this.entries[key] = {
42-
data,
43-
mode: 'copy'
44-
};
45-
}
39+
copy(projectId, key, data, mode = ClipboardService.Mode.COPY) {
40+
const clipboard = JSON.parse(localStorage.getItem('clipboard'));
41+
if (clipboard[projectId] == null) {
42+
clipboard[projectId] = {};
43+
}
4644

47-
/**
48-
* Cuts an item to the clipboard.
49-
*
50-
* @param {string} key - The key under which the data is accessed.
51-
* @param {any} data - The data to store.
52-
*/
53-
cut(key, data) {
54-
this.entries[key] = {
45+
clipboard[projectId][key] = {
5546
data,
56-
mode: 'cut'
47+
mode
5748
};
49+
50+
localStorage.setItem('clipboard', JSON.stringify(clipboard));
5851
}
5952

6053
/**
6154
* Pastes an item from the clipboard. Deletes the item if mode has been 'cut'.
6255
*
63-
* @param {string} key - The key whose data to get.
64-
* @returns {any|null}
56+
* @param {number} projectId The ID of the project.
57+
* @param {string} key The key whose data to get.
58+
* @returns {?any}
6559
*/
66-
paste(key) {
67-
if (this.entries[key]) {
68-
const item = this.entries[key];
69-
if (item.mode === 'cut') {
70-
delete this.entries[key];
60+
paste(projectId, key) {
61+
const clipboard = JSON.parse(localStorage.getItem('clipboard'));
62+
const entry = clipboard[projectId][key];
63+
if (entry) {
64+
if (entry.mode === ClipboardService.Mode.CUT) {
65+
delete clipboard[projectId][key];
66+
localStorage.setItem('clipboard', JSON.stringify(clipboard));
7167
}
72-
return item.data;
68+
return entry.data;
7369
} else {
7470
return null;
7571
}
7672
}
73+
74+
/** Clear the clipboard. */
75+
clear() {
76+
localStorage.setItem('clipboard', JSON.stringify({}));
77+
}
7778
}
79+
80+
ClipboardService.Mode = {
81+
COPY: 'copy',
82+
CUT: 'cut'
83+
};

frontend/src/main/javascript/src/js/services/user.service.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ export class UserService {
2222
* Constructor.
2323
*
2424
* @param {ProjectService} ProjectService
25+
* @param {ClipboardService} ClipboardService
2526
*/
2627
// @ngInject
27-
constructor(ProjectService) {
28+
constructor(ProjectService, ClipboardService) {
2829
this.projectService = ProjectService;
30+
this.clipboardService = ClipboardService;
2931

3032
/**
3133
* The store.
@@ -48,6 +50,7 @@ export class UserService {
4850
}
4951

5052
login(user, jwt = null) {
53+
this.clipboardService.clear();
5154
localStorage.setItem('user', JSON.stringify(user));
5255
this.store.currentUser = user;
5356
if (jwt) {
@@ -62,5 +65,6 @@ export class UserService {
6265
this.store.currentUser = null;
6366
this.store.jwt = null;
6467
this.projectService.reset();
68+
this.clipboardService.clear();
6569
}
6670
}

frontend/src/main/javascript/test/unit/services/clipboard.service.spec.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)