(embedEl: HTMLElement, tFile: TFile, plugin: CodeSpacePlugin, renderToken: number, startLine: number = 0, endLine: number = 0)
| 966 | } |
| 967 | |
| 968 | async function renderCodeEmbed(embedEl: HTMLElement, tFile: TFile, plugin: CodeSpacePlugin, renderToken: number, startLine: number = 0, endLine: number = 0) { |
| 969 | // Read file content |
| 970 | const fullContent = await plugin.app.vault.read(tFile); |
| 971 | if (embedRenderTokens.get(embedEl) !== renderToken) return; |
| 972 | |
| 973 | const ext = tFile.extension.toLowerCase(); |
| 974 | |
| 975 | // 处理起始行号:截取从起始行开始的内容 |
| 976 | let content = fullContent; |
| 977 | const fullLineCount = fullContent.split('\n').length; |
| 978 | const effectiveStartLine = startLine > 0 ? Math.min(startLine, fullLineCount) : 1; |
| 979 | // 如果指定了 endLine,则生效;否则为 0 表示不限制 |
| 980 | const effectiveEndLine = endLine > 0 ? Math.min(endLine, fullLineCount) : 0; |
| 981 | // 是否使用范围模式(忽略 maxEmbedLines 限制) |
| 982 | const useRangeMode = effectiveEndLine > 0 && effectiveEndLine >= effectiveStartLine; |
| 983 | |
| 984 | if (effectiveStartLine > 1 || useRangeMode) { |
| 985 | const lines = fullContent.split('\n'); |
| 986 | const endIndex = useRangeMode ? effectiveEndLine : fullLineCount; |
| 987 | content = lines.slice(effectiveStartLine - 1, endIndex).join('\n'); |
| 988 | } |
| 989 | |
| 990 | // 计算文件的行数 |
| 991 | const lineCount = content.split('\n').length; |
| 992 | // 范围模式下忽略 maxEmbedLines 设置;否则使用设置值 |
| 993 | const maxLines = useRangeMode ? 0 : (plugin.settings.maxEmbedLines || 0); |
| 994 | |
| 995 | if (embedChildren.has(embedEl)) { |
| 996 | disposeCodeEmbed(embedEl); |
| 997 | } |
| 998 | |
| 999 | // Replace the embed content with our custom code embed. |
| 1000 | embedEl.empty(); |
| 1001 | |
| 1002 | // Create embed container |
| 1003 | const embedContainer = embedEl.createDiv({ |
| 1004 | cls: "code-embed-container", |
| 1005 | }); |
| 1006 | |
| 1007 | const header = embedContainer.createDiv({ |
| 1008 | cls: "code-embed-header", |
| 1009 | attr: { title: t("EMBED_TOOLTIP_OPEN") }, |
| 1010 | }); |
| 1011 | |
| 1012 | // Strictly prevent any click on the code area from opening the file. |
| 1013 | // Only the header should trigger navigation. |
| 1014 | embedContainer.addEventListener("click", (e) => { |
| 1015 | // If click is on or inside the header, let the header handler handle it. |
| 1016 | if (header.contains(e.target as Node)) { |
| 1017 | return; |
| 1018 | } |
| 1019 | // Otherwise, prevent any navigation - only allow text selection. |
| 1020 | e.stopPropagation(); |
| 1021 | e.preventDefault(); |
| 1022 | }); |
| 1023 | |
| 1024 | // Allow single-click on the header to open the file |
| 1025 | header.addEventListener("click", (e) => { |
no test coverage detected