Skip to content

Commit 5d14dd9

Browse files
authored
Merge pull request #114 from codeitcodes/dev
Dev
2 parents 487a8b2 + 8ed7c36 commit 5d14dd9

File tree

13 files changed

+586
-49
lines changed

13 files changed

+586
-49
lines changed

api-link-parser.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
2+
// decodes cde.run links
3+
4+
// eg.
5+
// https://cde.run/mrdoob/three.js/examples/physics_ammo_break.html
6+
// or
7+
// https://cde.run/https://github.com/mrdoob/three.js/blob/dev/examples/physics_ammo_break.html
8+
9+
function decodeLink(url) {
10+
11+
// save link data
12+
13+
url = decodeURIComponent(url);
14+
15+
const isEmbed = url.endsWith('?embed=true');
16+
if (isEmbed) url = url.slice(0, -('?embed=true'.length));
17+
18+
const isDev = url.startsWith('https://dev.cde.run/');
19+
20+
if (!isDev) url = url.slice('https://cde.run/'.length);
21+
else url = url.slice('https://dev.cde.run/'.length);
22+
23+
const isGithub = url.startsWith('https://github.com/');
24+
if (isGithub) url = url.slice('https://github.com/'.length);
25+
26+
27+
let baseURL = 'https://codeit.codes';
28+
if (isDev) baseURL = 'https://dev.codeit.codes';
29+
30+
31+
let linkData = {};
32+
let link = url.split('/');
33+
34+
35+
// if link exists
36+
if (link.length > 1) {
37+
38+
linkData.user = link[0];
39+
linkData.repo = link[1];
40+
41+
linkData.contents = url.slice((linkData.user + '/' + linkData.repo).length);
42+
43+
if (linkData.contents.endsWith('/')) {
44+
45+
linkData.contents = linkData.contents.slice(0, -1);
46+
47+
}
48+
49+
50+
// if link includes a Github URL
51+
if (isGithub) {
52+
53+
linkData.contents = linkData.contents.slice('/blob'.length);
54+
55+
// if link includes a branch
56+
if (link[3]) {
57+
58+
linkData.repo += ':' + link[3];
59+
60+
linkData.contents = linkData.contents.slice(('/' + link[3]).length);
61+
62+
}
63+
64+
}
65+
66+
67+
const lastEntry = link[link.length - 1];
68+
69+
// if linking to file
70+
if (lastEntry !== linkData.repo
71+
&& lastEntry.split('.').length > 1) {
72+
73+
linkData.file = lastEntry;
74+
linkData.contents = linkData.contents.slice(0, (-lastEntry.length - 1));
75+
76+
// if linked file can be viewed live
77+
if (lastEntry.endsWith('.html') || lastEntry.endsWith('.svg')) {
78+
79+
// show file in live view
80+
linkData.openLive = true;
81+
82+
} else if (isEmbed) { // if link is embed
83+
84+
// show file link
85+
linkData.redirect = baseURL + '/full?dir=' +
86+
linkData.user + ',' + linkData.repo +
87+
',' + linkData.contents +
88+
'&file=' + linkData.file;
89+
90+
linkData.redirectText = 'Open ' + linkData.user + '/' + linkData.repo + ' with Codeit';
91+
92+
} // else, show the file's code
93+
94+
} else if (isEmbed) { // if linking to directory
95+
// and link is embed
96+
97+
// show directory link
98+
linkData.redirect = baseURL + '/full?dir=' +
99+
linkData.user + ',' + linkData.repo +
100+
',' + linkData.contents;
101+
102+
linkData.redirectText = 'Open ' + linkData.user + '/' + linkData.repo + ' with Codeit';
103+
104+
}
105+
106+
} else {
107+
108+
// show codeit link
109+
linkData.redirect = baseURL;
110+
linkData.redirectText = 'Open Codeit';
111+
112+
}
113+
114+
115+
// build link from data
116+
117+
let resp = baseURL;
118+
119+
// if redirect exists
120+
if (linkData.redirect) {
121+
122+
resp += '/redirect?to=' + linkData.redirect +
123+
'&text=' + linkData.redirectText;
124+
125+
} else {
126+
127+
resp += '/full?dir=' +
128+
linkData.user + ',' + linkData.repo +
129+
',' + linkData.contents;
130+
131+
// if file exists
132+
if (linkData.file) {
133+
134+
resp += '&file=' + linkData.file;
135+
136+
// if live view flag exists
137+
if (linkData.openLive) {
138+
139+
resp += '&openLive=true';
140+
141+
}
142+
143+
}
144+
145+
}
146+
147+
148+
return resp;
149+
150+
}
151+

api/link.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
2+
export default function handler(request, response) {
3+
4+
const query = request.query;
5+
6+
let title = 'Codeit | Mobile code editor connected to Git';
7+
8+
if (query.url) {
9+
10+
// parse URL
11+
let url = query.url.replace('https://cde.run/', '')
12+
.replace('https://dev.cde.run/', '')
13+
.replace('https://github.com/', '')
14+
.replace('https:/github.com/', '');
15+
16+
url = url.split('/');
17+
18+
if (url[0] && url[1]) {
19+
20+
if (url[url.length-1].endsWith('.html')
21+
|| url[url.length-1].endsWith('.svg')) {
22+
23+
title = 'Run ' + url[0] + '/' + url[1].split(':')[0] + ' with Codeit';
24+
25+
} else {
26+
27+
title = 'Open ' + url[0] + '/' + url[1].split(':')[0] + ' with Codeit';
28+
29+
}
30+
31+
}
32+
33+
}
34+
35+
36+
37+
const html = `
38+
<!DOCTYPE html>
39+
<html style="background: #313744" translate="no">
40+
<head>
41+
42+
<title>Codeit</title>
43+
44+
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover,shrink-to-fit=no">
45+
46+
<meta name="mobile-web-app-capable" content="yes">
47+
<meta name="theme-color" content="#313744">
48+
49+
<meta name="apple-mobile-web-app-status-bar-style" content="#313744">
50+
<meta name="apple-mobile-web-app-title" content="Codeit">
51+
52+
<meta http-equiv="origin-trial" content="At6bIDqQqUBcNVY46zIFOhsfQekTjYXogHK35lCdfrpna6/wqhxQsIh+kfTDRcLVPP0jyfFX5gTbBM4trLnn4g0AAABqeyJvcmlnaW4iOiJodHRwczovL2NvZGVpdC5jb2Rlczo0NDMiLCJmZWF0dXJlIjoiV2ViQXBwVXJsSGFuZGxpbmciLCJleHBpcnkiOjE2NDMxNTUxOTksImlzU3ViZG9tYWluIjp0cnVlfQ==">
53+
54+
<meta charset="utf-8">
55+
<meta name="description" content="Codeit runs on the web, open source, and free. Supports HTML, CSS, JavaScript, Python, Bootstrap, XML and more. Easy to use without how to.">
56+
57+
<meta property="og:title" content="`+ title +`">
58+
<meta property="og:description" content="Codeit runs on the web, open source, and free. Supports HTML, CSS, JavaScript, Python, Bootstrap, XML and more. Easy to use without how to.">
59+
<meta property="og:url" content="https://codeit.codes">
60+
<meta property="og:image" content="https://codeit.codes/images/banner-og.png">
61+
<meta property="og:type" content="application">
62+
<meta property="og:site_name" content="Codeit Code Editor">
63+
64+
<meta property="twitter:title" content="`+ title +`">
65+
<meta property="twitter:account_id" content="1484271514543345665">
66+
<meta name="twitter:card" content="summary">
67+
<meta property="twitter:domain" content="codeit.codes">
68+
<meta property="twitter:url" content="https://codeit.codes">
69+
<meta name="twitter:description" content="Codeit runs on the web, open source, and free. Supports HTML, CSS, JavaScript, Python, Bootstrap, XML and more. Easy to use without how to.">
70+
<meta name="twitter:image" content="https://codeit.codes/images/banner-og.png">
71+
72+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
73+
<meta name="referrer" content="origin-when-cross-origin">
74+
<meta name="keywords" content="code editor, version control tools, source code versioning, source code management tools, python, jquery demo, html, git, how to, bootstrap, jquery, javascript, javascript tutorial, javascript tutorial for beginners, javascript programming, html web form, create form in html, responsive web design, html web design, html design, responsive website development, html5 tutorial, html5 css3, html5 development, web design software, web development software, git tutorial, git howto, git repository, git command, git source code, top programming languages to learn, best programming language, best computer language, open source, open source code, open source applications, source code editor, software development tools, development tool, software developer tools list, programmer tool, web application development software">
75+
<link rel="canonical" href="https://codeit.codes/">
76+
<meta name="next-head-count" content="24">
77+
<meta name="robots" content="all">
78+
79+
<link rel="shortcut icon" href="https://codeit.codes/icons/android-app-512.png">
80+
<link rel="apple-touch-icon" href="https://codeit.codes/icons/iphone-app-180.png">
81+
82+
<script src="/api-link-parser.js"></script>
83+
84+
<script>
85+
86+
// decode link
87+
88+
const url = new URL(window.location.href).searchParams;
89+
90+
let link = url.get('url');
91+
let embed = url.get('embed');
92+
93+
const isDev = window.location.href.includes('dev');
94+
95+
if (link && !link.startsWith('https://cde.run')
96+
&& !link.startsWith('https://dev.cde.run')) {
97+
98+
if (!isDev) link = 'https://cde.run/' + link;
99+
else link = 'https://dev.cde.run/' + link;
100+
101+
}
102+
103+
if (link && link.startsWith('https:/github.com')) {
104+
105+
link = link.replace('https:/github.com', 'https://github.com');
106+
107+
}
108+
109+
if (link) {
110+
111+
if (embed) link += '?embed=true';
112+
113+
const resp = decodeLink(link);
114+
115+
// redirect to decoded URL
116+
window.location.href = resp;
117+
118+
} else {
119+
120+
window.location.href = window.location.origin;
121+
122+
}
123+
124+
</script>
125+
126+
</head>
127+
</html>
128+
`;
129+
130+
131+
132+
response.status(200).send(html);
133+
134+
}
135+

api/oembed.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
export default function handler(request, response) {
3+
4+
const query = request.query;
5+
6+
let html = 'Try adding a <code>?url=</code>.';
7+
8+
if (query.url) {
9+
10+
let url = 'https://codeit.codes';
11+
12+
if (query.url.startsWith('https://dev.cde.run/')) {
13+
url = 'https://dev.codeit.codes';
14+
}
15+
16+
html = `
17+
<oembed>
18+
<html><iframe src="`+ url +`/api/link?url=`+ query.url +`&embed=true" width="700" height="480"></html>
19+
<width>700</width>
20+
<height>480</height>
21+
</oembed>
22+
`;
23+
24+
}
25+
26+
response.status(200).send(html);
27+
28+
}
29+

filebrowser.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ async function renderSidebarHTML() {
148148
sidebarLogo.scrollTo({
149149
left: sidebarLogo.scrollWidth - sidebarLogo.offsetLeft
150150
});
151+
152+
scrolledSidebarTitle();
151153

152154
} else if (repo != '') {
153155

@@ -380,7 +382,7 @@ async function renderSidebarHTML() {
380382
left: sidebarLogo.scrollWidth - sidebarLogo.offsetLeft,
381383
behavior: 'smooth'
382384
});
383-
385+
384386
} else if (repo != '') {
385387

386388
// if repo is owned by logged user
@@ -644,11 +646,26 @@ async function renderSidebarHTML() {
644646

645647
// render repo
646648

649+
let fullName;
650+
651+
// if repo is owned by logged user
652+
if (modRepoName.split('/')[0] === loggedUser) {
653+
654+
// show repo name
655+
fullName = modRepoName.split('/')[1];
656+
657+
} else {
658+
659+
// show username and repo name
660+
fullName = modRepoName;
661+
662+
}
663+
647664
out = `
648665
<div class="item repo" ` + ('fullName="' + modRepoName + '"') + `>
649666
<div class="label">
650667
`+ repoIcon +`
651-
<a class="name">`+ modRepoName.split('/')[1] +`</a>
668+
<a class="name">`+ fullName +`</a>
652669
</div>
653670
`+ arrowIcon +`
654671
</div>
@@ -1309,6 +1326,26 @@ async function renderBranchMenuHTML(renderAll) {
13091326

13101327
// render selected branch
13111328

1329+
// if selected branch is not defined
1330+
if (!selectedBranch) {
1331+
1332+
// if default branch isn't fetched yet
1333+
if (!repoObj.selBranch) {
1334+
1335+
// await fetch
1336+
await repoPromise;
1337+
1338+
}
1339+
1340+
// add branch to tree
1341+
treeLoc[1] = repo.split(':')[0] + ':' + repoObj.selBranch;
1342+
saveTreeLocLS(treeLoc);
1343+
1344+
// update selected branch
1345+
selectedBranch = repoObj.selBranch;
1346+
1347+
}
1348+
13121349
const selBranchObj = branchResp.filter(branch => branch.name === selectedBranch)[0];
13131350

13141351
out += '<div class="icon selected">' + branchIcon + '<a>' + selectedBranch +'</a></div>';
@@ -2574,7 +2611,7 @@ function updateLineNumbersHTML() {
25742611

25752612
if (cd.querySelector('.line-numbers-rows')) {
25762613

2577-
cd.querySelector('.line-numbers-rows').textContent = '';
2614+
setAttr(cd.querySelector('.line-numbers-rows'), 'line-numbers', '');
25782615

25792616
}
25802617

0 commit comments

Comments
 (0)