jsketcher/web/app/sketcher/actions/index.js
2020-03-10 00:33:50 -07:00

51 lines
No EOL
1.3 KiB
JavaScript

import constraintActions from "./constraintActions";
import {getDescription, MatchIndex, matchSelection} from "../selectionMatcher";
import {toast} from "react-toastify";
const ALL_CONTEXTUAL_ACTIONS = [
...constraintActions,
//keep going here
];
const index = {};
ALL_CONTEXTUAL_ACTIONS.forEach(a => index[a.id] = a);
Object.freeze(index);
export function matchAvailableActions(selection) {
let matched = [];
let matchIndex = new MatchIndex(selection);
if (selection.length) {
for (let action of ALL_CONTEXTUAL_ACTIONS) {
if (matchSelection(action.selectionMatcher, matchIndex, true)) {
matched.push(action);
}
}
}
return matched;
}
export function getSketcherAction(actionId) {
return index[actionId];
}
//For backward compatibility
export function runActionOrToastWhyNot(actionId, selection, ctx, silent) {
const action = index[actionId];
if (action) {
const matched = matchSelection(action.selectionMatcher, new MatchIndex(selection), false);
if (matched) {
action.invoke(ctx, matched)
} else {
const msg = 'The action "' + action.shortName + ' ' + action.kind + '" requires selection of ' + getDescription(action.selectionMatcher);
if (silent) {
return msg;
} else {
toast(msg);
}
}
}
}