@patch-adams/core 1.6.0 → 1.6.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/cli.cjs CHANGED
@@ -5640,18 +5640,21 @@ window.pa_patcher = window.pa_patcher || {
5640
5640
 
5641
5641
  (function() {
5642
5642
  'use strict';
5643
- // Fetch server-controlled cache version (sync, once)
5644
- var ASSET_VERSION_URL = "${assetVersionUrl || ""}";
5645
- window.__pa_v = '';
5646
- if (ASSET_VERSION_URL) {
5647
- try {
5648
- var xhr = new XMLHttpRequest();
5649
- xhr.open('GET', ASSET_VERSION_URL, false);
5650
- xhr.send();
5651
- if (xhr.status === 200) {
5652
- window.__pa_v = JSON.parse(xhr.responseText).v || '1';
5653
- }
5654
- } catch(e) { window.__pa_v = '1'; }
5643
+ // Cache version \u2014 normally set by version resolver (injected before CSS Before).
5644
+ // Fallback: fetch here if version resolver didn't run (older patched packages).
5645
+ if (typeof window.__pa_v === 'undefined') {
5646
+ var ASSET_VERSION_URL = "${assetVersionUrl || ""}";
5647
+ window.__pa_v = '';
5648
+ if (ASSET_VERSION_URL) {
5649
+ try {
5650
+ var xhr = new XMLHttpRequest();
5651
+ xhr.open('GET', ASSET_VERSION_URL, false);
5652
+ xhr.send();
5653
+ if (xhr.status === 200) {
5654
+ window.__pa_v = JSON.parse(xhr.responseText).v || '1';
5655
+ }
5656
+ } catch(e) { window.__pa_v = '1'; }
5657
+ }
5655
5658
  }
5656
5659
 
5657
5660
  var REMOTE_URL = "${remoteUrl}" + (window.__pa_v ? "?v=" + window.__pa_v : "");
@@ -6034,6 +6037,7 @@ function hasClassMappings(mappings) {
6034
6037
  if (mappings.course?.trim()) return true;
6035
6038
  if (mappings.lessons && Object.values(mappings.lessons).some((v) => v?.trim())) return true;
6036
6039
  if (mappings.blocks && Object.values(mappings.blocks).some((v) => v?.trim())) return true;
6040
+ if (mappings.blockGroups && mappings.blockGroups.length > 0) return true;
6037
6041
  return false;
6038
6042
  }
6039
6043
  function generateClassMappingsScript(mappings) {
@@ -6055,6 +6059,10 @@ function generateClassMappingsScript(mappings) {
6055
6059
  }
6056
6060
  const hasBlocks = Object.keys(blockMap).length > 0;
6057
6061
  const hasLessons = Object.keys(lessonMap).length > 0;
6062
+ const blockGroups = (mappings.blockGroups || []).filter(
6063
+ (g) => g.blockIds.length >= 2 && g.className.trim() && g.lessonId
6064
+ );
6065
+ const hasGroups = blockGroups.length > 0;
6058
6066
  const parts = [];
6059
6067
  parts.push(`// PA-Patcher: Custom Class Mappings`);
6060
6068
  parts.push(`(function() {`);
@@ -6120,33 +6128,117 @@ function generateClassMappingsScript(mappings) {
6120
6128
  parts.push(` }`);
6121
6129
  parts.push(``);
6122
6130
  }
6131
+ if (hasGroups) {
6132
+ const groupData = blockGroups.map((g) => ({
6133
+ id: g.id,
6134
+ lessonId: g.lessonId,
6135
+ blockIds: g.blockIds,
6136
+ className: g.className.trim()
6137
+ }));
6138
+ parts.push(` // Block groups (adjacent blocks wrapped in container divs)`);
6139
+ parts.push(` var groupMap = ${JSON.stringify(groupData)};`);
6140
+ parts.push(``);
6141
+ parts.push(` function applyBlockGroups() {`);
6142
+ parts.push(` var hash = window.location.hash || '';`);
6143
+ parts.push(` var match = hash.match(/#\\/lessons\\/([^/]+)/);`);
6144
+ parts.push(` var currentLesson = match ? match[1] : null;`);
6145
+ parts.push(` for (var g = 0; g < groupMap.length; g++) {`);
6146
+ parts.push(` var group = groupMap[g];`);
6147
+ parts.push(` if (group.lessonId !== currentLesson) continue;`);
6148
+ parts.push(` if (document.querySelector('[data-pa-group-id="' + group.id + '"]')) continue;`);
6149
+ parts.push(` var blocks = [];`);
6150
+ parts.push(` var allPresent = true;`);
6151
+ parts.push(` for (var b = 0; b < group.blockIds.length; b++) {`);
6152
+ parts.push(` var el = document.querySelector('[data-block-id="' + group.blockIds[b] + '"]');`);
6153
+ parts.push(` if (!el) { allPresent = false; break; }`);
6154
+ parts.push(` blocks.push(el);`);
6155
+ parts.push(` }`);
6156
+ parts.push(` if (!allPresent) continue;`);
6157
+ parts.push(` var parent = blocks[0].parentNode;`);
6158
+ parts.push(` var sameParent = true;`);
6159
+ parts.push(` for (var s = 1; s < blocks.length; s++) {`);
6160
+ parts.push(` if (blocks[s].parentNode !== parent) { sameParent = false; break; }`);
6161
+ parts.push(` }`);
6162
+ parts.push(` if (!sameParent) continue;`);
6163
+ parts.push(` var wrapper = document.createElement('div');`);
6164
+ parts.push(` wrapper.setAttribute('data-pa-group-id', group.id);`);
6165
+ parts.push(` var cls = group.className.split(' ');`);
6166
+ parts.push(` for (var c = 0; c < cls.length; c++) {`);
6167
+ parts.push(` if (cls[c]) wrapper.classList.add(cls[c]);`);
6168
+ parts.push(` }`);
6169
+ parts.push(` parent.insertBefore(wrapper, blocks[0]);`);
6170
+ parts.push(` for (var m = 0; m < blocks.length; m++) {`);
6171
+ parts.push(` wrapper.appendChild(blocks[m]);`);
6172
+ parts.push(` }`);
6173
+ parts.push(` }`);
6174
+ parts.push(` }`);
6175
+ parts.push(``);
6176
+ parts.push(` function teardownBlockGroups() {`);
6177
+ parts.push(` var wrappers = document.querySelectorAll('[data-pa-group-id]');`);
6178
+ parts.push(` for (var w = 0; w < wrappers.length; w++) {`);
6179
+ parts.push(` var wr = wrappers[w];`);
6180
+ parts.push(` var par = wr.parentNode;`);
6181
+ parts.push(` while (wr.firstChild) {`);
6182
+ parts.push(` par.insertBefore(wr.firstChild, wr);`);
6183
+ parts.push(` }`);
6184
+ parts.push(` par.removeChild(wr);`);
6185
+ parts.push(` }`);
6186
+ parts.push(` }`);
6187
+ parts.push(``);
6188
+ }
6123
6189
  parts.push(` // Initialization`);
6124
6190
  parts.push(` function init() {`);
6125
6191
  if (hasBlocks) {
6126
6192
  parts.push(` applyBlockClasses();`);
6127
6193
  }
6194
+ if (hasGroups) {
6195
+ parts.push(` applyBlockGroups();`);
6196
+ }
6128
6197
  if (hasLessons) {
6129
6198
  parts.push(` applyLessonClasses();`);
6199
+ }
6200
+ if (hasLessons || hasGroups) {
6130
6201
  parts.push(``);
6131
6202
  parts.push(` // Watch for Rise SPA navigation`);
6132
6203
  parts.push(` window.addEventListener('hashchange', function() {`);
6133
- parts.push(` applyLessonClasses();`);
6204
+ if (hasGroups) {
6205
+ parts.push(` teardownBlockGroups();`);
6206
+ }
6207
+ if (hasLessons) {
6208
+ parts.push(` applyLessonClasses();`);
6209
+ }
6134
6210
  if (hasBlocks) {
6135
6211
  parts.push(` setTimeout(applyBlockClasses, 200);`);
6136
6212
  }
6213
+ if (hasGroups) {
6214
+ parts.push(` setTimeout(applyBlockGroups, 300);`);
6215
+ }
6137
6216
  parts.push(` });`);
6138
6217
  }
6139
- if (hasBlocks) {
6218
+ if (hasBlocks || hasGroups) {
6140
6219
  parts.push(``);
6141
6220
  parts.push(` // MutationObserver for lazily-rendered Rise blocks`);
6142
- parts.push(` var observer = new MutationObserver(function(mutations) {`);
6143
- parts.push(` for (var i = 0; i < mutations.length; i++) {`);
6144
- parts.push(` if (mutations[i].addedNodes.length > 0) {`);
6145
- parts.push(` applyBlockClasses();`);
6146
- parts.push(` return;`);
6147
- parts.push(` }`);
6148
- parts.push(` }`);
6149
- parts.push(` });`);
6221
+ if (hasGroups) {
6222
+ parts.push(` var debounceTimer = null;`);
6223
+ parts.push(` var observer = new MutationObserver(function() {`);
6224
+ parts.push(` if (debounceTimer) clearTimeout(debounceTimer);`);
6225
+ parts.push(` debounceTimer = setTimeout(function() {`);
6226
+ if (hasBlocks) {
6227
+ parts.push(` applyBlockClasses();`);
6228
+ }
6229
+ parts.push(` applyBlockGroups();`);
6230
+ parts.push(` }, 100);`);
6231
+ parts.push(` });`);
6232
+ } else {
6233
+ parts.push(` var observer = new MutationObserver(function(mutations) {`);
6234
+ parts.push(` for (var i = 0; i < mutations.length; i++) {`);
6235
+ parts.push(` if (mutations[i].addedNodes.length > 0) {`);
6236
+ parts.push(` applyBlockClasses();`);
6237
+ parts.push(` return;`);
6238
+ parts.push(` }`);
6239
+ parts.push(` }`);
6240
+ parts.push(` });`);
6241
+ }
6150
6242
  parts.push(` var container = document.querySelector('#app') || document.body;`);
6151
6243
  parts.push(` observer.observe(container, { childList: true, subtree: true });`);
6152
6244
  }
@@ -6212,6 +6304,7 @@ var HtmlInjector = class {
6212
6304
  inject(html) {
6213
6305
  let result = html;
6214
6306
  result = this.addHtmlAttributes(result);
6307
+ result = this.injectVersionResolver(result);
6215
6308
  if (this.config.cssBefore.enabled) {
6216
6309
  result = this.injectCssBefore(result);
6217
6310
  }
@@ -6315,6 +6408,20 @@ ${pluginJs}
6315
6408
  escapeAttr(str) {
6316
6409
  return str.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/'/g, "&#39;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
6317
6410
  }
6411
+ /**
6412
+ * Inject a tiny inline script that fetches the asset version BEFORE any loaders.
6413
+ * Sets window.__pa_v so CSS Before, JS Before, and all other loaders get ?v= cache bust.
6414
+ */
6415
+ injectVersionResolver(html) {
6416
+ const assetVersionUrl = this.config.assetVersionUrl || "";
6417
+ if (!assetVersionUrl) return html;
6418
+ const script = `<!-- === PATCH-ADAMS: VERSION RESOLVER === -->
6419
+ <script data-pa="version-resolver">
6420
+ (function(){try{var x=new XMLHttpRequest();x.open('GET','${assetVersionUrl}',false);x.send();if(x.status===200){window.__pa_v=JSON.parse(x.responseText).v||'1'}}catch(e){window.__pa_v='1'}})();
6421
+ </script>`;
6422
+ return html.replace(/<head([^>]*)>/i, `<head$1>
6423
+ ${script}`);
6424
+ }
6318
6425
  /**
6319
6426
  * Inject CSS Before loader at start of <head>
6320
6427
  */