@datawheel/bespoke 0.4.1 → 0.4.2

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
@@ -1104,7 +1104,7 @@ var init_valueInOptions = __esm({
1104
1104
  });
1105
1105
 
1106
1106
  // libs/selectors/runSelector.js
1107
- function runSelector(id, logic, formatterFunctions, blockContext, router = void 0) {
1107
+ function runSelector(id, logic, formatterFunctions, blockContext, replaceQuery = false) {
1108
1108
  const { variables } = parseBlockContext(blockContext);
1109
1109
  const transpiledLogic = varSwapRecursive_default({ logic }, formatterFunctions, blockContext).logic;
1110
1110
  const evalResult = mortarEval_default("variables", variables, transpiledLogic, formatterFunctions, void 0, blockContext);
@@ -1126,14 +1126,14 @@ function runSelector(id, logic, formatterFunctions, blockContext, router = void
1126
1126
  const fallbackValue = options && options[0] && options[0].id || options[0] || "";
1127
1127
  defaultValue = maybeFixForMulti2(fallbackValue);
1128
1128
  }
1129
- if (router) {
1129
+ if (replaceQuery) {
1130
1130
  const accesor = `selector${id}id`;
1131
- const { query, pathname } = router;
1131
+ const searchParams = new URLSearchParams(window.location.search);
1132
+ const query = Object.fromEntries(searchParams);
1132
1133
  const currentValue = query[accesor];
1133
- if (currentValue && !valueInOptions_default(type, currentValue, options)) {
1134
- const newQuery = { ...query };
1135
- delete newQuery[accesor];
1136
- router.replace({ pathname, query: newQuery }, void 0, { shallow: true });
1134
+ if (currentValue && !valueInOptions_default(type, currentValue, options) && currentValue !== void 0) {
1135
+ searchParams.delete(accesor);
1136
+ window.history.replaceState(null, null, `?${searchParams.toString()}`);
1137
1137
  }
1138
1138
  }
1139
1139
  const config = {
@@ -1175,7 +1175,7 @@ var init_selectorQueryToVariable = __esm({
1175
1175
  [`selector${id}label`]: (options || []).map((d) => d.label)
1176
1176
  };
1177
1177
  }
1178
- const queryId = queryObject.get(accessor) ? queryObject.get(accessor) : config.defaultValue;
1178
+ const queryId = queryObject.get(accessor) && config.options.find((d) => d.id === queryObject.get(accessor)) ? queryObject.get(accessor) : config.defaultValue;
1179
1179
  const option = config.options.find((d) => d.id === queryId);
1180
1180
  return {
1181
1181
  [`selector${id}id`]: queryId,
@@ -1361,7 +1361,7 @@ function BlockException(message) {
1361
1361
  this.name = "BlockException";
1362
1362
  this.message = message;
1363
1363
  }
1364
- async function runSingleBlock(block, formatterFunctions, blockContext, readMemberFn, router = void 0) {
1364
+ async function runSingleBlock(block, formatterFunctions, blockContext, readMemberFn) {
1365
1365
  const { locale, variables, query } = parseBlockContext(blockContext);
1366
1366
  const allowed = isBlockAllowed(block, blockContext, variables, formatterFunctions);
1367
1367
  let resp = null;
@@ -1426,7 +1426,7 @@ async function runSingleBlock(block, formatterFunctions, blockContext, readMembe
1426
1426
  block.contentByLocale[locale].content.logic,
1427
1427
  formatterFunctions,
1428
1428
  blockContext,
1429
- router
1429
+ true
1430
1430
  );
1431
1431
  return {
1432
1432
  outputVariables: selectorQueryToVariable_default(block.id, query, config),
@@ -1646,7 +1646,7 @@ var init_runConsumers = __esm({
1646
1646
  }
1647
1647
  return allowed;
1648
1648
  };
1649
- runConsumersV2 = async (blocks, sections, bid, formatterFunctions, blockContext, initialState4 = {}, readMemberFn, mode = "section", router = void 0) => {
1649
+ runConsumersV2 = async (blocks, sections, bid, formatterFunctions, blockContext, initialState4 = {}, readMemberFn, mode = "section") => {
1650
1650
  if (!bid && !sections)
1651
1651
  return { ...initialState4 };
1652
1652
  const variablesById = { ...initialState4.variables ?? {} };
@@ -1713,8 +1713,7 @@ var init_runConsumers = __esm({
1713
1713
  variables,
1714
1714
  locale: variables.locale
1715
1715
  },
1716
- readMemberFn,
1717
- router
1716
+ readMemberFn
1718
1717
  ).then(({ outputVariables, status }) => {
1719
1718
  if (status.error) {
1720
1719
  if (typeof window === "undefined") {
@@ -4301,7 +4300,9 @@ function useOnChangeSelector(blockId, section, selectorIdentifier, asComparison
4301
4300
  query: { ...querySelectors, [selectorIdentifier]: value },
4302
4301
  variables
4303
4302
  };
4304
- router.replace({ query: { ...query, [selectorIdentifierParams]: value } }, void 0, { shallow: true });
4303
+ const searchParams = new URLSearchParams(window.location.search);
4304
+ searchParams.set(selectorIdentifierParams, value);
4305
+ window.history.replaceState(null, null, `?${searchParams.toString()}`);
4305
4306
  runConsumersV2(
4306
4307
  blockRecords,
4307
4308
  void 0,
@@ -4313,8 +4314,7 @@ function useOnChangeSelector(blockId, section, selectorIdentifier, asComparison
4313
4314
  status: initialStatus
4314
4315
  },
4315
4316
  readMemberFn,
4316
- "report",
4317
- router
4317
+ "report"
4318
4318
  ).then((data) => {
4319
4319
  if (comparison.active && asComparison) {
4320
4320
  comparison.setComparisonVariables({
package/dist/server.js CHANGED
@@ -4633,7 +4633,7 @@ var valueInOptions_default = (type, value, options) => {
4633
4633
  };
4634
4634
 
4635
4635
  // libs/selectors/runSelector.js
4636
- function runSelector(id, logic, formatterFunctions, blockContext, router = void 0) {
4636
+ function runSelector(id, logic, formatterFunctions, blockContext, replaceQuery = false) {
4637
4637
  const { variables } = parseBlockContext(blockContext);
4638
4638
  const transpiledLogic = varSwapRecursive_default({ logic }, formatterFunctions, blockContext).logic;
4639
4639
  const evalResult = mortarEval_default("variables", variables, transpiledLogic, formatterFunctions, void 0, blockContext);
@@ -4655,14 +4655,14 @@ function runSelector(id, logic, formatterFunctions, blockContext, router = void
4655
4655
  const fallbackValue = options && options[0] && options[0].id || options[0] || "";
4656
4656
  defaultValue = maybeFixForMulti(fallbackValue);
4657
4657
  }
4658
- if (router) {
4658
+ if (replaceQuery) {
4659
4659
  const accesor = `selector${id}id`;
4660
- const { query, pathname } = router;
4660
+ const searchParams = new URLSearchParams(window.location.search);
4661
+ const query = Object.fromEntries(searchParams);
4661
4662
  const currentValue = query[accesor];
4662
- if (currentValue && !valueInOptions_default(type, currentValue, options)) {
4663
- const newQuery = { ...query };
4664
- delete newQuery[accesor];
4665
- router.replace({ pathname, query: newQuery }, void 0, { shallow: true });
4663
+ if (currentValue && !valueInOptions_default(type, currentValue, options) && currentValue !== void 0) {
4664
+ searchParams.delete(accesor);
4665
+ window.history.replaceState(null, null, `?${searchParams.toString()}`);
4666
4666
  }
4667
4667
  }
4668
4668
  const config = {
@@ -4688,7 +4688,7 @@ var selectorQueryToVariable = (id, query, config) => {
4688
4688
  [`selector${id}label`]: (options || []).map((d) => d.label)
4689
4689
  };
4690
4690
  }
4691
- const queryId = queryObject.get(accessor) ? queryObject.get(accessor) : config.defaultValue;
4691
+ const queryId = queryObject.get(accessor) && config.options.find((d) => d.id === queryObject.get(accessor)) ? queryObject.get(accessor) : config.defaultValue;
4692
4692
  const option = config.options.find((d) => d.id === queryId);
4693
4693
  return {
4694
4694
  [`selector${id}id`]: queryId,
@@ -4848,7 +4848,7 @@ function BlockException(message) {
4848
4848
  this.name = "BlockException";
4849
4849
  this.message = message;
4850
4850
  }
4851
- async function runSingleBlock(block, formatterFunctions, blockContext, readMemberFn, router = void 0) {
4851
+ async function runSingleBlock(block, formatterFunctions, blockContext, readMemberFn) {
4852
4852
  const { locale, variables, query } = parseBlockContext(blockContext);
4853
4853
  const allowed = isBlockAllowed(block, blockContext, variables, formatterFunctions);
4854
4854
  let resp = null;
@@ -4913,7 +4913,7 @@ async function runSingleBlock(block, formatterFunctions, blockContext, readMembe
4913
4913
  block.contentByLocale[locale].content.logic,
4914
4914
  formatterFunctions,
4915
4915
  blockContext,
4916
- router
4916
+ true
4917
4917
  );
4918
4918
  return {
4919
4919
  outputVariables: selectorQueryToVariable_default(block.id, query, config),
@@ -5091,7 +5091,7 @@ var isBlockAllowed = (block, blockContext, variables, formatterFunctions) => {
5091
5091
  }
5092
5092
  return allowed;
5093
5093
  };
5094
- var runConsumersV2 = async (blocks, sections, bid, formatterFunctions, blockContext, initialState4 = {}, readMemberFn, mode = "section", router = void 0) => {
5094
+ var runConsumersV2 = async (blocks, sections, bid, formatterFunctions, blockContext, initialState4 = {}, readMemberFn, mode = "section") => {
5095
5095
  if (!bid && !sections)
5096
5096
  return { ...initialState4 };
5097
5097
  const variablesById = { ...initialState4.variables ?? {} };
@@ -5158,8 +5158,7 @@ var runConsumersV2 = async (blocks, sections, bid, formatterFunctions, blockCont
5158
5158
  variables,
5159
5159
  locale: variables.locale
5160
5160
  },
5161
- readMemberFn,
5162
- router
5161
+ readMemberFn
5163
5162
  ).then(({ outputVariables, status }) => {
5164
5163
  if (status.error) {
5165
5164
  if (typeof window === "undefined") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datawheel/bespoke",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Content management system for creating automated data reports",
5
5
  "exports": {
6
6
  ".": {