pi-scenes 0.11.1 → 0.13.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.
@@ -0,0 +1,234 @@
1
+
2
+ // ==========================================================================
3
+ // 实体书真·A4 流式拆页与代码防跨页保护引擎 (True A4 Reflow & Anti-Split Engine)
4
+ // ==========================================
5
+ (function() {
6
+ const MAX_A4_BODY_HEIGHT = 970; // A4 净版心可用像素高度 (1123px - 页眉28px - 页脚25px - 留白)
7
+ let a4Pages = [];
8
+ let currentPageIdx = 0;
9
+ let isPagedMode = localStorage.getItem('course_book_paged_mode') !== 'false';
10
+
11
+ const prevBtn = document.getElementById('prevPageBtn');
12
+ const nextBtn = document.getElementById('nextPageBtn');
13
+ const pageInfo = document.getElementById('pageInfo');
14
+ const modeBtn = document.getElementById('modeToggleBtn');
15
+ const bookContainer = document.querySelector('.book');
16
+
17
+ // 创建一个空白的标准 A4 纸张 DOM
18
+ function createA4PageDOM(pageNum, moduleName, chapterTitle) {
19
+ const page = document.createElement('div');
20
+ page.className = `a4-page ${pageNum % 2 === 0 ? 'even-page' : 'odd-page'}`;
21
+ page.dataset.pageNum = pageNum;
22
+
23
+ // 页眉 (Running Header:单行精炼版)
24
+ // 模块名精炼:若带冒号,截取前半部分作为短模块名 (例如: 模块 5 · 上量部署)
25
+ let shortModule = moduleName || '随课生长深度实战';
26
+ if (shortModule.includes(':')) shortModule = shortModule.split(':')[0].trim();
27
+ else if (shortModule.includes(':')) shortModule = shortModule.split(':')[0].trim();
28
+
29
+ const header = document.createElement('header');
30
+ header.className = 'a4-header';
31
+ header.innerHTML = `
32
+ <span class="hdr-left"><span class="hdr-module">${shortModule}</span><span class="hdr-sep">&bull;</span><span class="hdr-chapter">${chapterTitle || ''}</span></span>
33
+ <span class="hdr-right">随课生长实战系列 · Feng-H</span>
34
+ `;
35
+
36
+ // 净版心内容区
37
+ const body = document.createElement('div');
38
+ body.className = 'a4-body';
39
+
40
+ // 页脚 (Running Footer)
41
+ const footer = document.createElement('footer');
42
+ footer.className = 'a4-footer';
43
+ footer.innerHTML = `
44
+ <span class="ftr-left">通过 Google Colab / Kaggle 学习模型微调</span>
45
+ <span class="ftr-page-num">— 第 ${pageNum} 页 —</span>
46
+ <span class="ftr-right">Feng-H 著</span>
47
+ `;
48
+
49
+ page.appendChild(header);
50
+ page.appendChild(body);
51
+ page.appendChild(footer);
52
+ return { page, body };
53
+ }
54
+
55
+ // 核心拆页排版主程序
56
+ function reflowBookIntoA4() {
57
+ const rawChapters = Array.from(document.querySelectorAll('.chapter'));
58
+ const covers = Array.from(document.querySelectorAll('.cover, .front-matter'));
59
+
60
+ // 如果已经拆过,不再重复拆
61
+ if (document.querySelectorAll('.a4-page').length > 0) return;
62
+
63
+ let globalPageNum = 1;
64
+ a4Pages = [];
65
+
66
+ // 1. 封面与扉页作为专属 A4 首页
67
+ covers.forEach(c => {
68
+ const a4 = document.createElement('div');
69
+ a4.className = `a4-page ${globalPageNum % 2 === 0 ? 'even-page' : 'odd-page'} front-a4-page`;
70
+ a4.dataset.pageNum = globalPageNum;
71
+
72
+ // 封面/扉页内嵌原始内容
73
+ const cloned = c.cloneNode(true);
74
+ cloned.style.display = 'block';
75
+ cloned.style.margin = '0';
76
+ cloned.style.border = 'none';
77
+ cloned.style.boxShadow = 'none';
78
+ cloned.style.padding = '0';
79
+ a4.appendChild(cloned);
80
+
81
+ // 隐藏原容器
82
+ c.style.display = 'none';
83
+ bookContainer.appendChild(a4);
84
+
85
+ a4Pages.push({
86
+ element: a4,
87
+ pageNum: globalPageNum,
88
+ title: c.querySelector('h1, h2')?.innerText?.trim() || '封面/扉页'
89
+ });
90
+ globalPageNum++;
91
+ });
92
+
93
+ // 2. 正文各章流式装订
94
+ rawChapters.forEach(chap => {
95
+ // 提取模块名与章节名
96
+ const moduleName = chap.querySelector('.kicker')?.innerText?.trim() || '';
97
+ const chapterTitle = chap.querySelector('h1')?.innerText?.trim() || '';
98
+ const chapId = chap.id;
99
+
100
+ // 提取所有顶层子元素
101
+ const children = Array.from(chap.children).filter(el => !el.classList.contains('kicker'));
102
+
103
+ // 初始化本章第一张 A4 纸
104
+ let currentA4 = createA4PageDOM(globalPageNum, moduleName, chapterTitle);
105
+ bookContainer.appendChild(currentA4.page);
106
+ a4Pages.push({
107
+ element: currentA4.page,
108
+ pageNum: globalPageNum,
109
+ title: chapterTitle,
110
+ chapterId: chapId
111
+ });
112
+ globalPageNum++;
113
+
114
+ // 逐个元素流式排版
115
+ children.forEach(child => {
116
+ // 判断是否为原子不可拆分块(代码块、公式卡片、图片、表格、练习盒)
117
+ const isAtomic = child.matches('pre, .example, .figure, .tablewrap, .formula-block, .lo, .summary, .callout');
118
+ const cloned = child.cloneNode(true);
119
+
120
+ currentA4.body.appendChild(cloned);
121
+
122
+ // 测量当前 A4 版心是否溢出
123
+ if (currentA4.body.scrollHeight > MAX_A4_BODY_HEIGHT) {
124
+ // 溢出了!从当前页移除
125
+ currentA4.body.removeChild(cloned);
126
+
127
+ // 开启新一张 A4 纸!
128
+ currentA4 = createA4PageDOM(globalPageNum, moduleName, chapterTitle);
129
+ bookContainer.appendChild(currentA4.page);
130
+ a4Pages.push({
131
+ element: currentA4.page,
132
+ pageNum: globalPageNum,
133
+ title: chapterTitle,
134
+ chapterId: chapId,
135
+ targetId: child.id || ''
136
+ });
137
+ globalPageNum++;
138
+
139
+ // 将该元素作为新页面的首个元素放入
140
+ currentA4.body.appendChild(cloned);
141
+ }
142
+ });
143
+
144
+ // 隐藏未拆排的原始长章节
145
+ chap.classList.add('chapter-raw-source');
146
+ });
147
+ }
148
+
149
+ // 3. 视图与翻页切换控制
150
+ function updateView() {
151
+ if (isPagedMode) {
152
+ document.body.classList.add('paged-mode');
153
+ a4Pages.forEach((p, idx) => {
154
+ if (idx === currentPageIdx) {
155
+ p.element.classList.add('active-page');
156
+ p.element.style.display = 'flex';
157
+ } else {
158
+ p.element.classList.remove('active-page');
159
+ p.element.style.display = 'none';
160
+ }
161
+ });
162
+
163
+ if (prevBtn) prevBtn.disabled = currentPageIdx === 0;
164
+ if (nextBtn) nextBtn.disabled = currentPageIdx === a4Pages.length - 1;
165
+
166
+ const cur = a4Pages[currentPageIdx];
167
+ let displayTitle = cur ? cur.title : '';
168
+ if (displayTitle.length > 20) displayTitle = displayTitle.substring(0, 20) + '...';
169
+
170
+ if (pageInfo) {
171
+ pageInfo.innerText = `[${cur ? cur.pageNum : 1} / ${a4Pages.length} 页] ${displayTitle}`;
172
+ }
173
+ if (modeBtn) modeBtn.innerText = "切换为长卷轴";
174
+ window.scrollTo({ top: 0, behavior: 'instant' });
175
+ } else {
176
+ document.body.classList.remove('paged-mode');
177
+ a4Pages.forEach(p => {
178
+ p.element.classList.remove('active-page');
179
+ p.element.style.display = 'flex';
180
+ });
181
+ if (pageInfo) pageInfo.innerText = `长卷轴模式 (共 ${a4Pages.length} 页)`;
182
+ if (modeBtn) modeBtn.innerText = "切换为单页翻书";
183
+ if (prevBtn) prevBtn.disabled = true;
184
+ if (nextBtn) nextBtn.disabled = true;
185
+ }
186
+ localStorage.setItem('course_book_paged_mode', isPagedMode);
187
+ }
188
+
189
+ function goToPage(idx) {
190
+ if (idx < 0 || idx >= a4Pages.length) return;
191
+ currentPageIdx = idx;
192
+ updateView();
193
+ }
194
+
195
+ // 4. 事件监听与键盘左右翻页
196
+ if (prevBtn) prevBtn.onclick = () => goToPage(currentPageIdx - 1);
197
+ if (nextBtn) nextBtn.onclick = () => goToPage(currentPageIdx + 1);
198
+
199
+ if (modeBtn) modeBtn.onclick = () => {
200
+ isPagedMode = !isPagedMode;
201
+ updateView();
202
+ };
203
+
204
+ window.addEventListener('keydown', (e) => {
205
+ if (!isPagedMode) return;
206
+ if (['INPUT', 'TEXTAREA'].includes(document.activeElement.tagName)) return;
207
+ if (e.key === 'ArrowLeft' || e.key === 'PageUp') {
208
+ goToPage(currentPageIdx - 1);
209
+ } else if (e.key === 'ArrowRight' || e.key === 'PageDown' || e.key === ' ') {
210
+ if (e.key === ' ') e.preventDefault();
211
+ goToPage(currentPageIdx + 1);
212
+ }
213
+ });
214
+
215
+ // 5. 目录与侧边栏锚点拦截:点击任意链接精准定位到具体的 A4 纸!
216
+ document.querySelectorAll('a[href^="#"]').forEach(a => {
217
+ a.addEventListener('click', (e) => {
218
+ const targetId = a.getAttribute('href').substring(1);
219
+ if (!targetId) return;
220
+
221
+ const pIdx = a4Pages.findIndex(p => p.chapterId === targetId || p.targetId === targetId);
222
+ if (pIdx !== -1) {
223
+ if (isPagedMode) {
224
+ e.preventDefault();
225
+ goToPage(pIdx);
226
+ }
227
+ }
228
+ });
229
+ });
230
+
231
+ // 执行 A4 流式拆页并渲染
232
+ reflowBookIntoA4();
233
+ updateView();
234
+ })();