corrections-view.js 3.8 KB

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