@xcanwin/manyoyo 6.2.5 → 6.2.7

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.
@@ -104,6 +104,7 @@
104
104
  <div class="files-preview-meta" data-role="preview-meta">请选择左侧文件或目录</div>
105
105
  </div>
106
106
  <div class="files-preview-actions">
107
+ <button type="button" class="secondary" data-action="toggle-markdown-view" hidden>查看源码</button>
107
108
  <button type="button" class="secondary" data-action="save" disabled>保存</button>
108
109
  </div>
109
110
  </header>
@@ -122,6 +123,7 @@
122
123
  const visitBtn = root.querySelector('[data-action="visit"]');
123
124
  const mkdirBtn = root.querySelector('[data-action="mkdir"]');
124
125
  const saveBtn = root.querySelector('[data-action="save"]');
126
+ const toggleMarkdownViewBtn = root.querySelector('[data-action="toggle-markdown-view"]');
125
127
 
126
128
  const state = {
127
129
  visible: false,
@@ -144,7 +146,8 @@
144
146
  editor: null,
145
147
  editorHost: null,
146
148
  previewReadOnly: true,
147
- previewDirty: false
149
+ previewDirty: false,
150
+ markdownViewMode: 'rendered'
148
151
  };
149
152
 
150
153
  function setStatus(text) {
@@ -161,6 +164,12 @@
161
164
  state.editorHost = null;
162
165
  }
163
166
 
167
+ function getPreviewLanguage() {
168
+ return state.selectedFile
169
+ ? (state.selectedFile.language || inferLanguageFromPath(state.selectedFile.path))
170
+ : '';
171
+ }
172
+
164
173
  function isEditablePreview() {
165
174
  return Boolean(
166
175
  state.selectedFile
@@ -168,6 +177,7 @@
168
177
  && state.previewReadOnly === false
169
178
  && state.historyOnly !== true
170
179
  && state.savingFile !== true
180
+ && (getPreviewLanguage() !== 'markdown' || state.markdownViewMode === 'source')
171
181
  );
172
182
  }
173
183
 
@@ -179,6 +189,28 @@
179
189
  saveBtn.textContent = state.savingFile ? '保存中...' : '保存';
180
190
  }
181
191
 
192
+ function syncMarkdownToggleButton() {
193
+ if (!toggleMarkdownViewBtn) {
194
+ return;
195
+ }
196
+ const isMarkdown = Boolean(state.selectedFile)
197
+ && state.selectedFile.kind === 'text'
198
+ && getPreviewLanguage() === 'markdown';
199
+ toggleMarkdownViewBtn.hidden = !isMarkdown;
200
+ toggleMarkdownViewBtn.textContent = state.markdownViewMode === 'source' ? '查看渲染' : '查看源码';
201
+ }
202
+
203
+ function resolveMarkdownImageUrl(basePath, relativeHref) {
204
+ try {
205
+ const lastSlash = String(basePath || '').lastIndexOf('/');
206
+ const baseDir = lastSlash >= 0 ? basePath.slice(0, lastSlash + 1) : '/';
207
+ const resolvedPath = new URL(relativeHref, 'http://manyoyo-internal' + baseDir).pathname;
208
+ return '/api/sessions/' + encodeURIComponent(state.sessionName) + '/fs/raw?path=' + encodeURIComponent(resolvedPath);
209
+ } catch (e) {
210
+ return '';
211
+ }
212
+ }
213
+
182
214
  function renderPreviewEmpty(title, description) {
183
215
  state.selectedFile = null;
184
216
  state.previewReadOnly = true;
@@ -193,6 +225,7 @@
193
225
  previewBodyNode.innerHTML = `<div class="files-empty">${escapeHtml(description)}</div>`;
194
226
  }
195
227
  destroyEditor();
228
+ syncMarkdownToggleButton();
196
229
  syncSaveButton();
197
230
  }
198
231
 
@@ -226,12 +259,34 @@
226
259
  previewMetaNode.textContent = `${payload.kind === 'text' ? '文本文件' : '文件'} · ${formatBytes(payload.size)} · ${modeLabel}${payload.truncated ? ' · 已截断预览' : ''}`;
227
260
  }
228
261
  if (!previewBodyNode) {
262
+ syncMarkdownToggleButton();
229
263
  syncSaveButton();
230
264
  return;
231
265
  }
232
266
 
233
267
  if (payload.kind === 'text') {
234
268
  const language = payload.language || inferLanguageFromPath(payload.path);
269
+ const showMarkdownRendered = language === 'markdown'
270
+ && state.markdownViewMode !== 'source'
271
+ && window.ManyoyoMarkdown
272
+ && typeof window.ManyoyoMarkdown.render === 'function';
273
+
274
+ if (showMarkdownRendered) {
275
+ destroyEditor();
276
+ const markdownNode = document.createElement('div');
277
+ markdownNode.className = 'md-content files-markdown-preview';
278
+ markdownNode.innerHTML = window.ManyoyoMarkdown.render(String(payload.content || ''), {
279
+ imageUrlResolver: function (relativeHref) {
280
+ return resolveMarkdownImageUrl(payload.path, relativeHref);
281
+ }
282
+ });
283
+ previewBodyNode.innerHTML = '';
284
+ previewBodyNode.appendChild(markdownNode);
285
+ syncMarkdownToggleButton();
286
+ syncSaveButton();
287
+ return;
288
+ }
289
+
235
290
  if (window.ManyoyoCodeEditor && typeof window.ManyoyoCodeEditor.create === 'function') {
236
291
  if (!state.editor || !state.editorHost || !previewBodyNode.contains(state.editorHost)) {
237
292
  destroyEditor();
@@ -252,18 +307,31 @@
252
307
  state.editor.setLanguage(language);
253
308
  state.editor.setReadOnly(state.previewReadOnly);
254
309
  }
310
+ syncMarkdownToggleButton();
255
311
  syncSaveButton();
256
312
  return;
257
313
  }
258
314
 
259
315
  destroyEditor();
260
316
  previewBodyNode.innerHTML = `<pre class="files-pre">${escapeHtml(String(payload.content || ''))}</pre>`;
317
+ syncMarkdownToggleButton();
318
+ syncSaveButton();
319
+ return;
320
+ }
321
+
322
+ if (payload.kind === 'image') {
323
+ destroyEditor();
324
+ const rawUrl = '/api/sessions/' + encodeURIComponent(state.sessionName) + '/fs/raw?path='
325
+ + encodeURIComponent(payload.path);
326
+ previewBodyNode.innerHTML = `<div class="files-image-preview"><img src="${escapeHtml(rawUrl)}" alt="${escapeHtml(payload.path || '')}" /></div>`;
327
+ syncMarkdownToggleButton();
261
328
  syncSaveButton();
262
329
  return;
263
330
  }
264
331
 
265
332
  destroyEditor();
266
333
  previewBodyNode.innerHTML = `<div class="files-note">当前文件暂不支持在线预览。文件类型:${escapeHtml(payload.kind || 'unknown')}</div>`;
334
+ syncMarkdownToggleButton();
267
335
  syncSaveButton();
268
336
  }
269
337
 
@@ -397,6 +465,7 @@
397
465
  state.loadingFile = true;
398
466
  state.selectedPath = pathText;
399
467
  state.selectedEntry = selectedEntry;
468
+ state.markdownViewMode = 'rendered';
400
469
  renderList();
401
470
  renderPreviewEmpty(pathText, '正在读取文件内容...');
402
471
  try {
@@ -584,6 +653,13 @@
584
653
  });
585
654
  }
586
655
 
656
+ if (toggleMarkdownViewBtn) {
657
+ toggleMarkdownViewBtn.addEventListener('click', function () {
658
+ state.markdownViewMode = state.markdownViewMode === 'source' ? 'rendered' : 'source';
659
+ renderPreviewPayload(state.selectedFile);
660
+ });
661
+ }
662
+
587
663
  renderList();
588
664
 
589
665
  return {
@@ -5,7 +5,8 @@
5
5
  configured: false,
6
6
  available: false,
7
7
  linkGuardBound: false,
8
- linkOpenHandler: null
8
+ linkOpenHandler: null,
9
+ currentImageResolver: null
9
10
  };
10
11
 
11
12
  function escapeHtml(value) {
@@ -247,8 +248,14 @@
247
248
  '[\uD83D\uDDBC\uFE0F点击查看图片:' + (safeText || escapeHtml(safeHref)) + ']'
248
249
  );
249
250
  }
250
- // 相对路径:正常渲染为图片
251
- let output = '<img src="' + escapeHtml(safeHref) + '" alt="' + escapeHtml(text || '') + '"';
251
+ // 相对路径:交给调用方按当前渲染上下文解析为可访问的 URL(未提供 resolver 时原样使用)
252
+ const resolvedHref = typeof runtime.currentImageResolver === 'function'
253
+ ? runtime.currentImageResolver(safeHref)
254
+ : safeHref;
255
+ if (!resolvedHref) {
256
+ return safeText || escapeHtml(token ? token.text : text || '');
257
+ }
258
+ let output = '<img src="' + escapeHtml(resolvedHref) + '" alt="' + escapeHtml(text || '') + '"';
252
259
  if (rawTitle) {
253
260
  output += ' title="' + escapeHtml(rawTitle) + '"';
254
261
  }
@@ -272,7 +279,7 @@
272
279
  return Boolean(msg && msg.mode === 'agent' && msg.role === 'assistant');
273
280
  }
274
281
 
275
- function render(content) {
282
+ function render(content, options) {
276
283
  const source = String(content == null ? '' : content);
277
284
  if (!source) {
278
285
  return '';
@@ -284,10 +291,15 @@
284
291
  if (!markedApi) {
285
292
  return '';
286
293
  }
294
+ runtime.currentImageResolver = options && typeof options.imageUrlResolver === 'function'
295
+ ? options.imageUrlResolver
296
+ : null;
287
297
  try {
288
298
  return String(markedApi.parse(source) || '');
289
299
  } catch (e) {
290
300
  return '';
301
+ } finally {
302
+ runtime.currentImageResolver = null;
291
303
  }
292
304
  }
293
305
 
@@ -1,33 +1,33 @@
1
- .bubble .md-content {
1
+ .md-content {
2
2
  color: var(--text);
3
3
  font-size: 13px;
4
4
  line-height: 1.6;
5
5
  word-break: break-word;
6
6
  }
7
7
 
8
- .bubble .md-content > :first-child {
8
+ .md-content > :first-child {
9
9
  margin-top: 0;
10
10
  }
11
11
 
12
- .bubble .md-content > :last-child {
12
+ .md-content > :last-child {
13
13
  margin-bottom: 0;
14
14
  }
15
15
 
16
- .bubble .md-content p,
17
- .bubble .md-content ul,
18
- .bubble .md-content ol,
19
- .bubble .md-content blockquote,
20
- .bubble .md-content pre,
21
- .bubble .md-content table {
16
+ .md-content p,
17
+ .md-content ul,
18
+ .md-content ol,
19
+ .md-content blockquote,
20
+ .md-content pre,
21
+ .md-content table {
22
22
  margin: 0.6em 0;
23
23
  }
24
24
 
25
- .bubble .md-content ul,
26
- .bubble .md-content ol {
25
+ .md-content ul,
26
+ .md-content ol {
27
27
  padding-left: 1.4em;
28
28
  }
29
29
 
30
- .bubble .md-content code {
30
+ .md-content code {
31
31
  font-family: var(--font-mono);
32
32
  font-size: 12px;
33
33
  padding: 1px 5px;
@@ -35,7 +35,7 @@
35
35
  background: rgba(194, 149, 79, 0.16);
36
36
  }
37
37
 
38
- .bubble .md-content pre {
38
+ .md-content pre {
39
39
  border-radius: 8px;
40
40
  padding: 9px 10px;
41
41
  border: 1px solid #e6d2b7;
@@ -43,7 +43,7 @@
43
43
  overflow-x: auto;
44
44
  }
45
45
 
46
- .bubble .md-content pre code {
46
+ .md-content pre code {
47
47
  background: transparent;
48
48
  border-radius: 0;
49
49
  padding: 0;
@@ -51,26 +51,26 @@
51
51
  line-height: 1.5;
52
52
  }
53
53
 
54
- .bubble .md-content blockquote {
54
+ .md-content blockquote {
55
55
  margin-left: 0;
56
56
  padding-left: 10px;
57
57
  border-left: 3px solid #dcb788;
58
58
  color: #66492d;
59
59
  }
60
60
 
61
- .bubble .md-content table {
61
+ .md-content table {
62
62
  border-collapse: collapse;
63
63
  width: 100%;
64
64
  }
65
65
 
66
- .bubble .md-content th,
67
- .bubble .md-content td {
66
+ .md-content th,
67
+ .md-content td {
68
68
  border: 1px solid #e6d2b7;
69
69
  padding: 4px 6px;
70
70
  text-align: left;
71
71
  }
72
72
 
73
- .bubble .md-content a {
73
+ .md-content a {
74
74
  color: #8a4f17;
75
75
  text-decoration-line: underline;
76
76
  text-decoration-thickness: 2px;
@@ -78,7 +78,7 @@
78
78
  text-decoration-color: rgba(138, 79, 23, 0.8);
79
79
  }
80
80
 
81
- .bubble .md-content a:hover,
82
- .bubble .md-content a:focus-visible {
81
+ .md-content a:hover,
82
+ .md-content a:focus-visible {
83
83
  text-decoration-color: #8a4f17;
84
84
  }