<commit_before>import Parchment from 'parchment';
import Embed from '../blots/embed';
import clone from 'clone';
import equal from 'deep-equal';
import Emitter from './emitter';
import logger from './logger';

let debug = logger('quill:selection');


class Range {
  constructor(index, length = 0) {
    this.index = index;
    this.length = length;
  }
}


class Selection {
  constructor(scroll, emitter) {
    this.emitter = emitter;
    this.scroll = scroll;
    this.composing = false;
    this.root = this.scroll.domNode;
    this.cursor = Parchment.create('cursor', this);
    // savedRange is last non-null range
    this.lastRange = this.savedRange = new Range(0, 0);
    this.handleComposition();
    this.handleDragging();
    this.handleEmbedSelection();
    this.emitter.on(Emitter.events.EDITOR_CHANGE, (type, delta) => {
      if (type === Emitter.events.TEXT_CHANGE && delta.length() > 0) {
        this.update(Emitter.sources.SILENT);
      }
    });
    this.emitter.on(Emitter.events.SCROLL_BEFORE_UPDATE, () => {
      if (!this.hasFocus()) return;
      let native = this.getNativeRange();
      if (native == null) return;
      if (native.start.node === this.cursor.textNode) return;  // cursor.restore() will handle
      // TODO unclear if this has negative side effects
      this.emitter.once(Emitter.events.SCROLL_UPDATE, () => {
        try {
          this.setNativeRange(native.start.node, native.start.offset, native.end.node, native.end.offset);
        } catch (ignored) {}
      });
    });
    this.emitter.on(Emitter.events.SCROLL_OPTIMIZE, (mutations, context) => {
      if (context.range) {
        const { startNode, startOffset, endNode, endOffset } = context.range;
        this.setNativeRange(startNode, startOffset, endNode, endOffset);
      }
    });
    this.update(Emitter.sources.SILENT);
  }

  fixInlineEmbed(native) {
    if (native == null) return;
    const [start, end] = [native.start, native.end].map(function(pos) {
      const blot = Parchment.find(pos.node, true);
      if (blot instanceof Embed) {
        let node, offset;
        if (pos.node === blot.leftGuard && pos.offset === 1) {
          [node, offset] = blot.position(blot.length());
          return { node, offset };
        } else if (pos.node === blot.rightGuard && pos.offset === 0) {
          [node, offset] = blot.position(0);
          return { node, offset };
        }
      }
      return pos;
    });
    if (native.start !== start || native.end !== end) {
      this.setNativeRange(start.node, start.offset, end.node, end.offset);
    }
  }

  handleComposition() {
    this.root.addEventListener('compositionstart', () => {
      this.composing = true;
    });
    this.root.addEventListener('compositionend', () => {
      this.composing = false;
      if (this.cursor.parent) {
        const range = this.cursor.restore();
        if (!range) return;
        setTimeout(() => {
          this.setNativeRange(range.startNode, range.startOffset, range.endNode, range.endOffset);
        }, 1);
      }
    });
  }

  handleDragging() {
    let mouseCount = 0;
    this.emitter.listenDOM('mousedown', document.body, () => {
      mouseCount += 1;
    });
    this.emitter.listenDOM('mouseup', document.body, () => {
      mouseCount -= 1;
      if (mouseCount === 0) {
        this.update(Emitter.sources.USER);
      }
    });
    this.emitter.listenDOM('selectionchange', document, () => {
      if (mouseCount === 0) {
        setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
      }
    });
  }

  handleEmbedSelection() {
    this.emitter.on(Emitter.events.SELECTION_CHANGE, () => {
      const selectedNode = document.querySelector('.ql-embed-selected');
      if (selectedNode) {
        selectedNode.classList.remove('ql-embed-selected');
      }
    });
    this.root.addEventListener('click', (e) => {
      const blot = Parchment.find(e.target, true);
      if (blot instanceof Parchment.Embed) {
        const range = new Range(blot.offset(scroll), blot.length());
        this.setRange(range, Emitter.sources.USER);
        blot.domNode.classList.add('ql-embed-selected');
      }
    });
  }

  focus() {<commit_msg>change to boolean, fixes #1654<commit_after>import Parchment from 'parchment';
import Embed from '../blots/embed';
import clone from 'clone';
import equal from 'deep-equal';
import Emitter from './emitter';
import logger from './logger';

let debug = logger('quill:selection');


class Range {
  constructor(index, length = 0) {
    this.index = index;
    this.length = length;
  }
}


class Selection {
  constructor(scroll, emitter) {
    this.emitter = emitter;
    this.scroll = scroll;
    this.composing = false;
    this.root = this.scroll.domNode;
    this.cursor = Parchment.create('cursor', this);
    // savedRange is last non-null range
    this.lastRange = this.savedRange = new Range(0, 0);
    this.handleComposition();
    this.handleDragging();
    this.handleEmbedSelection();
    this.emitter.on(Emitter.events.EDITOR_CHANGE, (type, delta) => {
      if (type === Emitter.events.TEXT_CHANGE && delta.length() > 0) {
        this.update(Emitter.sources.SILENT);
      }
    });
    this.emitter.on(Emitter.events.SCROLL_BEFORE_UPDATE, () => {
      if (!this.hasFocus()) return;
      let native = this.getNativeRange();
      if (native == null) return;
      if (native.start.node === this.cursor.textNode) return;  // cursor.restore() will handle
      // TODO unclear if this has negative side effects
      this.emitter.once(Emitter.events.SCROLL_UPDATE, () => {
        try {
          this.setNativeRange(native.start.node, native.start.offset, native.end.node, native.end.offset);
        } catch (ignored) {}
      });
    });
    this.emitter.on(Emitter.events.SCROLL_OPTIMIZE, (mutations, context) => {
      if (context.range) {
        const { startNode, startOffset, endNode, endOffset } = context.range;
        this.setNativeRange(startNode, startOffset, endNode, endOffset);
      }
    });
    this.update(Emitter.sources.SILENT);
  }

  fixInlineEmbed(native) {
    if (native == null) return;
    const [start, end] = [native.start, native.end].map(function(pos) {
      const blot = Parchment.find(pos.node, true);
      if (blot instanceof Embed) {
        let node, offset;
        if (pos.node === blot.leftGuard && pos.offset === 1) {
          [node, offset] = blot.position(blot.length());
          return { node, offset };
        } else if (pos.node === blot.rightGuard && pos.offset === 0) {
          [node, offset] = blot.position(0);
          return { node, offset };
        }
      }
      return pos;
    });
    if (native.start !== start || native.end !== end) {
      this.setNativeRange(start.node, start.offset, end.node, end.offset);
    }
  }

  handleComposition() {
    this.root.addEventListener('compositionstart', () => {
      this.composing = true;
    });
    this.root.addEventListener('compositionend', () => {
      this.composing = false;
      if (this.cursor.parent) {
        const range = this.cursor.restore();
        if (!range) return;
        setTimeout(() => {
          this.setNativeRange(range.startNode, range.startOffset, range.endNode, range.endOffset);
        }, 1);
      }
    });
  }

  handleDragging() {
    this.isMouseDown = false;
    this.emitter.listenDOM('mousedown', document.body, () => {
      this.isMouseDown = true;
    });
    this.emitter.listenDOM('mouseup', document.body, () => {
      this.isMouseDown = false;
      this.update(Emitter.sources.USER);
    });
    this.emitter.listenDOM('selectionchange', document, () => {
      if (!this.isMouseDown) {
        setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
      }
    });
  }

  handleEmbedSelection() {
    this.emitter.on(Emitter.events.SELECTION_CHANGE, () => {
      const selectedNode = document.querySelector('.ql-embed-selected');
      if (selectedNode) {
        selectedNode.classList.remove('ql-embed-selected');
      }
    });
    this.root.addEventListener('click', (e) => {
      const blot = Parchment.find(e.target, true);
      if (blot instanceof Parchment.Embed) {
        const range = new Range(blot.offset(scroll), blot.length());
        this.setRange(range, Emitter.sources.USER);
        blot.domNode.classList.add('ql-embed-selected');
      }
    });
  }

  focus() {
{'fraction_non_alphanumeric': 0.1544443115205168, 'fraction_numerical': 0.003230051441559995, 'mean_word_length': 2.6942112240388862, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1153650', 'n_tokens_mistral': 2644, 'n_tokens_neox': 2577, 'n_words': 499}