_ = require 'underscore-plus' React = require 'react-atom-fork' {div, span} = require 'reactionary-atom-fork' {debounce, isEqual, isEqualForProperties, multiplyString, toArray} = require 'underscore-plus' {$$} = require 'space-pen' Decoration = require './decoration' CursorsComponent = require './cursors-component' HighlightsComponent = require './highlights-component' textUtils = require './text-utils' DummyLineNode = $$(-> @div className: 'line', style: 'position: absolute; visibility: hidden;', => @span 'x')[0] AcceptFilter = {acceptNode: -> NodeFilter.FILTER_ACCEPT} WrapperDiv = document.createElement('div') module.exports = LinesComponent = React.createClass displayName: 'LinesComponent' render: -> {performedInitialMeasurement, cursorBlinkPeriod, cursorBlinkResumeDelay} = @props if performedInitialMeasurement {editor, highlightDecorations, scrollHeight, scrollWidth, placeholderText, backgroundColor} = @props {lineHeightInPixels, defaultCharWidth, scrollViewHeight, scopedCharacterWidthsChangeCount} = @props {scrollTop, scrollLeft, cursorPixelRects, mini} = @props style = height: Math.max(scrollHeight, scrollViewHeight) width: scrollWidth WebkitTransform: @getTransform() backgroundColor: if mini then null else backgroundColor div {className: 'lines', style}, div className: 'placeholder-text', placeholderText if placeholderText? CursorsComponent { cursorPixelRects, cursorBlinkPeriod, cursorBlinkResumeDelay, lineHeightInPixels, defaultCharWidth, scopedCharacterWidthsChangeCount, performedInitialMeasurement } HighlightsComponent { editor, highlightDecorations, lineHeightInPixels, defaultCharWidth, scopedCharacterWidthsChangeCount, performedInitialMeasurement } getTransform: -> {scrollTop, scrollLeft, useHardwareAcceleration} = @props if useHardwareAcceleration "translate3d(#{-scrollLeft}px, #{-scrollTop}px, 0px)" else "translate(#{-scrollLeft}px, #{-scrollTop}px)" componentWillMount: -> @measuredLines = new WeakSet @lineNodesByLineId = {} @screenRowsByLineId = {} @lineIdsByScreenRow = {} @renderedDecorationsByLineId = {} shouldComponentUpdate: (newProps) -> return true unless isEqualForProperties(newProps, @props, 'renderedRowRange', 'lineDecorations', 'highlightDecorations', 'lineHeightInPixels', 'defaultCharWidth', 'scrollTop', 'scrollLeft', 'showIndentGuide', 'scrollingVertically', 'visible', 'scrollViewHeight', 'mouseWheelScreenRow', 'scopedCharacterWidthsChangeCount', 'lineWidth', 'useHardwareAcceleration', 'placeholderText', 'performedInitialMeasurement', 'backgroundColor', 'cursorPixelRects' ) {renderedRowRange, pendingChanges} = newProps return false unless renderedRowRange? [renderedStartRow, renderedEndRow] = renderedRowRange for change in pendingChanges if change.screenDelta is 0 return true unless change.end < renderedStartRow or renderedEndRow <= change.start else return true unless renderedEndRow <= change.start false componentDidUpdate: (prevProps) -> {visible, scrollingVertically, performedInitialMeasurement} = @props return unless performedInitialMeasurement @clearScreenRowCaches() unless prevProps.lineHeightInPixels is @props.lineHeightInPixels @removeLineNodes() unless isEqualForProperties(prevProps, @props, 'showIndentGuide') @updateLines(@props.lineWidth isnt prevProps.lineWidth) @measureCharactersInNewLines() if visible and not scrollingVertically clearScreenRowCaches: -> @screenRowsByLineId = {} @lineIdsByScreenRow = {} updateLines: (updateWidth) -> {tokenizedLines, renderedRowRange, showIndentGuide, selectionChanged, lineDecorations} = @props [startRow] = renderedRowRange @removeLineNodes(tokenizedLines) @appendOrUpdateVisibleLineNodes(tokenizedLines, startRow, updateWidth) removeLineNodes: (visibleLines=[]) -> {mouseWheelScreenRow} = @props visibleLineIds = new Set visibleLineIds.add(line.id.toString()) for line in visibleLines node = @getDOMNode() for lineId, lineNode of @lineNodesByLineId when not visibleLineIds.has(lineId) screenRow = @screenRowsByLineId[lineId] if not screenRow? or screenRow isnt mouseWheelScreenRow delete @lineNodesByLineId[lineId] delete @lineIdsByScreenRow[screenRow] if @lineIdsByScreenRow[screenRow] is lineId delete @screenRowsByLineId[lineId] delete @renderedDecorationsByLineId[lineId] node.removeChild(lineNode) appendOrUpdateVisibleLineNodes: (visibleLines, startRow, updateWidth) -> {lineDecorations} = @props newLines = null newLinesHTML = null for line, index in visibleLines screenRow = startRow + index if @hasLineNode(line.id) @updateLineNode(line, screenRow, updateWidth) else newLines ?= [] newLinesHTML ?= "" newLines.push(line) newLinesHTML += @buildLineHTML(line, screenRow) @screenRowsByLineId[line.id] = screenRow @lineIdsByScreenRow[screenRow] = line.id @renderedDecorationsByLineId[line.id] = lineDecorations[screenRow] return unless newLines? WrapperDiv.innerHTML = newLinesHTML newLineNodes = toArray(WrapperDiv.children) node = @getDOMNode() for line, i in newLines lineNode = newLineNodes[i] @lineNodesByLineId[line.id] = lineNode node.appendChild(lineNode) hasLineNode: (lineId) -> @lineNodesByLineId.hasOwnProperty(lineId) buildLineHTML: (line, screenRow) -> {mini, showIndentGuide, lineHeightInPixels, lineDecorations, lineWidth} = @props {tokens, text, lineEnding, fold, isSoftWrapped, indentLevel} = line classes = '' if decorations = lineDecorations[screenRow] for id, decoration of decorations if Decoration.isType(decoration, 'line') classes += decoration.class + ' ' classes += 'line' top = screenRow * lineHeightInPixels lineHTML = "