src/client/components/PlainMarkdownInput/slate/transforms.js
function wrapEmphasis(accent, state) {
  const marker = EMPHASIS[accent];
  const markerLength = marker.length;
  const { startOffset, endOffset, focusText, texts } = state;
  const { text } = focusText;
  const focusKey = focusText.key;
  let change = state.change();

  // #1 no selection
  if (startOffset === endOffset) {
    change.insertText(`${marker}${marker}`).
      move(-markerLength);

    // selection (this edge is selection edge)
  } else {
    const characters = texts.get(0).charsData.characters;
    const delMarkers = delInternalMarkersBind({ change, focusKey, characters, accent });
    const {
      hasLeftOnEmphasis
    } = getLeftSelectionEdgeData({ accent, characters, startOffset });
    const {
      hasRightOnEmphasis
    } = getRightSelectionEdgeData({ accent, characters, endOffset, text });

    if (hasLeftOnEmphasis) {
      if (hasRightOnEmphasis) {
        // #2 both edges on emphasis, delete all internal markers
        change = unionEmphasis({
          change, focusKey, characters, text, accent, startOffset, endOffset, markerLength
        });
      } else {
        // #3 left edge on emphasis, right edge beyond markers
        const leftEmphEdge = getLeftEmphEdge(accent, characters, startOffset);
        const leftMarker = text.substr(leftEmphEdge, markerLength);
        delMarkers({ startPos: leftEmphEdge, endPos: endOffset - 1 });
        change.insertTextByKey(focusKey, endOffset - 1, leftMarker);
      }
    } else {
      if (hasRightOnEmphasis) {
        // #4 left edge beyond markers, right edge on emphasis
        const rightEmphEdge = getRightEmphEdge(accent, characters, endOffset - 1, text.length);
        const rightMarker = text.substr(rightEmphEdge - markerLength + 1, markerLength);
        delMarkers({ startPos: startOffset, endPos: rightEmphEdge });
        change.insertTextByKey(focusKey, startOffset, rightMarker);
      } else {
        // #5 both edges beyond markers
        // delete all internal markers, wrap selection in markers
        delMarkers({ startPos: startOffset, endPos: endOffset });
        change.wrapText(marker, marker).focus();
      }
    }
  }

  change.focus();
  return change.state;
}
function wrapEmphasis(accent, state) {
  const marker = EMPHASIS[accent];
  const markerLength = marker.length;
  const { startOffset, endOffset, focusText, texts } = state;
  const { text } = focusText;
  const focusKey = focusText.key;
  let change = state.change();

  // #1 no selection
  if (startOffset === endOffset) {
    change.insertText(`${marker}${marker}`).
      move(-markerLength);

    // selection (this edge is selection edge)
  } else {
    const characters = texts.get(0).charsData.characters;
    const delMarkers = delInternalMarkersBind({ change, focusKey, characters, accent });
    const {
      hasLeftOnEmphasis
    } = getLeftSelectionEdgeData({ accent, characters, startOffset });
    const {
      hasRightOnEmphasis
    } = getRightSelectionEdgeData({ accent, characters, endOffset, text });

    if (hasLeftOnEmphasis) {
      if (hasRightOnEmphasis) {
        // #2 both edges on emphasis, delete all internal markers
        change = unionEmphasis({
          change, focusKey, characters, text, accent, startOffset, endOffset, markerLength
        });
      } else {
        // #3 left edge on emphasis, right edge beyond markers
        const leftEmphEdge = getLeftEmphEdge(accent, characters, startOffset);
        const leftMarker = text.substr(leftEmphEdge, markerLength);
        delMarkers({ startPos: leftEmphEdge, endPos: endOffset - 1 });
        change.insertTextByKey(focusKey, endOffset - 1, leftMarker);
      }
    } else {
      if (hasRightOnEmphasis) {
        // #4 left edge beyond markers, right edge on emphasis
        const rightEmphEdge = getRightEmphEdge(accent, characters, endOffset - 1, text.length);
        const rightMarker = text.substr(rightEmphEdge - markerLength + 1, markerLength);
        delMarkers({ startPos: startOffset, endPos: rightEmphEdge });
        change.insertTextByKey(focusKey, startOffset, rightMarker);
      } else {
        // #5 both edges beyond markers
        // delete all internal markers, wrap selection in markers
        delMarkers({ startPos: startOffset, endPos: endOffset });
        change.wrapText(marker, marker).focus();
      }
    }
  }

  change.focus();
  return change.state;
}
function wrapEmphasis ( accent , state ) { const marker = EMPHASIS [ accent ] ; const markerLength = marker . length ; const { startOffset , endOffset , focusText , texts } = state ; const { text } = focusText ; const focusKey = focusText . key ; let change = state . change ( ) ; // #1 no selection if ( startOffset === endOffset ) { change . insertText ( ` ${ marker } ${ marker } ` ) . move ( - markerLength ) ; // selection (this edge is selection edge) } else { const characters = texts . get ( 0 ) . charsData . characters ; const delMarkers = delInternalMarkersBind ( { change , focusKey , characters , accent } ) ; const { hasLeftOnEmphasis } = getLeftSelectionEdgeData ( { accent , characters , startOffset } ) ; const { hasRightOnEmphasis } = getRightSelectionEdgeData ( { accent , characters , endOffset , text } ) ; if ( hasLeftOnEmphasis ) { if ( hasRightOnEmphasis ) { // #2 both edges on emphasis, delete all internal markers change = unionEmphasis ( { change , focusKey , characters , text , accent , startOffset , endOffset , markerLength } ) ; } else { // #3 left edge on emphasis, right edge beyond markers const leftEmphEdge = getLeftEmphEdge ( accent , characters , startOffset ) ; const leftMarker = text . substr ( leftEmphEdge , markerLength ) ; delMarkers ( { startPos : leftEmphEdge , endPos : endOffset - 1 } ) ; change . insertTextByKey ( focusKey , endOffset - 1 , leftMarker ) ; } } else { if ( hasRightOnEmphasis ) { // #4 left edge beyond markers, right edge on emphasis const rightEmphEdge = getRightEmphEdge ( accent , characters , endOffset - 1 , text . length ) ; const rightMarker = text . substr ( rightEmphEdge - markerLength + 1 , markerLength ) ; delMarkers ( { startPos : startOffset , endPos : rightEmphEdge } ) ; change . insertTextByKey ( focusKey , startOffset , rightMarker ) ; } else { // #5 both edges beyond markers // delete all internal markers, wrap selection in markers delMarkers ( { startPos : startOffset , endPos : endOffset } ) ; change . wrapText ( marker , marker ) . focus ( ) ; } } } change . focus ( ) ; return change . state ; }
Wrap text with accent

@param accent
@param state
Wrap text with accent