Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## UNRELEASED

### Adds

* Custom operations registered with `addCreateWidgetOperation` can now specify an `ifTypesIntersect` property containing an array of widget type names. If the area in question allows at least one, the operation is offered.

## 4.21.0 (2025-09-03)

### Adds
Expand Down
1 change: 1 addition & 0 deletions modules/@apostrophecms/area/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ module.exports = {
// interface regardless of whether `options.widgets` or `options.groups`
// was used.
getWidgets(options) {
// Keep in sync with client-side implementation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a new issue, but I avoided further duplication within the client side.

Right now we have some ESM/commonjs issues that prevent us from sharing code as well as I'd like between front and back end.

let widgets = options.widgets || {};

if (options.groups) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@

<script>
import { createId } from '@paralleldrive/cuid2';
import filterCreateWidgetOperations from '../lib/filter-create-widget-operations.js';

export default {
name: 'AposAreaContextualMenu',
Expand Down Expand Up @@ -201,7 +202,8 @@
return [];
}
const menu = [ ...this.contextMenuOptions.menu ];
for (const createWidgetOperation of this.moduleOptions.createWidgetOperations) {
const createWidgetOperations = filterCreateWidgetOperations(this.moduleOptions, this.options);

Check warning on line 205 in modules/@apostrophecms/area/ui/apos/components/AposAreaContextualMenu.vue

View workflow job for this annotation

GitHub Actions / build (20, 7)

This line has a length of 100. Maximum allowed is 90

Check warning on line 205 in modules/@apostrophecms/area/ui/apos/components/AposAreaContextualMenu.vue

View workflow job for this annotation

GitHub Actions / build (22, 7)

This line has a length of 100. Maximum allowed is 90

Check warning on line 205 in modules/@apostrophecms/area/ui/apos/components/AposAreaContextualMenu.vue

View workflow job for this annotation

GitHub Actions / build (22, 6)

This line has a length of 100. Maximum allowed is 90

Check warning on line 205 in modules/@apostrophecms/area/ui/apos/components/AposAreaContextualMenu.vue

View workflow job for this annotation

GitHub Actions / build (20, 8)

This line has a length of 100. Maximum allowed is 90

Check warning on line 205 in modules/@apostrophecms/area/ui/apos/components/AposAreaContextualMenu.vue

View workflow job for this annotation

GitHub Actions / build (22, 8)

This line has a length of 100. Maximum allowed is 90

Check warning on line 205 in modules/@apostrophecms/area/ui/apos/components/AposAreaContextualMenu.vue

View workflow job for this annotation

GitHub Actions / build (20, 6)

This line has a length of 100. Maximum allowed is 90
for (const createWidgetOperation of createWidgetOperations) {
menu.unshift({
type: 'operation',
...createWidgetOperation
Expand Down Expand Up @@ -329,6 +331,7 @@
this.$refs[name][0].querySelector('button').focus();
}
}

}
};
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
</template>

<script>
import filterCreateWidgetOperations from '../lib/filter-create-widget-operations.js';

export default {
name: 'AposAreaExpandedMenu',
props: {
Expand Down Expand Up @@ -177,7 +179,7 @@ export default {
return count ? +count > 1 && +count < 4 : true;
},
getCreateWidgetOperationsGroups() {
const operations = this.moduleOptions.createWidgetOperations;
const operations = filterCreateWidgetOperations(this.moduleOptions, this.options);
if (operations.length === 0) {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Requires the module options and an area field's .options property

import getWidgets from './get-widgets.js';

export default function filterCreateWidgetOperations(moduleOptions, areaOptions) {
const widgets = getWidgets(areaOptions);
return moduleOptions.createWidgetOperations.filter(createWidgetOperation => {
if (createWidgetOperation.ifTypesIntersect) {
const types = Object.keys(widgets);
if (!types.some(type => createWidgetOperation.ifTypesIntersect.includes(type))) {
return false;
}
}
return true;
});
}
14 changes: 14 additions & 0 deletions modules/@apostrophecms/area/ui/apos/lib/get-widgets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Requires an area field's .options property

export default function getWidgets(options) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't strictly have to factor this out today, but in the long run I'd like to have one implementation, so this is the right direction.

let widgets = options.widgets || {};
if (options.groups) {
for (const group of Object.keys(options.groups)) {
widgets = {
...widgets,
...options.groups[group].widgets
};
}
}
return widgets;
}
43 changes: 20 additions & 23 deletions modules/@apostrophecms/widget-type/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,6 @@ module.exports = {
origin: null,
preview: true
},
handlers(self) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registering the widget operations late prevents the section template library from altering them cleanly if some modules are configured before and some after, which is likely because of our support for modules.js files with nestedModuleSubdirs. Yes this took me a while to figure out.

return {
'apostrophe:modulesRegistered': {
composeWidgetOperations() {
self.widgetOperations = Object.entries(self.widgetOperations)
.map(([ name, operation ]) => {
if (!operation.label || !operation.modal) {
throw self.apos.error('invalid', 'widgetOperations requires label and modal properties.');
}

if (operation.secondaryLevel !== true && !operation.icon) {
throw self.apos.error('invalid', 'widgetOperations requires the icon property at primary level.');
}

return {
name,
...operation
};
});
}
}
};
},
init(self) {
self.isExplicitOrigin = self.options.origin !== null;
self.options.origin = self.options.origin || 'right';
Expand All @@ -170,6 +147,7 @@ module.exports = {
self.label = self.options.label;

self.composeSchema();
self.composeWidgetOperations();

self.apos.area.setWidgetManager(self.name, self);

Expand All @@ -187,6 +165,7 @@ module.exports = {
},
methods(self) {
return {

composeSchema() {
self.schema = self.apos.schema.compose({
addFields: self.apos.schema.fieldsToArray(`Module ${self.__meta.name}`, self.fields),
Expand All @@ -203,6 +182,24 @@ module.exports = {
});
},

composeWidgetOperations() {

@boutell boutell Sep 3, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no actual changes to the logic here.

self.widgetOperations = Object.entries(self.widgetOperations)
.map(([ name, operation ]) => {
if (!operation.label || !operation.modal) {
throw self.apos.error('invalid', 'widgetOperations requires label and modal properties.');
}

if (operation.secondaryLevel !== true && !operation.icon) {
throw self.apos.error('invalid', 'widgetOperations requires the icon property at primary level.');
}

return {
name,
...operation
};
});
},

// Returns markup for the widget. Invoked via `{% widget ... %}` in the
// `@apostrophecms/area` module as it iterates over widgets in
// an area. The default behavior is to render the template for the widget,
Expand Down