-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathumb-debug-ext.js
More file actions
310 lines (264 loc) · 8.41 KB
/
umb-debug-ext.js
File metadata and controls
310 lines (264 loc) · 8.41 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
* Plain-JavaScript version of umb-debug-src.js for Chrome extension injection.
*
* Injected as a <script type="module"> into the Umbraco backoffice page so it
* can use the page's import map to resolve @umbraco-cms/backoffice/* modules.
* Defines the <umb-debug-ext> custom element using the same logic as the
* Umbraco source, but without TypeScript decorators or the modal dialog feature.
*/
import { css, html, map, nothing, when } from '@umbraco-cms/backoffice/external/lit';
import { contextData, UmbContextDebugRequest } from '@umbraco-cms/backoffice/context-api';
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import { firstValueFrom } from '@umbraco-cms/backoffice/external/rxjs';
class UmbDebugElementExt extends UmbLitElement {
static properties = {
visible: { type: Boolean },
dialog: { type: Boolean },
_contexts: { state: true },
_debugPaneOpen: { state: true },
};
callbackTimeoutId = -1;
constructor() {
super();
this.visible = false;
this.dialog = false;
this._contexts = new Map();
this._debugPaneOpen = false;
this.#update();
}
connectedCallback() {
super.connectedCallback();
this.#internalLog("connected callback");
this.#update();
}
static get observedAttributes() {
return ['data-umb-command'];
}
attributeChangedCallback(name, oldValue, newValue) {
super.attributeChangedCallback?.(name, oldValue, newValue);
if (name === 'data-umb-command' && newValue) {
const { method, args } = JSON.parse(newValue);
if (typeof this[method] === 'function') {
this[method](...(args || []));
}
// Clear it so the same command can be re-triggered later
this.removeAttribute('data-umb-command');
}
}
test(ins) {
this.#getContextInfo(ins);
}
#update() {
this.#internalLog("update called");
this.dispatchEvent(
new UmbContextDebugRequest((contexts) => {
this.#internalLog("callback with context...", contexts);
this._contexts = contexts;
this.#exportContexts(contexts);
//this.#getContextInfo('UmbSectionSidebarContext');
}),
);
}
#getContextInfo(contextAlias) {
var contextProps = { alias: contextAlias, props: {} };
this.consumeContext(contextAlias, (contextBack) => {
this.#internalLog("Context kickback", {contextAlias, contextBack});
if(!contextBack){
return;
}
const props = [
...Object.keys(contextBack),
...Object.keys(Object.getPrototypeOf(contextBack))
];
props.forEach(key => {
const val = contextBack[key];
if (val && typeof val.subscribe === 'function') {
this.#internalLog(`[Observable found] ${key}`);
this.observe(val, (value) => {
this.#internalLog("I'm observing!", {key, value});
contextProps.props[key] = this.#safeValue(value, 0);
this.#updateContextAttribute(contextProps);
});
} else if (typeof val !== 'function') {
// Capture plain non-function values immediately
contextProps.props[key] = this.#safeValue(val, 0);
this.#updateContextAttribute(contextProps);
}
});
});
}
#updateContextAttribute(contextProps) {
this.#internalLog("Check the data out and push", contextProps);
try {
clearTimeout(this.callbackTimeoutId);
this.callbackTimeoutId = setTimeout(()=>{
// Send the data from here to the content.js -> panel.js
window.postMessage({ type: 'contextData', data: contextProps }, '*');
}, 50);
// this.setAttribute('data-umb-context-data', JSON.stringify(contextProps));
} catch (e) {
console.warn('[Context] Failed to serialise context props', e);
}
}
/**
* Serialize context data to a JSON attribute so the Chrome extension's
* content script (isolated world) can read it via the shared DOM.
*/
#exportContexts(contexts) {
try {
const data = contextData(contexts);
const serialized = Array.from(data).map(({ alias, data: instance }) => {
const entry = { alias, type: instance.type };
if (instance.type === 'object') {
entry.methods = instance.methods ?? [];
entry.properties = (instance.properties ?? []).map((p) => {
const prop = { key: p.key, type: p.type };
if (p.value !== undefined) {
prop.value = this.#safeValue(p.value, 0);
}
return prop;
});
} else if (instance.type === 'primitive') {
entry.value = this.#safeValue(instance.value, 0);
}
return entry;
});
var s = JSON.stringify(serialized);
this.setAttribute('data-umb-debug-contexts', JSON.stringify(serialized));
this.#internalLog("Serialised the ocntexts", s);
} catch (ex) {
// Ignore serialization errors
this.#internalLog("Unable to add attr", ex)
}
}
#safeValue(val, depth) {
if (depth > 6) return '[nested]';
if (val === null || val === undefined) return null;
const type = typeof val;
if (type === 'function') return '[Function]';
if (type === 'symbol') return '[Symbol]';
if (type !== 'object') return val;
if (Array.isArray(val)) {
return val.slice(0, 20).map((v) => this.#safeValue(v, depth + 1));
}
const keys = Object.keys(val);
const result = {};
let count = 0;
for (const k of keys) {
if (count++ > 30) { result['...'] = `${keys.length - 30} more keys`; break; }
try { result[k] = this.#safeValue(val[k], depth + 1); } catch { result[k] = '[Error]'; }
}
return result;
}
#toggleDebugPane() {
this._debugPaneOpen = !this._debugPaneOpen;
if (this._debugPaneOpen) {
this.#update();
}
}
#internalLog(...args) {
//console.log(...args);
}
render() {
if (!this.visible) return nothing;
return this.#renderPanel();
}
#renderPanel() {
return html`
<div id="container">
<uui-button color="danger" look="primary" @click=${this.#toggleDebugPane}>
<uui-icon name="icon-bug"></uui-icon>
<span>Debug</span>
</uui-button>
${when(this._debugPaneOpen, () => this.#renderContextAliases())}
</div>
`;
}
#renderContextAliases() {
const data = contextData(this._contexts);
return html`
<div class="events">
${map(data, (context) => html`
<details>
<summary><strong>${context.alias}</strong></summary>
${this.#renderInstance(context.data)}
</details>
`)}
</div>
`;
}
#renderInstance(instance) {
switch (instance.type) {
case 'function':
return html`<h3>Callable Function</h3>`;
case 'object':
return html`
<details>
<summary>Methods</summary>
<ul>
${map(instance.methods, (methodName) => html`<li>${methodName}</li>`)}
</ul>
</details>
<details>
<summary>Properties</summary>
<ul>
${map(instance.properties, (prop) => {
switch (prop.type) {
case 'string':
case 'number':
case 'boolean':
case 'object':
return html`<li>${prop.key} <em>(${prop.type})</em> = ${prop.value}</li>`;
default:
return html`<li>${prop.key} <em>(${prop.type})</em></li>`;
}
})}
</ul>
</details>
`;
case 'primitive':
return html`<p>Context is a primitive with value: ${instance.value}</p>`;
default:
return html`<p>Unknown type: ${instance.type}</p>`;
}
}
static styles = [
css`
:host {
float: right;
font-family: monospace;
position: relative;
z-index: 10000;
}
#container {
display: flex;
flex-direction: column;
align-items: flex-end;
}
uui-badge {
cursor: pointer;
gap: 0.5rem;
}
uui-icon {
font-size: 15px;
}
.events {
background-color: var(--uui-color-danger);
color: var(--uui-color-selected-contrast);
padding: 1rem;
}
summary {
cursor: pointer;
}
details > details {
margin-left: 1rem;
}
ul {
margin-top: 0;
}
`,
];
}
if (!customElements.get('umb-debug-ext')) {
customElements.define('umb-debug-ext', UmbDebugElementExt);
}