@datawheel/bespoke 0.3.11 → 0.3.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -914,7 +914,48 @@ var init_valueInOptions = __esm({
914
914
  });
915
915
 
916
916
  // libs/selectors/runSelector.js
917
- var runSelector_default;
917
+ function runSelector(id, logic, formatterFunctions, blockContext, router = void 0) {
918
+ const { variables } = parseBlockContext(blockContext);
919
+ const transpiledLogic = varSwapRecursive_default({ logic }, formatterFunctions, blockContext).logic;
920
+ const evalResult = mortarEval_default("variables", variables, transpiledLogic, formatterFunctions, void 0, blockContext);
921
+ const { vars, log, error } = evalResult;
922
+ const type = vars.type || SELECTOR_TYPES.SINGLE;
923
+ const component = vars.component || "Selector";
924
+ const hideLabel = vars.hideLabel || "false";
925
+ const name = vars.name || "Unlabeled Selector";
926
+ const options = scaffoldDynamic_default(vars.options || []);
927
+ const maybeFixForMulti2 = (defaultValue2) => type === SELECTOR_TYPES.MULTI && !Array.isArray(defaultValue2) ? [defaultValue2] : defaultValue2;
928
+ let potentialDefaultValue = vars.defaultValue;
929
+ let defaultValue;
930
+ if (potentialDefaultValue) {
931
+ potentialDefaultValue = maybeFixForMulti2(potentialDefaultValue);
932
+ if (valueInOptions_default(type, potentialDefaultValue, options))
933
+ defaultValue = potentialDefaultValue;
934
+ }
935
+ if (!defaultValue) {
936
+ const fallbackValue = options && options[0] && options[0].id || options[0] || "";
937
+ defaultValue = maybeFixForMulti2(fallbackValue);
938
+ }
939
+ if (router) {
940
+ const accesor = `selector${id}id`;
941
+ const { query, pathname } = router;
942
+ const currentValue = query[accesor];
943
+ if (currentValue && !valueInOptions_default(type, currentValue, options)) {
944
+ const newQuery = { ...query };
945
+ delete newQuery[accesor];
946
+ router.replace({ pathname, query: newQuery }, void 0, { shallow: true });
947
+ }
948
+ }
949
+ const config = {
950
+ name,
951
+ type,
952
+ options,
953
+ defaultValue,
954
+ component,
955
+ hideLabel
956
+ };
957
+ return { config, log, error };
958
+ }
918
959
  var init_runSelector = __esm({
919
960
  "libs/selectors/runSelector.js"() {
920
961
  init_esm_shims();
@@ -924,38 +965,6 @@ var init_runSelector = __esm({
924
965
  init_varSwapRecursive();
925
966
  init_scaffoldDynamic();
926
967
  init_valueInOptions();
927
- runSelector_default = (logic, formatterFunctions, blockContext) => {
928
- const { variables } = parseBlockContext(blockContext);
929
- const transpiledLogic = varSwapRecursive_default({ logic }, formatterFunctions, blockContext).logic;
930
- const evalResult = mortarEval_default("variables", variables, transpiledLogic, formatterFunctions, void 0, blockContext);
931
- const { vars, log, error } = evalResult;
932
- const type = vars.type || SELECTOR_TYPES.SINGLE;
933
- const component = vars.component || "Selector";
934
- const hideLabel = vars.hideLabel || "false";
935
- const name = vars.name || "Unlabeled Selector";
936
- const options = scaffoldDynamic_default(vars.options || []);
937
- const maybeFixForMulti2 = (defaultValue2) => type === SELECTOR_TYPES.MULTI && !Array.isArray(defaultValue2) ? [defaultValue2] : defaultValue2;
938
- let potentialDefaultValue = vars.defaultValue;
939
- let defaultValue;
940
- if (potentialDefaultValue) {
941
- potentialDefaultValue = maybeFixForMulti2(potentialDefaultValue);
942
- if (valueInOptions_default(type, potentialDefaultValue, options))
943
- defaultValue = potentialDefaultValue;
944
- }
945
- if (!defaultValue) {
946
- const fallbackValue = options && options[0] && options[0].id || options[0] || "";
947
- defaultValue = maybeFixForMulti2(fallbackValue);
948
- }
949
- const config = {
950
- name,
951
- type,
952
- options,
953
- defaultValue,
954
- component,
955
- hideLabel
956
- };
957
- return { config, log, error };
958
- };
959
968
  }
960
969
  });
961
970
 
@@ -1162,7 +1171,7 @@ function BlockException(message) {
1162
1171
  this.name = "BlockException";
1163
1172
  this.message = message;
1164
1173
  }
1165
- async function runSingleBlock(block, formatterFunctions, blockContext, readMemberFn) {
1174
+ async function runSingleBlock(block, formatterFunctions, blockContext, readMemberFn, router = void 0) {
1166
1175
  const { locale, variables, query } = parseBlockContext(blockContext);
1167
1176
  const allowed = isBlockAllowed(block, blockContext, variables, formatterFunctions);
1168
1177
  let resp = null;
@@ -1222,10 +1231,12 @@ async function runSingleBlock(block, formatterFunctions, blockContext, readMembe
1222
1231
  duration = Math.max(...apiResponses.map((ar) => ar.requestDuration));
1223
1232
  }
1224
1233
  if (block.type === BLOCK_TYPES.SELECTOR) {
1225
- const { config, log: log2, error: error2 } = runSelector_default(
1234
+ const { config, log: log2, error: error2 } = runSelector(
1235
+ block.id,
1226
1236
  block.contentByLocale[locale].content.logic,
1227
1237
  formatterFunctions,
1228
- blockContext
1238
+ blockContext,
1239
+ router
1229
1240
  );
1230
1241
  return {
1231
1242
  outputVariables: selectorQueryToVariable_default(block.id, query, config),
@@ -1445,7 +1456,7 @@ var init_runConsumers = __esm({
1445
1456
  }
1446
1457
  return allowed;
1447
1458
  };
1448
- runConsumersV2 = async (blocks, sections, bid, formatterFunctions, blockContext, initialState4 = {}, readMemberFn, mode = "section") => {
1459
+ runConsumersV2 = async (blocks, sections, bid, formatterFunctions, blockContext, initialState4 = {}, readMemberFn, mode = "section", router = void 0) => {
1449
1460
  if (!bid && !sections)
1450
1461
  return { ...initialState4 };
1451
1462
  const variablesById = { ...initialState4.variables ?? {} };
@@ -1512,7 +1523,8 @@ var init_runConsumers = __esm({
1512
1523
  variables,
1513
1524
  locale: variables.locale
1514
1525
  },
1515
- readMemberFn
1526
+ readMemberFn,
1527
+ router
1516
1528
  ).then(({ outputVariables, status }) => {
1517
1529
  if (status.error) {
1518
1530
  if (typeof window === "undefined") {
@@ -1935,7 +1947,8 @@ var init_statusSlice = __esm({
1935
1947
  tools: true,
1936
1948
  settings: false,
1937
1949
  reportEditor: false
1938
- }
1950
+ },
1951
+ isCMS: false
1939
1952
  };
1940
1953
  statusSlice = createSlice({
1941
1954
  name: "status",
@@ -4525,7 +4538,8 @@ function useOnChangeSelector(blockId, section, selectorIdentifier, asComparison
4525
4538
  status: initialStatus
4526
4539
  },
4527
4540
  readMemberFn,
4528
- "report"
4541
+ "report",
4542
+ router
4529
4543
  ).then((data) => {
4530
4544
  if (comparison.active && asComparison) {
4531
4545
  comparison.setComparisonVariables({
@@ -5252,7 +5266,10 @@ function StaticSelectorOptionRow({ option, editOption, deleteOption }) {
5252
5266
  {
5253
5267
  placeholder: "myOption",
5254
5268
  value: option.id,
5255
- onChange: (event) => editOption({ id: event.currentTarget.value })
5269
+ onChange: (event) => {
5270
+ editOption({ id: event.currentTarget.value });
5271
+ },
5272
+ error: option.id.indexOf(" ") > -1 ? "Please, avoid the usage of spaces in the ID" : false
5256
5273
  }
5257
5274
  ) }),
5258
5275
  /* @__PURE__ */ jsx("td", { children: /* @__PURE__ */ jsx(
@@ -5375,6 +5392,15 @@ function DynamicSelectorOptionsEditor({ id, dynamicOptions, setDynamicOptions })
5375
5392
  labelKey: null
5376
5393
  });
5377
5394
  }, [dynamicOptions, setDynamicOptions]);
5395
+ const optionsHasWarnings = useMemo(() => {
5396
+ let optionsWithSpaces = [];
5397
+ if (optionVariableMap && optionsVar && idKey && Array.isArray(optionVariableMap[optionsVar])) {
5398
+ optionsWithSpaces = optionVariableMap[optionsVar].filter((o) => `${o[idKey]}`.indexOf(" ") > 0);
5399
+ }
5400
+ return optionsWithSpaces.length > 0 ? `One or more ID's have spaces on it. (
5401
+ ${optionsWithSpaces.map((o) => `"${o[idKey]}"`).join(",")}
5402
+ ) Avoid this, please.` : false;
5403
+ }, [optionVariableMap, optionsVar, idKey]);
5378
5404
  if (!isInitialized)
5379
5405
  return null;
5380
5406
  const optionsVarList = Object.keys(optionVariableMap);
@@ -5400,7 +5426,8 @@ function DynamicSelectorOptionsEditor({ id, dynamicOptions, setDynamicOptions })
5400
5426
  required: !isPrimitiveType,
5401
5427
  disabled: isPrimitiveType,
5402
5428
  onChange: (val) => setState("idKey", val),
5403
- data: valueSelectorList
5429
+ data: valueSelectorList,
5430
+ error: optionsHasWarnings
5404
5431
  }
5405
5432
  ),
5406
5433
  /* @__PURE__ */ jsx(
@@ -6899,7 +6926,7 @@ function Block({ blockId, active = true, asComparison = false }) {
6899
6926
  if (![BLOCK_TYPES.GENERATOR, BLOCK_TYPES.VIZ, BLOCK_TYPES.NAV].includes(block.type)) {
6900
6927
  if (block?.type === BLOCK_TYPES.SELECTOR) {
6901
6928
  const blockContent2 = getBlockContent(block, locale);
6902
- const { config: renderVariables } = runSelector_default(blockContent2?.logic, formatterFunctions, blockContext);
6929
+ const { config: renderVariables } = runSelector(block.id, blockContent2?.logic, formatterFunctions, blockContext);
6903
6930
  const content2 = renderVariables;
6904
6931
  return { ...content2, id: block.id, section: block.section_id };
6905
6932
  }
@@ -6942,7 +6969,7 @@ function Block({ blockId, active = true, asComparison = false }) {
6942
6969
  }
6943
6970
  if (block.type === BLOCK_TYPES.SELECTOR) {
6944
6971
  const blockContent2 = getBlockContent(block, locale);
6945
- const { config: renderVariables } = runSelector_default(blockContent2.logic, formatterFunctions, blockContext);
6972
+ const { config: renderVariables } = runSelector(block.id, blockContent2.logic, formatterFunctions, blockContext);
6946
6973
  const content2 = renderVariables;
6947
6974
  setContent({ ...content2, id: block.id, section: block.section_id });
6948
6975
  return;
@@ -7289,10 +7316,13 @@ var guessResponse = (response) => {
7289
7316
  function DataTab(props) {
7290
7317
  const { section } = props;
7291
7318
  const readMemberFn = useReadMemberFn();
7292
- const locale = useAppSelector((state2) => state2.status.currentLocale);
7319
+ const router = useRouter();
7320
+ const isCMS = useAppSelector((state2) => state2.status.isCMS);
7321
+ const statusLocale = useAppSelector((state2) => state2.status.currentLocale);
7322
+ const routerLocale = router.locale;
7323
+ const locale = isCMS ? statusLocale : routerLocale;
7293
7324
  const PREVIEW_SIZE = 50;
7294
7325
  const formatterFunctions = useFormatterFunctionsForLocale(locale);
7295
- const router = useRouter();
7296
7326
  const optionsTranslations = useBespokeTranslations("options");
7297
7327
  const translations = { ...DEFAULT_TRANSLATIONS2, ...optionsTranslations["data_tab"] };
7298
7328
  const [sourcesOptions, setSourcesOptions] = useState([]);
@@ -15780,6 +15810,8 @@ function UsersEditor() {
15780
15810
  // views/BespokeManager.tsx
15781
15811
  init_ErrorBoundary();
15782
15812
  init_envvars();
15813
+ init_statusSlice();
15814
+ init_store2();
15783
15815
  function BespokeManager(options) {
15784
15816
  const {
15785
15817
  title = "Bespoke CMS",
@@ -15969,6 +16001,10 @@ function BespokeManagerShell(props) {
15969
16001
  }
15970
16002
  return /* @__PURE__ */ jsx(NotFoundView, {});
15971
16003
  }, [location, user]);
16004
+ const dispatch = useAppDispatch();
16005
+ useEffect(() => {
16006
+ dispatch(statusActions.setStatus({ isCMS: true }));
16007
+ }, []);
15972
16008
  return /* @__PURE__ */ jsx(AppShell, { styles: {
15973
16009
  main: {
15974
16010
  background: "#f8f9fa",
package/dist/server.js CHANGED
@@ -4226,7 +4226,7 @@ var valueInOptions_default = (type, value, options) => {
4226
4226
  };
4227
4227
 
4228
4228
  // libs/selectors/runSelector.js
4229
- var runSelector_default = (logic, formatterFunctions, blockContext) => {
4229
+ function runSelector(id, logic, formatterFunctions, blockContext, router = void 0) {
4230
4230
  const { variables } = parseBlockContext(blockContext);
4231
4231
  const transpiledLogic = varSwapRecursive_default({ logic }, formatterFunctions, blockContext).logic;
4232
4232
  const evalResult = mortarEval_default("variables", variables, transpiledLogic, formatterFunctions, void 0, blockContext);
@@ -4248,6 +4248,16 @@ var runSelector_default = (logic, formatterFunctions, blockContext) => {
4248
4248
  const fallbackValue = options && options[0] && options[0].id || options[0] || "";
4249
4249
  defaultValue = maybeFixForMulti(fallbackValue);
4250
4250
  }
4251
+ if (router) {
4252
+ const accesor = `selector${id}id`;
4253
+ const { query, pathname } = router;
4254
+ const currentValue = query[accesor];
4255
+ if (currentValue && !valueInOptions_default(type, currentValue, options)) {
4256
+ const newQuery = { ...query };
4257
+ delete newQuery[accesor];
4258
+ router.replace({ pathname, query: newQuery }, void 0, { shallow: true });
4259
+ }
4260
+ }
4251
4261
  const config = {
4252
4262
  name,
4253
4263
  type,
@@ -4257,7 +4267,7 @@ var runSelector_default = (logic, formatterFunctions, blockContext) => {
4257
4267
  hideLabel
4258
4268
  };
4259
4269
  return { config, log, error };
4260
- };
4270
+ }
4261
4271
 
4262
4272
  // libs/selectors/selectorQueryToVariable.js
4263
4273
  var selectorQueryToVariable = (id, query, config) => {
@@ -4434,7 +4444,7 @@ function BlockException(message) {
4434
4444
  this.name = "BlockException";
4435
4445
  this.message = message;
4436
4446
  }
4437
- async function runSingleBlock(block, formatterFunctions, blockContext, readMemberFn) {
4447
+ async function runSingleBlock(block, formatterFunctions, blockContext, readMemberFn, router = void 0) {
4438
4448
  const { locale, variables, query } = parseBlockContext(blockContext);
4439
4449
  const allowed = isBlockAllowed(block, blockContext, variables, formatterFunctions);
4440
4450
  let resp = null;
@@ -4494,10 +4504,12 @@ async function runSingleBlock(block, formatterFunctions, blockContext, readMembe
4494
4504
  duration = Math.max(...apiResponses.map((ar) => ar.requestDuration));
4495
4505
  }
4496
4506
  if (block.type === BLOCK_TYPES.SELECTOR) {
4497
- const { config, log: log2, error: error2 } = runSelector_default(
4507
+ const { config, log: log2, error: error2 } = runSelector(
4508
+ block.id,
4498
4509
  block.contentByLocale[locale].content.logic,
4499
4510
  formatterFunctions,
4500
- blockContext
4511
+ blockContext,
4512
+ router
4501
4513
  );
4502
4514
  return {
4503
4515
  outputVariables: selectorQueryToVariable_default(block.id, query, config),
@@ -4675,7 +4687,7 @@ var isBlockAllowed = (block, blockContext, variables, formatterFunctions) => {
4675
4687
  }
4676
4688
  return allowed;
4677
4689
  };
4678
- var runConsumersV2 = async (blocks, sections, bid, formatterFunctions, blockContext, initialState4 = {}, readMemberFn, mode = "section") => {
4690
+ var runConsumersV2 = async (blocks, sections, bid, formatterFunctions, blockContext, initialState4 = {}, readMemberFn, mode = "section", router = void 0) => {
4679
4691
  if (!bid && !sections)
4680
4692
  return { ...initialState4 };
4681
4693
  const variablesById = { ...initialState4.variables ?? {} };
@@ -4742,7 +4754,8 @@ var runConsumersV2 = async (blocks, sections, bid, formatterFunctions, blockCont
4742
4754
  variables,
4743
4755
  locale: variables.locale
4744
4756
  },
4745
- readMemberFn
4757
+ readMemberFn,
4758
+ router
4746
4759
  ).then(({ outputVariables, status }) => {
4747
4760
  if (status.error) {
4748
4761
  if (typeof window === "undefined") {
@@ -4998,7 +5011,8 @@ var initialState = {
4998
5011
  tools: true,
4999
5012
  settings: false,
5000
5013
  reportEditor: false
5001
- }
5014
+ },
5015
+ isCMS: false
5002
5016
  };
5003
5017
  var statusSlice = createSlice({
5004
5018
  name: "status",
@@ -5750,7 +5764,9 @@ function BespokeManagerServerSideProps(options) {
5750
5764
  dispatch(readEntity("report", {
5751
5765
  id: parseReportId(ctx.query[pathSegmentsKey]),
5752
5766
  include: true
5753
- }))
5767
+ })),
5768
+ // Mark the status as CMS environment
5769
+ dispatch(statusActions.setStatus({ isCMS: true }))
5754
5770
  ]);
5755
5771
  const postResult = await postResolve(store, ctx);
5756
5772
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datawheel/bespoke",
3
- "version": "0.3.11",
3
+ "version": "0.3.13",
4
4
  "description": "Content management system for creating automated data reports",
5
5
  "exports": {
6
6
  ".": {
@@ -129,4 +129,4 @@
129
129
  "peerDependencies": {
130
130
  "next": "^13.4.0"
131
131
  }
132
- }
132
+ }