|
1 | 1 | /** @babel */ |
2 | 2 |
|
3 | | -import SelectListView from 'atom-select-list' |
| 3 | +import SelectListView from 'atom-select-list'; |
4 | 4 |
|
5 | 5 | export default class CorrectionsView { |
6 | | - constructor (editor, corrections, marker, updateTarget, updateCallback) { |
7 | | - this.editor = editor |
8 | | - this.corrections = corrections |
9 | | - this.marker = marker |
10 | | - this.updateTarget = updateTarget |
11 | | - this.updateCallback = updateCallback |
12 | | - this.selectListView = new SelectListView({ |
13 | | - emptyMessage: 'No corrections', |
14 | | - items: this.corrections, |
15 | | - filterKeyForItem: (item) => item.label, |
16 | | - elementForItem: (item) => { |
17 | | - const element = document.createElement('li') |
18 | | - if (item.isSuggestion) { |
19 | | - // This is a word replacement suggestion. |
20 | | - element.textContent = item.label |
21 | | - } else { |
22 | | - // This is an operation such as add word. |
23 | | - const em = document.createElement('em') |
24 | | - em.textContent = item.label |
25 | | - element.appendChild(em) |
26 | | - } |
27 | | - return element |
28 | | - }, |
29 | | - didConfirmSelection: (item) => { |
30 | | - this.editor.transact(() => { |
31 | | - if (item.isSuggestion) { |
32 | | - // Update the buffer with the correction. |
33 | | - this.editor.setSelectedBufferRange(this.marker.getBufferRange()) |
34 | | - this.editor.insertText(item.suggestion) |
35 | | - } else { |
36 | | - // Build up the arguments object for this buffer and text. |
37 | | - let projectPath = null |
38 | | - let relativePath = null |
39 | | - if (this.editor && this.editor.buffer && this.editor.buffer.file && this.editor.buffer.file.path) { |
40 | | - [projectPath, relativePath] = atom.project.relativizePath(this.editor.buffer.file.path) |
41 | | - } |
| 6 | + constructor(editor, corrections, marker, updateTarget, updateCallback) { |
| 7 | + this.editor = editor; |
| 8 | + this.corrections = corrections; |
| 9 | + this.marker = marker; |
| 10 | + this.updateTarget = updateTarget; |
| 11 | + this.updateCallback = updateCallback; |
| 12 | + this.selectListView = new SelectListView({ |
| 13 | + emptyMessage: 'No corrections', |
| 14 | + items: this.corrections, |
| 15 | + filterKeyForItem: (item) => item.label, |
| 16 | + elementForItem: (item) => { |
| 17 | + const element = document.createElement('li'); |
| 18 | + if (item.isSuggestion) { |
| 19 | + // This is a word replacement suggestion. |
| 20 | + element.textContent = item.label; |
| 21 | + } else { |
| 22 | + // This is an operation such as add word. |
| 23 | + const em = document.createElement('em'); |
| 24 | + em.textContent = item.label; |
| 25 | + element.appendChild(em); |
| 26 | + } |
| 27 | + return element; |
| 28 | + }, |
| 29 | + didConfirmSelection: (item) => { |
| 30 | + this.editor.transact(() => { |
| 31 | + if (item.isSuggestion) { |
| 32 | + // Update the buffer with the correction. |
| 33 | + this.editor.setSelectedBufferRange( |
| 34 | + this.marker.getBufferRange() |
| 35 | + ); |
| 36 | + this.editor.insertText(item.suggestion); |
| 37 | + } else { |
| 38 | + // Build up the arguments object for this buffer and text. |
| 39 | + let projectPath = null; |
| 40 | + let relativePath = null; |
| 41 | + if ( |
| 42 | + this.editor && |
| 43 | + this.editor.buffer && |
| 44 | + this.editor.buffer.file && |
| 45 | + this.editor.buffer.file.path |
| 46 | + ) { |
| 47 | + [ |
| 48 | + projectPath, |
| 49 | + relativePath, |
| 50 | + ] = atom.project.relativizePath( |
| 51 | + this.editor.buffer.file.path |
| 52 | + ); |
| 53 | + } |
42 | 54 |
|
43 | | - const args = {id: this.id, projectPath, relativePath} |
44 | | - // Send the "add" request to the plugin. |
45 | | - item.plugin.add(args, item) |
46 | | - // Update the buffer to handle the corrections. |
47 | | - this.updateCallback.bind(this.updateTarget)() |
48 | | - } |
49 | | - }) |
50 | | - this.destroy() |
51 | | - }, |
52 | | - didConfirmEmptySelection: () => { |
53 | | - this.destroy() |
54 | | - }, |
55 | | - didCancelSelection: () => { |
56 | | - this.destroy() |
57 | | - } |
58 | | - }) |
59 | | - this.selectListView.element.classList.add('spell-check-corrections', 'corrections', 'popover-list') |
60 | | - } |
| 55 | + const args = { id: this.id, projectPath, relativePath }; |
| 56 | + // Send the "add" request to the plugin. |
| 57 | + item.plugin.add(args, item); |
| 58 | + // Update the buffer to handle the corrections. |
| 59 | + this.updateCallback.bind(this.updateTarget)(); |
| 60 | + } |
| 61 | + }); |
| 62 | + this.destroy(); |
| 63 | + }, |
| 64 | + didConfirmEmptySelection: () => { |
| 65 | + this.destroy(); |
| 66 | + }, |
| 67 | + didCancelSelection: () => { |
| 68 | + this.destroy(); |
| 69 | + }, |
| 70 | + }); |
| 71 | + this.selectListView.element.classList.add( |
| 72 | + 'spell-check-corrections', |
| 73 | + 'corrections', |
| 74 | + 'popover-list' |
| 75 | + ); |
| 76 | + } |
61 | 77 |
|
62 | | - attach () { |
63 | | - this.previouslyFocusedElement = document.activeElement |
64 | | - this.overlayDecoration = this.editor.decorateMarker(this.marker, {type: 'overlay', item: this.selectListView}) |
65 | | - process.nextTick(() => { |
66 | | - atom.views.readDocument(() => { this.selectListView.focus() }) |
67 | | - }) |
68 | | - } |
| 78 | + attach() { |
| 79 | + this.previouslyFocusedElement = document.activeElement; |
| 80 | + this.overlayDecoration = this.editor.decorateMarker(this.marker, { |
| 81 | + type: 'overlay', |
| 82 | + item: this.selectListView, |
| 83 | + }); |
| 84 | + process.nextTick(() => { |
| 85 | + atom.views.readDocument(() => { |
| 86 | + this.selectListView.focus(); |
| 87 | + }); |
| 88 | + }); |
| 89 | + } |
69 | 90 |
|
70 | | - async destroy () { |
71 | | - if (!this.destroyed) { |
72 | | - this.destroyed = true |
73 | | - this.overlayDecoration.destroy() |
74 | | - await this.selectListView.destroy() |
75 | | - if (this.previouslyFocusedElement) { |
76 | | - this.previouslyFocusedElement.focus() |
77 | | - this.previouslyFocusedElement = null |
78 | | - } |
| 91 | + async destroy() { |
| 92 | + if (!this.destroyed) { |
| 93 | + this.destroyed = true; |
| 94 | + this.overlayDecoration.destroy(); |
| 95 | + await this.selectListView.destroy(); |
| 96 | + if (this.previouslyFocusedElement) { |
| 97 | + this.previouslyFocusedElement.focus(); |
| 98 | + this.previouslyFocusedElement = null; |
| 99 | + } |
| 100 | + } |
79 | 101 | } |
80 | | - } |
81 | 102 | } |
0 commit comments