gutter.js
1 const { Emitter } = require('event-kit'); 2 3 const DefaultPriority = -100; 4 5 // Extended: Represents a gutter within a {TextEditor}. 6 // 7 // See {TextEditor::addGutter} for information on creating a gutter. 8 module.exports = class Gutter { 9 constructor(gutterContainer, options) { 10 this.gutterContainer = gutterContainer; 11 this.name = options && options.name; 12 this.priority = 13 options && options.priority != null ? options.priority : DefaultPriority; 14 this.visible = options && options.visible != null ? options.visible : true; 15 this.type = options && options.type != null ? options.type : 'decorated'; 16 this.labelFn = options && options.labelFn; 17 this.className = options && options.class; 18 19 this.onMouseDown = options && options.onMouseDown; 20 this.onMouseMove = options && options.onMouseMove; 21 22 this.emitter = new Emitter(); 23 } 24 25 /* 26 Section: Gutter Destruction 27 */ 28 29 // Essential: Destroys the gutter. 30 destroy() { 31 if (this.name === 'line-number') { 32 throw new Error('The line-number gutter cannot be destroyed.'); 33 } else { 34 this.gutterContainer.removeGutter(this); 35 this.emitter.emit('did-destroy'); 36 this.emitter.dispose(); 37 } 38 } 39 40 /* 41 Section: Event Subscription 42 */ 43 44 // Essential: Calls your `callback` when the gutter's visibility changes. 45 // 46 // * `callback` {Function} 47 // * `gutter` The gutter whose visibility changed. 48 // 49 // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. 50 onDidChangeVisible(callback) { 51 return this.emitter.on('did-change-visible', callback); 52 } 53 54 // Essential: Calls your `callback` when the gutter is destroyed. 55 // 56 // * `callback` {Function} 57 // 58 // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. 59 onDidDestroy(callback) { 60 return this.emitter.once('did-destroy', callback); 61 } 62 63 /* 64 Section: Visibility 65 */ 66 67 // Essential: Hide the gutter. 68 hide() { 69 if (this.visible) { 70 this.visible = false; 71 this.gutterContainer.scheduleComponentUpdate(); 72 this.emitter.emit('did-change-visible', this); 73 } 74 } 75 76 // Essential: Show the gutter. 77 show() { 78 if (!this.visible) { 79 this.visible = true; 80 this.gutterContainer.scheduleComponentUpdate(); 81 this.emitter.emit('did-change-visible', this); 82 } 83 } 84 85 // Essential: Determine whether the gutter is visible. 86 // 87 // Returns a {Boolean}. 88 isVisible() { 89 return this.visible; 90 } 91 92 // Essential: Add a decoration that tracks a {DisplayMarker}. When the marker moves, 93 // is invalidated, or is destroyed, the decoration will be updated to reflect 94 // the marker's state. 95 // 96 // ## Arguments 97 // 98 // * `marker` A {DisplayMarker} you want this decoration to follow. 99 // * `decorationParams` An {Object} representing the decoration. It is passed 100 // to {TextEditor::decorateMarker} as its `decorationParams` and so supports 101 // all options documented there. 102 // * `type` __Caveat__: set to `'line-number'` if this is the line-number 103 // gutter, `'gutter'` otherwise. This cannot be overridden. 104 // 105 // Returns a {Decoration} object 106 decorateMarker(marker, options) { 107 return this.gutterContainer.addGutterDecoration(this, marker, options); 108 } 109 110 getElement() { 111 if (this.element == null) this.element = document.createElement('div'); 112 return this.element; 113 } 114 };