All files / src/pwk caret.js

98.14% Statements 53/54
90% Branches 9/10
100% Functions 12/12
100% Lines 53/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167              1x   1x 1x 1x 1x   1x 1x                       120x             120x           120x           120x         120x     120x   120x   120x   120x         120x   120x       120x                       362x 362x 362x 362x 362x 362x 362x     362x 362x 362x         120x 120x         515x 515x 515x         285x 285x 285x         285x   285x   285x 285x 13x 13x 13x 11x                       21248x               21248x 21248x   21248x 369x 198x   171x                   1x  
/**
 * @fileoverview UI component that represents a caret in the document.
 * @author Dmytro Antonenko <dmitry.antonenko@pubwebkit.com>
 */
import {RuntimeTextStylesConfiguration} from './text/runtime_text_styles_configuration.js';
import {RuntimeTextStylesConfigurationEventType} from './text/runtime_text_styles_configuration_event_type.js';
 
goog.declareModuleId('pwk.Caret');
 
const Component = goog.require('goog.ui.Component');
const classlist = goog.require('goog.dom.classlist');
const events = goog.require('goog.events');
const style = goog.require('goog.style');
 
const DomHelper = goog.requireType('goog.dom.DomHelper');
const {RangeOffset} = goog.requireType('pwk.range.RangeOffset');
 
/**
 * Caret component.
 * @extends {Component}
 */
export class Caret extends Component {
  /**
   * @param {!DomHelper=} opt_domHelper Optional DOM helper, used for document
   * interaction.
   */
  constructor(opt_domHelper) {
    super(opt_domHelper);
 
    /**
     * Indicates if cursor visible.
     * @type {boolean}
     * @private
     */
    this.isVisible_ = false;
 
    /**
     * @type {number}
     * @private
     */
    this.blinkId_;
 
    /**
     * @const {!RuntimeTextStylesConfiguration}
     * @private
     */
    this.runtimeTextStylesConfiguration_ = RuntimeTextStylesConfiguration.getInstance();
  }
 
  /** @override */
  createDom() {
    super.createDom();
 
    // Create element and apply classes
    let element = this.getElement();
    // Set css class
    classlist.add(element, Caret.CSS_CLASS);
    // Set unselectable
    style.setUnselectable(element, true);
    // Hide
    this.hide();
  }
 
  /** @override */
  enterDocument() {
    super.enterDocument();
 
    events.listen(
        this.runtimeTextStylesConfiguration_,
        RuntimeTextStylesConfigurationEventType.FONT_CONFIGURATION_CHANGE,
        this.handleRuntimeFontConfigurationChange_, false, this);
    events.listen(
        this.runtimeTextStylesConfiguration_,
        RuntimeTextStylesConfigurationEventType.SELECTION_CHANGE,
        this.handleRuntimeFontConfigurationChange_, false, this);
  }
 
  /**
   * Set caret position from caret range offset.
   * @param {!RangeOffset} rangeOffset
   */
  updateCaretPositionFromRangeOffset(rangeOffset) {
    // Get coordinates
    const offsetBounds = rangeOffset.getNodeBounds();
    const element = this.getElement();
    const parentBounds = style.getBounds(element.parentElement);
    const nodeBounds = style.getBounds(rangeOffset.getNode().getElement());
    const leftDelta = nodeBounds.left - parentBounds.left;
    const caretLeft = leftDelta + offsetBounds.left;
    const caretTop = offsetBounds.top + nodeBounds.top - parentBounds.top;
 
    // Update position and height
    element.style.left = caretLeft + 'px';
    element.style.top = caretTop + 'px';
    element.style.height = offsetBounds.height + 'px';
  }
 
  /** @override */
  disposeInternal() {
    this.hide();
    super.disposeInternal();
  }
 
  /** Hides cursor. */
  hide() {
    clearInterval(this.blinkId_);
    this.isVisible_ = false;
    this.getElement().style.visibility = 'hidden';
  }
 
  /** Shows cursor. */
  show() {
    this.isVisible_ = true;
    this.getElement().style.visibility = 'visible';
    this.restartTimer();
  }
 
  /** Restarts blink interval. */
  restartTimer() {
    clearInterval(this.blinkId_);
 
    Iif (!this.isVisible_) { return; }
 
    const element = this.getElement();
    this.blinkId_ = setInterval(() => {
      element.style.visibility = 'hidden';
      setTimeout(() => {
        if (this.isVisible_) {
          element.style.visibility = 'visible';
        }
      }, 400);
    }, 1000);
  }
 
  /**
   * Handler for {@link pwk.text.RuntimeTextStylesConfigurationEventType.FONT_CONFIGURATION_CHANGE} event.
   * @param {!Event} e Selection event to handle.
   * @private
   */
  handleRuntimeFontConfigurationChange_(e) {
    this.updateCaretTilt_();
  }
 
  /**
   * Sets update caret tilt based on RuntimeTextStylesConfiguration state.
   * @private
   */
  updateCaretTilt_() {
    const element = this.getElement();
    const fontDetails = this.runtimeTextStylesConfiguration_.getFontStyleConfiguration();
 
    if (element) {
      if (fontDetails && fontDetails.fontStyle === 'italic') {
        element.style.transform = 'rotate(10deg)';
      } else {
        element.style.transform = 'none';
      }
    }
  }
}
 
/**
 * Default css class.
 * @const {string}
 */
Caret.CSS_CLASS = 'pwk-caret';