-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐎 Speed up rendering by sharing cache between instances
- Loading branch information
Showing
2 changed files
with
56 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Mixin = require 'mixto' | ||
|
||
module.exports = | ||
class DOMStylesReader extends Mixin | ||
@domStylesCache: {} | ||
|
||
# Internal: This function insert a dummy element in the DOM to compute | ||
# its style, return the specified property, and remove the element | ||
# from the DOM. | ||
# | ||
# scopes - An {Array} of {String} reprensenting the scope to reproduce. | ||
# property - The property {String} name. | ||
# | ||
# Returns a {String} of the property value. | ||
retrieveStyleFromDom: (scopes, property, shadowRoot=true) -> | ||
key = scopes.join(' ') | ||
|
||
if @constructor.domStylesCache[key]?[property]? | ||
return @constructor.domStylesCache[key][property] | ||
|
||
@ensureDummyNodeExistence(shadowRoot) | ||
@constructor.domStylesCache[key] ?= {} | ||
|
||
parent = @dummyNode | ||
for scope in scopes | ||
node = document.createElement('span') | ||
# css class is the scope without the dots, | ||
# see pushScope @ atom/atom/src/lines-component.coffee | ||
node.className = scope.replace(/\.+/g, ' ') | ||
parent.appendChild(node) if parent? | ||
parent = node | ||
|
||
value = getComputedStyle(parent).getPropertyValue(property) | ||
@dummyNode.innerHTML = '' | ||
|
||
@constructor.domStylesCache[key][property] = value | ||
|
||
# Internal: Creates a DOM node container for all the operations that | ||
# need to read styles properties from DOM. | ||
ensureDummyNodeExistence: (shadowRoot) -> | ||
unless @dummyNode? | ||
@dummyNode = document.createElement('span') | ||
@dummyNode.style.visibility = 'hidden' | ||
if shadowRoot | ||
@editorView.shadowRoot.appendChild(@dummyNode) | ||
else | ||
@editorView.appendChild(@dummyNode) | ||
|
||
invalidateCache: -> | ||
@constructor.domStylesCache = {} |