Sha256: f148b01ea5c464fa8a7c850beba90df2d31273d7fcbf6da20ec566f02bb2e5d5

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

#= require trix/views/block_view

{defer, makeElement} = Trix

class Trix.DocumentView extends Trix.ObjectView
  @render: (document) ->
    element = makeElement("div")
    view = new this document, {element}
    view.render()
    view.sync()
    element

  constructor: ->
    super
    {@element} = @options
    @elementStore = new Trix.ElementStore
    @setDocument(@object)

  setDocument: (document) ->
    unless document.isEqualTo(@document)
      @document = @object = document

  render: ->
    @childViews = []

    @shadowElement = makeElement("div")

    unless @document.isEmpty()
      objects = Trix.ObjectGroup.groupObjects(@document.getBlocks(), asTree: true)
      for object in objects
        view = @findOrCreateCachedChildView(Trix.BlockView, object)
        @shadowElement.appendChild(node) for node in view.getNodes()

  isSynced: ->
    elementsHaveEqualHTML(@shadowElement, @element)

  sync: ->
    fragment = @createDocumentFragmentForSync()
    @element.removeChild(@element.lastChild) while @element.lastChild
    @element.appendChild(fragment)
    @didSync()

  # Private

  didSync: ->
    @elementStore.reset(findStoredElements(@element))
    defer => @garbageCollectCachedViews()

  createDocumentFragmentForSync: ->
    fragment = document.createDocumentFragment()

    for node in @shadowElement.childNodes
      fragment.appendChild(node.cloneNode(true))

    for element in findStoredElements(fragment)
      if storedElement = @elementStore.remove(element)
        element.parentNode.replaceChild(storedElement, element)

    fragment

  findStoredElements = (element) ->
    element.querySelectorAll("[data-trix-store-key]")

  elementsHaveEqualHTML = (element, otherElement) ->
    ignoreSpaces(element.innerHTML) is ignoreSpaces(otherElement.innerHTML)

  ignoreSpaces = (html) ->
    html.replace(/ /g, " ")

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vapid-0.1.3 lib/vapid/vendor/trix/src/trix/views/document_view.coffee
vapid-0.1.2 lib/vapid/vendor/trix/src/trix/views/document_view.coffee
vapid-0.1.1 lib/vapid/vendor/trix/src/trix/views/document_view.coffee
vapid-0.1.0 lib/vapid/vendor/trix/src/trix/views/document_view.coffee