@crombolo/backoffice-host 0.40.0 → 0.42.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/package.json +3 -3
  2. package/plugin/addresses-dashboard.js +3 -2
  3. package/plugin/app-preview/umbraco-package.json +1 -1
  4. package/plugin/audit-dashboard.js +3 -2
  5. package/plugin/branding/localization-en.js +9 -3
  6. package/plugin/branding/sign-in.js +0 -31
  7. package/plugin/bundle-editor-code.js +2 -0
  8. package/plugin/bundle-editor-content.js +2 -0
  9. package/plugin/bundle-editor-context.js +95 -0
  10. package/plugin/bundle-editor-forms.js +2 -0
  11. package/plugin/bundle-editor-language.js +2 -0
  12. package/plugin/bundle-editor-routing.js +13 -0
  13. package/plugin/bundle-editor-sections.js +389 -0
  14. package/plugin/bundle-editor-types.js +2 -0
  15. package/plugin/bundle-editor-user-groups.js +2 -0
  16. package/plugin/bundle-editor-workflows.js +2 -0
  17. package/plugin/bundle-editor.js +140 -0
  18. package/plugin/bundles-client.js +12 -0
  19. package/plugin/bundles-created.js +2 -1
  20. package/plugin/changes-dashboard.js +3 -2
  21. package/plugin/edit-conflicts.js +11 -21
  22. package/plugin/jobs-dashboard.js +3 -2
  23. package/plugin/localizations/umbraco-package.json +1 -1
  24. package/plugin/notifications-dialog.js +8 -35
  25. package/plugin/seo/umbraco-package.json +1 -1
  26. package/plugin/side-panel/host.js +7 -63
  27. package/plugin/side-panel/layout.js +0 -15
  28. package/plugin/sign-in/umbraco-package.json +1 -1
  29. package/plugin/tsx-editors.js +56 -164
  30. package/plugin/umbraco-package.json +138 -32
  31. package/plugin/branding/branding.js +0 -188
  32. package/plugin/branding/bundle-builder-labels.js +0 -92
  33. package/plugin/branding/client-credentials.js +0 -49
  34. package/plugin/branding/log-viewer-menu.js +0 -70
  35. package/plugin/branding/query-builder.js +0 -28
@@ -1,26 +1,16 @@
1
1
  /**
2
2
  * Makes Umbraco's template and partial view editors write and read TSX instead
3
- * of Razor. Umbraco's client hard-codes a Razor scaffold for a new template,
4
- * writes a Razor `Layout = "x.cshtml"` block when a master template is chosen,
5
- * starts a new partial view empty, and opens both in an editor configured for
6
- * Razor. This entry point changes the methods that do so — the template
7
- * repository's scaffold, the template workspace's master-template update, the
8
- * partial view repository's scaffold and the code editor's own language — to
9
- * write what a crombolo view is: a default-exported component, with
10
- * `export const layout = '<alias>'` for its master, and for a partial view the
11
- * server's `Empty` snippet. Snippets inserted into a view are translated to TSX
12
- * (`tsx-snippets.js`), and the inserts that have no TSX equivalent — a partial
13
- * view, which needs an import as well as a tag, and Razor's sections — are hidden.
14
- *
15
- * The methods are patched on Umbraco's own classes rather than the extensions
16
- * re-registered: changing the registry once the backoffice is rendering makes
17
- * every open section re-evaluate its extensions, and a route set up in that
18
- * window throws inside Umbraco's code.
3
+ * of Razor. Umbraco's client starts a template with a Razor scaffold, writes a
4
+ * Razor `Layout = "x.cshtml"` block for a master template, starts a partial view
5
+ * empty, and opens both in an editor configured for Razor. The fork of the
6
+ * client (story 235) asks `umbCromboloHooks` at each of those places, and this
7
+ * answers: a default-exported component, `export const layout = '<alias>'` for
8
+ * its master, the server's `Empty` snippet for a partial view, and monaco's
9
+ * TypeScript on a `.tsx` model. Snippets inserted into a view are translated to
10
+ * TSX (`tsx-snippets.js`), and the inserts with no TSX equivalent, a partial view
11
+ * (an import as well as a tag) and Razor's sections, are hidden.
19
12
  */
20
- import { UmbCodeEditorElement } from '@umbraco-cms/backoffice/code-editor'
21
13
  import { PartialViewService } from '@umbraco-cms/backoffice/external/backend-api'
22
- import { UmbPartialViewDetailRepository } from '@umbraco-cms/backoffice/partial-view'
23
- import { UmbTemplateDetailRepository } from '@umbraco-cms/backoffice/template'
24
14
  import { toTsxSnippet } from './tsx-snippets.js'
25
15
 
26
16
  export const SCAFFOLD = `import type { PageProps } from '@crombolo/crombolo'
@@ -87,9 +77,6 @@ const COMPONENTS = 'file:///components'
87
77
  */
88
78
  const BUNDLER = 100
89
79
 
90
- /** Code editors showing a view, rather than a stylesheet, a script or JSON. */
91
- const views = new WeakSet()
92
-
93
80
  let opened = 0
94
81
 
95
82
  function pluginApi() {
@@ -184,154 +171,59 @@ async function configureTsx() {
184
171
  }
185
172
 
186
173
  /**
187
- * Teaches `umb-code-editor` the dialect: `language="tsx"`, and Umbraco's own
188
- * hard-coded `razor`, become monaco's `typescript` on a `.tsx` model.
189
- *
190
- * `language` is a Lit property, so the accessor to wrap is the one Lit defined
191
- * on the prototype. If a future version of either defines it elsewhere, the
192
- * editor keeps Umbraco's Razor rather than this entry point throwing on the way
193
- * in and taking the TSX scaffolds down with it.
174
+ * How a view's code editor works, for the fork's `viewEditor` seam: monaco's
175
+ * `typescript` on a model named as a `.tsx` file, configured before the editor is
176
+ * made, with inserted snippets translated.
194
177
  */
195
- function teachDialect(editors) {
196
- const language = Object.getOwnPropertyDescriptor(editors, 'language')
197
- if (!language?.set) return
198
- Object.defineProperty(editors, 'language', {
199
- ...language,
200
- set(value) {
201
- const view = value === TSX || value === 'razor'
202
- if (view) views.add(this)
203
- else views.delete(this)
204
- language.set.call(this, view ? 'typescript' : value)
178
+ function viewEditor() {
179
+ let configured = { monaco: undefined, ok: false }
180
+ return {
181
+ language: 'typescript',
182
+ async prepare() {
183
+ configured = await configureTsx()
205
184
  },
206
- })
207
-
208
- const firstUpdated = editors.firstUpdated
209
- editors.firstUpdated = async function (changed) {
210
- if (!views.has(this)) return await firstUpdated.call(this, changed)
211
- const { monaco, ok } = await configureTsx()
212
- await firstUpdated.call(this, changed)
213
- const editor = this.editor?.monacoEditor
214
- const created = editor?.getModel()
215
- if (!monaco || !editor || !created) return
216
- if (!ok) {
217
- // Without the dialect, TypeScript reads a view as a plain `.ts` file and
218
- // underlines every tag in it. Plain text says less and lies less.
219
- monaco.editor.setModelLanguage(created, 'plaintext')
220
- return
221
- }
222
- // monaco names the model it creates `inmemory://model/1`, and a name with no
223
- // `.tsx` on it is a file TypeScript refuses to read JSX in — every tag in
224
- // the view would be a syntax error. So the model is replaced by one named
225
- // as the file it is.
226
- const model = monaco.editor.createModel(
227
- created.getValue(),
228
- 'typescript',
229
- monaco.Uri.parse(`${COMPONENTS}/view-${++opened}.tsx`),
230
- )
231
- editor.setModel(model)
232
- created.dispose()
233
- editor.onDidDispose(() => model.dispose())
234
- }
235
-
236
- const insert = editors.insert
237
- editors.insert = function (text) {
238
- return insert.call(this, views.has(this) ? toTsxSnippet(text) : text)
185
+ created(editor) {
186
+ const { monaco, ok } = configured
187
+ const created = editor?.getModel?.()
188
+ if (!monaco || !created) return
189
+ if (!ok) {
190
+ // Without the dialect, TypeScript reads a view as a plain `.ts` file and
191
+ // underlines every tag in it. Plain text says less and lies less.
192
+ monaco.editor.setModelLanguage(created, 'plaintext')
193
+ return
194
+ }
195
+ // monaco names the model it creates `inmemory://model/1`, and a name with no
196
+ // `.tsx` on it is a file TypeScript refuses to read JSX in: every tag in the
197
+ // view would be a syntax error. So the model is replaced by one named as the
198
+ // file it is.
199
+ const model = monaco.editor.createModel(
200
+ created.getValue(),
201
+ 'typescript',
202
+ monaco.Uri.parse(`${COMPONENTS}/view-${++opened}.tsx`),
203
+ )
204
+ editor.setModel(model)
205
+ created.dispose()
206
+ editor.onDidDispose(() => model.dispose())
207
+ },
208
+ insert: toTsxSnippet,
239
209
  }
240
210
  }
241
211
 
242
- /**
243
- * The template editor's inserts that cannot become TSX. A partial view is an
244
- * import as well as a tag, which an insert at the cursor cannot write, and
245
- * sections are Razor's way for a layout to take content from a page, where a
246
- * TSX layout takes props.
247
- */
248
- async function hideRazorInserts() {
249
- const menu = (await customElements.whenDefined('umb-templating-insert-menu')).prototype
250
- Object.defineProperty(menu, 'hidePartialViews', {
251
- configurable: true,
252
- get: () => true,
253
- set: () => {},
254
- })
255
-
256
- const editor = (await customElements.whenDefined('umb-template-workspace-editor')).prototype
257
- const updated = editor.updated
258
- editor.updated = function (changed) {
259
- updated?.call(this, changed)
260
- const sections = this.shadowRoot?.querySelector('#sections-button')
261
- if (sections) sections.style.display = 'none'
262
- }
263
- }
264
-
265
- /**
266
- * The value builder's preview shows the snippet it will insert, so it shows the
267
- * TSX one. Its output is still Umbraco's, and is translated on insert with the rest.
268
- */
269
- async function previewFieldsAsTsx() {
270
- const modal = (await customElements.whenDefined('umb-templating-page-field-builder-modal'))
271
- .prototype
272
- const render = modal.render
273
- modal.render = function () {
274
- const result = render.call(this)
275
- if (Array.isArray(result?.values)) result.values = result.values.map(toTsxSnippet)
276
- return result
277
- }
278
- const updated = modal.updated
279
- modal.updated = function (changed) {
280
- updated?.call(this, changed)
281
- const block = this.shadowRoot?.querySelector('umb-code-block')
282
- if (block && block.getAttribute('language') !== 'TypeScript') {
283
- block.setAttribute('language', 'TypeScript')
284
- block.language = 'TypeScript'
285
- }
286
- }
212
+ /** The fork's seams, answered for TSX views. */
213
+ export const TSX_HOOKS = {
214
+ viewEditor: (language) => (language === TSX || language === 'razor' ? viewEditor() : undefined),
215
+ templateScaffold: () => SCAFFOLD,
216
+ async partialViewScaffold() {
217
+ const { data } = await PartialViewService.getPartialViewSnippetById({ path: { id: 'Empty' } })
218
+ return data?.content
219
+ },
220
+ writeLayout: withLayout,
221
+ translateSnippet: toTsxSnippet,
222
+ viewLanguageLabel: 'TypeScript',
223
+ hideRazorInserts: true,
287
224
  }
288
225
 
289
226
  export const onInit = async () => {
290
- teachDialect(UmbCodeEditorElement.prototype)
291
- // Registered lazily, so these wait for the editor to be opened
292
- hideRazorInserts()
293
- previewFieldsAsTsx()
294
-
295
- const repository = UmbTemplateDetailRepository.prototype
296
- const createScaffold = repository.createScaffold
297
- repository.createScaffold = async function (preset) {
298
- const result = await createScaffold.call(this, preset)
299
- if (result.data) result.data.content = SCAFFOLD
300
- return result
301
- }
302
-
303
- const partialViews = UmbPartialViewDetailRepository.prototype
304
- const createPartialViewScaffold = partialViews.createScaffold
305
- partialViews.createScaffold = async function (preset) {
306
- const result = await createPartialViewScaffold.call(this, preset)
307
- // Started from a snippet it has content already; empty, it gets the skeleton
308
- if (result.data && !result.data.content) {
309
- const { data } = await PartialViewService.getPartialViewSnippetById({ path: { id: 'Empty' } })
310
- if (data?.content) result.data.content = data.content
311
- }
312
- return result
313
- }
314
-
315
- // The workspace context is not exported; it is the module beside the package's
316
- // entry, which the import map resolves to the very module Umbraco loads
317
- const { UmbTemplateWorkspaceContext } = await import(
318
- new URL(
319
- './workspace/template-workspace.context.js',
320
- import.meta.resolve('@umbraco-cms/backoffice/template'),
321
- ).href
322
- )
323
- const workspace = UmbTemplateWorkspaceContext.prototype
324
- const setLayoutTemplate = workspace.setLayoutTemplate
325
- workspace.setLayoutTemplate = async function (unique, updateLayoutBlock) {
326
- // Umbraco's own update writes Razor; the state it keeps is what we want
327
- await setLayoutTemplate.call(this, unique, false)
328
- if (!updateLayoutBlock) return unique
329
- const items = unique ? (await this.itemRepository.requestItems([unique])).data : []
330
- const content = this._data.getCurrent()?.content ?? ''
331
- this._data.updateCurrent({
332
- content: withLayout(content, items?.[0]?.alias ?? null),
333
- layoutTemplate: unique ? { unique } : null,
334
- })
335
- return unique
336
- }
227
+ const { umbCromboloHooks } = await import('@umbraco-cms/backoffice/extension-registry')
228
+ Object.assign(umbCromboloHooks, TSX_HOOKS)
337
229
  }
@@ -2,7 +2,7 @@
2
2
  "$schema": "../../backoffice-dist/dist/umbraco-package-schema.json",
3
3
  "name": "Crombolo",
4
4
  "id": "Crombolo.Core",
5
- "version": "0.40.0",
5
+ "version": "0.42.1",
6
6
  "allowPublicAccess": false,
7
7
  "extensions": [
8
8
  {
@@ -170,6 +170,142 @@
170
170
  }
171
171
  ]
172
172
  },
173
+ {
174
+ "type": "workspace",
175
+ "alias": "Crombolo.Workspace.BundleEditor",
176
+ "name": "Crombolo Bundle Editor",
177
+ "element": "/crombolo-plugin/bundle-editor.js",
178
+ "overwrites": ["Umb.Workspace.PackageBuilder"],
179
+ "meta": {
180
+ "entityType": "package-builder"
181
+ }
182
+ },
183
+ {
184
+ "type": "workspaceView",
185
+ "alias": "Crombolo.WorkspaceView.BundleEditor.Content",
186
+ "name": "Crombolo Bundle Editor Content",
187
+ "element": "/crombolo-plugin/bundle-editor-content.js",
188
+ "weight": 400,
189
+ "meta": {
190
+ "label": "Content",
191
+ "pathname": "content",
192
+ "icon": "icon-document"
193
+ },
194
+ "conditions": [
195
+ {
196
+ "alias": "Umb.Condition.WorkspaceAlias",
197
+ "match": "Crombolo.Workspace.BundleEditor"
198
+ }
199
+ ]
200
+ },
201
+ {
202
+ "type": "workspaceView",
203
+ "alias": "Crombolo.WorkspaceView.BundleEditor.Types",
204
+ "name": "Crombolo Bundle Editor Types",
205
+ "element": "/crombolo-plugin/bundle-editor-types.js",
206
+ "weight": 300,
207
+ "meta": {
208
+ "label": "Types",
209
+ "pathname": "types",
210
+ "icon": "icon-item-arrangement"
211
+ },
212
+ "conditions": [
213
+ {
214
+ "alias": "Umb.Condition.WorkspaceAlias",
215
+ "match": "Crombolo.Workspace.BundleEditor"
216
+ }
217
+ ]
218
+ },
219
+ {
220
+ "type": "workspaceView",
221
+ "alias": "Crombolo.WorkspaceView.BundleEditor.Code",
222
+ "name": "Crombolo Bundle Editor Code",
223
+ "element": "/crombolo-plugin/bundle-editor-code.js",
224
+ "weight": 200,
225
+ "meta": {
226
+ "label": "Code",
227
+ "pathname": "code",
228
+ "icon": "icon-code"
229
+ },
230
+ "conditions": [
231
+ {
232
+ "alias": "Umb.Condition.WorkspaceAlias",
233
+ "match": "Crombolo.Workspace.BundleEditor"
234
+ }
235
+ ]
236
+ },
237
+ {
238
+ "type": "workspaceView",
239
+ "alias": "Crombolo.WorkspaceView.BundleEditor.Language",
240
+ "name": "Crombolo Bundle Editor Language",
241
+ "element": "/crombolo-plugin/bundle-editor-language.js",
242
+ "weight": 100,
243
+ "meta": {
244
+ "label": "Language",
245
+ "pathname": "language",
246
+ "icon": "icon-globe"
247
+ },
248
+ "conditions": [
249
+ {
250
+ "alias": "Umb.Condition.WorkspaceAlias",
251
+ "match": "Crombolo.Workspace.BundleEditor"
252
+ }
253
+ ]
254
+ },
255
+ {
256
+ "type": "workspaceView",
257
+ "alias": "Crombolo.WorkspaceView.BundleEditor.Forms",
258
+ "name": "Crombolo Bundle Editor Forms",
259
+ "element": "/crombolo-plugin/bundle-editor-forms.js",
260
+ "weight": 90,
261
+ "meta": {
262
+ "label": "Forms",
263
+ "pathname": "forms",
264
+ "icon": "icon-autofill"
265
+ },
266
+ "conditions": [
267
+ {
268
+ "alias": "Umb.Condition.WorkspaceAlias",
269
+ "match": "Crombolo.Workspace.BundleEditor"
270
+ }
271
+ ]
272
+ },
273
+ {
274
+ "type": "workspaceView",
275
+ "alias": "Crombolo.WorkspaceView.BundleEditor.Workflows",
276
+ "name": "Crombolo Bundle Editor Workflows",
277
+ "element": "/crombolo-plugin/bundle-editor-workflows.js",
278
+ "weight": 80,
279
+ "meta": {
280
+ "label": "Workflows",
281
+ "pathname": "workflows",
282
+ "icon": "icon-mindmap"
283
+ },
284
+ "conditions": [
285
+ {
286
+ "alias": "Umb.Condition.WorkspaceAlias",
287
+ "match": "Crombolo.Workspace.BundleEditor"
288
+ }
289
+ ]
290
+ },
291
+ {
292
+ "type": "workspaceView",
293
+ "alias": "Crombolo.WorkspaceView.BundleEditor.UserGroups",
294
+ "name": "Crombolo Bundle Editor User groups",
295
+ "element": "/crombolo-plugin/bundle-editor-user-groups.js",
296
+ "weight": 70,
297
+ "meta": {
298
+ "label": "User groups",
299
+ "pathname": "user-groups",
300
+ "icon": "icon-users"
301
+ },
302
+ "conditions": [
303
+ {
304
+ "alias": "Umb.Condition.WorkspaceAlias",
305
+ "match": "Crombolo.Workspace.BundleEditor"
306
+ }
307
+ ]
308
+ },
173
309
  {
174
310
  "type": "sectionView",
175
311
  "alias": "Crombolo.SectionView.Bundles.Installed",
@@ -374,12 +510,6 @@
374
510
  }
375
511
  ]
376
512
  },
377
- {
378
- "type": "backofficeEntryPoint",
379
- "alias": "Crombolo.EntryPoint.Branding",
380
- "name": "Crombolo Branding",
381
- "js": "/crombolo-plugin/branding/branding.js"
382
- },
383
513
  {
384
514
  "type": "backofficeEntryPoint",
385
515
  "alias": "Crombolo.EntryPoint.HelpMenu",
@@ -452,18 +582,6 @@
452
582
  "propertyEditorSchemaAlias": "Crombolo.FormPicker"
453
583
  }
454
584
  },
455
- {
456
- "type": "backofficeEntryPoint",
457
- "alias": "Crombolo.EntryPoint.ClientCredentials",
458
- "name": "Crombolo Client Credential Prefix",
459
- "js": "/crombolo-plugin/branding/client-credentials.js"
460
- },
461
- {
462
- "type": "backofficeEntryPoint",
463
- "alias": "Crombolo.EntryPoint.BundleBuilderLabels",
464
- "name": "Crombolo Bundle Builder Labels",
465
- "js": "/crombolo-plugin/branding/bundle-builder-labels.js"
466
- },
467
585
  {
468
586
  "type": "backofficeEntryPoint",
469
587
  "alias": "Crombolo.EntryPoint.ComponentsTree",
@@ -476,24 +594,12 @@
476
594
  "name": "Crombolo Content Type Labels",
477
595
  "js": "/crombolo-plugin/branding/content-type-labels.js"
478
596
  },
479
- {
480
- "type": "backofficeEntryPoint",
481
- "alias": "Crombolo.EntryPoint.QueryBuilder",
482
- "name": "Crombolo Query Builder Language",
483
- "js": "/crombolo-plugin/branding/query-builder.js"
484
- },
485
597
  {
486
598
  "type": "backofficeEntryPoint",
487
599
  "alias": "Crombolo.EntryPoint.SignIn",
488
600
  "name": "Crombolo Sign-in Button",
489
601
  "js": "/crombolo-plugin/branding/sign-in.js"
490
602
  },
491
- {
492
- "type": "backofficeEntryPoint",
493
- "alias": "Crombolo.EntryPoint.LogViewerMenu",
494
- "name": "Crombolo Log Viewer Search Menu",
495
- "js": "/crombolo-plugin/branding/log-viewer-menu.js"
496
- },
497
603
  {
498
604
  "type": "backofficeEntryPoint",
499
605
  "alias": "Crombolo.EntryPoint.NotificationsDialog",
@@ -579,7 +685,7 @@
579
685
  "alias": "Crombolo.SectionView.Workflow.Designer",
580
686
  "name": "Crombolo Workflow Designer",
581
687
  "element": "/crombolo-plugin/workflow-designer.js",
582
- "weight": 100,
688
+ "weight": 500,
583
689
  "meta": {
584
690
  "label": "Workflows",
585
691
  "pathname": "workflows",
@@ -1,188 +0,0 @@
1
- /**
2
- * The two places the vendored client names the product in code rather than in a
3
- * string table.
4
- *
5
- * `view.controller.js` builds the page title with a hardcoded suffix, which
6
- * `vendor:backoffice` rebrands to Crombolo. The suffix should be the site's own
7
- * name, which the shell states in `<meta name="application-name">` — so a site
8
- * called something other than Crombolo gets its own name rather than ours.
9
- *
10
- * Not read from `document.title`, although the shell puts the name there too.
11
- * This module loads after the client has started, and by then the client may
12
- * have written a title of its own. Taking `Content | Crombolo` for the site's
13
- * name makes every correction end in ` | Crombolo` again: the observer below
14
- * then answers its own change, forever, and the page never gets its main thread
15
- * back.
16
- */
17
- const CLIENT_SUFFIX = ' | Crombolo'
18
-
19
- /** The site's name as the shell stated it. */
20
- export function brandName(doc = document) {
21
- const stated = doc.querySelector('meta[name="application-name"]')?.getAttribute('content')
22
- return stated?.trim() || 'Crombolo'
23
- }
24
-
25
- /**
26
- * `title` with the client's suffix replaced by the site's name.
27
- *
28
- * A fixed point: its own output comes back unchanged, whatever the name is —
29
- * including a name that itself ends in the client's suffix. That is what makes
30
- * it safe to call from an observer of the very thing it writes.
31
- */
32
- export function brandedTitle(title, brand) {
33
- const branded = ` | ${brand}`
34
- if (title.endsWith(branded) || !title.endsWith(CLIENT_SUFFIX)) return title
35
- return `${title.slice(0, -CLIENT_SUFFIX.length)}${branded}`
36
- }
37
-
38
- /**
39
- * The logo popover carries a link to umbraco.com, hardcoded in
40
- * `backoffice-header-logo.element.js`. Its image is already served as crombolo's
41
- * (see `graphics.ts`), so only the link is left.
42
- *
43
- * This reaches into a vendored element's shadow root, which is worth naming as
44
- * the compromise it is: there is no extension point for the app shell's own
45
- * chrome. It is written to do nothing at all if that element or its anchor is not
46
- * what it was, so an upstream change costs a stale link and never an error.
47
- */
48
- function deepQuery(selector, root = document) {
49
- const direct = root.querySelector(selector)
50
- if (direct) return direct
51
- // The header logo lives inside the header's shadow root, so a plain
52
- // `document.querySelector` cannot see it. Nothing here is reachable without
53
- // walking through the roots.
54
- for (const element of root.querySelectorAll('*')) {
55
- if (!element.shadowRoot) continue
56
- const found = deepQuery(selector, element.shadowRoot)
57
- if (found) return found
58
- }
59
- return null
60
- }
61
-
62
- function correctLogoLink() {
63
- const logo = deepQuery('umb-backoffice-header-logo')
64
- const anchor = logo?.shadowRoot?.querySelector('a[href*="umbraco.com"]')
65
- if (!anchor) return false
66
- anchor.href = 'https://github.com/kineco-au/crombolo-cms'
67
- anchor.textContent = 'Crombolo on GitHub'
68
- const image = logo.shadowRoot.querySelector('img#logo')
69
- if (image) image.alt = brandName()
70
- return true
71
- }
72
-
73
- /** Where Umbraco's theme context keeps the choice, as the shells' early script reads it. */
74
- const THEME_KEY = 'umb-theme-alias'
75
-
76
- /** Whether a theme choice paints the back office dark: Dark, or Match my system on a dark computer. */
77
- export function isDarkTheme(alias, prefersDark) {
78
- return (
79
- alias === 'Crombolo.Theme.Dark' ||
80
- alias === 'umb-dark-theme' ||
81
- (alias === 'Crombolo.Theme.System' && Boolean(prefersDark))
82
- )
83
- }
84
-
85
- const GRAPHIC =
86
- /(\/security\/back-office\/graphics\/)(logo|logo-alternative|login-logo|login-logo-alternative)(-dark)?$/
87
-
88
- /**
89
- * A logo's address for the theme: the cream-lettered `-dark` variant on a dark
90
- * theme, the plain one otherwise. Anything that is not one of the logos comes
91
- * back as it was.
92
- */
93
- export function themedLogoUrl(src, dark) {
94
- const match = GRAPHIC.exec(src ?? '')
95
- if (!match) return src
96
- return `${src.slice(0, match.index)}${match[1]}${match[2]}${dark ? '-dark' : ''}`
97
- }
98
-
99
- const prefersDark = () => {
100
- try {
101
- return window.matchMedia('(prefers-color-scheme: dark)').matches
102
- } catch {
103
- return false
104
- }
105
- }
106
-
107
- const storedTheme = () => {
108
- try {
109
- return localStorage.getItem(THEME_KEY)
110
- } catch {
111
- return null
112
- }
113
- }
114
-
115
- /**
116
- * The header's logo, and the wordmark in its popover, in the theme's colours.
117
- *
118
- * `umb-app-logo` picks its graphic from Umbraco's own theme names, and the
119
- * header pins it to the one drawn for an orange bar, so its cocoa lettering
120
- * vanished on the dark header. Like the link above, this reaches into the
121
- * vendored element and does nothing if it is not what it was.
122
- */
123
- function applyThemedLogos() {
124
- const dark = isDarkTheme(storedTheme(), prefersDark())
125
- const header = deepQuery('umb-backoffice-header-logo')?.shadowRoot
126
- const mark = header?.querySelector('umb-app-logo')?.shadowRoot?.querySelector('img')
127
- if (!mark) return false
128
- const src = mark.getAttribute('src')
129
- const next = themedLogoUrl(src, dark)
130
- if (next !== src) mark.setAttribute('src', next)
131
- const popover = header.querySelector('img#logo')
132
- if (popover) {
133
- popover.dataset.lightSrc ??= popover.getAttribute('src') ?? ''
134
- const wanted = dark
135
- ? next.replace(/graphics\/logo-dark$/, 'graphics/logo-alternative-dark')
136
- : popover.dataset.lightSrc
137
- if (popover.getAttribute('src') !== wanted) popover.setAttribute('src', wanted)
138
- }
139
- return true
140
- }
141
-
142
- /** Applies the logos now and on every change of theme: the menu, another tab, or the computer's. */
143
- function followTheme() {
144
- const apply = () => setTimeout(applyThemedLogos, 0)
145
- // The theme context swaps the theme's stylesheet in the head when the choice changes.
146
- new MutationObserver(apply).observe(document.head, { childList: true })
147
- window.addEventListener('storage', (event) => {
148
- if (event.key === THEME_KEY) apply()
149
- })
150
- try {
151
- window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', apply)
152
- } catch {
153
- // Without media queries, Match my system stays light.
154
- }
155
- }
156
-
157
- export const onInit = () => {
158
- const brand = brandName()
159
- let themed = false
160
- const logos = setInterval(() => {
161
- if (applyThemedLogos() && !themed) {
162
- themed = true
163
- clearInterval(logos)
164
- followTheme()
165
- }
166
- }, 250)
167
- setTimeout(() => clearInterval(logos), 30_000)
168
- const correct = () => {
169
- const corrected = brandedTitle(document.title, brand)
170
- if (corrected !== document.title) document.title = corrected
171
- }
172
- correct()
173
- // The header renders after the app boots, and the popover's contents with it.
174
- let tries = 0
175
- const timer = setInterval(() => {
176
- if (correctLogoLink() || ++tries > 40) clearInterval(timer)
177
- }, 250)
178
- const head = document.querySelector('head')
179
- const title = document.querySelector('title')
180
- if (!head || !title) return
181
- // The element is replaced as well as retitled, so both are watched.
182
- new MutationObserver(correct).observe(title, {
183
- childList: true,
184
- characterData: true,
185
- subtree: true,
186
- })
187
- new MutationObserver(correct).observe(head, { childList: true })
188
- }