@very-simple/ajax 0.6.0 → 0.7.0

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.d.ts CHANGED
@@ -8,6 +8,7 @@ interface VisitOptions {
8
8
  loadingDelay?: number;
9
9
  progressHideDelay?: number;
10
10
  request?: RequestInit;
11
+ regions?: string[];
11
12
  }
12
13
  type MergeStrategy = 'replace' | 'morph' | 'before' | 'after' | 'prepend' | 'append' | 'update';
13
14
  interface Config {
@@ -42,7 +43,7 @@ declare global {
42
43
 
43
44
  declare const _default: {
44
45
  start: ({ watchHistory, merge, morphHeads, loadingDelay, }?: Config) => void;
45
- visit: (url: string, { action, emitEvents, isBackForward, morphHeads, merge: mergeStrategy, loadingDelay, progressHideDelay, autoFocus, request, }?: VisitOptions) => Promise<void>;
46
+ visit: (url: string, { action, emitEvents, isBackForward, morphHeads, merge: mergeStrategy, loadingDelay, progressHideDelay, autoFocus, request, regions, }?: VisitOptions) => Promise<void>;
46
47
  };
47
48
 
48
49
  export { _default as default };
package/dist/index.js CHANGED
@@ -12,7 +12,7 @@ var parser = new DOMParser();
12
12
  var currentLoadController;
13
13
  var progress = 0;
14
14
  var trickleInterval;
15
- var load = async (url, regionIds, options) => {
15
+ var load = async (url, regions, options) => {
16
16
  let progressDelayTimeout;
17
17
  try {
18
18
  currentLoadController?.abort();
@@ -28,7 +28,7 @@ var load = async (url, regionIds, options) => {
28
28
  headers: {
29
29
  ...options.request?.headers,
30
30
  "X-Very-Simple-Request": "true",
31
- "X-Very-Simple-Regions": regionIds.join(" ")
31
+ "X-Very-Simple-Regions": regions.join(" ")
32
32
  },
33
33
  signal: currentLoadController.signal
34
34
  });
@@ -147,7 +147,8 @@ var visit = async (url, {
147
147
  loadingDelay = globalConfig.loadingDelay,
148
148
  progressHideDelay = globalConfig.progressHideDelay,
149
149
  autoFocus = true,
150
- request
150
+ request,
151
+ regions = []
151
152
  } = {}) => {
152
153
  prevUrl = currentUrl;
153
154
  currentUrl = url;
@@ -156,10 +157,9 @@ var visit = async (url, {
156
157
  if (isBackForward) {
157
158
  newDocument = cache.get(url)?.cloneNode(true);
158
159
  }
159
- const regions = findRegions(document);
160
160
  if (!newDocument) {
161
161
  const options = { loadingDelay, progressHideDelay, request };
162
- const result = await load(url, Array.from(regions.keys()), options);
162
+ const result = await load(url, regions, options);
163
163
  if (result) ({ url, document: newDocument } = result);
164
164
  }
165
165
  if (!newDocument) return;
@@ -169,17 +169,16 @@ var visit = async (url, {
169
169
  cache.set(cacheId, document.cloneNode(true));
170
170
  }
171
171
  if (morphHeads) Idiomorph2.morph(document.head, newDocument.head);
172
- const getMergeStrategy = (oldEl, newEl) => newEl.dataset.simpleAjax || oldEl.dataset.simpleAjax || mergeStrategy;
172
+ const getMergeStrategy = (oldEl, newEl) => newEl.dataset.simpleMerge || oldEl.dataset.simpleMerge || mergeStrategy;
173
173
  let autoFocusEl;
174
- const newRegions = findRegions(newDocument);
175
- const regionKeys = new Set(regions.keys());
176
- const newRegionKeys = new Set(newRegions.keys());
177
- const commonRegionKeys = regionKeys.intersection(newRegionKeys);
178
- const hasCommonRegions = commonRegionKeys.size > 0;
179
- if (hasCommonRegions) {
180
- for (const id of commonRegionKeys) {
181
- const region = regions.get(id);
182
- const newRegion = newRegions.get(id);
174
+ const hasMatchingRegions = regions?.some(
175
+ (selector) => document.querySelector(selector) && newDocument.querySelector(selector)
176
+ );
177
+ if (hasMatchingRegions) {
178
+ for (const id of regions) {
179
+ const region = document.querySelector(id);
180
+ const newRegion = newDocument.querySelector(id);
181
+ if (!region || !newRegion) continue;
183
182
  const strategy = getMergeStrategy(region, newRegion);
184
183
  const result = merge(region, newRegion, strategy);
185
184
  autoFocusEl ??= result.autoFocusEl;
@@ -196,14 +195,6 @@ var visit = async (url, {
196
195
  else if (action === "push") history.pushState(null, "", url);
197
196
  if (emitEvents) emit("visit", { url, prevUrl });
198
197
  };
199
- var findRegions = (doc) => {
200
- let regions = /* @__PURE__ */ new Map();
201
- doc.querySelectorAll("[data-simple-ajax]").forEach((el) => {
202
- if (!el.id) console.warn(`Ajax region is missing an id:`, el);
203
- else regions.set(el.id, el);
204
- });
205
- return regions;
206
- };
207
198
 
208
199
  // src/start.ts
209
200
  var start = ({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@very-simple/ajax",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "A very simple turbolinks inspired library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",