@datawheel/bespoke 0.3.12 → 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") {
@@ -4526,7 +4538,8 @@ function useOnChangeSelector(blockId, section, selectorIdentifier, asComparison
4526
4538
  status: initialStatus
4527
4539
  },
4528
4540
  readMemberFn,
4529
- "report"
4541
+ "report",
4542
+ router
4530
4543
  ).then((data) => {
4531
4544
  if (comparison.active && asComparison) {
4532
4545
  comparison.setComparisonVariables({
@@ -5253,7 +5266,10 @@ function StaticSelectorOptionRow({ option, editOption, deleteOption }) {
5253
5266
  {
5254
5267
  placeholder: "myOption",
5255
5268
  value: option.id,
5256
- 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
5257
5273
  }
5258
5274
  ) }),
5259
5275
  /* @__PURE__ */ jsx("td", { children: /* @__PURE__ */ jsx(
@@ -5376,6 +5392,15 @@ function DynamicSelectorOptionsEditor({ id, dynamicOptions, setDynamicOptions })
5376
5392
  labelKey: null
5377
5393
  });
5378
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]);
5379
5404
  if (!isInitialized)
5380
5405
  return null;
5381
5406
  const optionsVarList = Object.keys(optionVariableMap);
@@ -5401,7 +5426,8 @@ function DynamicSelectorOptionsEditor({ id, dynamicOptions, setDynamicOptions })
5401
5426
  required: !isPrimitiveType,
5402
5427
  disabled: isPrimitiveType,
5403
5428
  onChange: (val) => setState("idKey", val),
5404
- data: valueSelectorList
5429
+ data: valueSelectorList,
5430
+ error: optionsHasWarnings
5405
5431
  }
5406
5432
  ),
5407
5433
  /* @__PURE__ */ jsx(
@@ -6900,7 +6926,7 @@ function Block({ blockId, active = true, asComparison = false }) {
6900
6926
  if (![BLOCK_TYPES.GENERATOR, BLOCK_TYPES.VIZ, BLOCK_TYPES.NAV].includes(block.type)) {
6901
6927
  if (block?.type === BLOCK_TYPES.SELECTOR) {
6902
6928
  const blockContent2 = getBlockContent(block, locale);
6903
- const { config: renderVariables } = runSelector_default(blockContent2?.logic, formatterFunctions, blockContext);
6929
+ const { config: renderVariables } = runSelector(block.id, blockContent2?.logic, formatterFunctions, blockContext);
6904
6930
  const content2 = renderVariables;
6905
6931
  return { ...content2, id: block.id, section: block.section_id };
6906
6932
  }
@@ -6943,7 +6969,7 @@ function Block({ blockId, active = true, asComparison = false }) {
6943
6969
  }
6944
6970
  if (block.type === BLOCK_TYPES.SELECTOR) {
6945
6971
  const blockContent2 = getBlockContent(block, locale);
6946
- const { config: renderVariables } = runSelector_default(blockContent2.logic, formatterFunctions, blockContext);
6972
+ const { config: renderVariables } = runSelector(block.id, blockContent2.logic, formatterFunctions, blockContext);
6947
6973
  const content2 = renderVariables;
6948
6974
  setContent({ ...content2, id: block.id, section: block.section_id });
6949
6975
  return;
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") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datawheel/bespoke",
3
- "version": "0.3.12",
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
+ }