forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathstyles-element.js
151 lines (127 loc) · 3.85 KB
/
styles-element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const { Emitter, CompositeDisposable } = require('event-kit');
class StylesElement extends HTMLElement {
constructor() {
super();
this.subscriptions = new CompositeDisposable();
this.emitter = new Emitter();
this.styleElementClonesByOriginalElement = new WeakMap();
this.context = null;
}
onDidAddStyleElement(callback) {
this.emitter.on('did-add-style-element', callback);
}
onDidRemoveStyleElement(callback) {
this.emitter.on('did-remove-style-element', callback);
}
onDidUpdateStyleElement(callback) {
this.emitter.on('did-update-style-element', callback);
}
connectedCallback() {
let left;
this.context =
(left = this.getAttribute('context')) != null ? left : undefined;
}
disconnectedCallback() {
this.subscriptions.dispose();
this.subscriptions = new CompositeDisposable();
}
static get observedAttributes() {
return ['context'];
}
attributeChangedCallback(attrName) {
if (attrName === 'context') {
return this.contextChanged();
}
}
initialize(styleManager) {
this.styleManager = styleManager;
if (this.styleManager == null) {
throw new Error(
'Must pass a styleManager parameter when initializing a StylesElement'
);
}
this.subscriptions.add(
this.styleManager.observeStyleElements(this.styleElementAdded.bind(this))
);
this.subscriptions.add(
this.styleManager.onDidRemoveStyleElement(
this.styleElementRemoved.bind(this)
)
);
this.subscriptions.add(
this.styleManager.onDidUpdateStyleElement(
this.styleElementUpdated.bind(this)
)
);
}
contextChanged() {
if (this.subscriptions == null) {
return;
}
for (let child of Array.from(Array.prototype.slice.call(this.children))) {
this.styleElementRemoved(child);
}
this.context = this.getAttribute('context');
for (let styleElement of Array.from(this.styleManager.getStyleElements())) {
this.styleElementAdded(styleElement);
}
}
styleElementAdded(styleElement) {
let insertBefore;
if (!this.styleElementMatchesContext(styleElement)) {
return;
}
const styleElementClone = styleElement.cloneNode(true);
styleElementClone.sourcePath = styleElement.sourcePath;
styleElementClone.context = styleElement.context;
styleElementClone.priority = styleElement.priority;
this.styleElementClonesByOriginalElement.set(
styleElement,
styleElementClone
);
const { priority } = styleElement;
if (priority != null) {
for (let child of this.children) {
if (child.priority > priority) {
insertBefore = child;
break;
}
}
}
this.insertBefore(styleElementClone, insertBefore);
this.emitter.emit('did-add-style-element', styleElementClone);
}
styleElementRemoved(styleElement) {
let left;
if (!this.styleElementMatchesContext(styleElement)) {
return;
}
const styleElementClone =
(left = this.styleElementClonesByOriginalElement.get(styleElement)) !=
null
? left
: styleElement;
styleElementClone.remove();
this.emitter.emit('did-remove-style-element', styleElementClone);
}
styleElementUpdated(styleElement) {
if (!this.styleElementMatchesContext(styleElement)) {
return;
}
const styleElementClone = this.styleElementClonesByOriginalElement.get(
styleElement
);
styleElementClone.textContent = styleElement.textContent;
this.emitter.emit('did-update-style-element', styleElementClone);
}
styleElementMatchesContext(styleElement) {
return this.context == null || styleElement.context === this.context;
}
}
window.customElements.define('atom-styles', StylesElement);
function createStylesElement() {
return document.createElement('atom-styles');
}
module.exports = {
createStylesElement
};