jsketcher/web/app/sketcher/actions/index.js
2020-03-02 22:47:46 -08:00

45 lines
No EOL
962 B
JavaScript

import constraintActions from "./constraintActions";
import {sortSelectionByType} from "./matchUtils";
const ALL_CONTEXTUAL_ACTIONS = [
...constraintActions,
//keep going here
];
export function matchAvailableActions(selection) {
let sortedByType = sortSelectionByType(selection);
let matched = [];
if (selection.length) {
for (let action of ALL_CONTEXTUAL_ACTIONS) {
if (Array.isArray(action.selectionMatcher)) {
action.selectionMatcher.forEach(matcher => {
if (matcher(selection, sortedByType)) {
matched.push(action);
}
})
} else {
if (action.selectionMatcher(selection, sortedByType)) {
matched.push(action);
}
}
}
}
return matched;
}
//For backward compatibility
export function getActionIfAvailable(actionId, selection, cb) {
matchAvailableActions(selection).forEach(a => {
if (a.id === actionId) {
cb(a);
}
})
}