Skip to content

Commit 133796f

Browse files
authored
V16/docs work extensions example (#19809)
* update workspace example * Update readme for workspace counter example * update workspace counter examples readme
1 parent 3498930 commit 133796f

File tree

5 files changed

+104
-5
lines changed

5 files changed

+104
-5
lines changed
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
# Workspace Context Counter Example
1+
# Workspace Extensions Complete Example
22

3-
This example demonstrates the essence of the Workspace Context.
3+
The Workspace Context serves as the central communication hub for all workspace extensions. In this example, the context manages a counter that can be manipulated and displayed by different extension types, showcasing the power of shared state management in workspace extensions.
44

5-
The Workspace Context is available for everything within the Workspace, giving any extension within the ability to communicate through this.
6-
In this example, the Workspace Context houses a counter, which can be incremented by a Workspace Action and shown in the Workspace View.
5+
## Extension types included
76

8-
To demonstrate this, the example comes with: A Workspace Context, A Workspace Action and a Workspace View.
7+
This complete example includes:
8+
9+
- **Workspace Context** - Manages shared counter state and provides communication between extensions
10+
- **Workspace Action** - Primary "Increment" button that increases the counter value
11+
- **Workspace Action Menu Item** - "Reset Counter" dropdown option that resets the counter to zero
12+
- **Workspace View** - Dedicated tab that displays the current counter value
13+
- **Workspace Footer App** - Status indicator showing the counter value in the workspace footer
14+
15+
## How it works
16+
17+
All extensions communicate through the shared workspace context. When you increment or reset the counter using the actions, the workspace view and footer app automatically update to show the new value, demonstrating reactive state management across workspace extensions.
18+
19+
This pattern shows how workspace extensions can work together to create cohesive functionality within Umbraco workspaces.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { customElement, html, state, LitElement } from '@umbraco-cms/backoffice/external/lit';
2+
import { UmbElementMixin } from '@umbraco-cms/backoffice/element-api';
3+
import { EXAMPLE_COUNTER_CONTEXT } from './counter-workspace-context.js';
4+
5+
@customElement('example-counter-status-footer-app')
6+
export class ExampleCounterStatusFooterAppElement extends UmbElementMixin(LitElement) {
7+
@state()
8+
private _counter = 0;
9+
10+
constructor() {
11+
super();
12+
this.#observeCounter();
13+
}
14+
15+
async #observeCounter() {
16+
const context = await this.getContext(EXAMPLE_COUNTER_CONTEXT);
17+
if (!context) return;
18+
19+
this.observe(
20+
context.counter,
21+
(counter: number) => {
22+
this._counter = counter;
23+
},
24+
);
25+
}
26+
27+
override render() {
28+
return html`<span>Counter: ${this._counter}</span>`;
29+
}
30+
}
31+
32+
export default ExampleCounterStatusFooterAppElement;
33+
34+
declare global {
35+
interface HTMLElementTagNameMap {
36+
'example-counter-status-footer-app': ExampleCounterStatusFooterAppElement;
37+
}
38+
}

src/Umbraco.Web.UI.Client/examples/workspace-context-counter/counter-workspace-context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export class WorkspaceContextCounterElement extends UmbContextBase {
1717
increment() {
1818
this.#counter.setValue(this.#counter.value + 1);
1919
}
20+
21+
reset() {
22+
this.#counter.setValue(0);
23+
}
2024
}
2125

2226
// Declare a api export, so Extension Registry can initialize this class:

src/Umbraco.Web.UI.Client/examples/workspace-context-counter/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,30 @@ export const manifests: Array<UmbExtensionManifest> = [
5050
},
5151
],
5252
},
53+
{
54+
type: 'workspaceActionMenuItem',
55+
kind: 'default',
56+
alias: 'example.workspaceActionMenuItem.resetCounter',
57+
name: 'Reset Counter Menu Item',
58+
api: () => import('./reset-counter-menu-item.action.js'),
59+
forWorkspaceActions: 'example.workspaceAction.incrementor',
60+
weight: 100,
61+
meta: {
62+
label: 'Reset Counter',
63+
icon: 'icon-refresh',
64+
},
65+
},
66+
{
67+
type: 'workspaceFooterApp',
68+
alias: 'example.workspaceFooterApp.counterStatus',
69+
name: 'Counter Status Footer App',
70+
element: () => import('./counter-status-footer-app.element.js'),
71+
weight: 900,
72+
conditions: [
73+
{
74+
alias: UMB_WORKSPACE_CONDITION_ALIAS,
75+
match: 'Umb.Workspace.Document',
76+
},
77+
],
78+
},
5379
];
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { EXAMPLE_COUNTER_CONTEXT } from './counter-workspace-context.js';
2+
import { UmbWorkspaceActionMenuItemBase } from '@umbraco-cms/backoffice/workspace';
3+
import type { UmbWorkspaceActionMenuItem } from '@umbraco-cms/backoffice/workspace';
4+
5+
export class ExampleResetCounterMenuItemAction extends UmbWorkspaceActionMenuItemBase implements UmbWorkspaceActionMenuItem {
6+
/**
7+
* This method is executed when the menu item is clicked
8+
*/
9+
override async execute() {
10+
const context = await this.getContext(EXAMPLE_COUNTER_CONTEXT);
11+
if (!context) {
12+
throw new Error('Could not get the counter context');
13+
}
14+
15+
// Reset the counter to 0
16+
context.reset();
17+
}
18+
}
19+
20+
export const api = ExampleResetCounterMenuItemAction;

0 commit comments

Comments
 (0)