<commit_before>import { Directive, Injectable, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core'
import { RichCollaborator, RichCollaboratorsService } from 'doc/rich-collaborators/'
import { NetworkService } from 'doc/network/'
import { ServiceIdentifier } from 'helper/ServiceIdentifier'

import { Subscription } from 'rxjs'

import * as CodeMirror from 'codemirror'
import { DocService, NetworkMessage } from 'mute-core'
import { Identifier } from 'mute-structs'

const pb = require('./cursor_pb.js')

@Injectable() @Directive({ selector: '[muteCursors]' })
export class CursorsDirective extends ServiceIdentifier implements OnChanges, OnInit {

  @Input() cmEditor: CodeMirror.Editor
  @Input() docService: DocService

  private messageSubscription: Subscription

  private cmCursors: Map<number, CmCursor> // CodeMirror cursors of other peers
  private isInited = false
  private pbCursor: any
  private pbPosition: any // My cursor

  constructor (
    private collabService: RichCollaboratorsService,
    private network: NetworkService
  ) {
    super('Cursor')
    this.cmCursors = new Map()
    this.pbPosition = new pb.Position()
    this.pbCursor = new pb.Cursor()
  }

  ngOnInit () {
    const cmDoc: CodeMirror.Doc = this.cmEditor.getDoc()

    this.collabService.onCollaboratorJoin.subscribe((collaborator: RichCollaborator) => {
      this.cmCursors.set(collaborator.id, new CmCursor(cmDoc, collaborator.color))
    })

    this.collabService.onCollaboratorLeave.subscribe((id: number) => {
      const cursor: CmCursor | undefined = this.cmCursors.get(id)
      if (cursor !== undefined) {
        if (cursor.cmBookmark !== null) {
          cursor.cmBookmark.clear()
        }
        cursor.stopClotting()
        this.cmCursors.delete(id)
      }
    })

    const updateCursor = (): void => {
      const cursor: {index: number, last?: number, base?: number[]} | null =
        this.docService.idFromIndex(cmDoc.indexFromPos(cmDoc.getCursor()))
      if (cursor === null) {
        this.pbCursor.setVisible(true)
      } else {
        this.pbPosition.setIndex(cursor.index)
        this.pbPosition.setLast(cursor.last)
        this.pbPosition.setBaseList(cursor.base)
        this.pbCursor.setPosition(this.pbPosition)
      }
      this.network.newSend(this.constructor.name, this.pbCursor.serializeBinary())
    }

    CodeMirror.on(this.cmEditor, 'blur', () => {
      this.pbCursor.setVisible(false)
      this.network.newSend(this.constructor.name, this.pbCursor.serializeBinary())
      CodeMirror.off(cmDoc, 'cursorActivity', updateCursor)
    })

    CodeMirror.on(this.cmEditor, 'focus', () => {
      updateCursor()
      CodeMirror.on(cmDoc, 'cursorActivity', updateCursor)
    })
  }

  ngOnChanges (changes: SimpleChanges): void {<commit_msg>fix(cursor): Edit directive to use NetworkService.send() instead of newSend()<commit_after>import { Directive, Injectable, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core'
import { RichCollaborator, RichCollaboratorsService } from 'doc/rich-collaborators/'
import { NetworkService } from 'doc/network/'
import { ServiceIdentifier } from 'helper/ServiceIdentifier'

import { Subscription } from 'rxjs'

import * as CodeMirror from 'codemirror'
import { DocService, NetworkMessage } from 'mute-core'
import { Identifier } from 'mute-structs'

const pb = require('./cursor_pb.js')

@Injectable() @Directive({ selector: '[muteCursors]' })
export class CursorsDirective extends ServiceIdentifier implements OnChanges, OnInit {

  @Input() cmEditor: CodeMirror.Editor
  @Input() docService: DocService

  private messageSubscription: Subscription

  private cmCursors: Map<number, CmCursor> // CodeMirror cursors of other peers
  private isInited = false
  private pbCursor: any
  private pbPosition: any // My cursor

  constructor (
    private collabService: RichCollaboratorsService,
    private network: NetworkService
  ) {
    super('Cursor')
    this.cmCursors = new Map()
    this.pbPosition = new pb.Position()
    this.pbCursor = new pb.Cursor()
  }

  ngOnInit () {
    const cmDoc: CodeMirror.Doc = this.cmEditor.getDoc()

    this.collabService.onCollaboratorJoin.subscribe((collaborator: RichCollaborator) => {
      this.cmCursors.set(collaborator.id, new CmCursor(cmDoc, collaborator.color))
    })

    this.collabService.onCollaboratorLeave.subscribe((id: number) => {
      const cursor: CmCursor | undefined = this.cmCursors.get(id)
      if (cursor !== undefined) {
        if (cursor.cmBookmark !== null) {
          cursor.cmBookmark.clear()
        }
        cursor.stopClotting()
        this.cmCursors.delete(id)
      }
    })

    const updateCursor = (): void => {
      const cursor: {index: number, last?: number, base?: number[]} | null =
        this.docService.idFromIndex(cmDoc.indexFromPos(cmDoc.getCursor()))
      if (cursor === null) {
        this.pbCursor.setVisible(true)
      } else {
        this.pbPosition.setIndex(cursor.index)
        this.pbPosition.setLast(cursor.last)
        this.pbPosition.setBaseList(cursor.base)
        this.pbCursor.setPosition(this.pbPosition)
      }
      this.network.send(this.constructor.name, this.pbCursor.serializeBinary())
    }

    CodeMirror.on(this.cmEditor, 'blur', () => {
      this.pbCursor.setVisible(false)
      this.network.send(this.constructor.name, this.pbCursor.serializeBinary())
      CodeMirror.off(cmDoc, 'cursorActivity', updateCursor)
    })

    CodeMirror.on(this.cmEditor, 'focus', () => {
      updateCursor()
      CodeMirror.on(cmDoc, 'cursorActivity', updateCursor)
    })
  }

  ngOnChanges (changes: SimpleChanges): void {
{'fraction_non_alphanumeric': 0.12131961688542037, 'fraction_numerical': 0.0, 'mean_word_length': 3.6603305785123967, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '6263177', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1703, 'n_words': 348}