MCPcopy Create free account
hub / github.com/Acode-Foundation/Acode / copyRelativePath

Function copyRelativePath

src/lib/openFolder.js:485–537  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

483 }
484
485 async function copyRelativePath() {
486 try {
487 // Validate inputs
488 if (!url) {
489 console.error("File path not available");
490 return;
491 }
492
493 if (!rootUrl) {
494 console.error("Root folder not found");
495 return;
496 }
497
498 let relativePath;
499
500 // Try using Url.pathname for protocol-based URLs
501 const rootPath = Url.pathname(rootUrl);
502 const targetPath = Url.pathname(url);
503
504 if (rootPath && targetPath) {
505 // Both pathnames extracted successfully
506 relativePath = Path.convertToRelative(rootPath, targetPath);
507 } else {
508 // Fallback: Use simple string comparison for URIs where pathname extraction fails
509 const cleanRoot = rootUrl.endsWith("/")
510 ? rootUrl.slice(0, -1)
511 : rootUrl;
512 const cleanTarget = url.endsWith("/") ? url.slice(0, -1) : url;
513
514 // Check if target URL starts with root URL
515 if (cleanTarget.startsWith(cleanRoot)) {
516 relativePath = cleanTarget.slice(cleanRoot.length + 1);
517 } else {
518 // If not a child path, just use basename
519 relativePath = Url.basename(url);
520 }
521 }
522
523 if (!relativePath) {
524 console.error("Unable to calculate relative path");
525 return;
526 }
527
528 if (cordova.plugins.clipboard) {
529 cordova.plugins.clipboard.copy(relativePath);
530 } else {
531 console.error("Clipboard not available");
532 toast("Clipboard not available");
533 }
534 } catch (error) {
535 console.error("Failed to copy relative path:", error);
536 }
537 }
538
539 async function openInTerminal() {
540 try {

Callers 1

execOperationFunction · 0.85

Calls 3

basenameMethod · 0.80
copyMethod · 0.65
errorMethod · 0.45

Tested by

no test coverage detected