|
| 1 | +import type { Meta, StoryFn, StoryObj } from "@storybook/react"; |
| 2 | +import h from "@macrostrat/hyper"; |
| 3 | +import { ActionDef, ActionsPreflightPanel } from "."; |
| 4 | +import Box from "ui-box"; |
| 5 | +import { FormGroup, NumericInput, Spinner } from "@blueprintjs/core"; |
| 6 | +import { |
| 7 | + NullableSlider, |
| 8 | + ToasterContext, |
| 9 | + useToaster, |
| 10 | +} from "@macrostrat/ui-components"; |
| 11 | +import { |
| 12 | + BaseDataTypeSelect, |
| 13 | + exampleMapLayers, |
| 14 | + MapLayer, |
| 15 | +} from "../item-select/examples"; |
| 16 | +import { ItemSelect } from "../item-select"; |
| 17 | + |
| 18 | +export enum SelectionActionType { |
| 19 | + Delete = "delete", |
| 20 | + Heal = "heal", |
| 21 | + ChangeType = "changeType", |
| 22 | + ChangeLayer = "changeLayer", |
| 23 | + AdjustWidth = "adjustWidth", |
| 24 | + AdjustCertainty = "adjustCertainty", |
| 25 | + ReverseLines = "reverseLines", |
| 26 | + RecalculateTopology = "recalculateTopology", |
| 27 | +} |
| 28 | + |
| 29 | +type MapboardActionDef = |
| 30 | + | ActionDef<SelectionActionType.Delete> |
| 31 | + | ActionDef<SelectionActionType.Heal> |
| 32 | + | ActionDef<SelectionActionType.RecalculateTopology> |
| 33 | + | ActionDef<SelectionActionType.ChangeType, string> |
| 34 | + | ActionDef<SelectionActionType.ChangeLayer, ChangeLayerState> |
| 35 | + | ActionDef<SelectionActionType.AdjustWidth, number> |
| 36 | + | ActionDef<SelectionActionType.AdjustCertainty, number | null> |
| 37 | + | ActionDef<SelectionActionType.ReverseLines>; |
| 38 | + |
| 39 | +const actions: MapboardActionDef[] = [ |
| 40 | + { |
| 41 | + id: SelectionActionType.Delete, |
| 42 | + name: "Delete", |
| 43 | + icon: "trash", |
| 44 | + description: "Delete selected features", |
| 45 | + intent: "danger", |
| 46 | + }, |
| 47 | + { |
| 48 | + id: SelectionActionType.Heal, |
| 49 | + name: "Heal", |
| 50 | + icon: "changes", |
| 51 | + description: "Heal selected features", |
| 52 | + }, |
| 53 | + { |
| 54 | + id: SelectionActionType.RecalculateTopology, |
| 55 | + name: "Recalculate topology", |
| 56 | + icon: "polygon-filter", |
| 57 | + description: "Recalculate the topology of selected features", |
| 58 | + }, |
| 59 | + { |
| 60 | + id: SelectionActionType.ChangeType, |
| 61 | + name: "Change type", |
| 62 | + icon: "edit", |
| 63 | + detailsForm: ChangeDataTypeForm, |
| 64 | + isReady(state) { |
| 65 | + return state != null; |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + id: SelectionActionType.ChangeLayer, |
| 70 | + name: "Change layer", |
| 71 | + icon: "layers", |
| 72 | + detailsForm: ChangeLayerForm, |
| 73 | + isReady(state) { |
| 74 | + return state?.selectedLayerID != null; |
| 75 | + }, |
| 76 | + }, |
| 77 | + { |
| 78 | + id: SelectionActionType.AdjustWidth, |
| 79 | + name: "Adjust width", |
| 80 | + icon: "horizontal-distribution", |
| 81 | + disabled: true, |
| 82 | + detailsForm: AdjustWidthForm, |
| 83 | + defaultState: 5, |
| 84 | + isReady(state) { |
| 85 | + return state != null; |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + id: SelectionActionType.AdjustCertainty, |
| 90 | + name: "Adjust certainty", |
| 91 | + icon: "confirm", |
| 92 | + disabled: true, |
| 93 | + detailsForm: AdjustCertaintyForm, |
| 94 | + }, |
| 95 | + { |
| 96 | + id: SelectionActionType.ReverseLines, |
| 97 | + name: "Reverse lines", |
| 98 | + icon: "swap-horizontal", |
| 99 | + disabled: true, |
| 100 | + }, |
| 101 | +]; |
| 102 | + |
| 103 | +function InstrumentedActionsPanel(props) { |
| 104 | + const Toaster = useToaster(); |
| 105 | + return h(ActionsPreflightPanel, { |
| 106 | + onRunAction(action: ActionDef, state) { |
| 107 | + Toaster.show({ |
| 108 | + message: h("div.action", [ |
| 109 | + h("h3", action.name), |
| 110 | + h("pre", JSON.stringify(state, null, 2)), |
| 111 | + ]), |
| 112 | + }); |
| 113 | + }, |
| 114 | + ...props, |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +// More on default export: https://storybook.js.org/docs/react/writing-stories/introduction#default-export |
| 119 | +export default { |
| 120 | + title: "Form components/Actions preflight", |
| 121 | + component: InstrumentedActionsPanel, |
| 122 | + // More on argTypes: https://storybook.js.org/docs/react/api/argtypes |
| 123 | + argTypes: {}, |
| 124 | + decorators: [ |
| 125 | + (Story: StoryFn<typeof ActionsPreflightPanel>) => |
| 126 | + h(Box, { width: "450px" }, h(ToasterContext, h(Story))), |
| 127 | + ], |
| 128 | +} as Meta<typeof ActionsPreflightPanel>; |
| 129 | + |
| 130 | +export const Primary: StoryObj<typeof ActionsPreflightPanel> = { |
| 131 | + args: { |
| 132 | + actions, |
| 133 | + }, |
| 134 | +}; |
| 135 | + |
| 136 | +export const Compact: StoryObj<typeof ActionsPreflightPanel> = { |
| 137 | + args: { |
| 138 | + actions, |
| 139 | + compact: true, |
| 140 | + }, |
| 141 | +}; |
| 142 | + |
| 143 | +interface ChangeLayerState { |
| 144 | + selectedLayerID: number; |
| 145 | +} |
| 146 | + |
| 147 | +function ChangeDataTypeForm({ state, setState }) { |
| 148 | + return h(BaseDataTypeSelect, { state, setState }); |
| 149 | +} |
| 150 | + |
| 151 | +function ChangeLayerForm({ |
| 152 | + state, |
| 153 | + setState, |
| 154 | +}: { |
| 155 | + state: ChangeLayerState | null; |
| 156 | + setState(state: ChangeLayerState): void; |
| 157 | +}) { |
| 158 | + const layers = exampleMapLayers; |
| 159 | + const currentLayer = null; |
| 160 | + |
| 161 | + const selectedLayerID = state?.selectedLayerID ?? currentLayer; |
| 162 | + const possibleLayers = layers.filter((d) => d.id != selectedLayerID); |
| 163 | + const currentLayerItem = layers.find((d) => d.id == selectedLayerID); |
| 164 | + |
| 165 | + return h(ItemSelect<MapLayer>, { |
| 166 | + items: possibleLayers, |
| 167 | + selectedItem: currentLayerItem, |
| 168 | + onSelectItem: (layer) => { |
| 169 | + setState({ selectedLayerID: layer.id }); |
| 170 | + }, |
| 171 | + label: "layer", |
| 172 | + icon: "layers", |
| 173 | + }); |
| 174 | +} |
| 175 | + |
| 176 | +function AdjustWidthForm({ state, setState }) { |
| 177 | + return h( |
| 178 | + FormGroup, |
| 179 | + { label: "Width", labelInfo: "pixels" }, |
| 180 | + h(NumericInput, { |
| 181 | + min: 0, |
| 182 | + max: 10, |
| 183 | + value: state, |
| 184 | + majorStepSize: 1, |
| 185 | + minorStepSize: 0.2, |
| 186 | + onValueChange(value) { |
| 187 | + setState(Math.max(Math.min(value, 10), 0)); |
| 188 | + }, |
| 189 | + }) |
| 190 | + ); |
| 191 | +} |
| 192 | + |
| 193 | +function AdjustCertaintyForm({ state, setState }) { |
| 194 | + return h( |
| 195 | + FormGroup, |
| 196 | + { label: "Certainty" }, |
| 197 | + h(NullableSlider, { |
| 198 | + min: 0, |
| 199 | + max: 10, |
| 200 | + value: state, |
| 201 | + onChange(value) { |
| 202 | + setState(value); |
| 203 | + }, |
| 204 | + }) |
| 205 | + ); |
| 206 | +} |
0 commit comments