Skip to content

Commit

Permalink
styles: actually add/remove the element
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlucas committed Mar 20, 2016
1 parent fc427b6 commit 3357536
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/styles/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ class StylesElement extends HTMLElement {

elementRemoved(ele) {
const clone = this.clones.get(ele) || ele
ele.remove()
clone.remove()
if (clone !== ele) ele.remove()
console.log('remove ele', ele)
this.emitter.emit('did-remove-style-element', clone)
}

Expand Down
24 changes: 22 additions & 2 deletions lib/styles/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const StylesElement = require('./element')
const EE = require('events')
const debug = require('debug')('eyearesee:styles:manager')

module.exports = Manager

Expand Down Expand Up @@ -33,6 +34,17 @@ Manager.prototype.onDidUpdateElement = function onDidUpdateElement(cb) {
this.emitter.on('did-update-style-element', cb)
}

Manager.prototype.removeStyleSheet = function removeStyleSheet(fp) {
const ele = this.elementsByPath.get(fp)
if (!ele) {
debug('cannot find ele to remove %s', fp)
return false
}

debug('remove ele %s', fp)
this.removeElement(ele)
}

Manager.prototype.getStyleElements = function getStyleElements() {
return this.elements.slice()
}
Expand All @@ -49,6 +61,8 @@ Manager.prototype.addStyleSheet = function addStyleSheet(source, params) {
const context = params.context
let ele
let updated = false

debug('addStyleSheet %s', sourcePath)
if (sourcePath && this.elementsByPath.has(sourcePath)) {
ele = this.elementsByPath.get(sourcePath)
updated = true
Expand All @@ -68,8 +82,10 @@ Manager.prototype.addStyleSheet = function addStyleSheet(source, params) {
ele.textContent = source

if (updated) {
debug('update existing ele')
this.emitter.emit('did-update-style-element', ele)
} else {
debug('add new ele')
this.addElement(ele)
}
}
Expand All @@ -84,11 +100,15 @@ Manager.prototype.addElement = function addElement(ele) {
}

Manager.prototype.removeElement = function removeElement(ele) {
debug('remove ele')
const idx = this.elements.indexOf(ele)
if (index === -1) return
if (idx === -1) {
debug('could not find ele')
return
}
this.elements.splice(idx, 1)
this.elementsByPath.delete(ele.sourcePath)
this.emit('did-remove-style-element', ele)
this.emitter.emit('did-remove-style-element', ele)
}

Manager.prototype.getSnapshot = function getSnapshot() {
Expand Down

0 comments on commit 3357536

Please sign in to comment.