Skip to content

Commit

Permalink
[4586] Allow to override the default behavior of commands
Browse files Browse the repository at this point in the history
Bug: eclipse-sirius#4586
Signed-off-by: Gwendal Daniel <[email protected]>
  • Loading branch information
gdaniel committed Feb 17, 2025
1 parent ffb0419 commit 6719291
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ For that, a new page `/libraries` showing all the libraries has been contributed
- https://github.com/eclipse-sirius/sirius-web/issues/4428[#4428] [core] Allow to contribute custom commands in the command palette.
A new search command as been contributed to the palette in Sirius Web, which can be selected to set the palette in a 'search' mode that searches elements in the project.
- https://github.com/eclipse-sirius/sirius-web/issues/4584[#4584] [core] Allow the command palette to execute custom commands.
- https://github.com/eclipse-sirius/sirius-web/issues/4586[#4586] [core] Allow to override the default behavior of commands in the command palette.
The `omniboxCommandOverrideContributionExtensionPoint` extension point can be used to contribute components to use when a command is selected.


=== Improvements
Expand Down
24 changes: 22 additions & 2 deletions packages/core/frontend/sirius-components-omnibox/src/Omnibox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import { useTextBasedObjects } from './useTextBasedObjects';
import { OmniboxCommandList } from './OmniboxCommandList';
import { OmniboxObjectList } from './OmniboxObjectList';
import { useExecuteOmniboxCommand } from './useExecuteOmniboxCommand';
import { useData } from '@eclipse-sirius/sirius-components-core';
import { OmniboxCommandOverrideContribution } from './OmniboxExtensionPoints.types';
import { omniboxCommandOverrideContributionExtensionPoint } from './OmniboxExtensionPoints';

const useOmniboxStyles = makeStyles()((theme) => ({
omnibox: {
Expand Down Expand Up @@ -69,12 +72,17 @@ export const Omnibox = ({ open, initialContextEntries, editingContextId, onClose
const [state, setState] = useState<OmniboxState>({
queryHasChanged: true,
mode: 'Command',
commandOverride: null,
});

const { getOmniboxCommands, loading: commandLoading, data: commandData } = useOmniboxCommands();
const { getTextBasedObjects, loading: objectLoading, data: objectData } = useTextBasedObjects();
const { executeOmniboxCommand } = useExecuteOmniboxCommand();

const { data: omniboxCommandOverrideContributions } = useData<OmniboxCommandOverrideContribution[]>(
omniboxCommandOverrideContributionExtensionPoint
);

const inputRef = useRef<HTMLInputElement>(null);
const listRef = useRef<HTMLUListElement>(null);

Expand Down Expand Up @@ -122,7 +130,15 @@ export const Omnibox = ({ open, initialContextEntries, editingContextId, onClose
};

const handleOnActionClick = (action: OmniboxAction) => {
if (action.id === 'searchContent') {
const commandOverrides = omniboxCommandOverrideContributions
.filter((contribution) => contribution.canHandle(action))
.map((contribution) => contribution.component);
if (commandOverrides.length > 0) {
setState((prevState) => ({
...prevState,
commandOverride: commandOverrides[0] ?? null,
}));
} else if (action.id === 'searchContent') {
setState((prevState) => ({
...prevState,
mode: 'Search',
Expand Down Expand Up @@ -198,7 +214,11 @@ export const Omnibox = ({ open, initialContextEntries, editingContextId, onClose
</>
);

return (
const CommandOverride = state.commandOverride;

return CommandOverride ? (
<CommandOverride onClose={onClose} />
) : (
<Dialog
open={open}
onClose={onClose}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface OmniboxProps {
export interface OmniboxState {
queryHasChanged: boolean;
mode: OmniboxMode;
commandOverride: React.ComponentType<OmniboxCommandOverrideComponentProps> | null;
}

export type OmniboxMode = 'Command' | 'Search';
Expand All @@ -38,3 +39,7 @@ export interface OmniboxAction {
icon: JSX.Element;
label: string;
}

export interface OmniboxCommandOverrideComponentProps {
onClose: () => void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*******************************************************************************
* Copyright (c) 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/

import { DataExtensionPoint } from '@eclipse-sirius/sirius-components-core';

import { OmniboxCommandOverrideContribution } from './OmniboxExtensionPoints.types';

export const omniboxCommandOverrideContributionExtensionPoint: DataExtensionPoint<
Array<OmniboxCommandOverrideContribution>
> = {
identifier: 'omnibox#commandOverrideContribution',
fallback: [],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*******************************************************************************
* Copyright (c) 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
import { OmniboxAction, OmniboxCommandOverrideComponentProps } from './Omnibox.types';

export interface OmniboxCommandOverrideContribution {
canHandle: (action: OmniboxAction) => boolean;
component: (props: OmniboxCommandOverrideComponentProps) => JSX.Element | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
* Obeo - initial API and implementation
*******************************************************************************/

export { type OmniboxContextEntry } from './Omnibox.types';
export {
type OmniboxAction,
type OmniboxCommandOverrideComponentProps,
type OmniboxContextEntry,
} from './Omnibox.types';
export * from './OmniboxButton';
export * from './OmniboxExtensionPoints';
export * from './OmniboxExtensionPoints.types';
export * from './OmniboxProvider';
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*******************************************************************************
* Copyright (c) 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.papaya.services.commands;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.sirius.components.collaborative.api.IOmniboxCommandProvider;
import org.eclipse.sirius.components.collaborative.dto.GetOmniboxCommandsInput;
import org.eclipse.sirius.components.collaborative.dto.OmniboxCommand;
import org.eclipse.sirius.components.collaborative.dto.OmniboxContextEntry;
import org.eclipse.sirius.web.papaya.services.api.IPapayaCapableEditingContextPredicate;
import org.springframework.stereotype.Service;

/**
* Provides the show documentation command for Papaya projects.
*
* @author gdaniel
*/
@Service
public class PapayaShowDocumentationCommandProvider implements IOmniboxCommandProvider {

private final IPapayaCapableEditingContextPredicate papayaCapableEditingContextPredicate;

public PapayaShowDocumentationCommandProvider(IPapayaCapableEditingContextPredicate papayaCapableEditingContextPredicate) {
this.papayaCapableEditingContextPredicate = Objects.requireNonNull(papayaCapableEditingContextPredicate);
}

@Override
public List<OmniboxCommand> getCommands(GetOmniboxCommandsInput input) {
List<OmniboxCommand> result = List.of();
Optional<String> optionalEditingContextId = input.contextEntries().stream().filter(entry -> Objects.equals(entry.kind(), "EditingContext")).map(OmniboxContextEntry::id).findFirst();
if (optionalEditingContextId.isPresent() && this.papayaCapableEditingContextPredicate.test(optionalEditingContextId.get())) {
result = List.of(new OmniboxCommand("showDocumentation", "Show documentation", List.of("/omnibox/show-documentation.svg"), "Navigate to Sirius Web's documentation"));
}
return result;
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ import { ProjectSettingTabContribution } from '../views/project-settings/Project
import { projectSettingsTabExtensionPoint } from '../views/project-settings/ProjectSettingsViewExtensionPoints';
import { ellipseNodeStyleDocumentTransform } from './EllipseNodeDocumentTransform';
import { referenceWidgetDocumentTransform } from './ReferenceWidgetDocumentTransform';
import { OmniboxCommandOverrideContribution } from '@eclipse-sirius/sirius-components-omnibox';
import { omniboxCommandOverrideContributionExtensionPoint } from '@eclipse-sirius/sirius-components-omnibox';
import { OmniboxAction } from '@eclipse-sirius/sirius-components-omnibox';
import { ShowDocumentationOmniboxCommandOverride } from '../omnibox/ShowDocumentationOmniboxCommandOverride';

const getType = (representation: RepresentationMetadata): string | null => {
const query = representation.kind.substring(representation.kind.indexOf('?') + 1, representation.kind.length);
Expand Down Expand Up @@ -444,4 +448,29 @@ defaultExtensionRegistry.addComponent(projectContextMenuEntryExtensionPoint, {
Component: ProjectDownloadMenuItemExtension,
});

/*******************************************************************************
*
* Omnibox command overrides
*
* Used to override the default rendering of omnibox commands
*
*******************************************************************************/

const omniboxCommandOverrides: OmniboxCommandOverrideContribution[] = [
{
canHandle: (action: OmniboxAction) => {
return action.id === 'showDocumentation';
},
component: ShowDocumentationOmniboxCommandOverride,
},
];

defaultExtensionRegistry.putData<OmniboxCommandOverrideContribution[]>(
omniboxCommandOverrideContributionExtensionPoint,
{
identifier: `siriusweb_${omniboxCommandOverrideContributionExtensionPoint.identifier}`,
data: omniboxCommandOverrides,
}
);

export { defaultExtensionRegistry };
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/

import { OmniboxCommandOverrideComponentProps } from '@eclipse-sirius/sirius-components-omnibox';

export const ShowDocumentationOmniboxCommandOverride = ({ onClose }: OmniboxCommandOverrideComponentProps) => {
window.open('https://www.github.com/eclipse-sirius/sirius-web', '_blank')?.focus();
onClose();
return null;
};

0 comments on commit 6719291

Please sign in to comment.