@patch-adams/core 1.6.0 → 1.6.1

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.cjs CHANGED
@@ -5702,6 +5702,7 @@ function hasClassMappings(mappings) {
5702
5702
  if (mappings.course?.trim()) return true;
5703
5703
  if (mappings.lessons && Object.values(mappings.lessons).some((v) => v?.trim())) return true;
5704
5704
  if (mappings.blocks && Object.values(mappings.blocks).some((v) => v?.trim())) return true;
5705
+ if (mappings.blockGroups && mappings.blockGroups.length > 0) return true;
5705
5706
  return false;
5706
5707
  }
5707
5708
  function generateClassMappingsScript(mappings) {
@@ -5723,6 +5724,10 @@ function generateClassMappingsScript(mappings) {
5723
5724
  }
5724
5725
  const hasBlocks = Object.keys(blockMap).length > 0;
5725
5726
  const hasLessons = Object.keys(lessonMap).length > 0;
5727
+ const blockGroups = (mappings.blockGroups || []).filter(
5728
+ (g) => g.blockIds.length >= 2 && g.className.trim() && g.lessonId
5729
+ );
5730
+ const hasGroups = blockGroups.length > 0;
5726
5731
  const parts = [];
5727
5732
  parts.push(`// PA-Patcher: Custom Class Mappings`);
5728
5733
  parts.push(`(function() {`);
@@ -5788,33 +5793,117 @@ function generateClassMappingsScript(mappings) {
5788
5793
  parts.push(` }`);
5789
5794
  parts.push(``);
5790
5795
  }
5796
+ if (hasGroups) {
5797
+ const groupData = blockGroups.map((g) => ({
5798
+ id: g.id,
5799
+ lessonId: g.lessonId,
5800
+ blockIds: g.blockIds,
5801
+ className: g.className.trim()
5802
+ }));
5803
+ parts.push(` // Block groups (adjacent blocks wrapped in container divs)`);
5804
+ parts.push(` var groupMap = ${JSON.stringify(groupData)};`);
5805
+ parts.push(``);
5806
+ parts.push(` function applyBlockGroups() {`);
5807
+ parts.push(` var hash = window.location.hash || '';`);
5808
+ parts.push(` var match = hash.match(/#\\/lessons\\/([^/]+)/);`);
5809
+ parts.push(` var currentLesson = match ? match[1] : null;`);
5810
+ parts.push(` for (var g = 0; g < groupMap.length; g++) {`);
5811
+ parts.push(` var group = groupMap[g];`);
5812
+ parts.push(` if (group.lessonId !== currentLesson) continue;`);
5813
+ parts.push(` if (document.querySelector('[data-pa-group-id="' + group.id + '"]')) continue;`);
5814
+ parts.push(` var blocks = [];`);
5815
+ parts.push(` var allPresent = true;`);
5816
+ parts.push(` for (var b = 0; b < group.blockIds.length; b++) {`);
5817
+ parts.push(` var el = document.querySelector('[data-block-id="' + group.blockIds[b] + '"]');`);
5818
+ parts.push(` if (!el) { allPresent = false; break; }`);
5819
+ parts.push(` blocks.push(el);`);
5820
+ parts.push(` }`);
5821
+ parts.push(` if (!allPresent) continue;`);
5822
+ parts.push(` var parent = blocks[0].parentNode;`);
5823
+ parts.push(` var sameParent = true;`);
5824
+ parts.push(` for (var s = 1; s < blocks.length; s++) {`);
5825
+ parts.push(` if (blocks[s].parentNode !== parent) { sameParent = false; break; }`);
5826
+ parts.push(` }`);
5827
+ parts.push(` if (!sameParent) continue;`);
5828
+ parts.push(` var wrapper = document.createElement('div');`);
5829
+ parts.push(` wrapper.setAttribute('data-pa-group-id', group.id);`);
5830
+ parts.push(` var cls = group.className.split(' ');`);
5831
+ parts.push(` for (var c = 0; c < cls.length; c++) {`);
5832
+ parts.push(` if (cls[c]) wrapper.classList.add(cls[c]);`);
5833
+ parts.push(` }`);
5834
+ parts.push(` parent.insertBefore(wrapper, blocks[0]);`);
5835
+ parts.push(` for (var m = 0; m < blocks.length; m++) {`);
5836
+ parts.push(` wrapper.appendChild(blocks[m]);`);
5837
+ parts.push(` }`);
5838
+ parts.push(` }`);
5839
+ parts.push(` }`);
5840
+ parts.push(``);
5841
+ parts.push(` function teardownBlockGroups() {`);
5842
+ parts.push(` var wrappers = document.querySelectorAll('[data-pa-group-id]');`);
5843
+ parts.push(` for (var w = 0; w < wrappers.length; w++) {`);
5844
+ parts.push(` var wr = wrappers[w];`);
5845
+ parts.push(` var par = wr.parentNode;`);
5846
+ parts.push(` while (wr.firstChild) {`);
5847
+ parts.push(` par.insertBefore(wr.firstChild, wr);`);
5848
+ parts.push(` }`);
5849
+ parts.push(` par.removeChild(wr);`);
5850
+ parts.push(` }`);
5851
+ parts.push(` }`);
5852
+ parts.push(``);
5853
+ }
5791
5854
  parts.push(` // Initialization`);
5792
5855
  parts.push(` function init() {`);
5793
5856
  if (hasBlocks) {
5794
5857
  parts.push(` applyBlockClasses();`);
5795
5858
  }
5859
+ if (hasGroups) {
5860
+ parts.push(` applyBlockGroups();`);
5861
+ }
5796
5862
  if (hasLessons) {
5797
5863
  parts.push(` applyLessonClasses();`);
5864
+ }
5865
+ if (hasLessons || hasGroups) {
5798
5866
  parts.push(``);
5799
5867
  parts.push(` // Watch for Rise SPA navigation`);
5800
5868
  parts.push(` window.addEventListener('hashchange', function() {`);
5801
- parts.push(` applyLessonClasses();`);
5869
+ if (hasGroups) {
5870
+ parts.push(` teardownBlockGroups();`);
5871
+ }
5872
+ if (hasLessons) {
5873
+ parts.push(` applyLessonClasses();`);
5874
+ }
5802
5875
  if (hasBlocks) {
5803
5876
  parts.push(` setTimeout(applyBlockClasses, 200);`);
5804
5877
  }
5878
+ if (hasGroups) {
5879
+ parts.push(` setTimeout(applyBlockGroups, 300);`);
5880
+ }
5805
5881
  parts.push(` });`);
5806
5882
  }
5807
- if (hasBlocks) {
5883
+ if (hasBlocks || hasGroups) {
5808
5884
  parts.push(``);
5809
5885
  parts.push(` // MutationObserver for lazily-rendered Rise blocks`);
5810
- parts.push(` var observer = new MutationObserver(function(mutations) {`);
5811
- parts.push(` for (var i = 0; i < mutations.length; i++) {`);
5812
- parts.push(` if (mutations[i].addedNodes.length > 0) {`);
5813
- parts.push(` applyBlockClasses();`);
5814
- parts.push(` return;`);
5815
- parts.push(` }`);
5816
- parts.push(` }`);
5817
- parts.push(` });`);
5886
+ if (hasGroups) {
5887
+ parts.push(` var debounceTimer = null;`);
5888
+ parts.push(` var observer = new MutationObserver(function() {`);
5889
+ parts.push(` if (debounceTimer) clearTimeout(debounceTimer);`);
5890
+ parts.push(` debounceTimer = setTimeout(function() {`);
5891
+ if (hasBlocks) {
5892
+ parts.push(` applyBlockClasses();`);
5893
+ }
5894
+ parts.push(` applyBlockGroups();`);
5895
+ parts.push(` }, 100);`);
5896
+ parts.push(` });`);
5897
+ } else {
5898
+ parts.push(` var observer = new MutationObserver(function(mutations) {`);
5899
+ parts.push(` for (var i = 0; i < mutations.length; i++) {`);
5900
+ parts.push(` if (mutations[i].addedNodes.length > 0) {`);
5901
+ parts.push(` applyBlockClasses();`);
5902
+ parts.push(` return;`);
5903
+ parts.push(` }`);
5904
+ parts.push(` }`);
5905
+ parts.push(` });`);
5906
+ }
5818
5907
  parts.push(` var container = document.querySelector('#app') || document.body;`);
5819
5908
  parts.push(` observer.observe(container, { childList: true, subtree: true });`);
5820
5909
  }