1 utils = require 'utils'
2 { Marker } = require 'marker'
3 { addHuffmanCodeWordsTo } = require 'huffman'
5 { interfaces: Ci } = Components
7 HTMLDocument = Ci.nsIDOMHTMLDocument
8 XULDocument = Ci.nsIDOMXULDocument
10 CONTAINER_ID = 'VimFxHintMarkerContainer'
12 createHintsContainer = (document) ->
13 container = document.createElement('div')
14 container.id = CONTAINER_ID
15 container.className = 'VimFxReset'
18 # Creates and injects hint markers into the DOM
19 injectHints = (document) ->
22 # First remove previous hints container
25 # For now we aren't able to handle hint markers in XUL Documents :(
26 if document instanceof HTMLDocument# or document instanceof XULDocument
27 if document.documentElement
28 # Find and create markers
29 markers = Marker.createMarkers(document)
31 container = createHintsContainer(document)
33 # For performance use Document Fragment
34 fragment = document.createDocumentFragment()
36 fragment.appendChild(marker.markerElement)
38 container.appendChild(fragment)
39 document.documentElement.appendChild(container)
41 for frame in document.defaultView.frames
42 markers = markers.concat(inner(frame.document))
46 markers = inner(document)
48 # Add hints to the markers
49 hintChars = utils.getHintChars()
50 weightsAndMarkers = markers.map((marker) -> [marker.weight, marker])
51 addHuffmanCodeWordsTo(weightsAndMarkers, {alphabet: hintChars})
52 for [weight, marker, hint] in weightsAndMarkers
57 # Remove previously injected hints from the DOM
58 removeHints = (document) ->
59 if container = document.getElementById(CONTAINER_ID)
60 document.documentElement.removeChild(container)
62 for frame in document.defaultView.frames
63 removeHints(frame.document)
66 exports.injectHints = injectHints
67 exports.removeHints = removeHints