<commit_before>import {Subject} from 'rxjs'
import {EditorChange, EditorSelection, PortableTextSlateEditor} from '../../types/editor'
import {debugWithName} from '../../utils/debug'
import {toPortableTextRange} from '../../utils/ranges'
import {SLATE_TO_PORTABLE_TEXT_RANGE} from '../../utils/weakMaps'

const debug = debugWithName('plugin:withPortableTextSelections')

export function createWithPortableTextSelections(change$: Subject<EditorChange>) {
  return function withPortableTextSelections(
    editor: PortableTextSlateEditor
  ): PortableTextSlateEditor {
    const {onChange} = editor
    const emitSelection = () => {
      if (editor.selection) {
        let ptRange: EditorSelection = null
        const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection)
        if (existing) {
          ptRange = existing
        } else {
          ptRange = toPortableTextRange(editor, editor.selection)
          SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange)
        }
        debug(`Emitting selection ${JSON.stringify(ptRange)}`)
        change$.next({type: 'selection', selection: ptRange})
      }
    }
    editor.onChange = () => {
      onChange()
      emitSelection()
    }
    return editor
  }
}<commit_msg>refactor(portable-text-editor): always emit selection when changed<commit_after>import {Path} from '@sanity/types/src'
import {Subject} from 'rxjs'
import {EditorChange, EditorSelection, PortableTextSlateEditor} from '../../types/editor'
import {debugWithName} from '../../utils/debug'
import {toPortableTextRange} from '../../utils/ranges'
import {SLATE_TO_PORTABLE_TEXT_RANGE} from '../../utils/weakMaps'

const debug = debugWithName('plugin:withPortableTextSelections')

export function createWithPortableTextSelections(change$: Subject<EditorChange>) {
  return function withPortableTextSelections(
    editor: PortableTextSlateEditor
  ): PortableTextSlateEditor {
    const getSelection = () => {
      let ptRange: EditorSelection = null
      if (editor.selection) {
        const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection)
        if (existing) {
          ptRange = existing
        } else {
          ptRange = toPortableTextRange(editor, editor.selection)
        }
        SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange)
      }
      return ptRange
    }

    const {onChange} = editor
    editor.onChange = () => {
      onChange()
      const sel = getSelection()
      if (sel) {
        debug(`Emitting selection ${JSON.stringify(sel)}`)
        change$.next({type: 'selection', selection: sel})
      } else {
        change$.next({type: 'selection', selection: null})
      }
    }
    return editor
  }
}
{'fraction_non_alphanumeric': 0.11847463902258423, 'fraction_numerical': 0.0, 'mean_word_length': 3.358064516129032, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '5400781', 'n_tokens_mistral': 772, 'n_tokens_neox': 779, 'n_words': 162}